Skip to content

Commit 805e130

Browse files
committed
Fix precedence ordering on envar operations
Work from left-to-right across the cmd line, applying env-related options as we go. When one operation affects the result of another, this preserves a user's common expectation. Add a "--set-env" option if the corresponding PMIx CLI is defined. Seemed a little weird that we had "prepend-env", "append-env", etc., but no "set-env". It's the equivalent of "-x foo=val". Signed-off-by: Ralph Castain <[email protected]>
1 parent bdbf4db commit 805e130

File tree

10 files changed

+144
-14
lines changed

10 files changed

+144
-14
lines changed

src/docs/prrte-rst-content/Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ dist_rst_DATA = \
4848
cli-no-app-prefix.rst \
4949
cli-rank-by.rst \
5050
cli-runtime-options.rst \
51+
cli-set-env.rst \
5152
cli-stream-buffering.rst \
5253
cli-tune.rst \
5354
cli-unset-env.rst \
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
.. -*- rst -*-
2+
3+
Copyright (c) 2022-2025 Nanook Consulting All rights reserved.
4+
Copyright (c) 2023 Jeffrey M. Squyres. All rights reserved.
5+
6+
$COPYRIGHT$
7+
8+
Additional copyrights may follow
9+
10+
$HEADER$
11+
12+
.. The following line is included so that Sphinx won't complain
13+
about this file not being directly included in some toctree
14+
15+
Set the named environmental variable to the specified value. This will overwrite the
16+
existing value, if it exists. Equivalent to the "-x foo=val" option

src/mca/rmaps/base/rmaps_base_map_job.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1366,8 +1366,8 @@ static void inherit_env_directives(prte_job_t *jdata,
13661366
}
13671367

13681368
// if it doesn't exist, then inherit it
1369-
prte_prepend_attribute(&jdata->attributes, attr->key, PRTE_ATTR_GLOBAL,
1370-
envar, PMIX_ENVAR);
1369+
prte_set_attribute(&jdata->attributes, attr->key, PRTE_ATTR_GLOBAL,
1370+
envar, PMIX_ENVAR);
13711371
}
13721372

