2 Copyright (c) 2014-2016 Intel Corporation. All Rights Reserved.
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions
8 * Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
10 * Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the distribution.
13 * Neither the name of Intel Corporation nor the names of its
14 contributors may be used to endorse or promote products derived
15 from this software without specific prior written permission.
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include "offload_env.h"
34 #include "offload_util.h"
35 #include "liboffload_error_codes.h"
37 // for environment variables valid on all cards
38 const int MicEnvVar::any_card
= -1;
40 MicEnvVar::~MicEnvVar()
42 for (std::list
<MicEnvVar::CardEnvVars
*>::const_iterator
43 it
= card_spec_list
.begin();
44 it
!= card_spec_list
.end(); it
++) {
45 CardEnvVars
*card_data
= *it
;
50 MicEnvVar::VarValue::~VarValue()
55 MicEnvVar::CardEnvVars::~CardEnvVars()
57 for (std::list
<MicEnvVar::VarValue
*>::const_iterator it
= env_vars
.begin();
58 it
!= env_vars
.end(); it
++) {
59 VarValue
*var_value
= *it
;
64 // Searching for card in "card_spec_list" list with the same "number"
66 MicEnvVar::CardEnvVars
* MicEnvVar::get_card(int number
)
68 if (number
== any_card
) {
71 for (std::list
<MicEnvVar::CardEnvVars
*>::const_iterator
72 it
= card_spec_list
.begin();
73 it
!= card_spec_list
.end(); it
++) {
74 CardEnvVars
*card_data
= *it
;
75 if (card_data
->card_number
== number
) {
82 // Searching for environment variable in "env_var" list with the same name
84 MicEnvVar::VarValue
* MicEnvVar::CardEnvVars::find_var(
86 int env_var_name_length
89 for (std::list
<MicEnvVar::VarValue
*>::const_iterator it
= env_vars
.begin();
90 it
!= env_vars
.end(); it
++) {
91 VarValue
*var_value
= *it
;
92 if (var_value
->length
== env_var_name_length
&&
93 !strncmp(var_value
->env_var
, env_var_name
,
94 env_var_name_length
)) {
101 void MicEnvVar::analyze_env_var(char *env_var_string
)
106 int env_var_name_length
;
107 MicEnvVarKind env_var_kind
;
109 env_var_kind
= get_env_var_kind(env_var_string
,
112 &env_var_name_length
,
114 switch (env_var_kind
) {
117 add_env_var(card_number
,
123 mic_parse_env_var_list(card_number
, env_var_def
);
131 void MicEnvVar::add_env_var(
134 int env_var_name_length
,
141 // The case corresponds to common env var definition of kind
142 // <mic-prefix>_<var>
143 if (card_number
== any_card
) {
147 card
= get_card(card_number
);
149 // definition for new card occurred
150 card
= new CardEnvVars(card_number
);
151 card_spec_list
.push_back(card
);
155 var
= card
->find_var(env_var_name
, env_var_name_length
);
157 // put new env var definition in "env_var" list
158 var
= new VarValue(env_var_name
, env_var_name_length
, env_var_def
);
159 card
->env_vars
.push_back(var
);
163 // The routine analyses string pointed by "env_var_string" argument
164 // according to the following syntax:
166 // Specification of prefix for MIC environment variables
167 // MIC_ENV_PREFIX=<mic-prefix>
169 // Setting single MIC environment variable
170 // <mic-prefix>_<var>=<value>
171 // <mic-prefix>_<card-number>_<var>=<value>
173 // Setting multiple MIC environment variables
174 // <mic-prefix>_<card-number>_ENV=<env-vars>
176 MicEnvVarKind
MicEnvVar::get_env_var_kind(
177 char *env_var_string
,
180 int *env_var_name_length
,
184 int len
= strlen(prefix
);
185 char *c
= env_var_string
;
187 bool card_is_set
= false;
189 if (strncmp(c
, prefix
, len
) != 0 || c
[len
] != '_') {
194 *card_number
= any_card
;
196 while (isdigit (*c
)) {
197 num
= (*c
++ - '0') + (num
* 10);
209 *env_var_name
= *env_var_def
= c
;
210 if (strncmp(c
, "ENV=", 4) == 0) {
212 *env_var_name_length
= 3;
213 *env_var_name
= *env_var_def
= c
;
214 *env_var_def
= strdup(*env_var_def
);
215 if (*env_var_def
== NULL
)
216 LIBOFFLOAD_ERROR(c_malloc
);
219 *env_var_def
= c
+ strlen("ENV=");
220 *env_var_def
= strdup(*env_var_def
);
221 if (*env_var_def
== NULL
)
222 LIBOFFLOAD_ERROR(c_malloc
);
223 return c_mic_card_env
;
226 *env_var_name_length
= 0;
227 while (isalnum(*c
) || *c
== '_') {
229 (*env_var_name_length
)++;
235 *env_var_def
= strdup(*env_var_def
);
236 if (*env_var_def
== NULL
)
237 LIBOFFLOAD_ERROR(c_malloc
);
238 return card_is_set
? c_mic_card_var
: c_mic_var
;
241 // analysing <env-vars> in form:
242 // <mic-prefix>_<card-number>_ENV=<env-vars>
247 // <env-vars> | <env-var>
254 void MicEnvVar::mic_parse_env_var_list(
255 int card_number
, char *env_vars_def_list
)
257 char *c
= env_vars_def_list
;
259 int env_var_name_length
;
267 var_is_quoted
= false;
269 env_var_name_length
= 0;
271 while (isalnum(*c
) || *c
== '_') {
273 env_var_name_length
++;
277 LIBOFFLOAD_ERROR(c_mic_parse_env_var_list1
);
281 LIBOFFLOAD_ERROR(c_mic_parse_env_var_list2
);
287 var_is_quoted
= true;
290 // Environment variable values that contain | will need to be escaped.
291 while (*c
!= 0 && *c
!= '|' &&
292 (!var_is_quoted
|| *c
!= '"'))
294 // skip escaped symbol
302 while (*c
!= 0 && *c
!= '|') {
307 int sz
= c
- env_var_name
;
308 env_var_def
= (char*)malloc(sz
);
309 if (env_var_def
== NULL
)
310 LIBOFFLOAD_ERROR(c_malloc
);
311 memcpy(env_var_def
, env_var_name
, sz
);
316 while (*c
!= 0 && *c
== ' ') {
320 add_env_var(card_number
,
327 // Collect all definitions for the card with number "card_num".
328 // The returned result is vector of string pointers defining one
329 // environment variable. The vector is terminated by NULL pointer.
330 // In the beginning of the vector there are env vars defined as
331 // <mic-prefix>_<card-number>_<var>=<value>
333 // <mic-prefix>_<card-number>_ENV=<env-vars>
334 // where <card-number> is equal to "card_num"
335 // They are followed by definitions valid for any card
336 // and absent in previous definitions.
338 char** MicEnvVar::create_environ_for_card(int card_num
)
341 VarValue
*var_value_find
;
342 CardEnvVars
*card_data
= get_card(card_num
);
343 CardEnvVars
*card_data_common
;
344 std::list
<char*> new_env
;
350 // There is no personel env var definitions for the card with
353 return create_environ_for_card(any_card
);
356 for (std::list
<MicEnvVar::VarValue
*>::const_iterator
357 it
= card_data
->env_vars
.begin();
358 it
!= card_data
->env_vars
.end(); it
++) {
360 new_env
.push_back(var_value
->env_var_value
);
363 if (card_num
!= any_card
) {
364 card_data_common
= get_card(any_card
);
365 for (std::list
<MicEnvVar::VarValue
*>::const_iterator
366 it
= card_data_common
->env_vars
.begin();
367 it
!= card_data_common
->env_vars
.end(); it
++) {
369 var_value_find
= card_data
->find_var(var_value
->env_var
,
371 if (!var_value_find
) {
372 new_env
.push_back(var_value
->env_var_value
);
377 int new_env_size
= new_env
.size();
378 rez
= (char**) malloc((new_env_size
+ 1) * sizeof(char*));
380 LIBOFFLOAD_ERROR(c_malloc
);
381 std::copy(new_env
.begin(), new_env
.end(), rez
);
382 rez
[new_env_size
] = 0;