Simplified captive_sandbox_spawn() prototype.
[captive.git] / src / libcaptive / sandbox / parent-Vfs.c
1 /* $Id$
2  * CORBA/ORBit client side of Vfs object of sandbox_parent()
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 "captive/parent-Vfs.h" /* self */
23 #include <glib/gmessages.h>
24 #include "split.h"
25 #include "../client/vfs.h"
26 #include "sandbox.h"
27 #include <unistd.h>
28 #include "server-GLogFunc.h"
29 #include "server-CaptiveIOChannel.h"
30 #include <signal.h>     /* for kill(2) */
31 #include <wait.h>
32 #include "../client/giochannel-blind.h" /* for captive_giochannel_blind_commit() */
33
34
35 GnomeVFSResult captive_sandbox_parent_vfs_new(CaptiveVfsObject *captive_vfs_object)
36 {
37 gboolean errbool;
38
39         g_return_val_if_fail(captive_vfs_object!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
40
41         captive_vfs_object->is_sandbox_parent=TRUE;
42
43         errbool=captive_sandbox_spawn(captive_vfs_object);
44         g_return_val_if_fail(errbool==TRUE,GNOME_VFS_ERROR_GENERIC);
45
46         return GNOME_VFS_OK;
47 }
48
49
50 GnomeVFSResult captive_sandbox_parent_vfs_close(CaptiveVfsObject *captive_vfs_object)
51 {
52 GnomeVFSResult r;
53 int errint;
54 impl_POA_Captive_GLogFunc *GLogFunc_servant;
55 impl_POA_Captive_CaptiveIOChannel *CaptiveIOChannel_servant;
56 GIOStatus erriostatus;
57
58         g_return_val_if_fail(captive_vfs_object!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
59
60         Captive_Vfs_shutdown(captive_vfs_object->corba_Vfs_object,&captive_corba_ev);
61         r=captive_sandbox_parent_return_from_CORBA_Environment(&captive_corba_ev);
62
63         CORBA_Object_release(captive_vfs_object->corba_Vfs_object,&captive_corba_ev);
64         g_assert(validate_CORBA_Environment(&captive_corba_ev));
65
66         /* Shutdown 'GLogFunc' servant. */
67         GLogFunc_servant=PortableServer_POA_reference_to_servant(captive_corba_poa,
68                         captive_vfs_object->corba_GLogFunc_object,&captive_corba_ev);
69         g_assert(validate_CORBA_Environment(&captive_corba_ev));
70         CORBA_Object_release(captive_vfs_object->corba_GLogFunc_object,&captive_corba_ev);
71         g_assert(validate_CORBA_Environment(&captive_corba_ev));
72         impl_Captive_GLogFunc__destroy(GLogFunc_servant,&captive_corba_ev);
73         g_assert(validate_CORBA_Environment(&captive_corba_ev));
74
75         /* Shutdown 'CaptiveIOChannel' servant. */
76         CaptiveIOChannel_servant=PortableServer_POA_reference_to_servant(captive_corba_poa,
77                         captive_vfs_object->corba_CaptiveIOChannel_object,&captive_corba_ev);
78         g_assert(validate_CORBA_Environment(&captive_corba_ev));
79         CORBA_Object_release(captive_vfs_object->corba_CaptiveIOChannel_object,&captive_corba_ev);
80         g_assert(validate_CORBA_Environment(&captive_corba_ev));
81         impl_Captive_CaptiveIOChannel__destroy(CaptiveIOChannel_servant,&captive_corba_ev);
82         g_assert(validate_CORBA_Environment(&captive_corba_ev));
83
84         /* Close parentheart_fd_write. */
85         if (captive_vfs_object->corba_parentheart_fds_1!=-1) {
86                 errint=close(captive_vfs_object->corba_parentheart_fds_1);
87                 g_return_val_if_fail(errint==0,FALSE);
88                 }
89
90         /* Cleanup the child process. */
91         if (captive_vfs_object->corba_child_pid!=(pid_t)-1) {
92                 /*
93                  *
94                  *
95                  *
96                  *
97                  */
98                 kill(captive_vfs_object->corba_child_pid,SIGKILL);      /* errors ignored */
99                 /*
100                  *
101                  */
102                 waitpid(captive_vfs_object->corba_child_pid,
103                                 NULL,   /* status */
104                                 WNOHANG);       /* options */
105                 }
106
107         if (r==GNOME_VFS_OK) {
108                 erriostatus=captive_giochannel_blind_commit(captive_vfs_object->corba_parent_giochanel_blind);
109                 g_assert(erriostatus==G_IO_STATUS_NORMAL);
110                 }
111         else {  /* sandbox child failure */
112                 /* Flush the channel to catch all the disk accesses to the bugreport. */
113                 erriostatus=g_io_channel_flush(
114                                 captive_vfs_object->corba_parent_giochanel_blind,       /* channel */
115                                 NULL);  /* error */
116                 g_assert(erriostatus==G_IO_STATUS_NORMAL);
117
118                 /* TODO: Summarize the bugreport. */
119                 }
120         g_io_channel_unref(captive_vfs_object->corba_parent_giochanel_blind);
121         captive_vfs_object->corba_parent_giochanel_blind=NULL;
122
123         return r;
124 }
125
126
127 GnomeVFSResult captive_sandbox_parent_vfs_commit(CaptiveVfsObject *captive_vfs_object)
128 {
129 GnomeVFSResult r_close,r_new;
130
131         g_return_val_if_fail(captive_vfs_object!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
132
133         r_close=captive_sandbox_parent_vfs_close(captive_vfs_object);   /* errors ignored */
134         r_new=captive_sandbox_parent_vfs_new(captive_vfs_object);
135
136         return (r_new!=GNOME_VFS_OK ? r_new : r_close);
137 }