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