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