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