Logo AND Algorithmique Numérique Distribuée

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