27c90dc75aba9e3b3c1b3dd63f96e153d0b712db
[captive.git] / src / client / fuse / main.c
1 /* $Id$
2  * client FUSE interface for libcaptive
3  * Copyright (C) 2005 Jan Kratochvil <project-captive@jankratochvil.net>
4  * 
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; exactly version 2 of June 1991 is required
8  * 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * 
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18
19
20 #include "config.h"
21
22 #include <fuse.h>
23 #include <popt.h>
24 #include <locale.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <syslog.h>
28
29 #include <captive/client-vfs.h>
30 #include <captive/macros.h>
31
32 #include "main.h"       /* self */
33 #include "op_statfs.h"
34 #include "op_fsync.h"
35 #include "op_fsyncdir.h"
36 #include "op_opendir.h"
37 #include "op_readdir.h"
38 #include "op_releasedir.h"
39 #include "op_open.h"
40 #include "op_read.h"
41 #include "op_release.h"
42 #include "op_getattr.h"
43 #include "op_mknod.h"
44 #include "op_unlink.h"
45 #include "op_mkdir.h"
46 #include "op_rmdir.h"
47 #include "op_chmod.h"
48 #include "op_truncate.h"
49 #include "op_write.h"
50 #include "op_rename.h"
51 #include "op_utime.h"
52
53
54 /* Config: */
55 /* FIXME: Dupe with libcaptive/client/options.c */
56 #define DEFAULT_SYSLOG_FACILITY LOG_DAEMON
57 /* Each element must be preceded by a comma (',')! */
58 #define LIBFUSE_ADDONS ",default_permissions,kernel_cache"
59
60
61 CaptiveVfsObject *capfuse_captive_vfs_object;
62
63 static const struct poptOption popt_table[]={
64         CAPTIVE_POPT_INCLUDE,
65         POPT_AUTOHELP
66         POPT_TABLEEND
67         };
68
69 static const struct fuse_operations capfuse_operations={
70         statfs:     op_statfs,
71         fsync:      op_fsync,
72         fsyncdir:   op_fsyncdir,
73         opendir:    op_opendir,
74         readdir:    op_readdir,
75         releasedir: op_releasedir,
76         open:       op_open,
77         read:       op_read,
78         release:    op_release,
79         getattr:    op_getattr,
80         mknod:      op_mknod,
81         unlink:     op_unlink,
82         mkdir:      op_mkdir,
83         rmdir:      op_rmdir,
84         chmod:      op_chmod,
85         truncate:   op_truncate,
86         write:      op_write,
87         rename:     op_rename,
88         utime:      op_utime,
89         };
90
91 /* argv[0] expected as the program name. */
92 /* Only options and mountpoint expected here. */
93 static void capfuse_run(int argc,const char **argv)
94 {
95 char *capfuse_mountpoint;
96 int capfuse_multithreaded,capfuse_fd;
97 struct fuse *capfuse_fuse;
98
99         if (!(capfuse_fuse=fuse_setup(
100                                 argc,   /* argc */
101                                 (/*de-const; broken fuset_setup()*/char **)argv,        /* argv */
102                                 &capfuse_operations,    /* op */
103                                 sizeof(capfuse_operations),     /* op_size */
104                         &capfuse_mountpoint,    /* mountpoint */
105                         &capfuse_multithreaded, /* multithreaded */
106                         &capfuse_fd)))  /* fd */
107                 g_error(_("FUSE fuse_setup() failed"));
108         if (fuse_loop(capfuse_fuse)) {
109                 /* Do not: g_error(_("FUSE fuse_loop() error"));
110                  * as it is caused on each umount(8).
111                  * FIXME: Why?
112                  */
113                 }
114         fuse_teardown(capfuse_fuse,capfuse_fd,capfuse_mountpoint);
115 }
116
117 int main(int argc,char **argv)
118 {
119 poptContext context;
120 int errint;
121 int rest_argc,i;
122 const char **rest_argv,**csp;
123 struct captive_options options;
124 const char **capfuse_argv;
125 const char *program_name=argv[0];
126 const char *sandbox_server_argv0=G_STRINGIFY(LIBEXECDIR) "/captive-sandbox-server";
127 const char *image_filename;
128
129         g_log_set_always_fatal(~(0
130                         |G_LOG_LEVEL_MESSAGE
131                         |G_LOG_LEVEL_INFO
132                         |G_LOG_LEVEL_DEBUG
133                         ));
134
135         /* Prevent output block buffering if redirecting stdout to file. */
136         setvbuf(stdout,(char *)NULL,_IONBF,0);
137         setvbuf(stderr,(char *)NULL,_IONBF,0);
138
139         /* Initialize the i18n stuff */
140         setlocale(LC_ALL,"");
141         bindtextdomain(PACKAGE,LOCALEDIR);
142         textdomain(PACKAGE);
143
144         /* Initialize GObject subsystem of GLib. */
145         g_type_init();
146
147         captive_options_init(&options);
148         captive_options=&options;       /* for parsing by 'CAPTIVE_POPT_INCLUDE' */
149
150         g_assert(!options.sandbox_server_argv);
151         g_assert(!options.sandbox_server_ior);
152         /* captive_options_free(&options) will: g_free(options.sandbox_server_argv); */
153         /* Allocation is so terrible to be compatible with: captive_options_copy() */
154         options.sandbox_server_argv=g_malloc(2*sizeof(*options.sandbox_server_argv)+strlen(sandbox_server_argv0)+1);
155         options.sandbox_server_argv[0]=(char *)(options.sandbox_server_argv+2);
156         options.sandbox_server_argv[1]=NULL;
157         strcpy(options.sandbox_server_argv[0],sandbox_server_argv0);
158         options.sandbox=TRUE;
159
160         g_assert(!options.bug_pathname);
161         options.bug_pathname=g_strdup(G_STRINGIFY(VARLIBCAPTIVEDIR) "/bug-%FT%T.captivebug.xml.gz");
162
163         options.syslog_facility=DEFAULT_SYSLOG_FACILITY;
164
165         context=poptGetContext(
166                         PACKAGE,        /* name */
167                         argc,(/*en-const*/const char **)argv,   /* argc,argv */
168                         popt_table,     /* options */
169                         POPT_CONTEXT_POSIXMEHARDER);    /* flags; && !POPT_CONTEXT_KEEP_FIRST */
170         if (context==NULL) {
171                 g_error(_("Error parsing command-line arguments"));
172                 return EXIT_FAILURE;
173                 }
174         errint=poptReadDefaultConfig(context,
175                         TRUE);  /* useEnv */
176         if (errint!=0)
177                 g_warning(_("Error reading default popt configuration"));
178         errint=poptGetNextOpt(context);
179         if (errint!=-1) {
180                 g_error(_("Error parsing (dash-prefixed) command-line argument"));
181                 return EXIT_FAILURE;
182                 }
183         rest_argv=poptGetArgs(context);
184         for (csp=rest_argv,rest_argc=0;csp && *csp;csp++)
185                 rest_argc++;
186
187         captive_options=NULL;   /* already parsed by 'CAPTIVE_POPT_INCLUDE' */
188
189         /* Override the (default) Captive options with mount(8) supplied "-o" argument. */
190         /* rest_argv[0] is the device now. */
191         /* rest_argv[1] is the mountpoint now. */
192         if (rest_argc>=4 && !strcmp(rest_argv[2],"-o")) {
193 const char *cs=rest_argv[3];
194
195                 while (cs&&*cs) {
196                         if ((!strncmp(cs,"ro",2) || !strncmp(cs,"rw",2)) && (cs[2]==',' || !cs[2])) {
197                                 if (!strncmp(cs,"ro",2))
198                                         options.rwmode=CAPTIVE_OPTION_RWMODE_RO;
199                                 if (!strncmp(cs,"rw",2))
200                                         options.rwmode=CAPTIVE_OPTION_RWMODE_RW;
201                                 }
202                         if ((cs=strchr(cs,',')))
203                                 cs++;
204                         }
205                 }
206
207         /* image_iochannel */
208         if (rest_argc<1) {
209                 g_error(_("File/device disk image pathname command-line argument required"));
210                 return EXIT_FAILURE;
211                 }
212         g_assert(options.image_iochannel==NULL);
213         image_filename=rest_argv[0];
214         rest_argc--;
215         rest_argv++;
216         if (!(options.image_iochannel=g_io_channel_new_file(
217                         image_filename, /* filename */
218                         (options.rwmode==CAPTIVE_OPTION_RWMODE_RW ? "r+" : "r"),        /* mode */
219                         NULL))) {       /* error */
220                 g_error(_("image_iochannel failed open of: %s"),image_filename);
221                 return EXIT_FAILURE;
222                 }
223
224         if (options.filesystem.type==CAPTIVE_OPTIONS_MODULE_TYPE_EMPTY) {
225 const char *self_prefix="mount.captive-";
226 size_t self_prefix_len=strlen(self_prefix);
227 const char *fsname;
228
229                 if ((fsname=strrchr(program_name,'/')))
230                         fsname++;
231                 else
232                         fsname=program_name;
233                 if (strncmp(fsname,self_prefix,self_prefix_len))
234                         g_error(_("Cannot detected default filesystem name from my basename: %s"),fsname);
235                 fsname+=self_prefix_len;
236                 if (!captive_options_module_load(&options.filesystem,
237                                 captive_printf_alloca("%s/%s.sys",G_STRINGIFY(VARLIBCAPTIVEDIR),fsname)))
238                         g_error(_("'--filesystem' option requires valid pathname ('ntfs.sys' suggested)"));
239                 g_assert(options.filesystem.type!=CAPTIVE_OPTIONS_MODULE_TYPE_EMPTY);
240                 }
241         if (!options.load_module) {
242 struct captive_options_module *options_module;
243
244                 captive_new(options_module);
245                 if (!captive_options_module_load(options_module,G_STRINGIFY(VARLIBCAPTIVEDIR) "/ntoskrnl.exe"))
246                         g_error(_("'--load-module' option requires valid pathname ('ntoskrnl.exe' suggested)"));
247
248                 options.load_module=g_list_append(options.load_module,options_module);
249                 }
250
251         if (GNOME_VFS_OK!=captive_vfs_new(&capfuse_captive_vfs_object,&options)) {
252                 g_error(_("captive_vfs_new() failed"));
253                 return EXIT_FAILURE;
254                 }
255         captive_options_free(&options);
256
257         /* Simulate argv[0] there as it got cut by popt. */
258         captive_newn_alloca(capfuse_argv,1+rest_argc+1+2);
259         capfuse_argv[0]=argv[0];
260         memcpy(capfuse_argv+1,rest_argv,sizeof(*rest_argv)*(rest_argc+1));
261
262         for (i=1;capfuse_argv[i];i++) {
263                 if (strcmp(capfuse_argv[i],"-o"))
264                         continue;
265                 capfuse_argv[i+1]=captive_printf_alloca("fsname=%s" LIBFUSE_ADDONS  ",%s",image_filename,capfuse_argv[i+1]);
266                 break;
267                 }
268         if (!capfuse_argv[i]) {
269                 capfuse_argv[i  ]="-o";
270                 capfuse_argv[i+1]=captive_printf_alloca("fsname=%s" LIBFUSE_ADDONS,image_filename);
271                 capfuse_argv[i+2]=NULL;
272                 }
273
274         /* FIXFUSE: fuse_main()/fuse_main_real() would be enough for Captive fuse but
275          * the public interface of fuse_main() is too broken.
276          */
277         capfuse_run(1+rest_argc,capfuse_argv);
278
279         /* 'rest_argv' gets cleared by 'poptFreeContext(context);' below */
280         poptFreeContext(context);
281
282         if (capfuse_captive_vfs_object) {
283                 g_object_unref(capfuse_captive_vfs_object);
284                 capfuse_captive_vfs_object=NULL;
285                 }
286
287         return EXIT_SUCCESS;
288 }