Merge pull request #725 from knocte/threadpool_init
[mono.git] / mono / metadata / mono-config.c
index 6b3033f2f49856ca0f8fb234adc0a6e1fb1763e0..7b7536cd05faa19d7d2f232f473001a4579846a6 100644 (file)
@@ -5,7 +5,8 @@
  *
  * Author: Paolo Molaro (lupus@ximian.com)
  *
- * (C) 2002 Ximian, Inc.
+ * Copyright 2002-2003 Ximian, Inc (http://www.ximian.com)
+ * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
  */
 #include "config.h"
 #include <glib.h>
 #include "mono/metadata/loader.h"
 #include "mono/metadata/mono-config.h"
 #include "mono/metadata/metadata-internals.h"
-#include "mono/utils/mono-logger.h"
+#include "mono/metadata/object-internals.h"
+#include "mono/utils/mono-logger-internal.h"
 
-#if defined(__linux__)
+#if defined(TARGET_PS3)
+#define CONFIG_OS "CellOS"
+#elif defined(__linux__)
 #define CONFIG_OS "linux"
 #elif defined(__APPLE__)
 #define CONFIG_OS "osx"
 #define CONFIG_OS "netbsd"
 #elif defined(__OpenBSD__)
 #define CONFIG_OS "openbsd"
-#elif defined(__WIN32__)
+#elif defined(__WIN32__) || defined(TARGET_WIN32)
 #define CONFIG_OS "windows"
 #elif defined(_IBMR2)
 #define CONFIG_OS "aix"
 #elif defined(__hpux)
 #define CONFIG_OS "hpux"
+#elif defined(__HAIKU__)
+#define CONFIG_OS "haiku"
 #else
 #warning Unknown operating system
 #define CONFIG_OS "unknownOS"
 #endif
 
-#if defined(__i386__)
+#ifndef CONFIG_CPU
+#if defined(__i386__) || defined(TARGET_X86)
 #define CONFIG_CPU "x86"
-#elif defined(__x86_64__)
+#define CONFIG_WORDSIZE "32"
+#elif defined(__x86_64__) || defined(TARGET_AMD64)
 #define CONFIG_CPU "x86-64"
+#define CONFIG_WORDSIZE "64"
 #elif defined(sparc) || defined(__sparc__)
 #define CONFIG_CPU "sparc"
+#define CONFIG_WORDSIZE "32"
+#elif defined(__ppc64__) || defined(__powerpc64__) || defined(TARGET_POWERPC)
+#define CONFIG_WORDSIZE "64"
+#ifdef __mono_ppc_ilp32__ 
+#   define CONFIG_CPU "ppc64ilp32"
+#else
+#   define CONFIG_CPU "ppc64"
+#endif
 #elif defined(__ppc__) || defined(__powerpc__)
 #define CONFIG_CPU "ppc"
+#define CONFIG_WORDSIZE "32"
 #elif defined(__s390x__)
 #define CONFIG_CPU "s390x"
+#define CONFIG_WORDSIZE "64"
 #elif defined(__s390__)
 #define CONFIG_CPU "s390"
+#define CONFIG_WORDSIZE "32"
 #elif defined(__arm__)
 #define CONFIG_CPU "arm"
+#define CONFIG_WORDSIZE "32"
 #elif defined(__ia64__)
 #define CONFIG_CPU "ia64"
-#elif defined(__alpha__)
-#define CONFIG_CPU "alpha"
-#elif defined(hppa) || defined(__hppa__)
-#define CONFIG_CPU "hppa"
+#define CONFIG_WORDSIZE "64"
 #elif defined(mips) || defined(__mips) || defined(_mips)
 #define CONFIG_CPU "mips"
+#define CONFIG_WORDSIZE "32"
 #else
-#warning Unknown CPU
+#error Unknown CPU
 #define CONFIG_CPU "unknownCPU"
 #endif
+#endif
 
 static void start_element (GMarkupParseContext *context, 
                            const gchar         *element_name,
@@ -106,6 +126,9 @@ mono_parser = {
 
 static GHashTable *config_handlers;
 
+static const char *mono_cfg_dir = NULL;
+static char *mono_cfg_dir_allocated = NULL;
+
 /* when this interface is stable, export it. */
 typedef struct MonoParseHandler MonoParseHandler;
 
@@ -120,6 +143,12 @@ struct MonoParseHandler {
        void (*finish) (gpointer user_data);
 };
 
+typedef struct {
+       MonoAssemblyBindingInfo *info;
+       void (*info_parsed)(MonoAssemblyBindingInfo *info, void *user_data);
+       void *user_data;
+} ParserUserData;
+
 typedef struct {
        MonoParseHandler *current;
        void *user_data;
@@ -186,6 +215,13 @@ static void parse_error   (GMarkupParseContext *context,
                            GError              *error,
                           gpointer             user_data)
 {
+       ParseState *state = user_data;
+       const gchar *msg;
+       const gchar *filename;
+
+       filename = state && state->user_data ? (gchar *) state->user_data : "<unknown>";
+       msg = error && error->message ? error->message : "";
+       g_warning ("Error parsing %s: %s", filename, msg);
 }
 
 static int
@@ -244,6 +280,8 @@ dllmap_start (gpointer user_data,
                                info->ignore = TRUE;
                        else if (strcmp (attribute_names [i], "cpu") == 0 && !arch_matches (CONFIG_CPU, attribute_values [i]))
                                info->ignore = TRUE;
+                       else if (strcmp (attribute_names [i], "wordsize") == 0 && !arch_matches (CONFIG_WORDSIZE, attribute_values [i]))
+                               info->ignore = TRUE;
                }
                if (!info->ignore)
                        mono_dllmap_insert (info->assembly, info->dll, NULL, info->target, NULL);
@@ -261,6 +299,8 @@ dllmap_start (gpointer user_data,
                                ignore = TRUE;
                        else if (strcmp (attribute_names [i], "cpu") == 0 && !arch_matches (CONFIG_CPU, attribute_values [i]))
                                ignore = TRUE;
+                       else if (strcmp (attribute_names [i], "wordsize") == 0 && !arch_matches (CONFIG_WORDSIZE, attribute_values [i]))
+                               ignore = TRUE;
                }
                if (!dll)
                        dll = info->dll;
@@ -289,6 +329,31 @@ dllmap_handler = {
        dllmap_finish
 };
 
+static void
+legacyUEP_start (gpointer user_data, 
+              const gchar         *element_name,
+              const gchar        **attribute_names,
+              const gchar        **attribute_values) {
+       if ((strcmp (element_name, "legacyUnhandledExceptionPolicy") == 0) &&
+                       (attribute_names [0] != NULL) &&
+                       (strcmp (attribute_names [0], "enabled") == 0)) {
+               if ((strcmp (attribute_values [0], "1") == 0) ||
+                               (g_ascii_strcasecmp (attribute_values [0], "true") == 0)) {
+                       mono_runtime_unhandled_exception_policy_set (MONO_UNHANDLED_POLICY_LEGACY);
+               }
+       }
+}
+
+static const MonoParseHandler
+legacyUEP_handler = {
+       "legacyUnhandledExceptionPolicy",
+       NULL, /* init */
+       legacyUEP_start,
+       NULL, /* text */
+       NULL, /* end */
+       NULL, /* finish */
+};
+
 static int inited = 0;
 
 static void
@@ -297,6 +362,15 @@ mono_config_init (void)
        inited = 1;
        config_handlers = g_hash_table_new (g_str_hash, g_str_equal);
        g_hash_table_insert (config_handlers, (gpointer) dllmap_handler.element_name, (gpointer) &dllmap_handler);
+       g_hash_table_insert (config_handlers, (gpointer) legacyUEP_handler.element_name, (gpointer) &legacyUEP_handler);
+}
+
+void
+mono_config_cleanup (void)
+{
+       if (config_handlers)
+               g_hash_table_destroy (config_handlers);
+       g_free (mono_cfg_dir_allocated);
 }
 
 /* FIXME: error handling */
@@ -320,15 +394,23 @@ mono_config_parse_xml_with_context (ParseState *state, const char *text, gsize l
 static int
 mono_config_parse_file_with_context (ParseState *state, const char *filename)
 {
-       char *text;
+       gchar *text;
        gsize len;
+       gint offset;
 
        mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_CONFIG,
                        "Config attempting to parse: '%s'.", filename);
 
        if (!g_file_get_contents (filename, &text, &len, NULL))
                return 0;
-       mono_config_parse_xml_with_context (state, text, len);
+
+
+       offset = 0;
+       if (len > 3 && text [0] == '\xef' && text [1] == (gchar) '\xbb' && text [2] == '\xbf')
+               offset = 3; /* Skip UTF-8 BOM */
+       if (state->user_data == NULL)
+               state->user_data = (gpointer) filename;
+       mono_config_parse_xml_with_context (state, text + offset, len - offset);
        g_free (text);
        return 1;
 }
@@ -343,7 +425,8 @@ void
 mono_config_parse_memory (const char *buffer)
 {
        ParseState state = {NULL};
-       
+
+       state.user_data = (gpointer) "<buffer>";
        mono_config_parse_xml_with_context (&state, buffer, strlen (buffer));
 }
 
@@ -351,6 +434,7 @@ static void
 mono_config_parse_file (const char *filename)
 {
        ParseState state = {NULL};
+       state.user_data = (gpointer) filename;
        mono_config_parse_file_with_context (&state, filename);
 }
 
@@ -382,6 +466,8 @@ struct _BundledConfig {
 
 static BundledConfig *bundled_configs = NULL;
 
+static const char *bundled_machine_config = NULL;
+
 void
 mono_register_config_for_assembly (const char* assembly_name, const char* config_xml)
 {
@@ -394,20 +480,33 @@ mono_register_config_for_assembly (const char* assembly_name, const char* config
        bundled_configs = bconfig;
 }
 
+const char *
+mono_config_string_for_assembly_file (const char *filename)
+{
+       BundledConfig *bconfig;
+       
+       for (bconfig = bundled_configs; bconfig; bconfig = bconfig->next) {
+               if (bconfig->aname && strcmp (bconfig->aname, filename) == 0)
+                       return bconfig->config_xml;
+       }
+       return NULL;
+}
+
 void 
 mono_config_for_assembly (MonoImage *assembly)
 {
        ParseState state = {NULL};
        int got_it = 0, i;
        char *aname, *cfg, *cfg_name;
+       const char *bundled_config;
        const char *home;
-       BundledConfig *bconfig;
        
        state.assembly = assembly;
 
-       for (bconfig = bundled_configs; bconfig; bconfig = bconfig->next) {
-               if (bconfig->aname && strcmp (bconfig->aname, assembly->module_name) == 0)
-                       mono_config_parse_xml_with_context (&state, bconfig->config_xml, strlen (bconfig->config_xml));
+       bundled_config = mono_config_string_for_assembly_file (assembly->module_name);
+       if (bundled_config) {
+               state.user_data = (gpointer) "<bundled>";
+               mono_config_parse_xml_with_context (&state, bundled_config, strlen (bundled_config));
        }
 
        cfg_name = g_strdup_printf ("%s.config", mono_image_get_filename (assembly));
@@ -423,7 +522,7 @@ mono_config_for_assembly (MonoImage *assembly)
                got_it += mono_config_parse_file_with_context (&state, cfg);
                g_free (cfg);
 
-#ifndef PLATFORM_WIN32
+#ifdef TARGET_WIN32
                cfg = g_build_filename (home, ".mono", "assemblies", aname, cfg_name, NULL);
                got_it += mono_config_parse_file_with_context (&state, cfg);
                g_free (cfg);
@@ -446,7 +545,7 @@ void
 mono_config_parse (const char *filename) {
        const char *home;
        char *mono_cfg;
-#ifndef PLATFORM_WIN32
+#ifndef TARGET_WIN32
        char *user_cfg;
 #endif
 
@@ -465,7 +564,7 @@ mono_config_parse (const char *filename) {
        mono_config_parse_file (mono_cfg);
        g_free (mono_cfg);
 
-#ifndef PLATFORM_WIN32
+#if !defined(TARGET_WIN32) && !defined(__native_client__)
        home = g_get_home_dir ();
        user_cfg = g_strconcat (home, G_DIR_SEPARATOR_S, ".mono/config", NULL);
        mono_config_parse_file (user_cfg);
@@ -473,8 +572,6 @@ mono_config_parse (const char *filename) {
 #endif
 }
 
-static const char *mono_cfg_dir = NULL;
-
 /* Invoked during startup */
 void
 mono_set_config_dir (const char *dir)
@@ -482,7 +579,7 @@ mono_set_config_dir (const char *dir)
        /* If this variable is set, overrides the directory computed */
        mono_cfg_dir = g_getenv ("MONO_CFG_DIR");
        if (mono_cfg_dir == NULL)
-               mono_cfg_dir = g_strdup (dir);
+               mono_cfg_dir = mono_cfg_dir_allocated = g_strdup (dir);
 }
 
 const char* 
@@ -494,17 +591,55 @@ mono_get_config_dir (void)
        return mono_cfg_dir;
 }
 
+void
+mono_register_machine_config (const char *config_xml)
+{
+       bundled_machine_config = config_xml;
+}
+
+const char *
+mono_get_machine_config (void)
+{
+       return bundled_machine_config;
+}
+
+static void
+assembly_binding_end (gpointer user_data, const char *element_name)
+{
+       ParserUserData *pud = user_data;
+
+       if (!strcmp (element_name, "dependentAssembly")) {
+               if (pud->info_parsed && pud->info) {
+                       pud->info_parsed (pud->info, pud->user_data);
+                       g_free (pud->info->name);
+                       g_free (pud->info->culture);
+               }
+       }
+}
+
 static void
 publisher_policy_start (gpointer user_data,
                const gchar *element_name,
                const gchar **attribute_names,
                const gchar **attribute_values)
 {
+       ParserUserData *pud;
        MonoAssemblyBindingInfo *info;
        int n;
 
-       info = user_data;
-       if (!strcmp (element_name, "assemblyIdentity")) {
+       pud = user_data;
+       info = pud->info;
+       if (!strcmp (element_name, "dependentAssembly")) {
+               info->name = NULL;
+               info->culture = NULL;
+               info->has_old_version_bottom = FALSE;
+               info->has_old_version_top = FALSE;
+               info->has_new_version = FALSE;
+               info->is_valid = FALSE;
+               memset (&info->old_version_bottom, 0, sizeof (info->old_version_bottom));
+               memset (&info->old_version_top, 0, sizeof (info->old_version_top));
+               memset (&info->new_version, 0, sizeof (info->new_version));
+       } if (!strcmp (element_name, "assemblyIdentity")) {
                for (n = 0; attribute_names [n]; n++) {
                        const gchar *attribute_name = attribute_names [n];
                        
@@ -608,9 +743,14 @@ publisher_policy_parser = {
 void
 mono_config_parse_publisher_policy (const gchar *filename, MonoAssemblyBindingInfo *info)
 {
+       ParserUserData user_data = {
+               info,
+               NULL,
+               NULL
+       };
        ParseState state = {
                &publisher_policy_parser, /* MonoParseHandler */
-               info, /* user_data */
+               &user_data, /* user_data */
                NULL, /* MonoImage (we don't need it right now)*/
                TRUE /* We are already inited */
        };
@@ -618,3 +758,49 @@ mono_config_parse_publisher_policy (const gchar *filename, MonoAssemblyBindingIn
        mono_config_parse_file_with_context (&state, filename);
 }
 
+static MonoParseHandler
+config_assemblybinding_parser = {
+       "", /* We don't need to use declare an xml element */
+       NULL,
+       publisher_policy_start,
+       NULL,
+       assembly_binding_end,
+       NULL
+};
+
+void
+mono_config_parse_assembly_bindings (const char *filename, int amajor, int aminor, void *user_data, void (*infocb)(MonoAssemblyBindingInfo *info, void *user_data))
+{
+       MonoAssemblyBindingInfo info;
+       ParserUserData pud;
+       ParseState state;
+
+       info.major = amajor;
+       info.minor = aminor;
+
+       pud.info = &info;
+       pud.info_parsed = infocb;
+       pud.user_data = user_data;
+
+       state.current = &config_assemblybinding_parser;  /* MonoParseHandler */
+       state.user_data = &pud;
+       state.assembly = NULL; /* MonoImage (we don't need it right now)*/
+       state.inited = TRUE; /* We are already inited */
+
+       mono_config_parse_file_with_context (&state, filename);
+}
+
+static gboolean mono_server_mode = FALSE;
+
+void
+mono_config_set_server_mode (gboolean server_mode)
+{
+       mono_server_mode = server_mode;
+}
+
+gboolean
+mono_config_is_server_mode (void)
+{
+       return mono_server_mode;
+}
+