+cleanup of dead sandbox children
[captive.git] / src / libcaptive / sandbox / split.c
1 /* $Id$
2  * Connection of captive-vfs interface through CORBA/ORBit
3  * Copyright (C) 2002 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 "split.h"      /* self */
23 #include "sandbox.h"
24 #include "server-GLogFunc.h"
25 #include "captive/macros.h"
26 #include <glib/gmacros.h>
27 #include <stdlib.h>
28 #include "captive/rtl-file.h"
29 #include <unistd.h>
30 #include <fcntl.h>
31 #include <linc/linc.h>  /* for linc_main_get_loop() */
32 #include "server-Directory.h"
33 #include "server-Vfs.h"
34 #include "../client/vfs.h"
35 #include "captive/parent-Vfs.h"
36 #include <dirent.h>
37 #include "server-CaptiveIOChannel.h"
38 #include <errno.h>
39
40
41 /* CONFIG: */
42
43 #define HEARTBEAT_SOURCE_CHECK_EVENTS (G_IO_IN|G_IO_PRI)
44 #define HEARTBEAT_SOURCE_CHECK_REVENTS (HEARTBEAT_SOURCE_CHECK_EVENTS|G_IO_ERR|G_IO_HUP|G_IO_NVAL)
45
46
47 gboolean validate_CORBA_Environment(CORBA_Environment *evp)
48 {
49         if (evp->_major==CORBA_NO_EXCEPTION)
50                 return TRUE;
51
52         g_message(_("CORBA Exception occured: id=\"%s\", value=%p"),
53                         CORBA_exception_id(evp),CORBA_exception_value(evp));
54         return FALSE;
55 }
56
57 CORBA_Environment captive_corba_ev;
58 CORBA_ORB captive_corba_orb;
59 PortableServer_POA captive_corba_poa;
60
61 static void corba_shutdown_atexit(void);
62
63 static gboolean corba_init(const char *pname,CORBA_Environment *evp,CORBA_ORB *orbp,PortableServer_POA *poap)
64 {
65 static gboolean done=FALSE;
66 int orb_argc=1;
67 gchar *orb_argv[]={
68                 (gchar *)captive_strdup_alloca(pname),
69                 NULL};
70
71         g_return_val_if_fail(evp!=NULL,FALSE);
72         g_return_val_if_fail(orbp!=NULL,FALSE);
73         g_return_val_if_fail(poap!=NULL,FALSE);
74
75         if (done)
76                 return TRUE;
77
78         /* Init 'ev' */
79         CORBA_exception_init(evp);
80
81         /* Init 'orb' */
82         *orbp=CORBA_ORB_init(&orb_argc,orb_argv,"orbit-local-orb",evp);
83         g_return_val_if_fail(*orbp!=CORBA_OBJECT_NIL,FALSE);
84         g_return_val_if_fail(validate_CORBA_Environment(evp),FALSE);
85
86         /* Init 'poa' */
87         *poap=(PortableServer_POA)CORBA_ORB_resolve_initial_references(*orbp,"RootPOA",evp);
88         g_return_val_if_fail(validate_CORBA_Environment(evp),FALSE);
89         {
90 PortableServer_POAManager poa_mgr;
91                 poa_mgr=PortableServer_POA__get_the_POAManager(*poap,evp);
92                 g_return_val_if_fail(validate_CORBA_Environment(evp),FALSE);
93                 PortableServer_POAManager_activate(poa_mgr,evp);
94                 g_return_val_if_fail(validate_CORBA_Environment(evp),FALSE);
95                 CORBA_Object_release((CORBA_Object)poa_mgr,evp);
96                 g_return_val_if_fail(validate_CORBA_Environment(evp),FALSE);
97                 }
98
99         g_atexit(corba_shutdown_atexit);
100
101         done=TRUE;
102         return TRUE;
103 }
104
105
106 static CORBA_ORB heartbeat_source_callback_orb=CORBA_OBJECT_NIL;
107
108 gboolean corba_shutdown(CORBA_Environment *evp,CORBA_ORB *orbp,PortableServer_POA *poap)
109 {
110 PortableServer_POA poa;
111 CORBA_ORB orb;
112
113         g_return_val_if_fail(evp!=NULL,FALSE);
114         g_return_val_if_fail(orbp!=NULL,FALSE);
115         g_return_val_if_fail(poap!=NULL,FALSE);
116
117         /* Shutdown 'poa' */
118         poa=*poap;
119         *poap=CORBA_OBJECT_NIL;
120         CORBA_Object_release((CORBA_Object)poa,evp);
121         g_return_val_if_fail(validate_CORBA_Environment(evp),FALSE);
122
123         /* Shutdown 'orb' */
124         orb=*orbp;
125         *orbp=CORBA_OBJECT_NIL;
126         heartbeat_source_callback_orb=CORBA_OBJECT_NIL;
127         CORBA_ORB_destroy(orb,evp);
128         g_return_val_if_fail(validate_CORBA_Environment(evp),FALSE);
129
130         /* Shutdown 'ev' */
131         CORBA_exception_free(evp);
132
133         return TRUE;
134 }
135
136 static void corba_shutdown_atexit(void)
137 {
138 gboolean errbool;
139
140         errbool=corba_shutdown(&captive_corba_ev,&captive_corba_orb,&captive_corba_poa);
141         g_assert(errbool==TRUE);
142 }
143
144
145 static GSource *captive_corba_sandbox_child_heartbeat_gsource;
146
147 void sandbox_child_prepare_shutdown(void)
148 {
149         /* Prevent during shutdown: Captive-WARNING **: CORBA Exception occured: id="IDL:omg.org/CORBA/COMM_FAILURE:1.0" */
150         if (captive_corba_sandbox_child_heartbeat_gsource) {
151                 g_source_destroy(captive_corba_sandbox_child_heartbeat_gsource);
152                 g_source_unref(captive_corba_sandbox_child_heartbeat_gsource);
153                 captive_corba_sandbox_child_heartbeat_gsource=NULL;
154                 }
155 }
156
157
158 void sandbox_child_shutdown(void)
159 {
160         /* Do not fail by passing logging messages to the master. */
161         impl_Captive_Vfs_init_g_log_func_disable();
162
163         g_main_loop_quit(linc_main_get_loop());
164 }
165
166
167 static gboolean heartbeat_source_callback(gpointer data /* unused */)
168 {
169         g_return_val_if_fail(heartbeat_source_callback_orb!=CORBA_OBJECT_NIL,FALSE);    /* the source should be removed */
170
171         sandbox_child_shutdown();
172
173         return FALSE;   /* the source should be removed */
174 }
175
176
177 static gboolean heartbeat_source_prepare(GSource *source,gint *timeout)
178 {
179         *timeout=-1;
180
181         return FALSE;
182 }
183
184 static GPollFD heartbeat_source_check_gpollfd;
185
186 static gboolean heartbeat_source_check(GSource *source)
187 {
188         return !!(heartbeat_source_check_gpollfd.revents & HEARTBEAT_SOURCE_CHECK_REVENTS);
189 }
190
191 static gboolean heartbeat_source_dispatch(GSource *source,GSourceFunc callback,gpointer user_data)
192 {
193         g_assert(callback!=NULL);
194         return (*callback)(user_data);
195 }
196
197 static GSourceFuncs heartbeat_source_watch_funcs={
198                 heartbeat_source_prepare,
199                 heartbeat_source_check,
200                 heartbeat_source_dispatch,
201                 NULL,   /* finalize */
202                 };
203
204 void captive_corba_sandbox_child(const gchar *chrooted_orbit_dir)
205 {
206 Captive_Vfs Vfs_object;
207 impl_POA_Captive_Vfs *Vfs_servant;
208 gboolean errbool;
209 guint errguint;
210 GSource *gsource;
211 int errint;
212
213         /* attach heartbeat_source_callback() to watch for any abnormalities
214          * on our open pipe 'parentheart_fds' and terminate the child if parent dies.
215          */
216         gsource=g_source_new(&heartbeat_source_watch_funcs,sizeof(GSource));
217         g_return_if_fail(gsource!=NULL);
218         g_source_set_callback(
219                         gsource,        /* source */
220                         heartbeat_source_callback,      /* func */
221                         NULL,   /* data */
222                         NULL);  /* notify */
223         heartbeat_source_check_gpollfd.fd=0 /* STDIN */;        /* parentheart_fd_read */
224         heartbeat_source_check_gpollfd.events=HEARTBEAT_SOURCE_CHECK_EVENTS;
225         heartbeat_source_check_gpollfd.revents=0;
226         g_source_add_poll(gsource,&heartbeat_source_check_gpollfd);
227
228         errbool=corba_init("captive-sandbox-child",&captive_corba_ev,&captive_corba_orb,&captive_corba_poa);
229         g_return_if_fail(errbool==TRUE);
230
231         heartbeat_source_callback_orb=captive_corba_orb;
232
233         /* linc_main_get_loop() makes sense only after corba_init() -> CORBA_ORB_init() */
234         errguint=g_source_attach(
235                         gsource,        /* source */
236                         g_main_loop_get_context(linc_main_get_loop())); /* context; NULL means 'default context' */
237         g_assert(errguint!=0);
238
239         /* Init 'Vfs_object' */
240         Vfs_object=impl_Captive_Vfs__create(captive_corba_poa,&captive_corba_ev);
241         g_assert(validate_CORBA_Environment(&captive_corba_ev));
242
243         /* Pass IOR to our parent.
244          * It will also create the socket needed for 'chrooted_orbit_dir' below
245          * by CORBA_ORB_object_to_string()->ORBit_marshal_object()->IOP_generate_profiles()->ORBit_ORB_start_servers...
246          */
247         {
248 char *Vfs_IOR;
249                 Vfs_IOR=CORBA_ORB_object_to_string(captive_corba_orb,Vfs_object,&captive_corba_ev);
250                 g_assert(validate_CORBA_Environment(&captive_corba_ev));
251                 g_assert(Vfs_IOR!=NULL);
252                 errint=printf("ior=%s\n",Vfs_IOR);
253                 g_assert(errint>=0);
254                 CORBA_free(Vfs_IOR);
255                 }
256
257         /* Default mode is 0700, permit parent non-"captive" users to access our IOR socket. */
258         if (chrooted_orbit_dir) {
259 DIR *dir;
260 struct dirent *dirent;
261 const gchar *socketname=NULL,*socketpathname;
262
263                 errint=chmod(chrooted_orbit_dir,0755);
264                 g_assert(errint==0);
265
266                 dir=opendir(chrooted_orbit_dir);
267                 g_assert(dir!=NULL);
268                 while (errno=0,(dirent=readdir(dir))) {
269                         if (!strcmp(dirent->d_name,".") || !strcmp(dirent->d_name,".."))
270                                 continue;
271                         g_assert(socketname==NULL);
272                         socketname=captive_strdup_alloca(dirent->d_name);
273                         }
274                 g_assert(errno==0);
275                 errint=closedir(dir);
276                 g_assert(errint==0);
277                 g_assert(socketname!=NULL);
278
279                 socketpathname=captive_printf_alloca("%s/%s",chrooted_orbit_dir,socketname);
280                 errint=chmod(socketpathname,0666);
281                 g_assert(errint==0);
282
283                 printf("socketname=%s\n",socketname);
284                 }
285
286         /* Close the output to flush it to our spawning parent. */
287         errint=fflush(stdout);
288         g_assert(errint==0);
289         errint=fclose(stdout);
290         g_assert(errint==0);
291
292         /* CORBA_ORB_run() -> linc_main_loop_run() -> g_main_loop_run()
293          * and therefore we should be safe with glib events handling.
294          */
295         CORBA_ORB_run(captive_corba_orb,&captive_corba_ev);
296         g_assert(validate_CORBA_Environment(&captive_corba_ev));
297
298         /* Shutdown 'Vfs' servant */
299         Vfs_servant=PortableServer_POA_reference_to_servant(captive_corba_poa,Vfs_object,&captive_corba_ev);
300         g_assert(validate_CORBA_Environment(&captive_corba_ev));
301         CORBA_Object_release(Vfs_object,&captive_corba_ev);
302         g_assert(validate_CORBA_Environment(&captive_corba_ev));
303         impl_Captive_Vfs__destroy(Vfs_servant,&captive_corba_ev);
304         g_assert(validate_CORBA_Environment(&captive_corba_ev));
305
306         errbool=corba_shutdown(&captive_corba_ev,&captive_corba_orb,&captive_corba_poa);
307         g_assert(errbool==TRUE);
308
309         _exit(EXIT_SUCCESS);
310 }
311
312
313 static void options_module_captive_to_options_module_corba
314                 (Captive_CaptiveOptionsModule *dest_options_module_corba,const struct captive_options_module *src_options_module_captive)
315 {
316         g_return_if_fail(dest_options_module_corba!=NULL);
317         g_return_if_fail(src_options_module_captive!=NULL);
318
319         g_return_if_fail(src_options_module_captive->type==CAPTIVE_OPTIONS_MODULE_TYPE_PE32);
320
321         dest_options_module_corba->pathname_utf8=g_strdup(src_options_module_captive->pathname_utf8);
322         dest_options_module_corba->data._buffer=g_memdup(src_options_module_captive->u.pe32.base,
323                         src_options_module_captive->u.pe32.length);
324         dest_options_module_corba->data._maximum=src_options_module_captive->u.pe32.length;
325         dest_options_module_corba->data._length =src_options_module_captive->u.pe32.length;
326         dest_options_module_corba->data._release=TRUE;
327 }
328
329
330 static const gchar *sandbox_parent_own_orbit_dir;
331 static const gchar *sandbox_parent_own_orbit_socket;
332
333 gchar *sandbox_parent_read_ior(int Vfs_IOR_fd_read,gchar **child_chroot_pid_hashkey_dirp)
334 {
335 gchar *data;
336 gsize data_size;
337 GHashTable *hash;
338 gchar *ior,*child_chroot_pid_hashkey_dir;
339 int errint;
340 gchar *s,*sd,*se;
341 gboolean errbool;
342
343         /* Initialize /tmp/orbit-$username directory for linking IOR socket. */
344         errbool=corba_init("captive-sandbox-parent",&captive_corba_ev,&captive_corba_orb,&captive_corba_poa);
345         g_assert(errbool==TRUE);
346
347         /* FIXME: Security: Duplicate giop_tmpdir_init() here. */
348         if (!sandbox_parent_own_orbit_dir) {
349                 sandbox_parent_own_orbit_dir=g_strdup_printf("/tmp/captive-orbit-%d",getpid());
350                 if (mkdir(sandbox_parent_own_orbit_dir,0700)) {
351 DIR *dir;
352 struct dirent *dirent;
353                         g_assert(errno==EEXIST);
354
355                         dir=opendir(sandbox_parent_own_orbit_dir);
356                         g_assert(dir!=NULL);
357
358                         while (errno=0,(dirent=readdir(dir))) {
359 gchar *pathname;
360                                 if (!strcmp(dirent->d_name,".") || !strcmp(dirent->d_name,".."))
361                                         continue;
362                                 pathname=g_strdup_printf("%s/%s",sandbox_parent_own_orbit_dir,dirent->d_name);
363                                 errint=unlink(pathname);
364                                 g_assert(errint==0);
365                                 g_free(pathname);
366                                 }
367                         g_assert(errno==0);
368                         errint=closedir(dir);
369                         g_assert(errint==0);
370                         }
371                 linc_set_tmpdir(sandbox_parent_own_orbit_dir);
372                 }
373
374         data=captive_rtl_file_read(Vfs_IOR_fd_read,&data_size); /* data_fd_read */
375         errint=close(Vfs_IOR_fd_read);
376         g_assert(errint==0);
377         g_assert(data!=NULL);
378         g_assert(data_size>=1);
379         g_assert(data[data_size-1]=='\n');
380
381         hash=g_hash_table_new(g_str_hash,g_str_equal);
382
383         for (s=data;s<data+data_size;s=se+1) {
384                 se=strchr(s,'\n');
385                 g_assert(se!=NULL);
386                 *se=0;
387                 sd=strchr(s,'=');
388                 g_assert(sd!=NULL);
389                 *sd=0;
390                 g_hash_table_insert(hash,s,sd+1);
391                 }
392         g_assert(s==data+data_size);
393
394         if ((child_chroot_pid_hashkey_dir=g_hash_table_lookup(hash,"chroot_pid_hashkey_dir"))) {
395 const gchar *socketname,*socketpathname_src,*socketpathname_dest;
396 const gchar *chrooted_orbit_dir;
397
398                 /* IOR contains the full pathname with the setuid username encoded. */
399                 chrooted_orbit_dir=g_hash_table_lookup(hash,"chrooted_orbit_dir");
400                 g_assert(chrooted_orbit_dir!=NULL);
401                 if (mkdir(chrooted_orbit_dir,S_ISVTX|0777)) {
402                         g_assert(errno==EEXIST);
403                         }
404
405                 socketname=g_hash_table_lookup(hash,"socketname");
406                 g_assert(socketname!=NULL);
407
408                 socketpathname_src=captive_printf_alloca("%s/%s/%s",child_chroot_pid_hashkey_dir,chrooted_orbit_dir,socketname);
409                 socketpathname_dest=captive_printf_alloca("%s/%s",chrooted_orbit_dir,socketname);
410                 errint=link(socketpathname_src,socketpathname_dest);
411                 g_assert(errint==0);
412                 }
413         if (child_chroot_pid_hashkey_dirp)
414                 *child_chroot_pid_hashkey_dirp=g_strdup(child_chroot_pid_hashkey_dir);
415
416         ior=g_hash_table_lookup(hash,"ior");
417         g_assert(ior!=NULL);
418         ior=g_strdup(ior);
419         g_free(data);
420
421         g_hash_table_destroy(hash);
422
423         return ior;
424 }
425
426
427 static void sandbox_parent(const gchar *Vfs_IOR,const gchar *child_chroot_pid_hashkey_dir,
428                 struct captive_options *options_captive,Captive_Vfs *corba_Vfs_object_return,
429                 Captive_GLogFunc *corba_GLogFunc_object_return,Captive_CaptiveIOChannel *corba_CaptiveIOChannel_object_return)
430 {
431 Captive_Vfs Vfs_object;
432 Captive_GLogFunc GLogFunc_object;
433 Captive_CaptiveIOChannel CaptiveIOChannel_object;
434 Captive_CaptiveOptions options_corba;
435 guint load_module_length,load_moduleui;
436 struct captive_options_module *options_module;
437 GList *load_module_node;
438 const gchar *child_chroot_parent_own_orbit_socket,*child_chroot_parent_own_orbit_dir;
439 int errint;
440
441         g_return_if_fail(Vfs_IOR!=NULL);
442         g_return_if_fail(corba_Vfs_object_return!=NULL);
443         g_return_if_fail(corba_GLogFunc_object_return!=NULL);
444         g_return_if_fail(corba_CaptiveIOChannel_object_return!=NULL);
445
446         Vfs_object=CORBA_ORB_string_to_object(captive_corba_orb,Vfs_IOR,&captive_corba_ev);
447         g_assert(validate_CORBA_Environment(&captive_corba_ev));
448
449         /* Init 'GLogFunc_object' */
450         GLogFunc_object=impl_Captive_GLogFunc__create(captive_corba_poa,&captive_corba_ev);
451         g_assert(validate_CORBA_Environment(&captive_corba_ev));
452
453         /* Init 'CaptiveIOChannel_object' */
454         CaptiveIOChannel_object=impl_Captive_CaptiveIOChannel__create(captive_corba_poa,
455                         options_captive->image_iochannel,&captive_corba_ev);
456         g_assert(validate_CORBA_Environment(&captive_corba_ev));
457
458         /* Create the socket needed for 'sandbox_parent_own_orbit_socket' below
459          * by CORBA_ORB_object_to_string()->ORBit_marshal_object()->IOP_generate_profiles()->ORBit_ORB_start_servers...
460          */
461         {
462 char *GLogFunc_IOR;
463                 GLogFunc_IOR=CORBA_ORB_object_to_string(captive_corba_orb,GLogFunc_object,&captive_corba_ev);
464                 g_assert(validate_CORBA_Environment(&captive_corba_ev));
465                 g_assert(GLogFunc_IOR!=NULL);
466                 CORBA_free(GLogFunc_IOR);
467                 }
468
469         if (!sandbox_parent_own_orbit_socket) {
470 DIR *dir;
471 struct dirent *dirent;
472
473                 g_assert(sandbox_parent_own_orbit_dir!=NULL);
474
475                 dir=opendir(sandbox_parent_own_orbit_dir);
476                 g_assert(dir!=NULL);
477
478                 while (errno=0,(dirent=readdir(dir))) {
479                         if (!strcmp(dirent->d_name,".") || !strcmp(dirent->d_name,".."))
480                                 continue;
481                         g_assert(sandbox_parent_own_orbit_socket==NULL);
482                         sandbox_parent_own_orbit_socket=g_strdup_printf("%s/%s",sandbox_parent_own_orbit_dir,dirent->d_name);
483                         }
484                 g_assert(errno==0);
485                 errint=closedir(dir);
486                 g_assert(errint==0);
487                 g_assert(sandbox_parent_own_orbit_socket!=NULL);
488                 }
489
490         child_chroot_parent_own_orbit_dir=captive_printf_alloca("%s/%s",child_chroot_pid_hashkey_dir,sandbox_parent_own_orbit_dir);
491         errint=mkdir(child_chroot_parent_own_orbit_dir,0777);
492         g_assert(errint==0);
493         child_chroot_parent_own_orbit_socket=captive_printf_alloca("%s/%s",
494                         child_chroot_pid_hashkey_dir,sandbox_parent_own_orbit_socket);
495         errint=link(sandbox_parent_own_orbit_socket,child_chroot_parent_own_orbit_socket);
496         g_assert(errint==0);
497         /* chmod(2)s also our orig. one (!) but this one is protected by its 0777 directory. */
498         errint=chmod(child_chroot_parent_own_orbit_socket,0666);
499         g_assert(errint==0);
500
501         options_corba.g_log_func=GLogFunc_object;
502         options_module_captive_to_options_module_corba(&options_corba.filesystem,&options_captive->filesystem);
503         options_corba.rwmode        =options_captive->rwmode;
504         options_corba.media         =options_captive->media;
505         options_corba.debug_messages=options_captive->debug_messages;
506         options_corba.image_iochannel=CaptiveIOChannel_object;
507
508         load_module_length=g_list_length(options_captive->load_module);
509         captive_newn(options_corba.load_module._buffer,load_module_length);
510         options_corba.load_module._maximum=load_module_length;
511         options_corba.load_module._length=load_module_length;
512         options_corba.load_module._release=TRUE;
513         for (load_moduleui=0,load_module_node=options_captive->load_module;
514                         load_module_node;
515                         load_moduleui++,load_module_node=load_module_node->next) {
516                 options_module=load_module_node->data;
517                 options_module_captive_to_options_module_corba(options_corba.load_module._buffer+load_moduleui,options_module);
518                 }
519
520         Captive_Vfs_init(Vfs_object,&options_corba,&captive_corba_ev);
521         g_assert(validate_CORBA_Environment(&captive_corba_ev));
522
523         /* FIXME: Free 'options_corba' - LEAK */
524
525         *corba_Vfs_object_return=Vfs_object;
526         *corba_GLogFunc_object_return=GLogFunc_object;
527         *corba_CaptiveIOChannel_object_return=CaptiveIOChannel_object;
528 }
529
530 static void fd_shiftup(int *fdp)
531 {
532         g_return_if_fail(fdp!=NULL);
533
534         while (*fdp<=2 /* STDERR */) {
535                 *fdp=dup(*fdp);
536                 g_assert(*fdp!=-1);
537                 }
538 }
539
540 void captive_sandbox_fd_closeup(int fd_first_to_delete)
541 {
542 DIR *dir;
543 int errint;
544 int dir_fd;
545 struct dirent *dirent;
546
547         dir=opendir("/proc/self/fd/");
548         g_return_if_fail(dir!=NULL);
549         dir_fd=dirfd(dir);
550         g_return_if_fail(dir_fd!=-1);
551
552         while (errno=0,(dirent=readdir(dir))) {
553 long dirent_fd;
554 char *endptr;
555
556                 if (0
557                                 || !strcmp(dirent->d_name,".")
558                                 || !strcmp(dirent->d_name,".."))
559                         continue;
560                 dirent_fd=strtol(dirent->d_name,&endptr,10 /* base */);
561                 g_assert(dirent_fd>=0 && (!endptr || !*endptr));
562                 if (dirent_fd<fd_first_to_delete || dirent_fd==dir_fd)
563                         continue;
564
565                 errint=close(dirent_fd);
566                 g_assert(errint==0);
567                 errno=0;
568                 errint=close(dirent_fd);
569                 g_assert(errint==-1); g_assert(errno==EBADF);
570                 }
571         g_return_if_fail(errno==0);     /* check for EOF */
572
573         errint=closedir(dir);
574         g_return_if_fail(errint==0);
575         errno=0;
576         close(dir_fd); g_assert(errno==EBADF);  /* just a bit of paranoia; it should be already closed by closedir() */
577 }
578
579 gboolean captive_sandbox_spawn(CaptiveVfsObject *child_captive_vfs_object,
580                 Captive_Vfs *corba_Vfs_object_return,Captive_GLogFunc *corba_GLogFunc_object_return,
581                 Captive_CaptiveIOChannel *corba_CaptiveIOChannel_object_return,int *parentheart_fds_1_return,
582                 pid_t *child_pid_return)
583 {
584 /* Vfs_IOR_fds[0] for reading by sandbox_parent() - client,
585  * Vfs_IOR_fds[1] for writing by sandbox_child()  - server
586  */
587 int Vfs_IOR_fds[2],parentheart_fds[2];
588 int errint;
589
590         g_return_val_if_fail(child_captive_vfs_object!=NULL,FALSE);
591         g_return_val_if_fail(corba_Vfs_object_return!=NULL,FALSE);
592         g_return_val_if_fail(corba_GLogFunc_object_return!=NULL,FALSE);
593         g_return_val_if_fail(parentheart_fds_1_return!=NULL,FALSE);
594         g_return_val_if_fail(child_pid_return!=NULL,FALSE);
595
596         errint=pipe(Vfs_IOR_fds);
597         g_return_val_if_fail(errint==0,FALSE);
598         errint=pipe(parentheart_fds);
599         g_return_val_if_fail(errint==0,FALSE);
600
601         /* Currently never called anywhere:
602          *     errbool=corba_shutdown(&captive_corba_ev,&captive_corba_orb,&captive_corba_poa);
603          *     g_assert(errbool==TRUE);
604          */
605
606         if (child_captive_vfs_object->options.sandbox_server_ior) {
607                 g_assert(child_captive_vfs_object->options.sandbox_server_argv==NULL);
608                 *parentheart_fds_1_return=-1;
609                 *child_pid_return=-1;
610                 sandbox_parent(
611                                 child_captive_vfs_object->options.sandbox_server_ior,   /* Vfs_IOR */
612                                 NULL,   /* child_chroot_pid_hashkey_dir */
613                                 &child_captive_vfs_object->options,     /* options_captive */
614                                 corba_Vfs_object_return,        /* corba_Vfs_object_return */
615                                 corba_GLogFunc_object_return,   /* corba_GLogFunc_object_return */
616                                 corba_CaptiveIOChannel_object_return);  /* corba_CaptiveIOChannel_object_return */
617                 return TRUE;
618                 }
619
620         g_assert(child_captive_vfs_object->options.sandbox_server_argv!=NULL);
621         switch ((*child_pid_return=fork())) {
622                 case -1:        /* error */
623                         g_return_val_if_reached(FALSE);
624
625                 case 0: { /* child */
626
627                         errint=close(Vfs_IOR_fds[0]);   /* close Vfs_IOR_fd_read */
628                         g_return_val_if_fail(errint==0,FALSE);
629                         errint=close(parentheart_fds[1]);       /* close parentheart_fd_write */
630                         g_return_val_if_fail(errint==0,FALSE);
631
632                         fd_shiftup(Vfs_IOR_fds+1);      /* Vfs_IOR_fd_write */
633                         fd_shiftup(parentheart_fds+0);  /* parentheart_fd_read */
634                         errint=dup2(Vfs_IOR_fds[1],1 /* STDOUT */);     /* Vfs_IOR_fd_write */
635                         g_return_val_if_fail(errint==1 /* STDOUT */,FALSE);
636                         errint=dup2(parentheart_fds[0],0 /* STDIN */);  /* parentheart_fd_read */
637                         g_return_val_if_fail(errint==0 /* STDIN */,FALSE);
638
639                         captive_sandbox_fd_closeup(2 /* STDERR */ +1);
640
641                         execv(child_captive_vfs_object->options.sandbox_server_argv[0],
642                                         /* re-const */ (char * const *)child_captive_vfs_object->options.sandbox_server_argv);
643                         g_return_val_if_reached(FALSE);
644                         } /* NOTREACHED */
645
646                 default: {      /* parent */
647 gchar *Vfs_IOR;
648 gchar *child_chroot_pid_hashkey_dir;
649
650                         errint=close(Vfs_IOR_fds[1]);   /* close Vfs_IOR_fd_write */
651                         g_return_val_if_fail(errint==0,FALSE);
652                         errint=close(parentheart_fds[0]);       /* close parentheart_fd_read */
653                         g_return_val_if_fail(errint==0,FALSE);
654                         errint=fcntl(parentheart_fds[1],F_SETFD,FD_CLOEXEC);
655                         /* This fcntl(2) may not be enough - some fork(2) may duplicate this
656                          * write descriptor and even if we do some process finish the child
657                          * may remain alone connected with some unknown fork(2)er from us.
658                          * Currently I am not aware such case would occur.
659                          */
660                         g_return_val_if_fail(errint==0,FALSE);
661                         *parentheart_fds_1_return=parentheart_fds[1];
662
663                         Vfs_IOR=sandbox_parent_read_ior(
664                                         Vfs_IOR_fds[0], /* Vfs_IOR_fd_read */
665                                         &child_chroot_pid_hashkey_dir);
666
667                         sandbox_parent(
668                                         Vfs_IOR,        /* Vfs_IOR */
669                                         child_chroot_pid_hashkey_dir,   /* child_chroot_pid_hashkey_dir */
670                                         &child_captive_vfs_object->options,     /* options_captive */
671                                         corba_Vfs_object_return,        /* corba_Vfs_object_return */
672                                         corba_GLogFunc_object_return,   /* corba_GLogFunc_object_return */
673                                         corba_CaptiveIOChannel_object_return);  /* corba_CaptiveIOChannel_object_return */
674
675                         g_free(Vfs_IOR);
676                         g_free(child_chroot_pid_hashkey_dir);
677
678                         /* 'parentheart_fds[1]' - parentheart_fd_write - is left open here */
679                         return TRUE;
680                         }
681                 }
682         /* NOTREACHED */
683         g_return_val_if_reached(FALSE);
684 }
685
686
687 GnomeVFSResult captive_sandbox_parent_return_from_CORBA_Environment(CORBA_Environment *evp)
688 {
689 GnomeVFSResult r;
690
691         if (evp->_major==CORBA_NO_EXCEPTION)
692                 return GNOME_VFS_OK;
693
694         if (evp->_major==CORBA_USER_EXCEPTION && !strcmp(ex_Captive_GnomeVFSResultException,CORBA_exception_id(evp)))
695                 r=((Captive_GnomeVFSResultException *)CORBA_exception_value(evp))->gnome_vfs_result;
696         else {
697                 r=GNOME_VFS_ERROR_GENERIC;
698                 g_warning(_("CORBA Exception occured: id=\"%s\", value=%p"),
699                                 CORBA_exception_id(evp),CORBA_exception_value(evp));
700                 }
701         CORBA_exception_free(evp);
702
703         return r;
704 }
705
706
707 gboolean captive_sandbox_parent_query_vfs_retry(CORBA_Environment *evp,CaptiveVfsObject *captive_vfs_object)
708 {
709 GnomeVFSResult errvfsresult;
710
711         g_return_val_if_fail(evp!=NULL,FALSE);
712         g_return_val_if_fail(captive_vfs_object!=NULL,FALSE);
713
714         /* If !child_captive_vfs_object->options.sandbox_server_argv it is child_captive_vfs_object->options.sandbox_server_ior
715          * where we cannot do any restart anyway.
716          */
717         if (!captive_vfs_object->options.sandbox_server_argv
718                         || !(evp->_major==CORBA_SYSTEM_EXCEPTION && !strcmp(ex_CORBA_COMM_FAILURE,CORBA_exception_id(evp))))
719                 return FALSE;   /* no retry */
720         CORBA_exception_free(evp);
721
722         captive_sandbox_parent_vfs_close(captive_vfs_object);   /* errors ignored */
723         errvfsresult=captive_sandbox_parent_vfs_new(captive_vfs_object);
724
725         return errvfsresult==GNOME_VFS_OK;      /* retry if restart succeeded */
726 }
727
728
729 void captive_sandbox_child_GnomeVFSResultException_throw(CORBA_Environment *evp,GnomeVFSResult errvfsresult)
730 {
731 Captive_GnomeVFSResultException *gnome_vfs_result_exception;
732
733         g_return_if_fail(evp!=NULL);
734
735         gnome_vfs_result_exception=Captive_GnomeVFSResultException__alloc();
736         gnome_vfs_result_exception->gnome_vfs_result=errvfsresult;
737         CORBA_exception_set(evp,CORBA_USER_EXCEPTION,ex_Captive_GnomeVFSResultException,gnome_vfs_result_exception);
738 }