+Workaround Linux kernel last device block inaccessibility.
[captive.git] / src / libcaptive / sandbox / split.c
index 72107a1..d58c8ee 100644 (file)
@@ -28,7 +28,6 @@
 #include "captive/rtl-file.h"
 #include <unistd.h>
 #include <fcntl.h>
-#include <linc/linc.h> /* for linc_main_get_loop() */
 #include "server-Directory.h"
 #include "server-Vfs.h"
 #include "../client/vfs.h"
 #include <errno.h>
 #include "../client/giochannel-blind.h"        /* for captive_giochannel_blind_new() */
 #include <signal.h>
+#include "../storage/relastblock.h"    /* for captive_storage_relastblock() */
+
+#ifdef HAVE_ORBIT_LINK
+void link_set_tmpdir(const char *dir);
+#else
+#include <linc/linc.h> /* for linc_main_get_loop() */
+#endif
 
 
 /* CONFIG: */
@@ -82,7 +88,13 @@ gchar *orb_argv[]={
        CORBA_exception_init(evp);
 
        /* Init 'orb' */
-       *orbp=CORBA_ORB_init(&orb_argc,orb_argv,"orbit-local-orb",evp);
+       /* libcaptive is single-threaded only, caller must lock it.
+        * If thread A spawned the sandbox while currently doing its own work
+        * and thread B calls the sandbox thread B waits on ORB_run()
+        * while the sandbox waits for the response of thread A ORB. Deadlock.
+        * "orbit-local-non-threaded-orb" requests thread unaware ORB.
+        */
+       *orbp=CORBA_ORB_init(&orb_argc,orb_argv,"orbit-local-non-threaded-orb",evp);
        g_return_val_if_fail(*orbp!=CORBA_OBJECT_NIL,FALSE);
        g_return_val_if_fail(validate_CORBA_Environment(evp),FALSE);
 
@@ -168,13 +180,30 @@ void sandbox_child_prepare_shutdown(void)
 }
 #endif
 
+GMainLoop *captive_corba_get_loop(void)
+{
+#ifdef HAVE_ORBIT_LINK
+static GMainLoop *link_loop;
+
+       if (!link_loop)
+               link_loop=g_main_loop_new(g_main_context_default(),TRUE);
+       return link_loop;
+#else
+       return linc_main_get_loop();
+#endif
+}
+
+GMainContext *captive_corba_get_context(void)
+{
+       return g_main_loop_get_context(captive_corba_get_loop());
+}
 
 void sandbox_child_shutdown(void)
 {
        /* Do not fail by passing logging messages to the master. */
        impl_Captive_Vfs_init_g_log_func_disable();
 
-       g_main_loop_quit(linc_main_get_loop());
+       g_main_loop_quit(captive_corba_get_loop());
 }
 
 
@@ -243,10 +272,10 @@ int errint;
 
        heartbeat_source_callback_orb=captive_corba_orb;
 
-       /* linc_main_get_loop() makes sense only after corba_init() -> CORBA_ORB_init() */
+       /* captive_corba_get_context() makes sense only after corba_init() -> CORBA_ORB_init() */
        errguint=g_source_attach(
                        captive_corba_sandbox_child_heartbeat_gsource,  /* source */
-                       g_main_loop_get_context(linc_main_get_loop())); /* context; NULL means 'default context' */
+                       captive_corba_get_context());   /* context; NULL means 'default context' */
        g_assert(errguint!=0);
 
        /* Init 'Vfs_object' */
@@ -305,8 +334,12 @@ const gchar *socketname=NULL,*socketpathname;
        /* CORBA_ORB_run() -> linc_main_loop_run() -> g_main_loop_run()
         * and therefore we should be safe with glib events handling.
         */
+#ifdef HAVE_ORBIT_LINK
+       g_main_loop_run(captive_corba_get_loop());
+#else
        CORBA_ORB_run(captive_corba_orb,&captive_corba_ev);
        g_assert(validate_CORBA_Environment(&captive_corba_ev));
+#endif
 
        /* Shutdown 'Vfs' servant */
        Vfs_servant=PortableServer_POA_reference_to_servant(captive_corba_poa,Vfs_object,&captive_corba_ev);
@@ -463,7 +496,11 @@ gboolean errbool;
                                g_assert_not_reached();
                        }
                sandbox_parent_own_orbit_dir_cleanup_init();
+#ifdef HAVE_ORBIT_LINK
+               link_set_tmpdir(sandbox_parent_own_orbit_dir);
+#else
                linc_set_tmpdir(sandbox_parent_own_orbit_dir);
+#endif
                }
 
        data=captive_rtl_file_read(Vfs_IOR_fd_read,&data_size); /* data_fd_read */
@@ -616,19 +653,32 @@ int errint;
        g_assert(validate_CORBA_Environment(&captive_corba_ev));
 
        /* Init 'CaptiveIOChannel_object' */
-       if (!captive_vfs_parent_object->corba_parent_giochanel_blind_source)
+       if (!captive_vfs_parent_object->corba_parent_giochanel_blind_source) {
+GIOChannel *giochannel;
+
+               giochannel=CAPTIVE_VFS_OBJECT(captive_vfs_parent_object)->options.image_iochannel;
+               g_io_channel_ref(giochannel);
+
+               giochannel=captive_storage_relastblock(giochannel);
+
                switch (CAPTIVE_VFS_OBJECT(captive_vfs_parent_object)->options.rwmode) {
                        case CAPTIVE_OPTION_RWMODE_RO:
                        case CAPTIVE_OPTION_RWMODE_RW:
-                               captive_vfs_parent_object->corba_parent_giochanel_blind_source=CAPTIVE_VFS_OBJECT(captive_vfs_parent_object)->options.image_iochannel;
                                break;
-                       case CAPTIVE_OPTION_RWMODE_BLIND:
-                               captive_vfs_parent_object->corba_parent_giochanel_blind_source=(GIOChannel *)captive_giochannel_blind_new(
-                                               CAPTIVE_VFS_OBJECT(captive_vfs_parent_object)->options.image_iochannel, /* giochannel_orig */
+                       case CAPTIVE_OPTION_RWMODE_BLIND: {
+GIOChannel *giochannel_orig;
+
+                               giochannel_orig=giochannel;
+                               giochannel=(GIOChannel *)captive_giochannel_blind_new(
+                                               giochannel,     /* giochannel_orig */
                                                TRUE); /* writeable */
-                               break;
+                               g_io_channel_unref(giochannel_orig);    /* reffed by captive_giochannel_blind_new() */
+                               } break;
                        default: g_assert_not_reached();
                        }
+               captive_vfs_parent_object->corba_parent_giochanel_blind_source=giochannel;
+               }
+
        if (!captive_vfs_parent_object->corba_parent_giochanel_blind)
                captive_vfs_parent_object->corba_parent_giochanel_blind=(GIOChannel *)captive_giochannel_blind_new(
                                captive_vfs_parent_object->corba_parent_giochanel_blind_source,  /* giochannel_orig */