Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
898311094e8ed4867ee2a4a1718a69009e598880
[simgrid.git] / src / gras / Virtu / rl_process.c
1 /* process_rl - GRAS process handling on real life                          */
2
3 /* Copyright (c) 2003, 2004 Martin Quinson. All rights reserved.            */
4
5 /* This program is free software; you can redistribute it and/or modify it
6  * under the terms of the license (GNU LGPL) which comes with this package. */
7
8 #include "gras_modinter.h"      /* module initialization interface */
9 #include "gras/Virtu/virtu_rl.h"
10 #include "portable.h"
11
12 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(gras_virtu_process);
13
14 /* globals */
15 static gras_procdata_t *_gras_procdata = NULL;
16 XBT_EXPORT_NO_IMPORT(char const *) _gras_procname = NULL;
17      static xbt_dict_t _process_properties = NULL;
18      static xbt_dict_t _host_properties = NULL;
19
20 # ifdef __APPLE__
21 /* 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 */
22 # include <crt_externs.h>
23 # define environ (*_NSGetEnviron())
24 # else
25  /* the environment, as specified by the opengroup, used to initialize the process properties */
26      extern char **environ;
27 # endif
28
29      void gras_process_init()
30 {
31   char **env_iter;
32   _gras_procdata = xbt_new0(gras_procdata_t, 1);
33   gras_procdata_init();
34
35   /* initialize the host & process properties */
36   _host_properties = xbt_dict_new();
37   _process_properties = xbt_dict_new();
38   env_iter = environ;
39   while (*env_iter) {
40     char *equal, *buf = xbt_strdup(*env_iter);
41     equal = strchr(buf, '=');
42     if (!equal) {
43       WARN1
44         ("The environment contains an entry without '=' char: %s (ignore it)",
45          *env_iter);
46       continue;
47     }
48     *equal = '\0';
49     xbt_dict_set(_process_properties, buf, xbt_strdup(equal + 1), xbt_free_f);
50     free(buf);
51     env_iter++;
52   }
53 }
54
55 void gras_process_exit()
56 {
57   gras_procdata_exit();
58   free(_gras_procdata);
59   xbt_dict_free(&_process_properties);
60 }
61
62 const char *xbt_procname(void)
63 {
64   if (_gras_procname)
65     return _gras_procname;
66   else
67     return "";
68 }
69
70 int gras_os_getpid(void)
71 {
72 #ifdef _WIN32
73   return (long int) GetCurrentProcessId();
74 #else
75   return (long int) getpid();
76 #endif
77 }
78
79 /* **************************************************************************
80  * Process data
81  * **************************************************************************/
82
83 gras_procdata_t *gras_procdata_get(void)
84 {
85   xbt_assert0(_gras_procdata, "Run gras_process_init (ie, gras_init)!");
86
87   return _gras_procdata;
88 }
89
90 void gras_agent_spawn(const char *name, void *data,
91                       xbt_main_func_t code, int argc, char *argv[],
92                       xbt_dict_t properties)
93 {
94   THROW_UNIMPLEMENTED;
95 }
96
97 /* **************************************************************************
98  * Properties
99  * **************************************************************************/
100
101 const char *gras_process_property_value(const char *name)
102 {
103   return xbt_dict_get_or_null(_process_properties, name);
104 }
105
106 xbt_dict_t gras_process_properties(void)
107 {
108   return _process_properties;
109 }
110
111 const char *gras_os_host_property_value(const char *name)
112 {
113   return xbt_dict_get_or_null(_host_properties, name);
114 }
115
116 xbt_dict_t gras_os_host_properties(void)
117 {
118   return _host_properties;
119 }