/
mca_base_var.c
2292 lines (1916 loc) · 72.7 KB
/
mca_base_var.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil -*- */
/*
* Copyright (c) 2004-2008 The Trustees of Indiana University and Indiana
* University Research and Technology
* Corporation. All rights reserved.
* Copyright (c) 2004-2021 The University of Tennessee and The University
* of Tennessee Research Foundation. All rights
* reserved.
* Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
* University of Stuttgart. All rights reserved.
* Copyright (c) 2004-2005 The Regents of the University of California.
* All rights reserved.
* Copyright (c) 2008-2021 Cisco Systems, Inc. All rights reserved
* Copyright (c) 2012-2018 Los Alamos National Security, LLC. All rights
* reserved.
* Copyright (c) 2014-2020 Intel, Inc. All rights reserved.
* Copyright (c) 2015 Research Organization for Information Science
* and Technology (RIST). All rights reserved.
* Copyright (c) 2017 IBM Corporation. All rights reserved.
* Copyright (c) 2018 Amazon.com, Inc. or its affiliates. All Rights reserved.
* Copyright (c) 2018 Triad National Security, LLC. All rights
* reserved.
* Copyright (c) 2020 Google, LLC. All rights reserved.
* Copyright (c) 2021 Nanook Consulting. All rights reserved.
* Copyright (c) 2022 Computer Architecture and VLSI Systems (CARV)
* Laboratory, ICS Forth. All rights reserved.
* Copyright (c) 2023 Jeffrey M. Squyres. All rights reserved.
* $COPYRIGHT$
*
* Additional copyrights may follow
*
* $HEADER$
*/
#include "opal_config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#ifdef HAVE_SYS_PARAM_H
# include <sys/param.h>
#endif
#include <errno.h>
#include "opal/constants.h"
#include "opal/include/opal_stdint.h"
#include "opal/mca/base/mca_base_alias.h"
#include "opal/mca/base/mca_base_vari.h"
#include "opal/mca/installdirs/installdirs.h"
#include "opal/mca/mca.h"
#include "opal/runtime/opal.h"
#include "opal/runtime/opal_params_core.h"
#include "opal/util/argv.h"
#include "opal/util/opal_environ.h"
#include "opal/util/os_path.h"
#include "opal/util/output.h"
#include "opal/util/path.h"
#include "opal/util/printf.h"
#include "opal/util/show_help.h"
/*
* local variables
*/
static opal_pointer_array_t mca_base_vars;
static const char *mca_prefix = OPAL_MCA_PREFIX;
static char *home = NULL;
static char *cwd = NULL;
bool mca_base_var_initialized = false;
static char *force_agg_path = NULL;
static char *mca_base_var_files = NULL;
static char *mca_base_envar_files = NULL;
static char **mca_base_var_file_list = NULL;
static char *mca_base_var_override_file = NULL;
static char *mca_base_var_file_prefix = NULL;
static char *mca_base_envar_file_prefix = NULL;
static char *mca_base_param_file_path = NULL;
char *mca_base_env_list = NULL;
char *mca_base_env_list_sep = MCA_BASE_ENV_LIST_SEP_DEFAULT;
char *mca_base_env_list_internal = NULL;
static bool mca_base_var_suppress_override_warning = false;
static opal_list_t mca_base_var_file_values;
static opal_list_t mca_base_envar_file_values;
static opal_list_t mca_base_var_override_values;
static int mca_base_var_count = 0;
static opal_hash_table_t mca_base_var_index_hash;
#define OPAL_MCA_VAR_MBV_ENUMERATOR_FREE(mbv_enumerator) \
{ \
if (mbv_enumerator && !mbv_enumerator->enum_is_static) { \
OBJ_RELEASE(mbv_enumerator); \
} \
}
const char *ompi_var_type_names[] = {
"int", "unsigned_int", "unsigned_long", "unsigned_long_long",
"size_t", "string", "version_string", "bool",
"double", "long", "int32_t", "uint32_t",
"int64_t", "uint64_t",
};
const size_t ompi_var_type_sizes[] = {
sizeof(int), sizeof(unsigned), sizeof(unsigned long), sizeof(unsigned long long),
sizeof(size_t), sizeof(char), sizeof(char), sizeof(bool),
sizeof(double), sizeof(long), sizeof(int32_t), sizeof(uint32_t),
sizeof(int64_t), sizeof(uint64_t),
};
static const char *var_source_names[] = {"default", "command line", "environment",
"file", "set", "override"};
static const char *info_lvl_strings[] = {"user/basic", "user/detail", "user/all",
"tuner/basic", "tuner/detail", "tuner/all",
"dev/basic", "dev/detail", "dev/all"};
/*
* local functions
*/
static int fixup_files(char **file_list, char *path, bool rel_path_search, char sep);
static int read_files(char *file_list, opal_list_t *file_values, char sep);
static int var_set_initial(mca_base_var_t *var, mca_base_var_t *original);
static int var_get(int vari, mca_base_var_t **var_out, bool original);
static int var_value_string(mca_base_var_t *var, char **value_string);
static void mca_base_var_finalize(void);
/*
* classes
*/
static void var_constructor(mca_base_var_t *p);
static void var_destructor(mca_base_var_t *p);
OBJ_CLASS_INSTANCE(mca_base_var_t, opal_object_t, var_constructor, var_destructor);
static void fv_constructor(mca_base_var_file_value_t *p);
static void fv_destructor(mca_base_var_file_value_t *p);
OBJ_CLASS_INSTANCE(mca_base_var_file_value_t, opal_list_item_t, fv_constructor, fv_destructor);
static const char *mca_base_var_source_file(const mca_base_var_t *var)
{
mca_base_var_file_value_t *fv = (mca_base_var_file_value_t *) var->mbv_file_value;
if (NULL != var->mbv_source_file) {
return var->mbv_source_file;
}
if (fv) {
return fv->mbvfv_file;
}
return NULL;
}
/*
* Generate a full name from three names
*/
int mca_base_var_generate_full_name4(const char *project, const char *framework,
const char *component, const char *variable, char **full_name)
{
const char *const names[] = {project, framework, component, variable};
char *name, *tmp;
size_t i, len;
*full_name = NULL;
for (i = 0, len = 0; i < 4; ++i) {
if (NULL != names[i]) {
/* Add space for the string + _ or \0 */
len += strlen(names[i]) + 1;
}
}
name = calloc(1, len);
if (NULL == name) {
return OPAL_ERR_OUT_OF_RESOURCE;
}
for (i = 0, tmp = name; i < 4; ++i) {
if (NULL != names[i]) {
if (name != tmp) {
*tmp++ = '_';
}
strncat(name, names[i], len - (size_t)(uintptr_t)(tmp - name));
tmp += strlen(names[i]);
}
}
*full_name = name;
return OPAL_SUCCESS;
}
static int compare_strings(const char *str1, const char *str2)
{
if ((NULL != str1 && 0 == strcmp(str1, "*")) || (NULL == str1 && NULL == str2)) {
return 0;
}
if (NULL != str1 && NULL != str2) {
return strcmp(str1, str2);
}
return 1;
}
/*
* Append a filename to the file list if it does not exist and return a
* pointer to the filename in the list.
*/
static char *append_filename_to_list(const char *filename)
{
int i, count;
(void) opal_argv_append_unique_nosize(&mca_base_var_file_list, filename, false);
count = opal_argv_count(mca_base_var_file_list);
for (i = count - 1; i >= 0; --i) {
if (0 == strcmp(mca_base_var_file_list[i], filename)) {
return mca_base_var_file_list[i];
}
}
/* *#@*? */
return NULL;
}
/*
* Set it up
*/
int mca_base_var_init(void)
{
int ret;
if (!mca_base_var_initialized) {
/* Init the value array for the param storage */
OBJ_CONSTRUCT(&mca_base_vars, opal_pointer_array_t);
/* These values are arbitrary */
ret = opal_pointer_array_init(&mca_base_vars, 128, 16384, 128);
if (OPAL_SUCCESS != ret) {
return ret;
}
mca_base_var_count = 0;
/* Init the file param value list */
OBJ_CONSTRUCT(&mca_base_var_file_values, opal_list_t);
OBJ_CONSTRUCT(&mca_base_envar_file_values, opal_list_t);
OBJ_CONSTRUCT(&mca_base_var_override_values, opal_list_t);
OBJ_CONSTRUCT(&mca_base_var_index_hash, opal_hash_table_t);
ret = opal_hash_table_init(&mca_base_var_index_hash, 1024);
if (OPAL_SUCCESS != ret) {
return ret;
}
ret = mca_base_var_group_init();
if (OPAL_SUCCESS != ret) {
return ret;
}
ret = mca_base_pvar_init();
if (OPAL_SUCCESS != ret) {
return ret;
}
/* We may need this later */
home = (char *) opal_home_directory();
if (NULL == home) {
opal_output(0, "Error: Unable to get the user home directory\n");
return OPAL_ERROR;
}
if (NULL == (cwd = getcwd(NULL, 0))) {
opal_output(0, "Error: Unable to get the current working directory\n");
cwd = strdup(".");
}
/* Set this before we register the parameter, below */
mca_base_var_initialized = true;
}
opal_finalize_register_cleanup(mca_base_var_finalize);
return OPAL_SUCCESS;
}
static void process_env_list(char *env_list, char ***argv, char sep)
{
char **tokens;
char *ptr, *value;
tokens = opal_argv_split(env_list, (int) sep);
if (NULL == tokens) {
return;
}
for (int i = 0; NULL != tokens[i]; ++i) {
if (NULL == (ptr = strchr(tokens[i], '='))) {
value = getenv(tokens[i]);
if (NULL == value) {
opal_show_help("help-mca-var.txt", "incorrect-env-list-param", true, tokens[i],
env_list);
break;
}
/* duplicate the value to silence tainted string coverity issue */
value = strdup(value);
if (NULL == value) {
/* out of memory */
break;
}
if (NULL != (ptr = strchr(value, '='))) {
*ptr = '\0';
opal_setenv(value, ptr + 1, true, argv);
} else {
opal_setenv(tokens[i], value, true, argv);
}
free(value);
} else {
*ptr = '\0';
opal_setenv(tokens[i], ptr + 1, true, argv);
/* NTH: don't bother resetting ptr to = since the string will not be used again */
}
}
opal_argv_free(tokens);
}
int mca_base_var_process_env_list(char *list, char ***argv)
{
char sep;
sep = ';';
if (NULL != mca_base_env_list_sep) {
if (1 == strlen(mca_base_env_list_sep)) {
sep = mca_base_env_list_sep[0];
} else {
opal_show_help("help-mca-var.txt", "incorrect-env-list-sep", true,
mca_base_env_list_sep);
return OPAL_SUCCESS;
}
}
if (NULL != list) {
process_env_list(list, argv, sep);
} else if (NULL != mca_base_env_list) {
process_env_list(mca_base_env_list, argv, sep);
}
return OPAL_SUCCESS;
}
int mca_base_var_process_env_list_from_file(char ***argv)
{
if (NULL != mca_base_env_list_internal) {
process_env_list(mca_base_env_list_internal, argv, ';');
}
return OPAL_SUCCESS;
}
static void resolve_relative_paths(char **file_prefix, char *file_path, bool rel_path_search,
char **files, char sep)
{
char *tmp_str;
/*
* Resolve all relative paths.
* the file list returned will contain only absolute paths
*/
if (OPAL_SUCCESS != fixup_files(file_prefix, file_path, rel_path_search, sep)) {
#if 0
/* JJH We need to die! */
abort();
#else
;
#endif
} else {
/* Prepend the files to the search list */
opal_asprintf(&tmp_str, "%s%c%s", *file_prefix, sep, *files);
free(*files);
*files = tmp_str;
}
}
int mca_base_var_load_extra_files(char *files, bool rel_path_search)
{
char *tmp1 = strdup(files);
resolve_relative_paths(&tmp1, mca_base_param_file_path, rel_path_search, &mca_base_var_files,
OPAL_ENV_SEP);
read_files(tmp1, &mca_base_var_file_values, ',');
free(tmp1);
return OPAL_SUCCESS;
}
int mca_base_var_cache_files(bool rel_path_search)
{
char *tmp = NULL;
int ret;
#if OPAL_WANT_HOME_CONFIG_FILES
if (NULL == getenv("OPAL_USER_PARAMS_GIVEN")) {
opal_asprintf(&tmp, "%s" OPAL_PATH_SEP ".openmpi" OPAL_PATH_SEP "mca-params.conf", home);
}
#endif
if (NULL == getenv("OPAL_SYS_PARAMS_GIVEN")) {
if (NULL != tmp) {
opal_asprintf(&mca_base_var_files, "%s,%s" OPAL_PATH_SEP "openmpi-mca-params.conf", tmp,
opal_install_dirs.sysconfdir);
free(tmp);
} else {
opal_asprintf(&mca_base_var_files, "%s" OPAL_PATH_SEP "openmpi-mca-params.conf",
opal_install_dirs.sysconfdir);
}
} else {
mca_base_var_files = strdup("none");
}
/* Initialize a parameter that says where MCA param files can be found.
We may change this value so set the scope to MCA_BASE_VAR_SCOPE_READONLY */
tmp = mca_base_var_files;
ret = mca_base_var_register("opal", "mca", "base", "param_files",
"Path for MCA "
"configuration files containing variable values",
MCA_BASE_VAR_TYPE_STRING, NULL, 0, 0, OPAL_INFO_LVL_2,
MCA_BASE_VAR_SCOPE_READONLY, &mca_base_var_files);
free(tmp);
if (0 > ret) {
return ret;
}
mca_base_envar_files = strdup(mca_base_var_files);
(void) mca_base_var_register_synonym(ret, "opal", "mca", NULL, "param_files",
MCA_BASE_VAR_SYN_FLAG_DEPRECATED);
ret = opal_asprintf(&mca_base_var_override_file,
"%s" OPAL_PATH_SEP "openmpi-mca-params-override.conf",
opal_install_dirs.sysconfdir);
if (0 > ret) {
return OPAL_ERR_OUT_OF_RESOURCE;
}
tmp = mca_base_var_override_file;
ret = mca_base_var_register("opal", "mca", "base", "override_param_file",
"Variables set in this file will override any value set in"
"the environment or another configuration file",
MCA_BASE_VAR_TYPE_STRING, NULL, 0, MCA_BASE_VAR_FLAG_DEFAULT_ONLY,
OPAL_INFO_LVL_2, MCA_BASE_VAR_SCOPE_CONSTANT,
&mca_base_var_override_file);
free(tmp);
if (0 > ret) {
return ret;
}
/* Disable reading MCA parameter files. */
if (0 == strcmp(mca_base_var_files, "none")) {
return OPAL_SUCCESS;
}
mca_base_var_suppress_override_warning = false;
ret = mca_base_var_register(
"opal", "mca", "base", "suppress_override_warning",
"Suppress warnings when attempting to set an overridden value (default: false)",
MCA_BASE_VAR_TYPE_BOOL, NULL, 0, 0, OPAL_INFO_LVL_2, MCA_BASE_VAR_SCOPE_LOCAL,
&mca_base_var_suppress_override_warning);
if (0 > ret) {
return ret;
}
/* Aggregate MCA parameter files
* A prefix search path to look up aggregate MCA parameter file
* requests that do not specify an absolute path
*/
mca_base_var_file_prefix = NULL;
ret = mca_base_var_register("opal", "mca", "base", "param_file_prefix",
"Aggregate MCA parameter file sets", MCA_BASE_VAR_TYPE_STRING, NULL,
0, 0, OPAL_INFO_LVL_3, MCA_BASE_VAR_SCOPE_READONLY,
&mca_base_var_file_prefix);
if (0 > ret) {
return ret;
}
mca_base_envar_file_prefix = NULL;
ret = mca_base_var_register("opal", "mca", "base", "envar_file_prefix",
"Aggregate MCA parameter file set for env variables",
MCA_BASE_VAR_TYPE_STRING, NULL, 0, 0, OPAL_INFO_LVL_3,
MCA_BASE_VAR_SCOPE_READONLY, &mca_base_envar_file_prefix);
if (0 > ret) {
return ret;
}
ret = opal_asprintf(&mca_base_param_file_path, "%s" OPAL_PATH_SEP "amca-param-sets%c%s",
opal_install_dirs.opaldatadir, OPAL_ENV_SEP, cwd);
if (0 > ret) {
return OPAL_ERR_OUT_OF_RESOURCE;
}
tmp = mca_base_param_file_path;
ret = mca_base_var_register("opal", "mca", "base", "param_file_path",
"Aggregate MCA parameter Search path", MCA_BASE_VAR_TYPE_STRING,
NULL, 0, 0, OPAL_INFO_LVL_3, MCA_BASE_VAR_SCOPE_READONLY,
&mca_base_param_file_path);
free(tmp);
if (0 > ret) {
return ret;
}
force_agg_path = NULL;
ret = mca_base_var_register("opal", "mca", "base", "param_file_path_force",
"Forced Aggregate MCA parameter Search path",
MCA_BASE_VAR_TYPE_STRING, NULL, 0, 0, OPAL_INFO_LVL_3,
MCA_BASE_VAR_SCOPE_READONLY, &force_agg_path);
if (0 > ret) {
return ret;
}
if (NULL != force_agg_path) {
if (NULL != mca_base_param_file_path) {
char *tmp_str = mca_base_param_file_path;
opal_asprintf(&mca_base_param_file_path, "%s%c%s", force_agg_path, OPAL_ENV_SEP,
tmp_str);
free(tmp_str);
} else {
mca_base_param_file_path = strdup(force_agg_path);
}
}
if (NULL != mca_base_var_file_prefix) {
resolve_relative_paths(&mca_base_var_file_prefix, mca_base_param_file_path, rel_path_search,
&mca_base_var_files, OPAL_ENV_SEP);
}
read_files(mca_base_var_files, &mca_base_var_file_values, ',');
if (NULL != mca_base_envar_file_prefix) {
resolve_relative_paths(&mca_base_envar_file_prefix, mca_base_param_file_path,
rel_path_search, &mca_base_envar_files, ',');
}
read_files(mca_base_envar_files, &mca_base_envar_file_values, ',');
if (0 == access(mca_base_var_override_file, F_OK)) {
read_files(mca_base_var_override_file, &mca_base_var_override_values, OPAL_ENV_SEP);
}
return OPAL_SUCCESS;
}
/*
* Look up an integer MCA parameter.
*/
int mca_base_var_get_value(int vari, const void *value, mca_base_var_source_t *source,
const char **source_file)
{
mca_base_var_t *var;
void **tmp = (void **) value;
int ret;
ret = var_get(vari, &var, true);
if (OPAL_SUCCESS != ret) {
return ret;
}
if (!VAR_IS_VALID(var[0])) {
return OPAL_ERR_NOT_FOUND;
}
if (NULL != value) {
/* Return a pointer to our backing store (either a char **, int *,
or bool *) */
*tmp = var->mbv_storage;
}
if (NULL != source) {
*source = var->mbv_source;
}
if (NULL != source_file) {
*source_file = mca_base_var_source_file(var);
}
return OPAL_SUCCESS;
}
static int var_set_string(mca_base_var_t *var, char *value)
{
char *tmp;
int ret;
if (NULL != var->mbv_storage->stringval) {
free(var->mbv_storage->stringval);
}
var->mbv_storage->stringval = NULL;
if (NULL == value || 0 == strlen(value)) {
return OPAL_SUCCESS;
}
/* Replace all instances of ~/ in a path-style string with the
user's home directory. This may be handled by the enumerator
in the future. */
if (0 == strncmp(value, "~/", 2)) {
if (NULL != home) {
ret = opal_asprintf(&value, "%s/%s", home, value + 2);
if (0 > ret) {
return OPAL_ERROR;
}
} else {
value = strdup(value + 2);
}
} else {
value = strdup(value);
}
if (NULL == value) {
return OPAL_ERR_OUT_OF_RESOURCE;
}
while (NULL != (tmp = strstr(value, ":~/"))) {
tmp[0] = '\0';
tmp += 3;
ret = opal_asprintf(&tmp, "%s:%s%s%s", value, home ? home : "", home ? "/" : "", tmp);
free(value);
if (0 > ret) {
return OPAL_ERR_OUT_OF_RESOURCE;
}
value = tmp;
}
var->mbv_storage->stringval = value;
return OPAL_SUCCESS;
}
static int int_from_string(const char *src, mca_base_var_enum_t *enumerator, uint64_t *value_out)
{
uint64_t value;
bool is_int;
char *tmp;
if (NULL == src || 0 == strlen(src)) {
if (NULL == enumerator) {
*value_out = 0;
}
return OPAL_SUCCESS;
}
if (enumerator) {
int int_val, ret;
ret = enumerator->value_from_string(enumerator, src, &int_val);
if (OPAL_SUCCESS != ret) {
return ret;
}
*value_out = (uint64_t) int_val;
return OPAL_SUCCESS;
}
/* Check for an integer value */
value = strtoull(src, &tmp, 0);
is_int = tmp[0] == '\0';
if (!is_int && tmp != src) {
switch (tmp[0]) {
case 'G':
case 'g':
value <<= 10;
case 'M':
case 'm':
value <<= 10;
case 'K':
case 'k':
value <<= 10;
break;
default:
break;
}
}
*value_out = value;
return OPAL_SUCCESS;
}
static int var_set_from_string(mca_base_var_t *var, char *src)
{
mca_base_var_storage_t *dst = var->mbv_storage;
uint64_t int_value = 0;
int ret;
switch (var->mbv_type) {
case MCA_BASE_VAR_TYPE_INT:
case MCA_BASE_VAR_TYPE_INT32_T:
case MCA_BASE_VAR_TYPE_UINT32_T:
case MCA_BASE_VAR_TYPE_LONG:
case MCA_BASE_VAR_TYPE_UNSIGNED_INT:
case MCA_BASE_VAR_TYPE_UNSIGNED_LONG:
case MCA_BASE_VAR_TYPE_INT64_T:
case MCA_BASE_VAR_TYPE_UINT64_T:
case MCA_BASE_VAR_TYPE_UNSIGNED_LONG_LONG:
case MCA_BASE_VAR_TYPE_BOOL:
case MCA_BASE_VAR_TYPE_SIZE_T:
ret = int_from_string(src, var->mbv_enumerator, &int_value);
if (OPAL_SUCCESS != ret
|| (MCA_BASE_VAR_TYPE_INT == var->mbv_type && ((int) int_value != (int64_t) int_value))
|| (MCA_BASE_VAR_TYPE_UNSIGNED_INT == var->mbv_type
&& ((unsigned int) int_value != int_value))) {
if (var->mbv_enumerator) {
char *valid_values;
(void) var->mbv_enumerator->dump(var->mbv_enumerator, &valid_values,
MCA_BASE_VAR_ENUM_DUMP_READABLE);
opal_show_help("help-mca-var.txt", "invalid-value-enum", true, var->mbv_full_name,
src, valid_values);
free(valid_values);
} else {
opal_show_help("help-mca-var.txt", "invalid-value", true, var->mbv_full_name, src);
}
return OPAL_ERR_VALUE_OUT_OF_BOUNDS;
}
if (MCA_BASE_VAR_TYPE_INT == var->mbv_type
|| MCA_BASE_VAR_TYPE_UNSIGNED_INT == var->mbv_type) {
int *castme = (int *) var->mbv_storage;
*castme = int_value;
} else if (MCA_BASE_VAR_TYPE_INT32_T == var->mbv_type
|| MCA_BASE_VAR_TYPE_UINT32_T == var->mbv_type) {
int32_t *castme = (int32_t *) var->mbv_storage;
*castme = int_value;
} else if (MCA_BASE_VAR_TYPE_INT64_T == var->mbv_type
|| MCA_BASE_VAR_TYPE_UINT64_T == var->mbv_type) {
int64_t *castme = (int64_t *) var->mbv_storage;
*castme = int_value;
} else if (MCA_BASE_VAR_TYPE_LONG == var->mbv_type) {
long *castme = (long *) var->mbv_storage;
*castme = (long) int_value;
} else if (MCA_BASE_VAR_TYPE_UNSIGNED_LONG == var->mbv_type) {
unsigned long *castme = (unsigned long *) var->mbv_storage;
*castme = (unsigned long) int_value;
} else if (MCA_BASE_VAR_TYPE_UNSIGNED_LONG_LONG == var->mbv_type) {
unsigned long long *castme = (unsigned long long *) var->mbv_storage;
*castme = (unsigned long long) int_value;
} else if (MCA_BASE_VAR_TYPE_SIZE_T == var->mbv_type) {
size_t *castme = (size_t *) var->mbv_storage;
*castme = (size_t) int_value;
} else if (MCA_BASE_VAR_TYPE_BOOL == var->mbv_type) {
bool *castme = (bool *) var->mbv_storage;
*castme = !!int_value;
}
return ret;
case MCA_BASE_VAR_TYPE_DOUBLE:
dst->lfval = strtod(src, NULL);
break;
case MCA_BASE_VAR_TYPE_STRING:
case MCA_BASE_VAR_TYPE_VERSION_STRING:
var_set_string(var, src);
break;
case MCA_BASE_VAR_TYPE_MAX:
return OPAL_ERROR;
}
return OPAL_SUCCESS;
}
/*
* Set a variable
*/
int mca_base_var_set_value(int vari, const void *value, size_t size, mca_base_var_source_t source,
const char *source_file)
{
mca_base_var_t *var;
int ret;
ret = var_get(vari, &var, true);
if (OPAL_SUCCESS != ret) {
return ret;
}
if (!VAR_IS_VALID(var[0])) {
return OPAL_ERR_BAD_PARAM;
}
if (!VAR_IS_SETTABLE(var[0])) {
return OPAL_ERR_PERM;
}
if (NULL != var->mbv_enumerator) {
/* Validate */
ret = var->mbv_enumerator->string_from_value(var->mbv_enumerator, ((int *) value)[0], NULL);
if (OPAL_SUCCESS != ret) {
return ret;
}
}
if (MCA_BASE_VAR_TYPE_STRING != var->mbv_type
&& MCA_BASE_VAR_TYPE_VERSION_STRING != var->mbv_type) {
memmove(var->mbv_storage, value, ompi_var_type_sizes[var->mbv_type]);
} else {
var_set_string(var, (char *) value);
}
var->mbv_source = source;
if (MCA_BASE_VAR_SOURCE_FILE == source && NULL != source_file) {
var->mbv_file_value = NULL;
var->mbv_source_file = append_filename_to_list(source_file);
}
return OPAL_SUCCESS;
}
/*
* Deregister a parameter
*/
int mca_base_var_deregister(int vari)
{
mca_base_var_t *var;
int ret;
ret = var_get(vari, &var, false);
if (OPAL_SUCCESS != ret) {
return ret;
}
if (!VAR_IS_VALID(var[0])) {
return OPAL_ERR_BAD_PARAM;
}
/* Mark this parameter as invalid but keep its info in case this
parameter is reregistered later */
var->mbv_flags &= ~MCA_BASE_VAR_FLAG_VALID;
/* Done deregistering synonym */
if (MCA_BASE_VAR_FLAG_SYNONYM & var->mbv_flags) {
return OPAL_SUCCESS;
}
/* Release the current value if it is a string. */
if ((MCA_BASE_VAR_TYPE_STRING == var->mbv_type
|| MCA_BASE_VAR_TYPE_VERSION_STRING == var->mbv_type)
&& var->mbv_storage->stringval) {
free(var->mbv_storage->stringval);
var->mbv_storage->stringval = NULL;
} else {
OPAL_MCA_VAR_MBV_ENUMERATOR_FREE(var->mbv_enumerator);
}
var->mbv_enumerator = NULL;
var->mbv_storage = NULL;
return OPAL_SUCCESS;
}
static int var_get(int vari, mca_base_var_t **var_out, bool original)
{
mca_base_var_t *var;
if (var_out) {
*var_out = NULL;
}
/* Check for bozo cases */
if (!mca_base_var_initialized) {
return OPAL_ERROR;
}
if (vari < 0) {
return OPAL_ERR_BAD_PARAM;
}
var = opal_pointer_array_get_item(&mca_base_vars, vari);
if (NULL == var) {
return OPAL_ERR_BAD_PARAM;
}
if (VAR_IS_SYNONYM(var[0]) && original) {
return var_get(var->mbv_synonym_for, var_out, false);
}
if (var_out) {
*var_out = var;
}
return OPAL_SUCCESS;
}
int mca_base_var_env_name(const char *param_name, char **env_name)
{
int ret;
assert(NULL != env_name);
ret = opal_asprintf(env_name, "%s%s", mca_prefix, param_name);
if (0 > ret) {
return OPAL_ERR_OUT_OF_RESOURCE;
}
return OPAL_SUCCESS;
}
/*
* Find the index for an MCA parameter based on its names.
*/
static int var_find_by_name(const char *full_name, int *vari, bool invalidok)
{
mca_base_var_t *var = NULL;
void *tmp;
int rc;
rc = opal_hash_table_get_value_ptr(&mca_base_var_index_hash, full_name, strlen(full_name),
&tmp);
if (OPAL_SUCCESS != rc) {
return rc;
}
(void) var_get((int) (uintptr_t) tmp, &var, false);
if (invalidok || (var && VAR_IS_VALID(var[0]))) {
*vari = (int) (uintptr_t) tmp;
return OPAL_SUCCESS;
}
return OPAL_ERR_NOT_FOUND;
}
static int var_find(const char *project_name, const char *framework_name,
const char *component_name, const char *variable_name, bool invalidok)
{
char *full_name;
int ret, vari;
ret = mca_base_var_generate_full_name4(NULL, framework_name, component_name, variable_name,
&full_name);
if (OPAL_SUCCESS != ret) {
return OPAL_ERROR;
}
ret = var_find_by_name(full_name, &vari, invalidok);
/* NTH: should we verify the name components match? */
free(full_name);
if (OPAL_SUCCESS != ret) {
return ret;
}
return vari;
}
/*
* Find the index for an MCA parameter based on its name components.
*/
int mca_base_var_find(const char *project_name, const char *framework_name,
const char *component_name, const char *variable_name)
{
return var_find(project_name, framework_name, component_name, variable_name, false);
}
/*
* Find the index for an MCA parameter based on full name.
*/
int mca_base_var_find_by_name(const char *full_name, int *vari)
{
return var_find_by_name(full_name, vari, false);
}
int mca_base_var_set_flag(int vari, mca_base_var_flag_t flag, bool set)
{
mca_base_var_t *var;
int ret;
ret = var_get(vari, &var, true);
if (OPAL_SUCCESS != ret || VAR_IS_SYNONYM(var[0])) {
return OPAL_ERR_BAD_PARAM;
}
var->mbv_flags = (var->mbv_flags & ~flag) | (set ? flag : 0);
/* All done */
return OPAL_SUCCESS;
}
/*
* Return info on a parameter at an index
*/
int mca_base_var_get(int vari, const mca_base_var_t **var)
{