captive_options module-loading generalized to be CORBA transportable
authorshort <>
Sat, 7 Jun 2003 07:12:30 +0000 (07:12 +0000)
committershort <>
Sat, 7 Jun 2003 07:12:30 +0000 (07:12 +0000)
src/libcaptive/client/Makefile.am
src/libcaptive/client/init.c
src/libcaptive/client/options-module.c [new file with mode: 0644]
src/libcaptive/include/captive/Makefile.am
src/libcaptive/include/captive/ldr.h
src/libcaptive/include/captive/options-module.h [new file with mode: 0644]
src/libcaptive/ldr/loader.c

index 29d6e88..277f760 100644 (file)
@@ -32,6 +32,7 @@ libclient_la_SOURCES= \
                lib.c \
                lib.h \
                options.c \
+               options-module.c \
                result.c \
                result.h \
                usecount.c \
index bfeb479..96d3774 100644 (file)
@@ -84,7 +84,7 @@ gboolean errbool;
 GIOStatus erriostatus;
 
        g_return_val_if_fail(captive_options!=NULL,FALSE);
-       g_return_val_if_fail(captive_options->filesystem!=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 */
@@ -152,17 +152,18 @@ GIOStatus erriostatus;
        g_return_val_if_fail(errbool==TRUE,FALSE);
 
        while (captive_options->load_module) {
-gchar *modulename=captive_options->load_module->data;
 PMODULE_OBJECT ModuleObject_tmp;
 NTSTATUS err;
 
                /* load the module */
-               err=LdrLoadModule(
-                               captive_utf8_to_UnicodeString_alloca(modulename),       /* ModuleName */
+               err=captive_LdrLoadModule(
+                               captive_options->load_module->data,
                                &ModuleObject_tmp);     /* ModuleObjectp */
                g_return_val_if_fail(NT_SUCCESS(err),FALSE);
 
-               captive_options->load_module=g_list_remove(captive_options->load_module,modulename);
+               captive_options_module_free(captive_options->load_module->data);
+               /* also frees 'options->load_module->data' */
+               captive_options->load_module=g_list_delete_link(captive_options->load_module,captive_options->load_module);
                }
 
        /* Patch 'ntoskrnl.exe' loaded by 'captive_options->load_module' above. */
@@ -199,7 +200,7 @@ NTSTATUS err;
        captive_DriverObject_ReinitRoutine=NULL;
        err=captive_LdrpLoadAndCallImage(
                        &ModuleObject,  /* ModuleObjectp */
-                       captive_utf8_to_UnicodeString_alloca(captive_options->filesystem),      /* ModuleName */
+                       &captive_options->filesystem,   /* options_module */
                        &captive_DriverObject,  /* DriverEntry_DriverObject */
                        captive_utf8_to_UnicodeString_alloca("\\captive\\filesystem")); /* DriverEntry_RegistryPath */
        g_return_val_if_fail(NT_SUCCESS(err),FALSE);
diff --git a/src/libcaptive/client/options-module.c b/src/libcaptive/client/options-module.c
new file mode 100644 (file)
index 0000000..daefd7e
--- /dev/null
@@ -0,0 +1,115 @@
+/* $Id$
+ * User options handling code of libcaptive
+ * Copyright (C) 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
+ * the Free Software Foundation; exactly version 2 of June 1991 is required
+ * 
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+
+#include "config.h"
+
+#include "captive/options.h"   /* self */
+#include <glib/gmessages.h>
+#include "captive/macros.h"
+#include <glib/gstrfuncs.h>
+#include <stdlib.h>
+#include "captive/rtl-file.h"
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <sys/mman.h>
+
+
+gboolean captive_options_module_load(struct captive_options_module *options_module,const gchar *pathname_utf8)
+{
+       g_return_val_if_fail(options_module!=NULL,FALSE);
+       g_return_val_if_fail(pathname_utf8!=NULL,FALSE);
+
+       /* Open the Module */
+       options_module->type=CAPTIVE_OPTIONS_MODULE_TYPE_PE32;
+       options_module->pathname_utf8=g_strdup(pathname_utf8);
+       options_module->u.pe32.mapped=TRUE;
+       options_module->u.pe32.base=captive_rtl_file_mmap(
+                       &options_module->u.pe32.length, /* lenp */
+                       pathname_utf8,  /* path */
+                       O_RDONLY,       /* open_flags */
+                       PROT_READ,      /* mmap_prot */
+                       MAP_SHARED);    /* mmap_flags */
+       /* FIXME: convert errno instead of STATUS_INSUFFICIENT_RESOURCES */
+       g_return_val_if_fail(options_module->u.pe32.base!=NULL,FALSE);
+
+       if (options_module->u.pe32.length>=2
+                       && (('M'<<8U)|('Z'<<0U))==GUINT16_FROM_BE(*(const guint16 *)options_module->u.pe32.base)) {
+               /* already done above */
+               }
+       else {
+               captive_rtl_file_munmap(options_module->u.pe32.base);
+               options_module->type=CAPTIVE_OPTIONS_MODULE_TYPE_GMODULE;
+               options_module->u.gmodule.pathname=g_strdup(pathname_utf8);
+               }
+
+       return TRUE;
+}
+
+
+void captive_options_module_copy(struct captive_options_module *dest,const struct captive_options_module *src)
+{
+       g_return_if_fail(dest!=NULL);
+       g_return_if_fail(src!=NULL);
+
+       dest->pathname_utf8=g_strdup(src->pathname_utf8);
+
+       dest->type=src->type;
+       switch (src->type) {
+               case CAPTIVE_OPTIONS_MODULE_TYPE_EMPTY: g_assert_not_reached();
+
+               case CAPTIVE_OPTIONS_MODULE_TYPE_PE32:
+                       dest->u.pe32.base=g_memdup(src->u.pe32.base,src->u.pe32.length);
+                       dest->u.pe32.length=src->u.pe32.length;
+                       dest->u.pe32.mapped=FALSE;
+                       break;
+
+               case CAPTIVE_OPTIONS_MODULE_TYPE_GMODULE:
+                       dest->u.gmodule.pathname=g_strdup(src->u.gmodule.pathname);
+                       break;
+
+               default: g_assert_not_reached();
+               }
+}
+
+
+void captive_options_module_free(struct captive_options_module *options_module)
+{
+       g_return_if_fail(options_module!=NULL);
+
+       g_free(options_module->pathname_utf8);
+
+       switch (options_module->type) {
+               case CAPTIVE_OPTIONS_MODULE_TYPE_EMPTY:
+                       break;
+
+               case CAPTIVE_OPTIONS_MODULE_TYPE_PE32:
+                       if (options_module->u.pe32.mapped)
+                               captive_rtl_file_munmap(options_module->u.pe32.base);
+                       else
+                               g_free(options_module->u.pe32.base);
+                       break;
+
+               case CAPTIVE_OPTIONS_MODULE_TYPE_GMODULE:
+                       g_free(options_module->u.gmodule.pathname);
+                       break;
+
+               default: g_assert_not_reached();
+               }
+}
index c658ebc..1247b1a 100644 (file)
@@ -31,6 +31,7 @@ pkginclude_HEADERS+= \
                macros.h \
                mm.h \
                options.h \
+               options-module.h \
                parent-Directory.h \
                parent-File.h \
                parent-Vfs.h \
index bbbbcc0..462e8ec 100644 (file)
 #include <glib/gmacros.h>
 #include "reactos/internal/ldr.h"
 #include <glib/gtypes.h>
+#include "captive/options-module.h"
 
 
 G_BEGIN_DECLS
 
-NTSTATUS captive_LdrpLoadAndCallImage(PMODULE_OBJECT *ModuleObjectp,PUNICODE_STRING ModuleName,
-    PDRIVER_OBJECT DriverEntry_DriverObject,PUNICODE_STRING DriverEntry_RegistryPath);
-/* LdrLoadModule() declared in reactos includes */
+NTSTATUS captive_LdrpLoadAndCallImage(PMODULE_OBJECT *ModuleObjectp,struct captive_options_module *options_module,
+               PDRIVER_OBJECT DriverEntry_DriverObject,PUNICODE_STRING DriverEntry_RegistryPath);
+NTSTATUS captive_LdrLoadModule(struct captive_options_module *options_module,PMODULE_OBJECT *ModuleObjectp);
 struct captive_ModuleList_patchpoint *captive_ModuleList_patchpoint_find(gconstpointer ExportAddress);
 G_CONST_RETURN gchar *captive_ModuleList_function_disable_find(gconstpointer ExportAddress);
 void *captive_Module_GetExportAddress(const gchar *ModuleName_utf8,const gchar *FunctionName);
diff --git a/src/libcaptive/include/captive/options-module.h b/src/libcaptive/include/captive/options-module.h
new file mode 100644 (file)
index 0000000..dcffdca
--- /dev/null
@@ -0,0 +1,61 @@
+/* $Id$
+ * Include file with captive_options_module definition of libcaptive
+ * Copyright (C) 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
+ * the Free Software Foundation; exactly version 2 of June 1991 is required
+ * 
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+
+#ifndef _CAPTIVE_CLIENT_OPTIONS_MODULE_H
+#define _CAPTIVE_CLIENT_OPTIONS_MODULE_H 1
+
+
+#include <glib/gtypes.h>
+#include <glib/giochannel.h>
+#include <popt.h>
+#include <glib/glist.h>
+
+
+G_BEGIN_DECLS
+
+enum captive_options_module_type {
+       CAPTIVE_OPTIONS_MODULE_TYPE_EMPTY,
+       CAPTIVE_OPTIONS_MODULE_TYPE_PE32,
+       CAPTIVE_OPTIONS_MODULE_TYPE_GMODULE,
+       };
+
+struct captive_options_module {
+       gchar *pathname_utf8;
+       enum captive_options_module_type type;
+       union {
+               struct {
+                       guint8 *base;
+                       size_t length;
+                       gboolean mapped;        /* otherwise g_malloc()ed */
+                       } pe32;
+               struct {
+                       gchar *pathname;
+                       } gmodule;
+               } u;
+       };
+
+/* Functions handle just the fields, not the structure itself. */
+gboolean captive_options_module_load(struct captive_options_module *options_module,const gchar *pathname_utf8);
+void captive_options_module_copy(struct captive_options_module *dest,const struct captive_options_module *src);
+void captive_options_module_free(struct captive_options_module *options_module);
+
+G_END_DECLS
+
+
+#endif /* _CAPTIVE_CLIENT_OPTIONS_MODULE_H */
index 1cac046..fe73f4d 100644 (file)
@@ -37,6 +37,7 @@
 #include "reactos/internal/debug.h"    /* for assert() */
 #include <glib/gutils.h>       /* for g_path_get_basename() */
 #include <gmodule.h>
