Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
121a7ed2be9df35267bafecb1656981858c3536a
[simgrid.git] / src / gras / Virtu / rl_process.c
1 /* process_rl - GRAS process handling on real life                          */
2
3 /* Copyright (c) 2004, 2005, 2006, 2007, 2008, 2009, 2010. The SimGrid Team.
4  * All rights reserved.                                                     */
5
6 /* This program is free software; you can redistribute it and/or modify it
7  * under the terms of the license (GNU LGPL) which comes with this package. */
8
9 #include "gras_modinter.h"      /* module initialization interface */
10 #include "gras/Virtu/virtu_rl.h"
11 #include "portable.h"
12 #include "xbt/xbt_os_thread.h"
13
14 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(gras_virtu_process);
15
16 /* globals */
17 static gras_procdata_t *_gras_procdata = NULL;
18 XBT_EXPORT_NO_IMPORT(char const *) _gras_procname = NULL;
19 static xbt_dict_t _process_properties = NULL;
20 static xbt_dict_t _host_properties = NULL;
21
22 # ifdef __APPLE__
23         /* under darwin, the environment gets added to the process at startup time. So, it's not defined at library link time, forcing us to extra tricks */
24 # include <crt_externs.h>
25 # define environ (*_NSGetEnviron())
26 # else
27 #ifdef _XBT_WIN32
28                  /* the environment, as specified by the opengroup, used to initialize the process properties */
29 extern char **wenviron;
30 #else
31 extern char **environ;
32 #endif
33 # endif
34
35 void gras_process_init()
36 {
37   char **env_iter;
38   _gras_procdata = xbt_new0(gras_procdata_t, 1);
39   gras_procdata_init();
40
41   /* initialize the host & process properties */
42   _host_properties = xbt_dict_new();
43   _process_properties = xbt_dict_new();
44   env_iter = environ;
45   while (*env_iter) {
46     char *equal, *buf = xbt_strdup(*env_iter);
47     equal = strchr(buf, '=');
48     if (!equal) {
49       XBT_WARN
50           ("The environment contains an entry without '=' char: %s (ignore it)",
51            *env_iter);
52       continue;
53     }
54     *equal = '\0';
55     xbt_dict_set(_process_properties, buf, xbt_strdup(equal + 1),
56                  xbt_free_f);
57     free(buf);
58     env_iter++;
59   }
60 }
61
62 void gras_process_exit()
63 {
64   gras_procdata_exit();
65   free(_gras_procdata);
66   xbt_dict_free(&_process_properties);
67   xbt_dict_free(&_host_properties);
68 }
69
70 const char *xbt_procname(void)
71 {
72   if (_gras_procname)
73     return _gras_procname;
74   else
75     return "";
76 }
77
78 int gras_os_getpid(void)
79 {
80 #ifdef _XBT_WIN32
81   return (long int) GetCurrentProcessId();
82 #else
83   return (long int) getpid();
84 #endif
85 }
86
87 /* **************************************************************************
88  * Process data
89  * **************************************************************************/
90
91 gras_procdata_t *gras_procdata_get(void)
92 {
93   xbt_assert0(_gras_procdata, "Run gras_process_init (ie, gras_init)!");
94
95   return _gras_procdata;
96 }
97
98 typedef struct {
99   xbt_main_func_t code;
100   int argc;
101   char **argv;
102 } spawner_wrapper_args;
103
104 static void *spawner_wrapper(void *data) {
105   spawner_wrapper_args *a = data;
106   (*(a->code))(a->argc,a->argv);
107   free(a);
108   return NULL;
109 }
110
111 void gras_agent_spawn(const char *name,
112                       xbt_main_func_t code, int argc, char *argv[],
113                       xbt_dict_t properties)
114 {
115   spawner_wrapper_args *args =malloc(sizeof(spawner_wrapper_args));
116   args->argc=argc;
117   args->argv=argv;
118   args->code=code;
119   xbt_os_thread_create(name,spawner_wrapper,args, NULL);
120 }
121
122 /* **************************************************************************
123  * Properties
124  * **************************************************************************/
125
126 const char *gras_process_property_value(const char *name)
127 {
128   return xbt_dict_get_or_null(_process_properties, name);
129 }
130
131 xbt_dict_t gras_process_properties(void)
132 {
133   return _process_properties;
134 }
135
136 const char *gras_os_host_property_value(const char *name)
137 {
138   return xbt_dict_get_or_null(_host_properties, name);
139 }
140
141 xbt_dict_t gras_os_host_properties(void)
142 {
143   return _host_properties;
144 }