@@ -341,25 +341,31 @@ run_child(char *run_ptest, int fd_stdout, int fd_stderr)
/* exit(1); not needed? */
}
+/* Returns an integer.
+ * If it returns < 0, an error has occurred.
+ * Otherwise, return 0 means successfully
+ * fp should be writable, likely stdout/err.
+ */
+static int
+setup_master_pty(int* pty_master, int* pty_slave, char*pty_name, FILE *fp) {
+ if (openpty(pty_master, pty_slave, pty_name, NULL, NULL) < 0) {
+ fprintf(fp, "ERROR: openpty() failed with: %s.\n", strerror(errno));
+ return -1;
+ }
+ return 0;
+}
+
/* Returns an integer file descriptor.
* If it returns < 0, an error has occurred.
* Otherwise, it has returned the slave pty file descriptor.
* fp should be writable, likely stdout/err.
*/
static int
-setup_slave_pty(FILE *fp) {
- int pty_master = -1;
- int pty_slave = -1;
- char pty_name[256];
+setup_slave_pty(char* pty_name, FILE *fp) {
struct group *gptr;
gid_t gid;
int slave = -1;
- if (openpty(&pty_master, &pty_slave, pty_name, NULL, NULL) < 0) {
- fprintf(fp, "ERROR: openpty() failed with: %s.\n", strerror(errno));
- return -1;
- }
-
if ((gptr = getgrnam(pty_name)) != 0) {
gid = gptr->gr_gid;
} else {
@@ -384,9 +390,9 @@ setup_slave_pty(FILE *fp) {
if ((slave = open(pty_name, O_RDWR)) == -1) {
fprintf(fp, "ERROR: open() failed with: %s.\n", strerror(errno));
}
- return (slave);
-}
+ return slave;
+}
int
run_ptests(struct ptest_list *head, const struct ptest_options opts,
@@ -406,16 +412,13 @@ run_ptests(struct ptest_list *head, const struct ptest_options opts,
do
{
- if (isatty(0) && ioctl(0, TIOCNOTTY) == -1) {
- fprintf(fp, "ERROR: Unable to detach from controlling tty, %s\n", strerror(errno));
- }
-
-
fprintf(fp, "START: %s\n", progname);
PTEST_LIST_ITERATE_START(head, p)
int pipefd_stdout[2] = {-1, -1};
int pipefd_stderr[2] = {-1, -1};
- int pgid = -1;
+ int pty_master = -1;
+ int pty_slave = -1;
+ char pty_name[256] = {0};
if (pipe2(pipefd_stdout, 0) == -1) {
rc = -1;
@@ -435,8 +438,9 @@ run_ptests(struct ptest_list *head, const struct ptest_options opts,
}
dirname(ptest_dir);
- if ((pgid = getpgid(0)) == -1) {
- fprintf(fp, "ERROR: getpgid() failed, %s\n", strerror(errno));
+ if (setup_master_pty(&pty_master, &pty_slave, pty_name, fp) < 0) {
+ fprintf(fp, "ERROR: setup_master_pty failed, %s\n", strerror(errno));
+ break;
}
pid_t child = fork();
@@ -447,25 +451,32 @@ run_ptests(struct ptest_list *head, const struct ptest_options opts,
} else if (child == 0) {
int slave;
- close(0);
/* Close read ends of the pipe */
do_close(&pipefd_stdout[PIPE_READ]);
do_close(&pipefd_stderr[PIPE_READ]);
+ do_close(&pty_master);
- if ((slave = setup_slave_pty(fp)) < 0) {
+ if ((slave = setup_slave_pty(pty_name, fp)) < 0) {
fprintf(fp, "ERROR: could not setup pty (%d).", slave);
}
- if (setpgid(0,pgid) == -1) {
- fprintf(fp, "ERROR: setpgid() failed, %s\n", strerror(errno));
- }
if (setsid() == -1) {
fprintf(fp, "ERROR: setsid() failed, %s\n", strerror(errno));
}
- if (ioctl(0, TIOCSCTTY, NULL) == -1) {
+ if (ioctl(slave, TIOCSCTTY, NULL) == -1) {
fprintf(fp, "ERROR: Unable to attach to controlling tty, %s\n", strerror(errno));
}
+ if (slave > 2) {
+ close(slave);
+ }
+
+ if (dup2(pty_slave, STDIN_FILENO) < 0) {
+ fprintf(fp, "ERROR: Unable to dup slave pty to stdin, %s\n", strerror(errno));
+ }
+ if (pty_slave > 2) {
+ close(pty_slave);
+ }
if (chdir(ptest_dir) == -1) {
fprintf(fp, "ERROR: Unable to chdir(%s), %s\n", ptest_dir, strerror(errno));
@@ -481,10 +492,6 @@ run_ptests(struct ptest_list *head, const struct ptest_options opts,
do_close(&pipefd_stdout[PIPE_WRITE]);
do_close(&pipefd_stderr[PIPE_WRITE]);
- if (setpgid(child, pgid) == -1) {
- fprintf(fp, "ERROR: setpgid() failed, %s\n", strerror(errno));
- }
-
time_t start_time= time(NULL);
fprintf(fp, "%s\n", get_stime(stime, GET_STIME_BUF_SIZE, start_time));
fprintf(fp, "BEGIN: %s\n", ptest_dir);
@@ -608,6 +615,7 @@ run_ptests(struct ptest_list *head, const struct ptest_options opts,
do_close(&pipefd_stdout[PIPE_WRITE]);
do_close(&pipefd_stderr[PIPE_READ]);
do_close(&pipefd_stderr[PIPE_WRITE]);
+ do_close(&pty_master);
fflush(fp);
fflush(fp_stderr);