This source file includes following definitions.
- orte_pre_condition_transports_use_rand
- orte_pre_condition_transports_print
- orte_pre_condition_transports
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 #include "orte_config.h"
24
25 #include <string.h>
26 #ifdef HAVE_UNISTD_H
27 #include <unistd.h>
28 #endif
29 #ifdef HAVE_SYS_TYPES_H
30 #include <sys/types.h>
31 #endif
32 #ifdef HAVE_SYS_STAT_H
33 #include <sys/stat.h>
34 #endif
35 #ifdef HAVE_FCNTL_H
36 #include <fcntl.h>
37 #endif
38 #include <time.h>
39
40 #include "opal/mca/base/mca_base_var.h"
41 #include "opal/util/alfg.h"
42 #include "opal/util/opal_environ.h"
43 #include "opal/util/printf.h"
44
45 #include "orte/constants.h"
46 #include "orte/types.h"
47 #include "orte/mca/errmgr/errmgr.h"
48 #include "orte/util/attr.h"
49
50 #include "orte/util/pre_condition_transports.h"
51
52
53
54
55
56
57
58
59
60 static inline void orte_pre_condition_transports_use_rand(uint64_t* unique_key) {
61 opal_rng_buff_t rng;
62 opal_srand(&rng,(unsigned int)time(NULL));
63 unique_key[0] = opal_rand(&rng);
64 unique_key[1] = opal_rand(&rng);
65 }
66
67 char* orte_pre_condition_transports_print(uint64_t *unique_key)
68 {
69 unsigned int *int_ptr;
70 size_t i, j, string_key_len, written_len;
71 char *string_key = NULL, *format = NULL;
72
73
74
75
76 string_key_len = (sizeof(uint64_t) * 2) * 2 + strlen("-") + 1;
77 string_key = (char*) malloc(string_key_len);
78 if (NULL == string_key) {
79 return NULL;
80 }
81
82 string_key[0] = '\0';
83 written_len = 0;
84
85
86
87
88
89
90
91
92
93 opal_asprintf(&format, "%%0%dx", (int)(sizeof(unsigned int)) * 2);
94
95
96 int_ptr = (unsigned int*) &unique_key[0];
97 for (i = 0 ; i < sizeof(uint64_t) / sizeof(unsigned int) ; ++i) {
98 if (0 == int_ptr[i]) {
99
100 for (j=0; j < sizeof(unsigned int); j++) {
101 int_ptr[i] |= j << j;
102 }
103 }
104 snprintf(string_key + written_len,
105 string_key_len - written_len,
106 format, int_ptr[i]);
107 written_len = strlen(string_key);
108 }
109
110
111 snprintf(string_key + written_len, string_key_len - written_len, "-");
112 written_len = strlen(string_key);
113
114
115 int_ptr = (unsigned int*) &unique_key[1];
116 for (i = 0 ; i < sizeof(uint64_t) / sizeof(unsigned int) ; ++i) {
117 if (0 == int_ptr[i]) {
118
119 for (j=0; j < sizeof(unsigned int); j++) {
120 int_ptr[i] |= j << j;
121 }
122 }
123 snprintf(string_key + written_len,
124 string_key_len - written_len,
125 format, int_ptr[i]);
126 written_len = strlen(string_key);
127 }
128 free(format);
129
130 return string_key;
131 }
132
133
134 int orte_pre_condition_transports(orte_job_t *jdata, char **key)
135 {
136 uint64_t unique_key[2];
137 int n;
138 orte_app_context_t *app;
139 char *string_key, *cs_env;
140 int fd_rand;
141 size_t bytes_read;
142 struct stat buf;
143
144
145
146
147 if(0 != stat("/dev/urandom", &buf)) {
148
149 orte_pre_condition_transports_use_rand(unique_key);
150 }
151
152 if(-1 == (fd_rand = open("/dev/urandom", O_RDONLY))) {
153 orte_pre_condition_transports_use_rand(unique_key);
154 } else {
155 bytes_read = read(fd_rand, (char *) unique_key, 16);
156 if(bytes_read != 16) {
157 orte_pre_condition_transports_use_rand(unique_key);
158 }
159 close(fd_rand);
160 }
161
162 if (NULL == (string_key = orte_pre_condition_transports_print(unique_key))) {
163 ORTE_ERROR_LOG(ORTE_ERR_OUT_OF_RESOURCE);
164 return ORTE_ERR_OUT_OF_RESOURCE;
165 }
166
167
168 if (NULL != jdata) {
169 orte_set_attribute(&jdata->attributes, ORTE_JOB_TRANSPORT_KEY, ORTE_ATTR_LOCAL, string_key, OPAL_STRING);
170
171 if (OPAL_SUCCESS != mca_base_var_env_name ("orte_precondition_transports", &cs_env)) {
172 ORTE_ERROR_LOG(ORTE_ERR_OUT_OF_RESOURCE);
173 free(string_key);
174 return ORTE_ERR_OUT_OF_RESOURCE;
175 }
176
177 for (n=0; n < jdata->apps->size; n++) {
178 if (NULL == (app = (orte_app_context_t*)opal_pointer_array_get_item(jdata->apps, n))) {
179 continue;
180 }
181 opal_setenv(cs_env, string_key, true, &app->env);
182 }
183 free(cs_env);
184 free(string_key);
185 } else if (NULL != key) {
186 *key = string_key;
187 } else {
188 free(string_key);
189 }
190
191 return ORTE_SUCCESS;
192 }