2 * Connection of captive-vfs interface through CORBA/ORBit
3 * Copyright (C) 2002 Jan Kratochvil <project-captive@jankratochvil.net>
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
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.
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
22 #include "split.h" /* self */
24 #include "server-GLogFunc.h"
25 #include "captive/macros.h"
26 #include <glib/gmacros.h>
28 #include "captive/rtl-file.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"
37 #include "server-CaptiveIOChannel.h"
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)
47 gboolean validate_CORBA_Environment(CORBA_Environment *evp)
49 if (evp->_major==CORBA_NO_EXCEPTION)
52 g_message(_("CORBA Exception occured: id=\"%s\", value=%p"),
53 CORBA_exception_id(evp),CORBA_exception_value(evp));
57 CORBA_Environment captive_corba_ev;
58 CORBA_ORB captive_corba_orb;
59 PortableServer_POA captive_corba_poa;
61 static void corba_shutdown_atexit(void);
63 static gboolean corba_init(const char *pname,CORBA_Environment *evp,CORBA_ORB *orbp,PortableServer_POA *poap)
65 static gboolean done=FALSE;
68 (gchar *)captive_strdup_alloca(pname),
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);
79 CORBA_exception_init(evp);
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);
87 *poap=(PortableServer_POA)CORBA_ORB_resolve_initial_references(*orbp,"RootPOA",evp);
88 g_return_val_if_fail(validate_CORBA_Environment(evp),FALSE);
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);
99 g_atexit(corba_shutdown_atexit);
106 static CORBA_ORB heartbeat_source_callback_orb=CORBA_OBJECT_NIL;
108 gboolean corba_shutdown(CORBA_Environment *evp,CORBA_ORB *orbp,PortableServer_POA *poap)
110 PortableServer_POA poa;
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);
119 *poap=CORBA_OBJECT_NIL;
120 CORBA_Object_release((CORBA_Object)poa,evp);
121 g_return_val_if_fail(validate_CORBA_Environment(evp),FALSE);
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);
131 CORBA_exception_free(evp);
136 static void corba_shutdown_atexit(void)
140 errbool=corba_shutdown(&captive_corba_ev,&captive_corba_orb,&captive_corba_poa);
141 g_assert(errbool==TRUE);
145 static GSource *captive_corba_sandbox_child_heartbeat_gsource;
147 void sandbox_child_prepare_shutdown(void)
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;
158 void sandbox_child_shutdown(void)
160 /* Do not fail by passing logging messages to the master. */
161 impl_Captive_Vfs_init_g_log_func_disable();
163 g_main_loop_quit(linc_main_get_loop());
167 static gboolean heartbeat_source_callback(gpointer data /* unused */)
169 g_return_val_if_fail(heartbeat_source_callback_orb!=CORBA_OBJECT_NIL,FALSE); /* the source should be removed */
171 sandbox_child_shutdown();
173 return FALSE; /* the source should be removed */
177 static gboolean heartbeat_source_prepare(GSource *source,gint *timeout)
184 static GPollFD heartbeat_source_check_gpollfd;
186 static gboolean heartbeat_source_check(GSource *source)
188 return !!(heartbeat_source_check_gpollfd.revents & HEARTBEAT_SOURCE_CHECK_REVENTS);
191 static gboolean heartbeat_source_dispatch(GSource *source,GSourceFunc callback,gpointer user_data)
193 g_assert(callback!=NULL);
194 return (*callback)(user_data);
197 static GSourceFuncs heartbeat_source_watch_funcs={
198 heartbeat_source_prepare,
199 heartbeat_source_check,
200 heartbeat_source_dispatch,
204 void captive_corba_sandbox_child(void)
206 Captive_Vfs Vfs_object;
207 impl_POA_Captive_Vfs *Vfs_servant;
213 /* attach heartbeat_source_callback() to watch for any abnormalities
214 * on our open pipe 'parentheart_fds' and terminate the child if parent dies.
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 */
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);
228 errbool=corba_init("captive-sandbox-child",&captive_corba_ev,&captive_corba_orb,&captive_corba_poa);
229 g_return_if_fail(errbool==TRUE);
231 heartbeat_source_callback_orb=captive_corba_orb;
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);
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));
243 /* pass IOR to our parent */
246 Vfs_IOR=CORBA_ORB_object_to_string(captive_corba_orb,Vfs_object,&captive_corba_ev);
247 g_assert(validate_CORBA_Environment(&captive_corba_ev));
248 g_assert(Vfs_IOR!=NULL);
249 errint=write(1 /* STDOUT */,Vfs_IOR,strlen(Vfs_IOR)+1);
250 g_assert((unsigned)errint==strlen(Vfs_IOR)+1);
251 errint=close(1 /* STDOUT */);
256 /* CORBA_ORB_run() -> linc_main_loop_run() -> g_main_loop_run()
257 * and therefore we should be safe with glib events handling.
259 CORBA_ORB_run(captive_corba_orb,&captive_corba_ev);
260 g_assert(validate_CORBA_Environment(&captive_corba_ev));
262 /* Shutdown 'Vfs' servant */
263 Vfs_servant=PortableServer_POA_reference_to_servant(captive_corba_poa,Vfs_object,&captive_corba_ev);
264 g_assert(validate_CORBA_Environment(&captive_corba_ev));
265 CORBA_Object_release(Vfs_object,&captive_corba_ev);
266 g_assert(validate_CORBA_Environment(&captive_corba_ev));
267 impl_Captive_Vfs__destroy(Vfs_servant,&captive_corba_ev);
268 g_assert(validate_CORBA_Environment(&captive_corba_ev));
270 errbool=corba_shutdown(&captive_corba_ev,&captive_corba_orb,&captive_corba_poa);
271 g_assert(errbool==TRUE);
277 static void options_module_captive_to_options_module_corba
278 (Captive_CaptiveOptionsModule *dest_options_module_corba,const struct captive_options_module *src_options_module_captive)
280 g_return_if_fail(dest_options_module_corba!=NULL);
281 g_return_if_fail(src_options_module_captive!=NULL);
283 g_return_if_fail(src_options_module_captive->type==CAPTIVE_OPTIONS_MODULE_TYPE_PE32);
285 dest_options_module_corba->pathname_utf8=g_strdup(src_options_module_captive->pathname_utf8);
286 dest_options_module_corba->data._buffer=g_memdup(src_options_module_captive->u.pe32.base,
287 src_options_module_captive->u.pe32.length);
288 dest_options_module_corba->data._maximum=src_options_module_captive->u.pe32.length;
289 dest_options_module_corba->data._length =src_options_module_captive->u.pe32.length;
290 dest_options_module_corba->data._release=TRUE;
294 static void sandbox_parent(const char *Vfs_IOR,struct captive_options *options_captive,Captive_Vfs *corba_Vfs_object_return,
295 Captive_GLogFunc *corba_GLogFunc_object_return,Captive_CaptiveIOChannel *corba_CaptiveIOChannel_object_return)
297 Captive_Vfs Vfs_object;
298 Captive_GLogFunc GLogFunc_object;
299 Captive_CaptiveIOChannel CaptiveIOChannel_object;
300 Captive_CaptiveOptions options_corba;
301 guint load_module_length,load_moduleui;
302 struct captive_options_module *options_module;
304 GList *load_module_node;
306 g_return_if_fail(Vfs_IOR!=NULL);
307 g_return_if_fail(corba_Vfs_object_return!=NULL);
308 g_return_if_fail(corba_GLogFunc_object_return!=NULL);
310 errbool=corba_init("captive-sandbox-parent",&captive_corba_ev,&captive_corba_orb,&captive_corba_poa);
311 g_assert(errbool==TRUE);
313 Vfs_object=CORBA_ORB_string_to_object(captive_corba_orb,Vfs_IOR,&captive_corba_ev);
314 g_assert(validate_CORBA_Environment(&captive_corba_ev));
316 /* Init 'GLogFunc_object' */
317 GLogFunc_object=impl_Captive_GLogFunc__create(captive_corba_poa,&captive_corba_ev);
318 g_assert(validate_CORBA_Environment(&captive_corba_ev));
320 /* Init 'CaptiveIOChannel_object' */
321 CaptiveIOChannel_object=impl_Captive_CaptiveIOChannel__create(captive_corba_poa,
322 options_captive->image_iochannel,&captive_corba_ev);
323 g_assert(validate_CORBA_Environment(&captive_corba_ev));
325 options_corba.g_log_func=GLogFunc_object;
326 options_module_captive_to_options_module_corba(&options_corba.filesystem,&options_captive->filesystem);
327 options_corba.rwmode =options_captive->rwmode;
328 options_corba.media =options_captive->media;
329 options_corba.debug_messages=options_captive->debug_messages;
330 options_corba.image_iochannel=CaptiveIOChannel_object;
332 load_module_length=g_list_length(options_captive->load_module);
333 captive_newn(options_corba.load_module._buffer,load_module_length);
334 options_corba.load_module._maximum=load_module_length;
335 options_corba.load_module._length=load_module_length;
336 options_corba.load_module._release=TRUE;
337 for (load_moduleui=0,load_module_node=options_captive->load_module;
339 load_moduleui++,load_module_node=load_module_node->next) {
340 options_module=load_module_node->data;
341 options_module_captive_to_options_module_corba(options_corba.load_module._buffer+load_moduleui,options_module);
344 Captive_Vfs_init(Vfs_object,&options_corba,&captive_corba_ev);
345 g_assert(validate_CORBA_Environment(&captive_corba_ev));
347 /* FIXME: Free 'options_corba' - LEAK */
349 *corba_Vfs_object_return=Vfs_object;
350 *corba_GLogFunc_object_return=GLogFunc_object;
351 *corba_CaptiveIOChannel_object_return=CaptiveIOChannel_object;
354 static void fd_shiftup(int *fdp)
356 g_return_if_fail(fdp!=NULL);
358 while (*fdp<=2 /* STDERR */) {
364 void captive_sandbox_fd_closeup(int fd_first_to_delete)
369 struct dirent *dirent;
371 dir=opendir("/proc/self/fd/");
372 g_return_if_fail(dir!=NULL);
374 g_return_if_fail(dir_fd!=-1);
376 while (errno=0,(dirent=readdir(dir))) {
381 || !strcmp(dirent->d_name,".")
382 || !strcmp(dirent->d_name,".."))
384 dirent_fd=strtol(dirent->d_name,&endptr,10 /* base */);
385 g_assert(dirent_fd>=0 && (!endptr || !*endptr));
386 if (dirent_fd<fd_first_to_delete || dirent_fd==dir_fd)
389 errint=close(dirent_fd);
392 errint=close(dirent_fd);
393 g_assert(errint==-1); g_assert(errno==EBADF);
395 g_return_if_fail(errno==0); /* check for EOF */
397 errint=closedir(dir);
398 g_return_if_fail(errint==0);
400 close(dir_fd); g_assert(errno==EBADF); /* just a bit of paranoia; it should be already closed by closedir() */
403 gboolean captive_sandbox_spawn(CaptiveVfsObject *child_captive_vfs_object,
404 Captive_Vfs *corba_Vfs_object_return,Captive_GLogFunc *corba_GLogFunc_object_return,
405 Captive_CaptiveIOChannel *corba_CaptiveIOChannel_object_return,int *parentheart_fds_1_return)
407 /* Vfs_IOR_fds[0] for reading by sandbox_parent() - client,
408 * Vfs_IOR_fds[1] for writing by sandbox_child() - server
410 int Vfs_IOR_fds[2],parentheart_fds[2];
413 g_return_val_if_fail(child_captive_vfs_object!=NULL,FALSE);
414 g_return_val_if_fail(corba_Vfs_object_return!=NULL,FALSE);
415 g_return_val_if_fail(corba_GLogFunc_object_return!=NULL,FALSE);
416 g_return_val_if_fail(parentheart_fds_1_return!=NULL,FALSE);
418 errint=pipe(Vfs_IOR_fds);
419 g_return_val_if_fail(errint==0,FALSE);
420 errint=pipe(parentheart_fds);
421 g_return_val_if_fail(errint==0,FALSE);
423 /* Currently never called anywhere:
424 * errbool=corba_shutdown(&captive_corba_ev,&captive_corba_orb,&captive_corba_poa);
425 * g_assert(errbool==TRUE);
428 if (captive_sandbox_server_ior) {
429 *parentheart_fds_1_return=-1;
431 captive_sandbox_server_ior, /* Vfs_IOR */
432 &child_captive_vfs_object->options, /* options_captive */
433 corba_Vfs_object_return, /* corba_Vfs_object_return */
434 corba_GLogFunc_object_return, /* corba_GLogFunc_object_return */
435 corba_CaptiveIOChannel_object_return); /* corba_CaptiveIOChannel_object_return */
439 g_assert(captive_sandbox_server_argv!=NULL);
442 g_return_val_if_reached(FALSE);
444 case 0: { /* child */
446 errint=close(Vfs_IOR_fds[0]); /* close Vfs_IOR_fd_read */
447 g_return_val_if_fail(errint==0,FALSE);
448 errint=close(parentheart_fds[1]); /* close parentheart_fd_write */
449 g_return_val_if_fail(errint==0,FALSE);
451 fd_shiftup(Vfs_IOR_fds+1); /* Vfs_IOR_fd_write */
452 fd_shiftup(parentheart_fds+0); /* parentheart_fd_read */
453 errint=dup2(Vfs_IOR_fds[1],1 /* STDOUT */); /* Vfs_IOR_fd_write */
454 g_return_val_if_fail(errint==1 /* STDOUT */,FALSE);
455 errint=dup2(parentheart_fds[0],0 /* STDIN */); /* parentheart_fd_read */
456 g_return_val_if_fail(errint==0 /* STDIN */,FALSE);
458 captive_sandbox_fd_closeup(2 /* STDERR */ +1);
460 execv(captive_sandbox_server_argv[0],/* re-const */ (char * const *)captive_sandbox_server_argv);
461 g_return_val_if_reached(FALSE);
464 default: { /* parent */
468 errint=close(Vfs_IOR_fds[1]); /* close Vfs_IOR_fd_write */
469 g_return_val_if_fail(errint==0,FALSE);
470 errint=close(parentheart_fds[0]); /* close parentheart_fd_read */
471 g_return_val_if_fail(errint==0,FALSE);
472 errint=fcntl(parentheart_fds[1],F_SETFD,FD_CLOEXEC);
473 /* This fcntl(2) may not be enough - some fork(2) may duplicate this
474 * write descriptor and even if we do some process finish the child
475 * may remain alone connected with some unknown fork(2)er from us.
476 * Currently I am not aware such case would occur.
478 g_return_val_if_fail(errint==0,FALSE);
479 *parentheart_fds_1_return=parentheart_fds[1];
481 Vfs_IOR=captive_rtl_file_read(Vfs_IOR_fds[0],&Vfs_IOR_size); /* Vfs_IOR_fd_read */
482 g_assert(Vfs_IOR!=NULL);
483 g_assert(Vfs_IOR_size>=1);
484 g_assert(memchr(Vfs_IOR,0,Vfs_IOR_size)==Vfs_IOR+(Vfs_IOR_size-1)); /* check exactly for 0-terminated string */
485 errint=close(Vfs_IOR_fds[0]); /* Vfs_IOR_fd_read */
489 Vfs_IOR, /* Vfs_IOR */
490 &child_captive_vfs_object->options, /* options_captive */
491 corba_Vfs_object_return, /* corba_Vfs_object_return */
492 corba_GLogFunc_object_return, /* corba_GLogFunc_object_return */
493 corba_CaptiveIOChannel_object_return); /* corba_CaptiveIOChannel_object_return */
497 /* 'parentheart_fds[1]' - parentheart_fd_write - is left open here */
502 g_return_val_if_reached(FALSE);
506 GnomeVFSResult captive_sandbox_parent_return_from_CORBA_Environment(CORBA_Environment *evp)
510 if (evp->_major==CORBA_NO_EXCEPTION)
513 if (evp->_major==CORBA_USER_EXCEPTION && !strcmp(ex_Captive_GnomeVFSResultException,CORBA_exception_id(evp)))
514 r=((Captive_GnomeVFSResultException *)CORBA_exception_value(evp))->gnome_vfs_result;
516 r=GNOME_VFS_ERROR_GENERIC;
517 g_warning(_("CORBA Exception occured: id=\"%s\", value=%p"),
518 CORBA_exception_id(evp),CORBA_exception_value(evp));
520 CORBA_exception_free(evp);
526 gboolean captive_sandbox_parent_query_vfs_retry(CORBA_Environment *evp,CaptiveVfsObject *captive_vfs_object)
528 GnomeVFSResult errvfsresult;
530 g_return_val_if_fail(evp!=NULL,FALSE);
531 g_return_val_if_fail(captive_vfs_object!=NULL,FALSE);
533 /* If !captive_sandbox_server_argv it is captive_sandbox_server_ior
534 * where we cannot do any restart anyway.
536 if (!captive_sandbox_server_argv
537 || !(evp->_major==CORBA_SYSTEM_EXCEPTION && !strcmp(ex_CORBA_COMM_FAILURE,CORBA_exception_id(evp))))
538 return FALSE; /* no retry */
539 CORBA_exception_free(evp);
541 captive_sandbox_parent_vfs_close(captive_vfs_object); /* errors ignored */
542 errvfsresult=captive_sandbox_parent_vfs_new(captive_vfs_object);
544 return errvfsresult==GNOME_VFS_OK; /* retry if restart succeeded */
548 void captive_sandbox_child_GnomeVFSResultException_throw(CORBA_Environment *evp,GnomeVFSResult errvfsresult)
550 Captive_GnomeVFSResultException *gnome_vfs_result_exception;
552 g_return_if_fail(evp!=NULL);
554 gnome_vfs_result_exception=Captive_GnomeVFSResultException__alloc();
555 gnome_vfs_result_exception->gnome_vfs_result=errvfsresult;
556 CORBA_exception_set(evp,CORBA_USER_EXCEPTION,ex_Captive_GnomeVFSResultException,gnome_vfs_result_exception);