offload_env.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /*
  2. Copyright (c) 2014 Intel Corporation. All Rights Reserved.
  3. Redistribution and use in source and binary forms, with or without
  4. modification, are permitted provided that the following conditions
  5. are met:
  6. * Redistributions of source code must retain the above copyright
  7. notice, this list of conditions and the following disclaimer.
  8. * Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in the
  10. documentation and/or other materials provided with the distribution.
  11. * Neither the name of Intel Corporation nor the names of its
  12. contributors may be used to endorse or promote products derived
  13. from this software without specific prior written permission.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  15. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  16. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  17. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  18. HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  19. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  20. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  21. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  22. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "offload_env.h"
  27. #include <string.h>
  28. #include <ctype.h>
  29. #include "offload_util.h"
  30. #include "liboffload_error_codes.h"
  31. // for environment variables valid on all cards
  32. const int MicEnvVar::any_card = -1;
  33. MicEnvVar::~MicEnvVar()
  34. {
  35. for (std::list<MicEnvVar::CardEnvVars*>::const_iterator
  36. it = card_spec_list.begin();
  37. it != card_spec_list.end(); it++) {
  38. CardEnvVars *card_data = *it;
  39. delete card_data;
  40. }
  41. }
  42. MicEnvVar::VarValue::~VarValue()
  43. {
  44. free(env_var_value);
  45. }
  46. MicEnvVar::CardEnvVars::~CardEnvVars()
  47. {
  48. for (std::list<MicEnvVar::VarValue*>::const_iterator it = env_vars.begin();
  49. it != env_vars.end(); it++) {
  50. VarValue *var_value = *it;
  51. delete var_value;
  52. }
  53. }
  54. // Searching for card in "card_spec_list" list with the same "number"
  55. MicEnvVar::CardEnvVars* MicEnvVar::get_card(int number)
  56. {
  57. if (number == any_card) {
  58. return &common_vars;
  59. }
  60. for (std::list<MicEnvVar::CardEnvVars*>::const_iterator
  61. it = card_spec_list.begin();
  62. it != card_spec_list.end(); it++) {
  63. CardEnvVars *card_data = *it;
  64. if (card_data->card_number == number) {
  65. return card_data;
  66. }
  67. }
  68. return NULL;
  69. }
  70. // Searching for environment variable in "env_var" list with the same name
  71. MicEnvVar::VarValue* MicEnvVar::CardEnvVars::find_var(
  72. char* env_var_name,
  73. int env_var_name_length
  74. )
  75. {
  76. for (std::list<MicEnvVar::VarValue*>::const_iterator it = env_vars.begin();
  77. it != env_vars.end(); it++) {
  78. VarValue *var_value = *it;
  79. if (var_value->length == env_var_name_length &&
  80. !strncmp(var_value->env_var, env_var_name,
  81. env_var_name_length)) {
  82. return var_value;
  83. }
  84. }
  85. return NULL;
  86. }
  87. void MicEnvVar::analyze_env_var(char *env_var_string)
  88. {
  89. char *env_var_name;
  90. char *env_var_def;
  91. int card_number;
  92. int env_var_name_length;
  93. MicEnvVarKind env_var_kind;
  94. env_var_kind = get_env_var_kind(env_var_string,
  95. &card_number,
  96. &env_var_name,
  97. &env_var_name_length,
  98. &env_var_def);
  99. switch (env_var_kind) {
  100. case c_mic_var:
  101. case c_mic_card_var:
  102. add_env_var(card_number,
  103. env_var_name,
  104. env_var_name_length,
  105. env_var_def);
  106. break;
  107. case c_mic_card_env:
  108. mic_parse_env_var_list(card_number, env_var_def);
  109. break;
  110. case c_no_mic:
  111. default:
  112. break;
  113. }
  114. }
  115. void MicEnvVar::add_env_var(
  116. int card_number,
  117. char *env_var_name,
  118. int env_var_name_length,
  119. char *env_var_def
  120. )
  121. {
  122. VarValue *var;
  123. CardEnvVars *card;
  124. // The case corresponds to common env var definition of kind
  125. // <mic-prefix>_<var>
  126. if (card_number == any_card) {
  127. card = &common_vars;
  128. }
  129. else {
  130. card = get_card(card_number);
  131. if (!card) {
  132. // definition for new card occured
  133. card = new CardEnvVars(card_number);
  134. card_spec_list.push_back(card);
  135. }
  136. }
  137. var = card->find_var(env_var_name, env_var_name_length);
  138. if (!var) {
  139. // put new env var definition in "env_var" list
  140. var = new VarValue(env_var_name, env_var_name_length, env_var_def);
  141. card->env_vars.push_back(var);
  142. }
  143. }
  144. // The routine analyses string pointed by "env_var_string" argument
  145. // according to the following syntax:
  146. //
  147. // Specification of prefix for MIC environment variables
  148. // MIC_ENV_PREFIX=<mic-prefix>
  149. //
  150. // Setting single MIC environment variable
  151. // <mic-prefix>_<var>=<value>
  152. // <mic-prefix>_<card-number>_<var>=<value>
  153. // Setting multiple MIC environment variables
  154. // <mic-prefix>_<card-number>_ENV=<env-vars>
  155. MicEnvVarKind MicEnvVar::get_env_var_kind(
  156. char *env_var_string,
  157. int *card_number,
  158. char **env_var_name,
  159. int *env_var_name_length,
  160. char **env_var_def
  161. )
  162. {
  163. int len = strlen(prefix);
  164. char *c = env_var_string;
  165. int num = 0;
  166. bool card_is_set = false;
  167. if (strncmp(c, prefix, len) != 0 || c[len] != '_') {
  168. return c_no_mic;
  169. }
  170. c += len + 1;
  171. *card_number = any_card;
  172. if (isdigit(*c)) {
  173. while (isdigit (*c)) {
  174. num = (*c++ - '0') + (num * 10);
  175. }
  176. if (*c != '_') {
  177. return c_no_mic;
  178. }
  179. c++;
  180. *card_number = num;
  181. card_is_set = true;
  182. }
  183. if (!isalpha(*c)) {
  184. return c_no_mic;
  185. }
  186. *env_var_name = *env_var_def = c;
  187. if (strncmp(c, "ENV=", 4) == 0) {
  188. if (!card_is_set) {
  189. *env_var_name_length = 3;
  190. *env_var_name = *env_var_def = c;
  191. *env_var_def = strdup(*env_var_def);
  192. return c_mic_var;
  193. }
  194. *env_var_def = c + strlen("ENV=");
  195. *env_var_def = strdup(*env_var_def);
  196. return c_mic_card_env;
  197. }
  198. if (isalpha(*c)) {
  199. *env_var_name_length = 0;
  200. while (isalnum(*c) || *c == '_') {
  201. c++;
  202. (*env_var_name_length)++;
  203. }
  204. }
  205. if (*c != '=') {
  206. return c_no_mic;
  207. }
  208. *env_var_def = strdup(*env_var_def);
  209. return card_is_set? c_mic_card_var : c_mic_var;
  210. }
  211. // analysing <env-vars> in form:
  212. // <mic-prefix>_<card-number>_ENV=<env-vars>
  213. // where:
  214. //
  215. // <env-vars>:
  216. // <env-var>
  217. // <env-vars> | <env-var>
  218. //
  219. // <env-var>:
  220. // variable=value
  221. // variable="value"
  222. // variable=
  223. void MicEnvVar::mic_parse_env_var_list(
  224. int card_number, char *env_vars_def_list)
  225. {
  226. char *c = env_vars_def_list;
  227. char *env_var_name;
  228. int env_var_name_length;
  229. char *env_var_def;
  230. bool var_is_quoted;
  231. if (*c == '"') {
  232. c++;
  233. }
  234. while (*c != 0) {
  235. var_is_quoted = false;
  236. env_var_name = c;
  237. env_var_name_length = 0;
  238. if (isalpha(*c)) {
  239. while (isalnum(*c) || *c == '_') {
  240. c++;
  241. env_var_name_length++;
  242. }
  243. }
  244. else {
  245. LIBOFFLOAD_ERROR(c_mic_parse_env_var_list1);
  246. return;
  247. }
  248. if (*c != '=') {
  249. LIBOFFLOAD_ERROR(c_mic_parse_env_var_list2);
  250. return;
  251. }
  252. c++;
  253. if (*c == '"') {
  254. var_is_quoted = true;
  255. c++;
  256. }
  257. // Environment variable values that contain | will need to be escaped.
  258. while (*c != 0 && *c != '|' &&
  259. (!var_is_quoted || *c != '"'))
  260. {
  261. // skip escaped symbol
  262. if (*c == '\\') {
  263. c++;
  264. }
  265. c++;
  266. }
  267. if (var_is_quoted) {
  268. c++; // for "
  269. while (*c != 0 && *c != '|') {
  270. c++;
  271. }
  272. }
  273. int sz = c - env_var_name;
  274. env_var_def = (char*)malloc(sz);
  275. if (env_var_def == NULL)
  276. LIBOFFLOAD_ERROR(c_malloc);
  277. memcpy(env_var_def, env_var_name, sz);
  278. env_var_def[sz] = 0;
  279. if (*c == '|') {
  280. c++;
  281. while (*c != 0 && *c == ' ') {
  282. c++;
  283. }
  284. }
  285. add_env_var(card_number,
  286. env_var_name,
  287. env_var_name_length,
  288. env_var_def);
  289. }
  290. }
  291. // Collect all definitions for the card with number "card_num".
  292. // The returned result is vector of string pointers defining one
  293. // environment variable. The vector is terminated by NULL pointer.
  294. // In the begining of the vector there are env vars defined as
  295. // <mic-prefix>_<card-number>_<var>=<value>
  296. // or
  297. // <mic-prefix>_<card-number>_ENV=<env-vars>
  298. // where <card-number> is equal to "card_num"
  299. // They are followed by definitions valid for any card
  300. // and absent in previous definitions.
  301. char** MicEnvVar::create_environ_for_card(int card_num)
  302. {
  303. VarValue *var_value;
  304. VarValue *var_value_find;
  305. CardEnvVars *card_data = get_card(card_num);
  306. CardEnvVars *card_data_common;
  307. std::list<char*> new_env;
  308. char **rez;
  309. if (!prefix) {
  310. return NULL;
  311. }
  312. // There is no personel env var definitions for the card with
  313. // number "card_num"
  314. if (!card_data) {
  315. return create_environ_for_card(any_card);
  316. }
  317. for (std::list<MicEnvVar::VarValue*>::const_iterator
  318. it = card_data->env_vars.begin();
  319. it != card_data->env_vars.end(); it++) {
  320. var_value = *it;
  321. new_env.push_back(var_value->env_var_value);
  322. }
  323. if (card_num != any_card) {
  324. card_data_common = get_card(any_card);
  325. for (std::list<MicEnvVar::VarValue*>::const_iterator
  326. it = card_data_common->env_vars.begin();
  327. it != card_data_common->env_vars.end(); it++) {
  328. var_value = *it;
  329. var_value_find = card_data->find_var(var_value->env_var,
  330. var_value->length);
  331. if (!var_value_find) {
  332. new_env.push_back(var_value->env_var_value);
  333. }
  334. }
  335. }
  336. int new_env_size = new_env.size();
  337. rez = (char**) malloc((new_env_size + 1) * sizeof(char*));
  338. if (rez == NULL)
  339. LIBOFFLOAD_ERROR(c_malloc);
  340. std::copy(new_env.begin(), new_env.end(), rez);
  341. rez[new_env_size] = 0;
  342. return rez;
  343. }