From 13fdd5292e69437f52d053ca28263512f1e1b92d Mon Sep 17 00:00:00 2001
From: "Mark A. Grondona" <mgrondona@llnl.gov>
Date: Wed, 21 Dec 2011 10:29:01 -0800
Subject: [PATCH] [PATCH] slurmstped: fix dup2() error with --pty

In commit

 commit b33cd7c8dc99a4ded01d23025a5e6ade6c632be8
 Author: Mark A. Grondona <mgrondona@llnl.gov>
 Date:   Fri Oct 7 13:52:43 2011 -0700

a minor bug was introduced because the io_dup_stdio() call was moved
out from under the 'else' case of the empty if

 if (job->pty && (task->gtid == 0))

in task.c:exec_task() This results in the error

 dup2(stdin): Bad file descriptor

For task 0 when --pty is used, because io_dup_stdio() is
inappropriately being called after login_tty().

This patch collects the stdio setup into prepare_tty(), renames
that function prepare_stdio(), and removes the io_dup_stdio()
call from exec_task().  This removes the last io_* call from
slurmstepd/task.c, so the io.h header is removed.

I didn't see a problem with moving the io_dup_stdio() call earlier in
the task setup path, but it could probably use another set of eyes.
----------
Note: This problem was at least partly fixed with
https://github.com/SchedMD/slurm/commit/4d17300c5de7a8f146072bae8baaf773ca1fe98a
---
 src/slurmd/slurmstepd/mgr.c  | 11 ++++++++---
 src/slurmd/slurmstepd/task.c |  4 ----
 2 files changed, 8 insertions(+), 7 deletions(-)

diff --git a/src/slurmd/slurmstepd/mgr.c b/src/slurmd/slurmstepd/mgr.c
index a8c4922f018..87b5fdaf04e 100644
--- a/src/slurmd/slurmstepd/mgr.c
+++ b/src/slurmd/slurmstepd/mgr.c
@@ -1192,7 +1192,7 @@ static int exec_wait_signal (struct exec_wait_info *e, slurmd_job_t *job)
 	return (0);
 }
 
-static void prepare_tty (slurmd_job_t *job, slurmd_task_info_t *task)
+static void prepare_stdio (slurmd_job_t *job, slurmd_task_info_t *task)
 {
 #ifdef HAVE_PTY_H
 	if (job->pty && (task->gtid == 0)) {
@@ -1200,8 +1200,10 @@ static void prepare_tty (slurmd_job_t *job, slurmd_task_info_t *task)
 			error("login_tty: %m");
 		else
 			debug3("login_tty good");
+		return;
 	}
 #endif
+	io_dup_stdio(task);
 	return;
 }
 
@@ -1320,9 +1322,12 @@ _fork_all_tasks(slurmd_job_t *job)
 			xsignal_unblock(slurmstepd_blocked_signals);
 
 			/*
-			 *  Setup tty before any setpgid() calls
+			 *  Need to setup stdio before setpgid() is called
+			 *   in case we are setting up a tty. (login_tty()
+			 *   must be called before setpgid() or it is
+			 *   effectively disabled).
 			 */
-			prepare_tty (job, job->task[i]);
+			prepare_stdio (job, job->task[i]);
 
 			/*
 			 *  Block until parent notifies us that it is ok to
diff --git a/src/slurmd/slurmstepd/task.c b/src/slurmd/slurmstepd/task.c
index 390943bb49c..0137fdc7edb 100644
--- a/src/slurmd/slurmstepd/task.c
+++ b/src/slurmd/slurmstepd/task.c
@@ -83,7 +83,6 @@
 #include "src/common/xmalloc.h"
 
 #include "src/slurmd/slurmd/slurmd.h"
-#include "src/slurmd/slurmstepd/io.h"
 #include "src/slurmd/slurmstepd/pdebug.h"
 #include "src/slurmd/slurmstepd/task.h"
 #include "src/slurmd/slurmstepd/ulimits.h"
@@ -392,9 +391,6 @@ exec_task(slurmd_job_t *job, int i)
 		}
 	}
 
-	if (!job->pty)
-		 io_dup_stdio(task);
-
 	/* task-specific pre-launch activities */
 
 	if (spank_user_task (job, i) < 0) {
-- 
GitLab