Tue Sep 11 16:08:08 CEST 2007 Paolo Molaro <lupus@ximian.com>
[mono.git] / mono / metadata / mono-config.c
1 /*
2  * mono-config.c
3  *
4  * Runtime and assembly configuration file support routines.
5  *
6  * Author: Paolo Molaro (lupus@ximian.com)
7  *
8  * (C) 2002 Ximian, Inc.
9  */
10 #include "config.h"
11 #include <glib.h>
12 #include <string.h>
13
14 #include "mono/metadata/assembly.h"
15 #include "mono/metadata/loader.h"
16 #include "mono/metadata/mono-config.h"
17 #include "mono/metadata/metadata-internals.h"
18 #include "mono/utils/mono-logger.h"
19
20 #if defined(__linux__)
21 #define CONFIG_OS "linux"
22 #elif defined(__APPLE__)
23 #define CONFIG_OS "osx"
24 #elif defined(sun)
25 #define CONFIG_OS "solaris"
26 #elif defined(__FreeBSD__)
27 #define CONFIG_OS "freebsd"
28 #elif defined(__NetBSD__)
29 #define CONFIG_OS "netbsd"
30 #elif defined(__OpenBSD__)
31 #define CONFIG_OS "openbsd"
32 #elif defined(__WIN32__)
33 #define CONFIG_OS "windows"
34 #elif defined(_IBMR2)
35 #define CONFIG_OS "aix"
36 #elif defined(__hpux)
37 #define CONFIG_OS "hpux"
38 #else
39 #warning Unknown operating system
40 #define CONFIG_OS "unknownOS"
41 #endif
42
43 #if defined(__i386__)
44 #define CONFIG_CPU "x86"
45 #elif defined(__x86_64__)
46 #define CONFIG_CPU "x86-64"
47 #elif defined(sparc) || defined(__sparc__)
48 #define CONFIG_CPU "sparc"
49 #elif defined(__ppc__) || defined(__powerpc__)
50 #define CONFIG_CPU "ppc"
51 #elif defined(__s390x__)
52 #define CONFIG_CPU "s390x"
53 #elif defined(__s390__)
54 #define CONFIG_CPU "s390"
55 #elif defined(__arm__)
56 #define CONFIG_CPU "arm"
57 #elif defined(__ia64__)
58 #define CONFIG_CPU "ia64"
59 #elif defined(__alpha__)
60 #define CONFIG_CPU "alpha"
61 #elif defined(hppa) || defined(__hppa__)
62 #define CONFIG_CPU "hppa"
63 #elif defined(mips) || defined(__mips) || defined(_mips)
64 #define CONFIG_CPU "mips"
65 #else
66 #warning Unknown CPU
67 #define CONFIG_CPU "unknownCPU"
68 #endif
69
70 static void start_element (GMarkupParseContext *context, 
71                            const gchar         *element_name,
72                            const gchar        **attribute_names,
73                            const gchar        **attribute_values,
74                            gpointer             user_data,
75                            GError             **error);
76
77 static void end_element   (GMarkupParseContext *context,
78                            const gchar         *element_name,
79                            gpointer             user_data,
80                            GError             **error);
81
82 static void parse_text    (GMarkupParseContext *context,
83                            const gchar         *text,
84                            gsize                text_len,
85                            gpointer             user_data,
86                            GError             **error);
87
88 static void passthrough   (GMarkupParseContext *context,
89                            const gchar         *text,
90                            gsize                text_len,
91                            gpointer             user_data,
92                            GError             **error);
93
94 static void parse_error   (GMarkupParseContext *context,
95                            GError              *error,
96                            gpointer             user_data);
97
98 static const GMarkupParser 
99 mono_parser = {
100         start_element,
101         end_element,
102         parse_text,
103         passthrough,
104         parse_error
105 };
106
107 static GHashTable *config_handlers;
108
109 /* when this interface is stable, export it. */
110 typedef struct MonoParseHandler MonoParseHandler;
111
112 struct MonoParseHandler {
113         const char *element_name;
114         void*(*init)   (MonoImage *assembly);
115         void (*start)  (gpointer user_data, const gchar *name,
116                         const gchar **attributes,
117                         const gchar **values);
118         void (*text)   (gpointer user_data, const char *text, gsize test_len);
119         void (*end)    (gpointer user_data, const char *name);
120         void (*finish) (gpointer user_data);
121 };
122
123 typedef struct {
124         MonoParseHandler *current;
125         void *user_data;
126         MonoImage *assembly;
127         int inited;
128 } ParseState;
129
130 static void start_element (GMarkupParseContext *context, 
131                            const gchar         *element_name,
132                            const gchar        **attribute_names,
133                            const gchar        **attribute_values,
134                            gpointer             user_data,
135                            GError             **error)
136 {
137         ParseState *state = user_data;
138         if (!state->current) {
139                 state->current = g_hash_table_lookup (config_handlers, element_name);
140                 if (state->current && state->current->init)
141                         state->user_data = state->current->init (state->assembly);
142         }
143         if (state->current && state->current->start)
144                 state->current->start (state->user_data, element_name, attribute_names, attribute_values);
145 }
146
147 static void end_element   (GMarkupParseContext *context,
148                            const gchar         *element_name,
149                            gpointer             user_data,
150                            GError             **error)
151 {
152         ParseState *state = user_data;
153         if (state->current) {
154                 if (state->current->end)
155                         state->current->end (state->user_data, element_name);
156                 if (strcmp (state->current->element_name, element_name) == 0) {
157                         if (state->current->finish)
158                                 state->current->finish (state->user_data);
159                         state->current = NULL;
160                         state->user_data = NULL;
161                 }
162         }
163 }
164
165 static void parse_text    (GMarkupParseContext *context,
166                            const gchar         *text,
167                            gsize                text_len,
168                            gpointer             user_data,
169                            GError             **error)
170 {
171         ParseState *state = user_data;
172         if (state->current && state->current->text)
173                 state->current->text (state->user_data, text, text_len);
174 }
175
176 static void passthrough   (GMarkupParseContext *context,
177                            const gchar         *text,
178                            gsize                text_len,
179                            gpointer             user_data,
180                            GError             **error)
181 {
182         /* do nothing */
183 }
184
185 static void parse_error   (GMarkupParseContext *context,
186                            GError              *error,
187                            gpointer             user_data)
188 {
189 }
190
191 static int
192 arch_matches (const char* arch, const char *value)
193 {
194         char **splitted, **p;
195         int found = FALSE;
196         if (value [0] == '!')
197                 return !arch_matches (arch, value + 1);
198         p = splitted = g_strsplit (value, ",", 0);
199         while (*p) {
200                 if (strcmp (arch, *p) == 0) {
201                         found = TRUE;
202                         break;
203                 }
204                 p++;
205         }
206         g_strfreev (splitted);
207         return found;
208 }
209
210 typedef struct {
211         char *dll;
212         char *target;
213         int ignore;
214         MonoImage *assembly;
215 } DllInfo;
216
217 static void*
218 dllmap_init (MonoImage *assembly) {
219         DllInfo *info = g_new0 (DllInfo, 1);
220         info->assembly = assembly;
221         return info;
222 }
223
224 static void
225 dllmap_start (gpointer user_data, 
226               const gchar         *element_name,
227               const gchar        **attribute_names,
228               const gchar        **attribute_values)
229 {
230         int i;
231         DllInfo *info = user_data;
232         
233         if (strcmp (element_name, "dllmap") == 0) {
234                 g_free (info->dll);
235                 g_free (info->target);
236                 info->dll = info->target = NULL;
237                 info->ignore = FALSE;
238                 for (i = 0; attribute_names [i]; ++i) {
239                         if (strcmp (attribute_names [i], "dll") == 0)
240                                 info->dll = g_strdup (attribute_values [i]);
241                         else if (strcmp (attribute_names [i], "target") == 0)
242                                 info->target = g_strdup (attribute_values [i]);
243                         else if (strcmp (attribute_names [i], "os") == 0 && !arch_matches (CONFIG_OS, attribute_values [i]))
244                                 info->ignore = TRUE;
245                         else if (strcmp (attribute_names [i], "cpu") == 0 && !arch_matches (CONFIG_CPU, attribute_values [i]))
246                                 info->ignore = TRUE;
247                 }
248                 if (!info->ignore)
249                         mono_dllmap_insert (info->assembly, info->dll, NULL, info->target, NULL);
250         } else if (strcmp (element_name, "dllentry") == 0) {
251                 const char *name = NULL, *target = NULL, *dll = NULL;
252                 int ignore = FALSE;
253                 for (i = 0; attribute_names [i]; ++i) {
254                         if (strcmp (attribute_names [i], "dll") == 0)
255                                 dll = attribute_values [i];
256                         else if (strcmp (attribute_names [i], "target") == 0)
257                                 target = attribute_values [i];
258                         else if (strcmp (attribute_names [i], "name") == 0)
259                                 name = attribute_values [i];
260                         else if (strcmp (attribute_names [i], "os") == 0 && !arch_matches (CONFIG_OS, attribute_values [i]))
261                                 ignore = TRUE;
262                         else if (strcmp (attribute_names [i], "cpu") == 0 && !arch_matches (CONFIG_CPU, attribute_values [i]))
263                                 ignore = TRUE;
264                 }
265                 if (!dll)
266                         dll = info->dll;
267                 if (!info->ignore && !ignore)
268                         mono_dllmap_insert (info->assembly, info->dll, name, dll, target);
269         }
270 }
271
272 static void
273 dllmap_finish (gpointer user_data)
274 {
275         DllInfo *info = user_data;
276
277         g_free (info->dll);
278         g_free (info->target);
279         g_free (info);
280 }
281
282 static const MonoParseHandler
283 dllmap_handler = {
284         "dllmap",
285         dllmap_init,
286         dllmap_start,
287         NULL, /* text */
288         NULL, /* end */
289         dllmap_finish
290 };
291
292 static int inited = 0;
293
294 static void
295 mono_config_init (void)
296 {
297         inited = 1;
298         config_handlers = g_hash_table_new (g_str_hash, g_str_equal);
299         g_hash_table_insert (config_handlers, (gpointer) dllmap_handler.element_name, (gpointer) &dllmap_handler);
300 }
301
302 /* FIXME: error handling */
303
304 static void
305 mono_config_parse_xml_with_context (ParseState *state, const char *text, gsize len)
306 {
307         GMarkupParseContext *context;
308
309         if (!inited)
310                 mono_config_init ();
311
312         context = g_markup_parse_context_new (&mono_parser, 0, state, NULL);
313         if (g_markup_parse_context_parse (context, text, len, NULL)) {
314                 g_markup_parse_context_end_parse (context, NULL);
315         }
316         g_markup_parse_context_free (context);
317 }
318
319 /* If assembly is NULL, parse in the global context */
320 static int
321 mono_config_parse_file_with_context (ParseState *state, const char *filename)
322 {
323         char *text;
324         gsize len;
325
326         mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_CONFIG,
327                         "Config attempting to parse: '%s'.", filename);
328
329         if (!g_file_get_contents (filename, &text, &len, NULL))
330                 return 0;
331         mono_config_parse_xml_with_context (state, text, len);
332         g_free (text);
333         return 1;
334 }
335
336 /**
337  * mono_config_parse_memory:
338  * @buffer: a pointer to an string XML representation of the configuration
339  *
340  * Parses the configuration from a buffer
341  */
342 void
343 mono_config_parse_memory (const char *buffer)
344 {
345         ParseState state = {NULL};
346         
347         mono_config_parse_xml_with_context (&state, buffer, strlen (buffer));
348 }
349
350 static void
351 mono_config_parse_file (const char *filename)
352 {
353         ParseState state = {NULL};
354         mono_config_parse_file_with_context (&state, filename);
355 }
356
357 /* 
358  * use the equivalent lookup code from the GAC when available.
359  * Depending on state, this should give something like:
360  *      aname/version-pubtoken/
361  *      aname/version/
362  *      aname
363  */
364 static char*
365 get_assembly_filename (MonoImage *image, int state)
366 {
367         switch (state) {
368         case 0:
369                 return g_strdup (mono_image_get_name (image));
370         default:
371                 return NULL;
372         }
373 }
374
375 typedef struct _BundledConfig BundledConfig;
376
377 struct _BundledConfig {
378         BundledConfig *next;
379         const char* aname;
380         const char* config_xml;
381 };
382
383 static BundledConfig *bundled_configs = NULL;
384
385 static const char *bundled_machine_config = NULL;
386
387 void
388 mono_register_config_for_assembly (const char* assembly_name, const char* config_xml)
389 {
390         BundledConfig *bconfig;
391
392         bconfig = g_new0 (BundledConfig, 1);
393         bconfig->aname = assembly_name;
394         bconfig->config_xml = config_xml;
395         bconfig->next = bundled_configs;
396         bundled_configs = bconfig;
397 }
398
399 const char *
400 mono_config_string_for_assembly_file (const char *filename)
401 {
402         BundledConfig *bconfig;
403         
404         for (bconfig = bundled_configs; bconfig; bconfig = bconfig->next) {
405                 if (bconfig->aname && strcmp (bconfig->aname, filename) == 0)
406                         return bconfig->config_xml;
407         }
408         return NULL;
409 }
410
411 void 
412 mono_config_for_assembly (MonoImage *assembly)
413 {
414         ParseState state = {NULL};
415         int got_it = 0, i;
416         char *aname, *cfg, *cfg_name;
417         const char *bundled_config;
418         const char *home;
419         
420         state.assembly = assembly;
421
422         bundled_config = mono_config_string_for_assembly_file (assembly->module_name);
423         if (bundled_config)
424                 mono_config_parse_xml_with_context (&state, bundled_config, strlen (bundled_config));
425
426         cfg_name = g_strdup_printf ("%s.config", mono_image_get_filename (assembly));
427         mono_config_parse_file_with_context (&state, cfg_name);
428         g_free (cfg_name);
429
430         cfg_name = g_strdup_printf ("%s.config", mono_image_get_name (assembly));
431
432         home = g_get_home_dir ();
433
434         for (i = 0; (aname = get_assembly_filename (assembly, i)) != NULL; ++i) {
435                 cfg = g_build_filename (mono_get_config_dir (), "mono", "assemblies", aname, cfg_name, NULL);
436                 got_it += mono_config_parse_file_with_context (&state, cfg);
437                 g_free (cfg);
438
439 #ifndef PLATFORM_WIN32
440                 cfg = g_build_filename (home, ".mono", "assemblies", aname, cfg_name, NULL);
441                 got_it += mono_config_parse_file_with_context (&state, cfg);
442                 g_free (cfg);
443 #endif
444                 g_free (aname);
445                 if (got_it)
446                         break;
447         }
448         g_free (cfg_name);
449 }
450
451 /**
452  * mono_config_parse:
453  * @filename: the filename to load the configuration variables from.
454  *
455  * Pass a NULL filename to parse the default config files
456  * (or the file in the MONO_CONFIG env var).
457  */
458 void
459 mono_config_parse (const char *filename) {
460         const char *home;
461         char *mono_cfg;
462 #ifndef PLATFORM_WIN32
463         char *user_cfg;
464 #endif
465
466         if (filename) {
467                 mono_config_parse_file (filename);
468                 return;
469         }
470
471         home = g_getenv ("MONO_CONFIG");
472         if (home) {
473                 mono_config_parse_file (home);
474                 return;
475         }
476
477         mono_cfg = g_build_filename (mono_get_config_dir (), "mono", "config", NULL);
478         mono_config_parse_file (mono_cfg);
479         g_free (mono_cfg);
480
481 #ifndef PLATFORM_WIN32
482         home = g_get_home_dir ();
483         user_cfg = g_strconcat (home, G_DIR_SEPARATOR_S, ".mono/config", NULL);
484         mono_config_parse_file (user_cfg);
485         g_free (user_cfg);
486 #endif
487 }
488
489 static const char *mono_cfg_dir = NULL;
490
491 /* Invoked during startup */
492 void
493 mono_set_config_dir (const char *dir)
494 {
495         /* If this variable is set, overrides the directory computed */
496         mono_cfg_dir = g_getenv ("MONO_CFG_DIR");
497         if (mono_cfg_dir == NULL)
498                 mono_cfg_dir = g_strdup (dir);
499 }
500
501 const char* 
502 mono_get_config_dir (void)
503 {
504         if (mono_cfg_dir == NULL)
505                 mono_set_dirs (NULL, NULL);
506
507         return mono_cfg_dir;
508 }
509
510 void
511 mono_register_machine_config (const char *config_xml)
512 {
513         bundled_machine_config = config_xml;
514 }
515
516 const char *
517 mono_get_machine_config (void)
518 {
519         return bundled_machine_config;
520 }
521
522 static void
523 publisher_policy_start (gpointer user_data,
524                 const gchar *element_name,
525                 const gchar **attribute_names,
526                 const gchar **attribute_values)
527 {
528         MonoAssemblyBindingInfo *info;
529         int n;
530
531         info = user_data;
532         if (!strcmp (element_name, "assemblyIdentity")) {
533                 for (n = 0; attribute_names [n]; n++) {
534                         const gchar *attribute_name = attribute_names [n];
535                         
536                         if (!strcmp (attribute_name, "name"))
537                                 info->name = g_strdup (attribute_values [n]);
538                         else if (!strcmp (attribute_name, "publicKeyToken")) {
539                                 if (strlen (attribute_values [n]) == MONO_PUBLIC_KEY_TOKEN_LENGTH - 1)
540                                         g_strlcpy ((char *) info->public_key_token, attribute_values [n], MONO_PUBLIC_KEY_TOKEN_LENGTH);
541                         } else if (!strcmp (attribute_name, "culture")) {
542                                 if (!strcmp (attribute_values [n], "neutral"))
543                                         info->culture = g_strdup ("");
544                                 else
545                                         info->culture = g_strdup (attribute_values [n]);
546                         }
547                 }
548         } else if (!strcmp (element_name, "bindingRedirect")) {
549                 for (n = 0; attribute_names [n]; n++) {
550                         const gchar *attribute_name = attribute_names [n];
551
552                         if (!strcmp (attribute_name, "oldVersion")) {
553                                 gchar **numbers, **version, **versions;
554                                 gint major, minor, build, revision;
555
556                                 /* Invalid value */
557                                 if (!strcmp (attribute_values [n], ""))
558                                         return;
559                                 
560                                 versions = g_strsplit (attribute_values [n], "-", 2);
561                                 version = g_strsplit (*versions, ".", 4);
562
563                                 /* We assign the values to gint vars to do the checks */
564                                 numbers = version;
565                                 major = *numbers ? atoi (*numbers++) : -1;
566                                 minor = *numbers ? atoi (*numbers++) : -1;
567                                 build = *numbers ? atoi (*numbers++) : -1;
568                                 revision = *numbers ? atoi (*numbers) : -1;
569                                 g_strfreev (version);
570                                 if (major < 0 || minor < 0 || build < 0 || revision < 0) {
571                                         g_strfreev (versions);
572                                         return;
573                                 }
574
575                                 info->old_version_bottom.major = major;
576                                 info->old_version_bottom.minor = minor;
577                                 info->old_version_bottom.build = build;
578                                 info->old_version_bottom.revision = revision;
579                                 info->has_old_version_bottom = TRUE;
580
581                                 if (!*(versions + 1)) {
582                                         g_strfreev (versions);
583                                         continue;
584                                 }
585                                 
586                                 numbers = version = g_strsplit (*(versions + 1), ".", 4);
587                                 major = *numbers ? atoi (*numbers++) : -1;
588                                 minor = *numbers ? atoi (*numbers++) : -1;
589                                 build = *numbers ? atoi (*numbers++) : -1;
590                                 revision = *numbers ? atoi (*numbers) : 1;
591                                 g_strfreev (version);
592                                 if (major < 0 || minor < 0 || build < 0 || revision < 0) {
593                                         g_strfreev (versions);
594                                         return;
595                                 }
596
597                                 info->old_version_top.major = major;
598                                 info->old_version_top.minor = minor;
599                                 info->old_version_top.build = build;
600                                 info->old_version_top.revision = revision;
601                                 info->has_old_version_top = TRUE;
602
603                                 g_strfreev (versions);
604                         } else if (!strcmp (attribute_name, "newVersion")) {
605                                 gchar **numbers, **version;
606
607                                 /* Invalid value */
608                                 if (!strcmp (attribute_values [n], ""))
609                                         return;
610
611                                 numbers = version = g_strsplit (attribute_values [n], ".", 4);
612                                 info->new_version.major = *numbers ? atoi (*numbers++) : -1;
613                                 info->new_version.minor = *numbers ? atoi (*numbers++) : -1;
614                                 info->new_version.build = *numbers ? atoi (*numbers++) : -1;
615                                 info->new_version.revision = *numbers ? atoi (*numbers) : -1;
616                                 info->has_new_version = TRUE;
617                                 g_strfreev (version);
618                         }
619                 }
620         }
621 }
622
623 static MonoParseHandler
624 publisher_policy_parser = {
625         "", /* We don't need to use declare an xml element */
626         NULL,
627         publisher_policy_start,
628         NULL,
629         NULL,
630         NULL
631 };
632
633 void
634 mono_config_parse_publisher_policy (const gchar *filename, MonoAssemblyBindingInfo *info)
635 {
636         ParseState state = {
637                 &publisher_policy_parser, /* MonoParseHandler */
638                 info, /* user_data */
639                 NULL, /* MonoImage (we don't need it right now)*/
640                 TRUE /* We are already inited */
641         };
642         
643         mono_config_parse_file_with_context (&state, filename);
644 }
645