+general captive shutdown
[captive.git] / src / libcaptive / client / init.c
index 343e3c3..f90f4ba 100644 (file)
@@ -42,6 +42,7 @@
 #include <popt.h>
 #include <glib/gstrfuncs.h>
 #include <glib/glist.h>
+#include <glib/gutils.h>       /* for g_atexit() */
 
 
 /* Are we initialized? */
@@ -280,6 +281,9 @@ NTSTATUS err;
 }
 
 
+static void captive_shutdown_atexit(void);
+
+
 /**
  * captive_init:
  * @captive_args: String with possible options to parse by popt.
@@ -371,6 +375,42 @@ args_err:
        errbool=captive_w32_init();
        g_return_val_if_fail(errbool==TRUE,FALSE);
 
+       g_atexit(captive_shutdown_atexit);
+
        active=TRUE;
        return TRUE;
 }
+
+
+/**
+ * captive_shutdown:
+ *
+ * Closes down %libcaptive. It should flush all pending buffers and successfuly
+ * close the filesystem. Variable #captive_image_iochannel will be set to %NULL,
+ * you should close such channel yourself.
+ *
+ * Returns: %TRUE if successfuly shutdown.
+ */
+gboolean captive_shutdown(void)
+{
+       g_return_val_if_fail(active==TRUE,FALSE);
+       g_return_val_if_fail(captive_image_iochannel!=NULL,FALSE);
+
+       /* FIXME: ntoskrnl/ex/power.c/NtShutdownSystem() does
+        * IoShutdownRegistered{Devices,FileSystems} order; is it correct?
+        */
+       IoShutdownRegisteredFileSystems();
+       IoShutdownRegisteredDevices();
+
+       captive_image_iochannel=NULL;
+
+       active=FALSE;
+       return TRUE;
+}
+
+
+static void captive_shutdown_atexit(void)
+{
+       if (active)
+               captive_shutdown();
+}