Skip to content

Commit c759f12

Browse files
authored
Merge pull request #5261 from grondo/issue#5258
shell: set correct HOSTNAME in job environment if necessary
2 parents 86e05aa + 6f983b3 commit c759f12

File tree

4 files changed

+28
-4
lines changed

4 files changed

+28
-4
lines changed

src/shell/shell.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1668,7 +1668,7 @@ int main (int argc, char *argv[])
16681668
while (taskid != IDSET_INVALID_ID) {
16691669
struct shell_task *task;
16701670

1671-
if (!(task = shell_task_create (shell.info, i, taskid)))
1671+
if (!(task = shell_task_create (&shell, i, taskid)))
16721672
shell_die (1, "shell_task_create index=%d", i);
16731673

16741674
task->pre_exec_cb = shell_task_exec;

src/shell/task.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
* . FLUX_JOB_NNODES
2323
* . FLUX_JOB_ID
2424
* . FLUX_URI (if not running standalone)
25+
* . correct HOSTNAME if set in job environment
2526
*
2627
* Current working directory
2728
* Ignore - shell should already be in it.
@@ -91,10 +92,11 @@ struct shell_task *shell_task_new (void)
9192
return NULL;
9293
}
9394

94-
struct shell_task *shell_task_create (struct shell_info *info,
95+
struct shell_task *shell_task_create (flux_shell_t *shell,
9596
int index,
9697
int taskid)
9798
{
99+
struct shell_info *info = shell->info;
98100
struct shell_task *task;
99101
const char *key;
100102
json_t *entry;
@@ -153,6 +155,17 @@ struct shell_task *shell_task_create (struct shell_info *info,
153155
getenv ("FLUX_KVS_NAMESPACE")) < 0)
154156
goto error;
155157
}
158+
159+
/* If HOSTNAME is set in job environment it is almost certain to be
160+
* incorrect. Overwrite with the correct hostname.
161+
*/
162+
if (flux_cmd_getenv (task->cmd, "HOSTNAME")
163+
&& flux_cmd_setenvf (task->cmd,
164+
1,
165+
"HOSTNAME",
166+
"%s",
167+
shell->hostname) < 0)
168+
goto error;
156169
return task;
157170
error:
158171
shell_task_destroy (task);

src/shell/task.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ struct shell_task {
5454

5555
void shell_task_destroy (struct shell_task *task);
5656

57-
struct shell_task *shell_task_create (struct shell_info *info,
57+
struct shell_task *shell_task_create (flux_shell_t *shell,
5858
int index,
5959
int taskid);
6060

t/t2602-job-shell.t

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,18 @@ test_expect_success 'job-shell: fails if FLUX_EXEC_PROTOCOL_FD incorrect' '
340340
-n2 -N2 hostname 2>protocol_fd_invalid.err &&
341341
grep FLUX_EXEC_PROTOCOL_FD protocol_fd_invalid.err
342342
'
343-
343+
test_expect_success 'job-shell: corrects HOSTNAME environment variable' '
344+
HOSTNAME=incorrect \
345+
flux run -n1 printenv HOSTNAME >hostname.out &&
346+
test_debug "cat hostname.out" &&
347+
test "$(hostname)" = "$(cat hostname.out)"
348+
'
349+
test_expect_success 'job-shell: leaves HOSTNAME unset if not set' '
350+
( unset HOSTNAME &&
351+
test_expect_code 1 flux run -n1 printenv HOSTNAME >hostname.out2
352+
) &&
353+
test_must_be_empty hostname.out2
354+
'
344355
# Note: in below tests, os.exit(True) returns with nonzero exit code,
345356
# so the sense of the tests is reversed so the tasks exit with zero exit
346357
# code for success.

0 commit comments

Comments
 (0)