13731373
/* There is no one-to-one correlation between the apps, but we can
@@ -1419,8 +1419,8 @@ static void inherit_env_directives(prte_job_t *jdata,
14191419
}
14201420

14211421
// if it doesn't exist, then inherit it
1422-
prte_prepend_attribute(&app2->attributes, attr->key, PRTE_ATTR_GLOBAL,
1423-
envar, PMIX_ENVAR);
1422+
prte_set_attribute(&app2->attributes, attr->key, PRTE_ATTR_GLOBAL,
1423+
envar, PMIX_ENVAR);
14241424
}
14251425
}
14261426

src/mca/schizo/ompi/schizo-ompi-cli.rstxt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ Launch options
8484
* ``-x <var>``: Export an environment variable, optionally specifying a value.
8585
:ref:`See below for details <label-schizo-ompi-x>`.
8686

87+
* ``--set-env <var>=<val>``: Set an environment variable.
88+
:ref:`See below for details <label-schizo-ompi-set-env>`.
89+
8790
* ``--unset-env <var>``: Unset an environment variable.
8891
:ref:`See below for details <label-schizo-ompi-unset-env>`.
8992

@@ -454,6 +457,12 @@ The ``-x`` option
454457

455458
.. include:: /prrte-rst-content/cli-x.rst
456459

460+
.. _label-schizo-ompi-set-env:
461+
462+
The ``--set-env`` option
463+
~~~~~~~~~~~~~~~~~~~~~~~~~~
464+
.. include:: /prrte-rst-content/cli-set-env.rst
465+
457466
.. _label-schizo-ompi-unset-env:
458467

459468
The ``--unset-env`` option

src/mca/schizo/ompi/schizo_ompi.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,9 @@ static struct option ompioptions[] = {
159159
PMIX_OPTION_DEFINE(PRTE_CLI_PRELOAD_FILES, PMIX_ARG_REQD),
160160
PMIX_OPTION_SHORT_DEFINE(PRTE_CLI_PRELOAD_BIN, PMIX_ARG_NONE, 's'),
161161
PMIX_OPTION_SHORT_DEFINE(PRTE_CLI_FWD_ENVAR, PMIX_ARG_REQD, 'x'),
162+
#ifdef PMIX_CLI_SET_ENVAR
163+
PMIX_OPTION_DEFINE(PMIX_CLI_SET_ENVAR, PMIX_ARG_REQD),
164+
#endif
162165
#ifdef PMIX_CLI_PREPEND_ENVAR
163166
PMIX_OPTION_DEFINE(PMIX_CLI_PREPEND_ENVAR, PMIX_ARG_REQD),
164167
#endif

src/mca/schizo/prte/help-prterun.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,11 @@ option to the help request as "--help <option>".
222222
| | current environmental variables starting with |
223223
| | "foo") |
224224
+----------------------+-----------------------------------------------+
225+
| "--set-env | Set the named environmental variable to the |
226+
| <name>=<value>" | specified value. This will overwrite the |
227+
| | existing value, if it exists. Equivalent to |
228+
| | the "-x foo=val" option |
229+
+----------------------+-----------------------------------------------+
225230
| "--unset-env <name>" | Unset the named environmental variable. Note |
226231
| | "--unset-env foo*" unsets all current |
227232
| | environmental variables starting with "foo" |
@@ -555,6 +560,11 @@ variable name "foo" and sets its value to "bar" in the started processes.
555560
Note that "-x foo*" exports all current environmental variables starting with
556561
"foo"
557562
#
563+
[set-env]
564+
565+
Set the named environmental variable to the specified value. This will overwrite the
566+
existing value, if it exists. Equivalent to the "-x foo=val" option
567+
#
558568
[unset-env]
559569

560570
Unset the named environmental variable. Note "--unset-env foo*" unsets all

src/mca/schizo/prte/help-prun.txt

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,25 @@ option to the help request as "--help <option>".
215215
| | current environmental variables starting with |
216216
| | "foo") |
217217
+----------------------+-----------------------------------------------+
218+
| "--set-env | Set the named environmental variable to the |
219+
| <name>=<value>" | specified value. This will overwrite the |
220+
| | existing value, if it exists. Equivalent to |
221+
| | the "-x foo=val" option |
222+
+----------------------+-----------------------------------------------+
223+
| "--unset-env <name>" | Unset the named environmental variable. Note |
224+
| | "--unset-env foo*" unsets all current |
225+
| | environmental variables starting with "foo" |
226+
+----------------------+-----------------------------------------------+
227+
| "--append-env | Append the named environment variable with |
228+
| <name[c]> <value>" | given value. The "[c]" must be appended to |
229+
| | the name to specify the separator to be used |
230+
| | when appending the value. |
231+
+----------------------+-----------------------------------------------+
232+
| "--prepend-env | Prepend the named environment variable with |
233+
| <name[c]> <value>" | given value. The "[c]" must be appended to |
234+
| | the name to specify the separator to be used |
235+
| | when prepending the value. |
236+
+----------------------+-----------------------------------------------+
218237
| "--gpu-support <val>"| Direct application to either enable (true) or |
219238
| | disable (false) its internal library's GPU |
220239
| | support |
@@ -311,6 +330,45 @@ about the PRRTE session directory.
311330

312331
Delay specified number of seconds before trying to connect
313332
#
333+
[x]
334+
335+
Export an environment variable, optionally specifying a value. For example,
336+
"-x foo" exports the environment variable "foo" and takes its value
337+
from the current environment, while "-x foo=bar" exports the environment
338+
variable name "foo" and sets its value to "bar" in the started processes.
339+
Note that "-x foo*" exports all current environmental variables starting with
340+
"foo"
341+
#
342+
[set-env]
343+
344+
Set the named environmental variable to the specified value. This will overwrite the
345+
existing value, if it exists. Equivalent to the "-x foo=val" option
346+
#
347+
[unset-env]
348+
349+
Unset the named environmental variable. Note "--unset-env foo*" unsets all
350+
current environmental variables starting with "foo"
351+
#
352+
[append-env]
353+
354+
Append the named environment variable with the given value. The "[c]" must
355+
be appended to the name to specify the separator to be used when appending
356+
the value.
357+
358+
Example: "--append-envar LD_LIBRARY_PATH[:] foo/lib" will result in:
359+
360+
LD_LIBRARY_PATH=$LD_LIBRARY_PATH:foo/lib
361+
#
362+
[prepend-env]
363+
364+
Prepend the named environment variable with the given value. The "[c]" must
365+
be appended to the name to specify the separator to be used when appending
366+
the value.
367+
368+
Example: "--prepend-envar LD_LIBRARY_PATH[:] foo/lib" will result in:
369+
370+
LD_LIBRARY_PATH=foo/lib:$LD_LIBRARY_PATH
371+
#
314372
[hostfile]
315373

316374
PRRTE supports several levels of user-specified hostfiles based on an

src/mca/schizo/prte/schizo_prte.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,9 @@ static struct option prterunoptions[] = {
187187
PMIX_OPTION_DEFINE(PRTE_CLI_STOP_IN_INIT, PMIX_ARG_NONE),
188188
PMIX_OPTION_DEFINE(PRTE_CLI_STOP_IN_APP, PMIX_ARG_NONE),
189189
PMIX_OPTION_SHORT_DEFINE(PRTE_CLI_FWD_ENVAR, PMIX_ARG_REQD, 'x'),
190+
#ifdef PMIX_CLI_SET_ENVAR
191+
PMIX_OPTION_DEFINE(PMIX_CLI_SET_ENVAR, PMIX_ARG_REQD),
192+
#endif
190193
#ifdef PMIX_CLI_PREPEND_ENVAR
191194
PMIX_OPTION_DEFINE(PMIX_CLI_PREPEND_ENVAR, PMIX_ARG_REQD),
192195
#endif
@@ -315,6 +318,9 @@ static struct option prunoptions[] = {
315318
PMIX_OPTION_DEFINE(PRTE_CLI_STOP_IN_INIT, PMIX_ARG_NONE),
316319
PMIX_OPTION_DEFINE(PRTE_CLI_STOP_IN_APP, PMIX_ARG_NONE),
317320
PMIX_OPTION_SHORT_DEFINE(PRTE_CLI_FWD_ENVAR, PMIX_ARG_REQD, 'x'),
321+
#ifdef PMIX_CLI_SET_ENVAR
322+
PMIX_OPTION_DEFINE(PMIX_CLI_SET_ENVAR, PMIX_ARG_REQD),
323+
#endif
318324
#ifdef PMIX_CLI_PREPEND_ENVAR
319325
PMIX_OPTION_DEFINE(PMIX_CLI_PREPEND_ENVAR, PMIX_ARG_REQD),
320326
#endif

src/prted/pmix/pmix_server_dyn.c

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -598,29 +598,30 @@ int prte_pmix_xfer_job_info(prte_job_t *jdata,
598598
envar.envar = info->value.data.envar.envar;
599599
envar.value = info->value.data.envar.value;
600600
envar.separator = info->value.data.envar.separator;
601-
prte_prepend_attribute(&jdata->attributes, PRTE_JOB_SET_ENVAR,
602-
PRTE_ATTR_GLOBAL, &envar, PMIX_ENVAR);
601+
prte_set_attribute(&jdata->attributes, PRTE_JOB_SET_ENVAR,
602+
PRTE_ATTR_GLOBAL, &envar, PMIX_ENVAR);
603603
} else if (PMIX_CHECK_KEY(info, PMIX_ADD_ENVAR)) {
604604
envar.envar = info->value.data.envar.envar;
605605
envar.value = info->value.data.envar.value;
606606
envar.separator = info->value.data.envar.separator;
607-
prte_prepend_attribute(&jdata->attributes, PRTE_JOB_ADD_ENVAR,
608-
PRTE_ATTR_GLOBAL, &envar, PMIX_ENVAR);
607+
prte_set_attribute(&jdata->attributes, PRTE_JOB_ADD_ENVAR,
608+
PRTE_ATTR_GLOBAL, &envar, PMIX_ENVAR);
609609
} else if (PMIX_CHECK_KEY(info, PMIX_UNSET_ENVAR)) {
610-
prte_prepend_attribute(&jdata->attributes, PRTE_JOB_UNSET_ENVAR,
611-
PRTE_ATTR_GLOBAL, info->value.data.string, PMIX_STRING);
610+
prte_set_attribute(&jdata->attributes, PRTE_JOB_UNSET_ENVAR,
611+
PRTE_ATTR_GLOBAL, info->value.data.string, PMIX_STRING);
612612
} else if (PMIX_CHECK_KEY(info, PMIX_PREPEND_ENVAR)) {
613613
envar.envar = info->value.data.envar.envar;
614614
envar.value = info->value.data.envar.value;
615615
envar.separator = info->value.data.envar.separator;
616-
prte_prepend_attribute(&jdata->attributes, PRTE_JOB_PREPEND_ENVAR,
617-
PRTE_ATTR_GLOBAL, &envar, PMIX_ENVAR);
616+
prte_set_attribute(&jdata->attributes, PRTE_JOB_PREPEND_ENVAR,
617+
PRTE_ATTR_GLOBAL, &envar, PMIX_ENVAR);
618618
} else if (PMIX_CHECK_KEY(info, PMIX_APPEND_ENVAR)) {
619619
envar.envar = info->value.data.envar.envar;
620620
envar.value = info->value.data.envar.value;
621621
envar.separator = info->value.data.envar.separator;
622-
prte_prepend_attribute(&jdata->attributes, PRTE_JOB_APPEND_ENVAR,
623-
PRTE_ATTR_GLOBAL, &envar, PMIX_ENVAR);
622+
prte_set_attribute(&jdata->attributes, PRTE_JOB_APPEND_ENVAR,
623+
PRTE_ATTR_GLOBAL, &envar, PMIX_ENVAR);
624+
624625
} else if (PMIX_CHECK_KEY(info, PMIX_SPAWN_TOOL)) {
625626
PRTE_FLAG_SET(jdata, PRTE_JOB_FLAG_TOOL);
626627

src/prted/prte_app_parse.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,32 @@ static int create_app(prte_schizo_base_module_t *schizo, char **argv,
439439
}
440440
}
441441
#endif
442+
443+
#ifdef PMIX_CLI_SET_ENVAR
444+
opt = pmix_cmd_line_get_param(&results, PMIX_CLI_SET_ENVAR);
445+
if (NULL != opt) {
446+
for (n=0; NULL != opt->values[n]; n+=2) {
447+
param = strdup(opt->values[n]);
448+
// find the '=' separating name from value
449+
tval = strchr(param, '=');
450+
if (NULL == tval) {
451+
pmix_show_help("help-prun.txt", "malformed-envar", true,
452+
"set", app->app.cmd, param);
453+
rc = PRTE_ERR_SILENT;
454+
free(param);
455+
goto cleanup;
456+
}
457+
*tval = '\0';
458+
++tval;
459+
PMIX_ENVAR_CONSTRUCT(&envt);
460+
envt.envar = param;
461+
envt.value = strdup(tval);
462+
PMIX_INFO_LIST_ADD(rc, app->info, PMIX_SET_ENVAR, &envt, PMIX_ENVAR);
463+
PMIX_ENVAR_DESTRUCT(&envt);
464+
}
465+
}
466+
#endif
467+
442468
#ifdef PMIX_CLI_UNSET_ENVAR
443469
opt = pmix_cmd_line_get_param(&results, PMIX_CLI_UNSET_ENVAR);
444470
if (NULL != opt) {

0 commit comments

Comments
 (0)