+Workaround Linux kernel last device block inaccessibility.
[captive.git] / src / libcaptive / client / init.c
index 97876c1..7a963a0 100644 (file)
@@ -1,6 +1,6 @@
 /* $Id$
  * Init and cleanup code of libcaptive to be called by client application
- * Copyright (C) 2002 Jan Kratochvil <project-captive@jankratochvil.net>
+ * Copyright (C) 2002-2003 Jan Kratochvil <project-captive@jankratochvil.net>
  * 
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -19,6 +19,7 @@
 
 #include "config.h"
 
+#include "init.h"      /* self */
 #include "captive/ldr.h"
 #include "captive/ldr_exports.h"
 #include "captive/unicode.h"
@@ -31,7 +32,7 @@
 #include <fcntl.h>
 #include <sys/mman.h>  /* for PROT_READ, MAP_SHARED */
 #include "reactos/ddk/kefuncs.h"       /* for KeInitializeSpinLock() */
-#include "reactos/internal/ntoskrnl.h" /* for RtlpInitNlsTables() and IoInit() */
+#include "reactos/internal/ntoskrnl.h" /* for IoInit() */
 #include "reactos/internal/ps.h"       /* for PsInitProcessManagment() and PsInitThreadManagment() */
 #include "reactos/ddk/iofuncs.h"       /* for IoCreateFile() */
 #include "captive/storage.h"
@@ -49,6 +50,9 @@
 #include <libgnomevfs/gnome-vfs-result.h>
 #include "lib.h"
 #include <reactos/ddk/obfuncs.h>
+#include <syslog.h>
+#include "captive/macros.h"
+#include "../storage/relastblock.h"    /* for captive_storage_relastblock() */
 
 
 struct captive_options *captive_options;
@@ -71,29 +75,16 @@ static TOP_LEVEL_IRP TopLevelIrp;   /* TODO:thread */
 void *_local_unwind2_addr;
 
 
-/* Acceleration hack for ntoskrnl/dbg/print.c/DbgPrint() */
-gboolean captive_get_debug_messages(void)
-{
-       g_return_val_if_fail(captive_options!=NULL,TRUE);
-
-       return captive_options->debug_messages;
-}
-
+gboolean captive_debug_messages_disabled=FALSE;
 
 static gboolean captive_w32_init(void)
 {
 NTSTATUS err;
 gboolean errbool;
-GIOStatus erriostatus;
 
        g_return_val_if_fail(captive_options!=NULL,FALSE);
        g_return_val_if_fail(captive_options->filesystem.type!=CAPTIVE_OPTIONS_MODULE_TYPE_EMPTY,FALSE);
 
-       erriostatus=g_io_channel_set_encoding(captive_image_iochannel,
-                       NULL,   /* encoding; force binary data */
-                       NULL);  /* error */
-       g_assert(erriostatus==G_IO_STATUS_NORMAL);
-
        /* captive_giochannel_size() only _after_ g_io_channel_set_encoding() ! */
        captive_image_size=captive_giochannel_size(captive_image_iochannel);
        g_return_val_if_fail(captive_image_size>0,FALSE);
@@ -109,8 +100,8 @@ GIOStatus erriostatus;
                        /* Part of reactos/ntoskrnl/ldr/loader.c/LdrInit1() ends. */
                KeLowerIrql(DISPATCH_LEVEL);
                /*...*/
-               /* create default nls tables */
-               RtlpInitNlsTables();
+               /* FIXME: create default nls tables? Really still needed? */
+               /* Obsolete: RtlpInitNlsTables(); */
                /*...*/
                /* KeInit2() */
                        /*...*/
@@ -177,14 +168,17 @@ NTSTATUS err;
                *KeNumberProcessorsp=KeNumberProcessors;
                g_assert(*KeNumberProcessorsp==1);
                }
-       /* Apply captive_kernel_patches() AFTER any symbols sanity checks above! */
-       captive_kernel_patches();
+       /* Apply AFTER any symbols sanity checks above! */
+       if (captive_options->debug_messages)
+               captive_kernel_patches_debug();
+       else
+               captive_kernel_patches_nondebug();
 
        _local_unwind2_addr=captive_Module_GetExportAddress("ntoskrnl.exe","_local_unwind2");
 
        /* Initialize 'FsRtlLegalAnsiCharacterArray'.
         * It requires 'ntoskrnl.exe' loaded by 'captive_options->load_module' above;
-        * captive_kernel_patches() should not be needed.
+        * captive_kernel_patches_debug()/captive_kernel_patches_nondebug() should not be needed.
         */
        captive_FsRtlLegalAnsiCharacterArray_init();
 