+#include "captive/options-module.h"
 
 
 /* map: ExportAddress -> (struct captive_ModuleList_patchpoint *) */
@@ -119,7 +120,7 @@ gboolean errbool;
 
 
 /**
- * LdrLoadModule:
+ * captive_LdrLoadModule:
  * @Filename: Filename of module to load based on host OS disk.
  * Loading of already loaded module is forbidden.
  * @ModuleObject: Returns initialized module object.
@@ -128,50 +129,42 @@ gboolean errbool;
  *
  * Returns: STATUS_SUCCESS if the module was loaded successfuly during the call.
  */
-NTSTATUS LdrLoadModule(PUNICODE_STRING Filename,PMODULE_OBJECT *ModuleObjectp)
+NTSTATUS captive_LdrLoadModule(struct captive_options_module *options_module,PMODULE_OBJECT *ModuleObjectp)
 {
-PVOID ModuleLoadBase;
 PMODULE_OBJECT Module;
 NTSTATUS err;
-gchar *Filename_utf8=(/* de-const */ gchar *)captive_UnicodeString_to_utf8_alloca(Filename);
+gchar *Filename_utf8=options_module->pathname_utf8;
 gchar *Filename_bslash_utf8,*s;
 UNICODE_STRING *Filename_bslash;
 
        *ModuleObjectp=NULL;
 
-       /* Open the Module */
-       ModuleLoadBase=captive_rtl_file_mmap(
-                       NULL,   /* lenp */
-                       Filename_utf8,  /* path */
-                       O_RDONLY,       /* open_flags */
-                       PROT_READ,      /* mmap_prot */
-                       MAP_SHARED);    /* mmap_flags */
-       /* FIXME: convert errno instead of STATUS_INSUFFICIENT_RESOURCES */
-       g_return_val_if_fail(ModuleLoadBase!=NULL,STATUS_INSUFFICIENT_RESOURCES);
-
        /* Filename=~s#/#\\#g -> Filename_bslash to satisfy basename-derivations by reactos. */
-       Filename_bslash_utf8=g_strdup(Filename_utf8);
+       Filename_bslash_utf8=(/* de-const as we do not change the length */ gchar *)captive_strdup_alloca(Filename_utf8);
        for (s=Filename_bslash_utf8;(s=strchr(s,'/'));s++)
                *s='\\';
        Filename_bslash=captive_utf8_to_UnicodeString_alloca(Filename_bslash_utf8);
 
-       if ((('M'<<8U)|('Z'<<0U))==GUINT16_FROM_BE(*(const guint16 *)ModuleLoadBase)) {
-               g_log(G_LOG_DOMAIN,G_LOG_LEVEL_DEBUG,"%s: Loading module format: %s",G_STRLOC,"MZ/PE-32");
-               /* examine/relocate the module */
-               err=LdrProcessModule(ModuleLoadBase,Filename_bslash,&Module);
-               if (!NT_SUCCESS(err)) {
-                       g_error("LdrLoadModule(): LdrProcessModule()=0x%08lX",(gulong)err);
-                       goto err_captive_rtl_file_munmap;
-                       }
-               }
-       else {
-               g_log(G_LOG_DOMAIN,G_LOG_LEVEL_DEBUG,"%s: Loading module format: %s",G_STRLOC,"native .so");
-               Module=captive_LdrLoadModule_gmodule(Filename_utf8,Filename_bslash_utf8);
-               g_assert(Module!=NULL);
+       switch (options_module->type) {
+               case CAPTIVE_OPTIONS_MODULE_TYPE_EMPTY: g_assert_not_reached();
+
+               case CAPTIVE_OPTIONS_MODULE_TYPE_PE32:
+                       g_log(G_LOG_DOMAIN,G_LOG_LEVEL_DEBUG,"%s: Loading module format: %s",G_STRLOC,"MZ/PE-32");
+                       /* examine/relocate the module */
+                       err=LdrProcessModule(options_module->u.pe32.base,Filename_bslash,&Module);
+                       if (!NT_SUCCESS(err)) {
+                               g_error("LdrLoadModule(): LdrProcessModule()=0x%08lX",(gulong)err);
+                               goto err;
+                               }
+                       break;
+
+               case CAPTIVE_OPTIONS_MODULE_TYPE_GMODULE:
+                       g_log(G_LOG_DOMAIN,G_LOG_LEVEL_DEBUG,"%s: Loading module format: %s",G_STRLOC,"native .so");
+                       Module=captive_LdrLoadModule_gmodule(Filename_utf8,Filename_bslash_utf8);
+                       g_assert(Module!=NULL);
                }
 
        /* we were successful */
-       captive_rtl_file_munmap(ModuleLoadBase);
        *ModuleObjectp=Module;
 
        /* Hook for KDB on loading a driver. */
@@ -179,14 +172,17 @@ UNICODE_STRING *Filename_bslash;
 
        return STATUS_SUCCESS;
 
-       /* Error recoveries */
-err_captive_rtl_file_munmap:
-       captive_rtl_file_munmap(ModuleLoadBase);
-
+err:
        g_return_val_if_reached(err);
 }
 
 
+NTSTATUS LdrLoadModule(PUNICODE_STRING Filename,PMODULE_OBJECT *ModuleObject)
+{
+       return STATUS_OBJECT_NAME_NOT_FOUND;
+}
+
+
 /**
  * captive_LdrpLoadAndCallImage:
  * @ModuleObjectp: Returns PMODULE_OBJECT successfuly loaded.
@@ -202,22 +198,24 @@ err_captive_rtl_file_munmap:
  * Returns: STATUS_SUCCESS if the driver module was loaded and initialized
  * successfuly during this call. Ignore returned @ModuleObjectp if function failed.
  */
-NTSTATUS captive_LdrpLoadAndCallImage(PMODULE_OBJECT *ModuleObjectp,PUNICODE_STRING ModuleName,
+NTSTATUS captive_LdrpLoadAndCallImage(PMODULE_OBJECT *ModuleObjectp,struct captive_options_module *options_module,
                PDRIVER_OBJECT DriverEntry_DriverObject,PUNICODE_STRING DriverEntry_RegistryPath)
 {
 PDRIVER_INITIALIZE DriverEntry;
 PMODULE_OBJECT ModuleObject_tmp;
 NTSTATUS err;
 
+#if 0  /* TODO */
        /* check for duplicity */
        g_return_val_if_fail(LdrGetModuleObject(ModuleName)==NULL,STATUS_IMAGE_ALREADY_LOADED);
+#endif
 
        /* provide our temporary storage if the caller doesn't want to know ModuleObject address */
        if (!ModuleObjectp)
                ModuleObjectp=&ModuleObject_tmp;
 
        /* load the module */
-       err=LdrLoadModule(ModuleName,ModuleObjectp);
+       err=captive_LdrLoadModule(options_module,ModuleObjectp);
        g_return_val_if_fail(NT_SUCCESS(err),err);
 
        /* initialize the module */