Initial original import from: fuse-2.4.2-2.fc4
[captive.git] / src / libcaptive / sandbox / server-Vfs.c
1 /* $Id$
2  * CORBA/ORBit server side of Vfs object, ran by sandbox_child()
3  * Copyright (C) 2003 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 "server-Vfs.h" /* self */
23 #include "sandbox.h"
24 #include <glib/gmessages.h>
25 #include "captive/macros.h"
26 #include "server-Directory.h"
27 #include "server-File.h"
28 #include "split.h"
29 #include "server-GLogFunc.h"
30 #include "client-CaptiveIOChannel.h"
31 #include "../client/vfs-slave.h"
32
33
34 static void impl_Captive_Vfs_fini(impl_POA_Captive_Vfs *servant,CORBA_Environment *ev);
35 static void impl_Captive_Vfs_init
36                 (impl_POA_Captive_Vfs *servant,const Captive_CaptiveOptions *options_corba,CORBA_Environment *ev);
37 static void impl_Captive_Vfs_shutdown(impl_POA_Captive_Vfs *servant,CORBA_Environment *ev);
38 static void impl_Captive_Vfs_volume_info_get
39                 (impl_POA_Captive_Vfs *servant,Captive_CaptiveVfsVolumeInfo *volume_info_corba,CORBA_Environment *ev);
40
41 static PortableServer_ServantBase__epv impl_Captive_Vfs_base_epv={
42         NULL,   /* _private data */
43         (gpointer)&impl_Captive_Vfs_fini,       /* finalize routine */
44         NULL,   /* default_POA routine */
45         };
46 static POA_Captive_Vfs__epv impl_Captive_Vfs_epv={
47         NULL,   /* _private */
48         (gpointer)&impl_Captive_Vfs_init,
49         (gpointer)&impl_Captive_Vfs_shutdown,
50         (gpointer)&impl_Captive_Vfs_directory_new_open,
51         (gpointer)&impl_Captive_Vfs_directory_new_make,
52         (gpointer)&impl_Captive_Vfs_file_new_open,
53         (gpointer)&impl_Captive_Vfs_file_new_create,
54         (gpointer)&impl_Captive_Vfs_volume_info_get,
55         };
56 static POA_Captive_Vfs__vepv impl_Captive_Vfs_vepv={
57         &impl_Captive_Vfs_base_epv,
58         &impl_Captive_Vfs_epv,
59         };
60
61
62 Captive_Vfs impl_Captive_Vfs__create(PortableServer_POA poa,CORBA_Environment *ev)
63 {
64 Captive_Vfs retval;
65 impl_POA_Captive_Vfs *newservant;
66 PortableServer_ObjectId *objid;
67
68         captive_new0(newservant);       /* FIXME: leak */
69         newservant->servant.vepv=&impl_Captive_Vfs_vepv;
70         newservant->poa=poa;
71         newservant->captive_vfs_object=NULL;
72         POA_Captive_Vfs__init((PortableServer_Servant)newservant,ev);
73         objid=PortableServer_POA_activate_object(poa,newservant,ev);
74         CORBA_free(objid);
75         retval=PortableServer_POA_servant_to_reference(poa,newservant,ev);
76
77         return retval;
78 }
79
80
81 static void impl_Captive_Vfs_fini(impl_POA_Captive_Vfs *servant,CORBA_Environment *ev)
82 {
83         if (servant->captive_vfs_object) {
84                 g_object_unref(servant->captive_vfs_object);
85                 servant->captive_vfs_object=NULL;
86                 }
87 }
88
89
90 static Captive_CaptiveIOChannel options_corba_image_iochannel_copy;
91
92 void impl_Captive_Vfs__destroy(impl_POA_Captive_Vfs *servant,CORBA_Environment *ev)
93 {
94 PortableServer_ObjectId *objid;
95
96         objid=PortableServer_POA_servant_to_id(servant->poa,servant,ev);
97         PortableServer_POA_deactivate_object(servant->poa,objid,ev);
98         CORBA_free(objid);
99         impl_Captive_Vfs_fini(servant,ev);
100         CORBA_Object_release(options_corba_image_iochannel_copy,ev);
101         g_free(servant);
102 }
103
104
105 static void options_module_corba_to_options_module_captive
106                 (struct captive_options_module *dest_options_module_captive,const Captive_CaptiveOptionsModule *src_options_module_corba)
107 {
108         g_return_if_fail(dest_options_module_captive!=NULL);
109         g_return_if_fail(src_options_module_corba!=NULL);
110
111         dest_options_module_captive->pathname_utf8=g_strdup(src_options_module_corba->pathname_utf8);
112         dest_options_module_captive->type=CAPTIVE_OPTIONS_MODULE_TYPE_PE32;
113         dest_options_module_captive->u.pe32.base=g_memdup(src_options_module_corba->data._buffer,
114                         src_options_module_corba->data._length);
115         dest_options_module_captive->u.pe32.length=src_options_module_corba->data._length;
116         dest_options_module_captive->u.pe32.mapped=FALSE;
117         /* 'md5' is never used in sandbox client; XML is dumped by the master. */
118         dest_options_module_captive->u.pe32.md5=g_strdup("<sandbox-client:undef>");
119 }
120
121
122 static void impl_Captive_Vfs_init
123                 (impl_POA_Captive_Vfs *servant,const Captive_CaptiveOptions *options_corba,CORBA_Environment *ev)
124 {
125 struct captive_options options_captive;
126 GnomeVFSResult errvfsresult;
127 guint moduleui;
128
129         g_return_if_fail(servant->captive_vfs_object==NULL);
130
131         options_corba_image_iochannel_copy=CORBA_Object_duplicate(options_corba->image_iochannel,ev);
132         if (ev->_major!=CORBA_NO_EXCEPTION)
133                 return;
134
135         /* impl_Captive_Vfs_init_g_log_func() does its own copy of 'options_corba->g_log_func'. */
136         impl_Captive_Vfs_init_g_log_func(options_corba->g_log_func,options_corba->debug_messages,ev);
137
138         captive_options_init(&options_captive);
139
140         options_module_corba_to_options_module_captive(&options_captive.filesystem,&options_corba->filesystem);
141
142         options_captive.debug_messages=options_corba->debug_messages;
143         options_captive.rwmode        =options_corba->rwmode;
144         options_captive.media         =options_corba->media;
145
146         options_captive.image_iochannel=(GIOChannel *)captive_io_channel_new(
147                         options_corba_image_iochannel_copy,     /* corba_captive_io_channel */
148                         (options_captive.rwmode!=CAPTIVE_OPTION_RWMODE_RO));    /* writeable */
149
150         for (moduleui=0;moduleui<options_corba->load_module._length;moduleui++) {
151 struct captive_options_module *options_module;
152
153                 captive_new(options_module);
154                 options_module_corba_to_options_module_captive(options_module,options_corba->load_module._buffer+moduleui);
155                 options_captive.load_module=g_list_append(options_captive.load_module,options_module);
156                 }
157
158         options_captive.sandbox=TRUE;
159         g_assert(options_captive.sandbox_server_argv==NULL);
160         g_assert(options_captive.sandbox_server_ior==NULL);
161
162         if (GNOME_VFS_OK!=(errvfsresult=captive_vfs_new(&servant->captive_vfs_object,&options_captive))) {
163                 CORBA_exception_set(ev,CORBA_USER_EXCEPTION,ex_Captive_GnomeVFSResultException,GINT_TO_POINTER((gint)errvfsresult));
164                 captive_options_free(&options_captive);
165                 return;
166                 }
167
168         captive_options_free(&options_captive);
169
170         g_assert(CAPTIVE_VFS_SLAVE_IS_OBJECT(servant->captive_vfs_object));
171 }
172
173
174 #if 0   /* Currently unused - see impl_Captive_Vfs_shutdown() */
175 static gboolean impl_Captive_Vfs_shutdown_idle(gpointer data /* unused */)
176 {
177         sandbox_child_shutdown();
178
179         return FALSE;   /* remove me */
180 }
181 #endif
182
183 static void impl_Captive_Vfs_shutdown(impl_POA_Captive_Vfs *servant,CORBA_Environment *ev)
184 {
185 #if 0   /* Currently unused - see impl_Captive_Vfs_shutdown() */
186 GSource *source;
187 #endif
188
189         /* Shutdown 'servant->captive_vfs_object' synchronously as it may
190          * flush its buffers needed to be transferred to our parent.
191          */
192         impl_Captive_Vfs_fini(servant,&captive_corba_ev);
193         g_assert(validate_CORBA_Environment(&captive_corba_ev));
194
195         /* Currently we do not do any sandbox child shutdown here as it sometimes
196          * crashes the parent with COMM_FAILURE without proper finish of this
197          * shutdown() CORBA method call. Parent will kill(2) us soon
198          * from its parent-Vfs.c:captive_sandbox_parent_vfs_close() anyway.
199          */
200 #if 0
201         sandbox_child_prepare_shutdown();
202
203         /* Do not call sandbox_child_shutdown() directly as we would fail
204          * to finish this CORBA method call properly.
205          * Do not call g_idle_add_full() as it would miss linc main loop.
206          */
207   source=g_idle_source_new ();
208         g_source_set_priority(source,G_PRIORITY_LOW);
209   g_source_set_callback(
210                         source, /* source */
211                         (GSourceFunc)impl_Captive_Vfs_shutdown_idle,    /* func */
212                         servant,        /* data */
213                         NULL);  /* notify */
214   g_source_attach(source,
215                         captive_corba_get_context());   /* context; NULL means 'default context' */
216   g_source_unref(source);
217 #endif
218 }
219
220
221 static void impl_Captive_Vfs_volume_info_get
222                 (impl_POA_Captive_Vfs *servant,Captive_CaptiveVfsVolumeInfo *volume_info_corba,CORBA_Environment *ev)
223 {
224 CaptiveVfsVolumeInfo volume_info_captive;
225 GnomeVFSResult errvfsresult;
226
227         if (GNOME_VFS_OK!=(errvfsresult=captive_vfs_volume_info_get(servant->captive_vfs_object,&volume_info_captive))) {
228                 captive_sandbox_child_GnomeVFSResultException_throw(ev,errvfsresult);
229                 return;
230                 }
231
232         volume_info_corba->block_size     =volume_info_captive.block_size;
233         volume_info_corba->bytes          =volume_info_captive.bytes;
234         volume_info_corba->bytes_free     =volume_info_captive.bytes_free;
235         volume_info_corba->bytes_available=volume_info_captive.bytes_available;
236 }