@@ -208,21 +202,108 @@ NTSTATUS err;
                        captive_utf8_to_UnicodeString_alloca("\\captive\\filesystem")); /* DriverEntry_RegistryPath */
        g_return_val_if_fail(NT_SUCCESS(err),FALSE);
        if (captive_DriverObject_ReinitRoutine) {
-               (*captive_DriverObject_ReinitRoutine)(
+               captive_stdcall_call_12((CaptiveStdCallFunc12)captive_DriverObject_ReinitRoutine,
                                &captive_DriverObject,  /* DriverObject */
                                captive_DriverObject_ReinitRoutine_Context,     /* Context */
-                               1);     /* Count: # of calls of ReinitRoutine incl. the current one */
+                               (gpointer)1);   /* Count: # of calls of ReinitRoutine incl. the current one */
                }
 
        return TRUE;
 }
 
 
-static void    log_discard_func(const gchar *log_domain,GLogLevelFlags log_level,const gchar *message,gpointer user_data)
+static void    captive_log_init_g_log_func_discard
+               (const gchar *log_domain,GLogLevelFlags log_level,const gchar *message,gpointer user_data)
 {
+       g_return_if_fail(message!=NULL);
+
        /* NOP */
 }
 
+static void captive_log_init_g_log_func
+               (const gchar *log_domain,GLogLevelFlags log_level,const gchar *message,gpointer user_data /* unused */)
+{
+int priority;
+
+       g_return_if_fail(message!=NULL);
+
+               /* unused: LOG_EMERG */
+               /* unused: LOG_ALERT */
+       /**/ if (log_level&G_LOG_LEVEL_ERROR)
+               priority=LOG_CRIT;
+       else if (log_level&G_LOG_LEVEL_CRITICAL)
+               priority=LOG_ERR;
+       else if (log_level&G_LOG_LEVEL_WARNING)
+               priority=LOG_WARNING;
+       else if (log_level&G_LOG_LEVEL_MESSAGE)
+               priority=LOG_NOTICE;
+       else if (log_level&G_LOG_LEVEL_INFO)
+               priority=LOG_INFO;
+       else if (log_level&G_LOG_LEVEL_DEBUG)
+               priority=LOG_DEBUG;
+       else /* bogus? */
+               priority=LOG_WARNING;
+
+       syslog(priority,"%s%s%s",
+                       (!(log_level&G_LOG_FLAG_RECURSION) ? "" : "RECURSION: "),
+                       (!(log_level&G_LOG_FLAG_FATAL    ) ? "" : "FATAL: "),
+                       message);
+}
+
+void captive_log_init(const struct captive_options *captive_options)
+{
+       g_return_if_fail(captive_options!=NULL);
+
+       /* FIXME: Fix sharing of different 'debug_messages' for various sandboxes. */
+       captive_debug_messages_disabled=!captive_options->debug_messages;
+
+       /* FIXME: Fix sharing of different 'debug_messages' for various sandboxes. */
+       if (!captive_options->debug_messages) {
+               /* FIXME: Save handler_id and destroy it in captive_vfs_close(). */
+               g_log_set_handler(
+                               G_LOG_DOMAIN,   /* log_domain; "Captive" */
+                               0       /* log_levels */
+                                               | G_LOG_FLAG_RECURSION
+                                               | G_LOG_FLAG_FATAL
+                                               /* The same mask is in:
+                                                * libcaptive/sandbox/server-GLogFunc.c
+                                                * libcaptive/client/init.c
+                                                */
+                                               | G_LOG_LEVEL_MESSAGE
+                                               | G_LOG_LEVEL_INFO
+                                               | G_LOG_LEVEL_DEBUG,
+                               captive_log_init_g_log_func_discard,    /* log_func */
+                               NULL);  /* user_data */
+               }
+       /* We are not the sandboxed slave;
+        * 'syslog_facility' would be '-1' in slave anyway as it is not transferred through CORBA.
+        */
+       if (!captive_options->sandbox || (captive_options->sandbox_server_argv || captive_options->sandbox_server_ior)) {
+               if (captive_options->syslog_facility!=-1) {
+                       openlog(
+                                       /* FIXME: Prefix 'ident' by device/mountpoint. */
+                                       G_LOG_DOMAIN,   /* ident; "Captive"; FIXME: lowercase it for syslog(3)? */
+                                       LOG_CONS|LOG_PID,       /* options */
+                                       captive_options->syslog_facility);      /* facility */
+                       /* FIXME: Save handler_id and destroy it in captive_vfs_close(). */
+                       g_log_set_handler(
+                                       G_LOG_DOMAIN,   /* log_domain; "Captive" */
+                                       0       /* log_levels */
+                                                       | 0     /* !G_LOG_FLAG_RECURSION */
+                                                       | G_LOG_FLAG_FATAL
+                                                       | (G_LOG_LEVEL_MASK & ~(captive_options->debug_messages ? 0 : 0
+                                                                       /* The same mask is in:
+                                                                        * libcaptive/sandbox/server-GLogFunc.c
+                                                                        * libcaptive/client/init.c
+                                                                        */
+                                                                       | G_LOG_LEVEL_MESSAGE
+                                                                       | G_LOG_LEVEL_INFO
+                                                                       | G_LOG_LEVEL_DEBUG)),
+                                       (GLogFunc)captive_log_init_g_log_func,  /* log_func */
+                                       NULL);  /* user_data */
+                       }
+               }
+}
 
 /**
  * captive_init:
@@ -253,29 +334,21 @@ gboolean errbool;
        /* Initialize GObject subsystem of GLib. */
        g_type_init();
 
-       if (!captive_options->debug_messages) {
-               g_log_set_handler(
-                               G_LOG_DOMAIN,   /* log_domain; "Captive" */
-                               0       /* log_levels */
-                                               | G_LOG_FLAG_RECURSION
-                                               | G_LOG_FLAG_FATAL
-                                               /* The same mask is in:
-                                                * libcaptive/sandbox/server-GLogFunc.c
-                                                * libcaptive/client/init.c
-                                                * libcaptive/client/vfs.c
-                                                */
-                                               | G_LOG_LEVEL_MESSAGE
-                                               | G_LOG_LEVEL_INFO
-                                               | G_LOG_LEVEL_DEBUG,
-                               log_discard_func,       /* log_func */
-                               NULL);  /* user_data */
-               }
+       captive_log_init(captive_options);
+
+       captive_image_iochannel=captive_options->image_iochannel;
+       g_io_channel_ref(captive_image_iochannel);
 
-       if (captive_options->rwmode==CAPTIVE_OPTION_RWMODE_BLIND)
-               captive_image_iochannel=(GIOChannel *)captive_giochannel_blind_new(captive_options->image_iochannel,
+       captive_image_iochannel=captive_storage_relastblock(captive_image_iochannel);
+
+       if (captive_options->rwmode==CAPTIVE_OPTION_RWMODE_BLIND) {
+               GIOChannel *captive_image_iochannel_orig;
+
+               captive_image_iochannel_orig=captive_image_iochannel;
+               captive_image_iochannel=(GIOChannel *)captive_giochannel_blind_new(captive_image_iochannel,
                                TRUE);  /* writeable */
-       else
-               captive_image_iochannel=captive_options->image_iochannel;
+               g_io_channel_unref(captive_image_iochannel_orig);       /* reffed by captive_giochannel_blind_new() */
+               }
 
        /* Do not initialize 'captive_image_size' by captive_giochannel_size() here
         * as we yet need to do g_io_channel_set_encoding().
@@ -302,8 +375,11 @@ FILE_OBJECT *FileObject;
 GnomeVFSResult errvfsresult;
 NTSTATUS err;
 IO_STATUS_BLOCK dir_IoStatusBlock;
+#if 0
 static const ULONG fsctls[2]={ FSCTL_LOCK_VOLUME,FSCTL_DISMOUNT_VOLUME };
 int fsctlsi;
+#endif
+WCHAR wzero;
 
        errvfsresult=captive_ObjectAttributes_init("/!Captive!del",&dir_ObjectAttributes);
        g_return_if_fail(errvfsresult==GNOME_VFS_OK);
@@ -311,6 +387,8 @@ int fsctlsi;
        /* wanted: * IoCreateFile()->ObCreateObject(,,,IoFileObjectType)->
         * ->(IoFileObjectType->Create==IopCreateFile)()->IoMountVolume()
         */
+       CAPTIVE_MEMZERO(&dir_IoStatusBlock);    /* FIXME: Try to pre-clear it - uninitialized otherwise? */
+       dir_IoStatusBlock.Information=FILE_OPENED;      /* FIXME: Try to pre-set it - uninitialized otherwise? */
        err=IoCreateFile(
                        &dir_Handle,    /* FileHandle */
                        GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE|0x80,    /* DesiredAccess; 0xC0100080=GENERIC_READ|GENERIC_WRITE|SYNCHRONIZE|0x80 */
@@ -332,14 +410,23 @@ int fsctlsi;
                        NULL,   /* ExtraCreateParameters */
                        0);     /* Options */
        g_free(dir_ObjectAttributes.ObjectName);        /* left from captive_gnomevfs_uri_parent_init() */
-       g_return_if_fail(NT_SUCCESS(err)==NT_SUCCESS(dir_IoStatusBlock.Status));
        g_return_if_fail(NT_SUCCESS(err));
+       g_return_if_fail(NT_SUCCESS(err)==NT_SUCCESS(dir_IoStatusBlock.Status));
        g_return_if_fail(dir_IoStatusBlock.Information==FILE_OPENED);
 
        Status=ObReferenceObjectByHandle(dir_Handle,FILE_LIST_DIRECTORY,IoFileObjectType,UserMode,(PVOID *)&FileObject,NULL);
        g_assert(NT_SUCCESS(Status));
 
-       for (fsctlsi=0;fsctlsi<2;fsctlsi++) {
+       g_assert(FileObject->FileName.Length==0);
+       /* 'FileObject->FileName.MaximumLength' is not reset by IoCreateFile(). */
+       g_assert(FileObject->FileName.Buffer==NULL);
+       FileObject->FileName.MaximumLength=2;
+       FileObject->FileName.Buffer=&wzero;
+
+#if 0
+       for (fsctlsi=0;fsctlsi<2;fsctlsi++)
+#endif
+       {
                Irp=IoAllocateIrp(DeviceObject->StackSize,TRUE);
                g_return_if_fail(Irp!=NULL);
 
@@ -348,18 +435,24 @@ int fsctlsi;
                Irp->Tail.Overlay.Thread=PsGetCurrentThread();
 
                StackPtr=IoGetNextIrpStackLocation(Irp);
+#if 0
                StackPtr->MajorFunction=IRP_MJ_FILE_SYSTEM_CONTROL;
                StackPtr->MinorFunction=IRP_MN_USER_FS_REQUEST;
+#else
+               StackPtr->MajorFunction=IRP_MJ_FLUSH_BUFFERS;
+#endif
                StackPtr->Flags=0;
                StackPtr->Control=0;
                StackPtr->DeviceObject=DeviceObject;    /* FIXME: FileObject->Vpb->DeviceObject ? */
                StackPtr->FileObject=FileObject;
                StackPtr->CompletionRoutine=NULL;
 
+#if 0
                StackPtr->Parameters.FileSystemControl.OutputBufferLength=0;
                StackPtr->Parameters.FileSystemControl.InputBufferLength=0;
                StackPtr->Parameters.FileSystemControl.FsControlCode=fsctls[fsctlsi];
                StackPtr->Parameters.FileSystemControl.Type3InputBuffer=NULL;
+#endif
 
                /* IoCallDriver() will do one ObDereferenceObject(FileObject)
                 * in its IoSecondStageCompletion().
@@ -372,7 +465,8 @@ int fsctlsi;
                        KeWaitForSingleObject(&FileObject->Event,Executive,KernelMode,FALSE,NULL);
                        Status=IoStatusBlock.Status;
                        }
-               g_assert(NT_SUCCESS(Status));
+               g_assert(NT_SUCCESS(Status)
+                               || (Status==STATUS_MEDIA_WRITE_PROTECTED && captive_options->rwmode==CAPTIVE_OPTION_RWMODE_RO));
        }
 
        ObDereferenceObject(FileObject);
@@ -405,7 +499,9 @@ GIOStatus erriostatus;
                        FALSE)) /* may_block */
                g_log(G_LOG_DOMAIN,G_LOG_LEVEL_DEBUG,"%s: g_main_context_iteration() proceeded",G_STRLOC);
 
-       captive_cc_flush();     /* based on captive_leave(), not g_main idle */
+       /* Do not: captive_cc_flush();  * based on captive_leave(), not g_main idle *
+        * replaced by IRP_MJ_FLUSH_BUFFERS.
+        */
 
        dismount_volume();
 
@@ -415,7 +511,9 @@ GIOStatus erriostatus;
                        FALSE)) /* may_block */
                g_log(G_LOG_DOMAIN,G_LOG_LEVEL_DEBUG,"%s: g_main_context_iteration() proceeded",G_STRLOC);
 
+#if 0
        captive_PoQueueShutdownWorkItem_hooklist_invoke();
+#endif
 
        /* Do not: captive_cc_flush();
         * as the dirty blocks should have been already commited by dismount_volume(),
@@ -445,21 +543,13 @@ GIOStatus erriostatus;
 
        IoShutdownRegisteredDevices();
 
-       /* libcaptive is not authorized to shutdown 'captive_image_channel'. */
+       /* Just a sanity if 'captive_image_iochannel' is already reffed a bit more... */
        erriostatus=g_io_channel_flush(
                        captive_image_iochannel,        /* channel */
                        NULL);  /* error */
        g_assert(erriostatus==G_IO_STATUS_NORMAL);
 
-       /* 'captive_image_iochannel' may be blinded wrapper of 'captive_options->image_iochannel'. */
-       if (captive_image_iochannel!=captive_options->image_iochannel) {
-               erriostatus=g_io_channel_flush(
-                               captive_options->image_iochannel,       /* channel */
-                               NULL);  /* error */
-               g_assert(erriostatus==G_IO_STATUS_NORMAL);
-               g_io_channel_unref(captive_image_iochannel);
-               }
-
+       g_io_channel_unref(captive_image_iochannel);
        captive_image_iochannel=NULL;
 
        active=FALSE;