Brad Bishop | 15ae250 | 2019-06-18 21:44:24 -0400 | [diff] [blame^] | 1 | From 79698d3205dedba887e0d2492de945d3079de029 Mon Sep 17 00:00:00 2001 |
| 2 | From: Randy MacLeod <Randy.MacLeod@windriver.com> |
| 3 | Date: Thu, 6 Jun 2019 17:03:50 -0400 |
| 4 | Subject: [PATCH] utils: ensure child can be session leader |
| 5 | |
| 6 | When running the run-execscript bash ptest as a user rather than root, a warning: |
| 7 | bash: cannot set terminal process group (16036): Inappropriate ioctl for device |
| 8 | bash: no job control in this shell |
| 9 | contaminates the bash log files causing the test to fail. This happens only |
| 10 | when run under ptest-runner and not when interactively testing! |
| 11 | |
| 12 | The changes made to fix this include: |
| 13 | 1. Get the process group id (pgid) before forking, |
| 14 | 2. Set the pgid in both the parent and child to avoid a race, |
| 15 | 3. Find, open and set permission on the child tty, and |
| 16 | 4. Allow the child to attach to controlling tty. |
| 17 | |
| 18 | Also add '-lutil' to Makefile. This lib is from libc and provides openpty. |
| 19 | |
| 20 | Upstream-Status: Submitted [yocto@yoctoproject.org] |
| 21 | |
| 22 | Signed-off-by: Sakib Sajal <sakib.sajal@windriver.com> |
| 23 | Signed-off-by: Randy MacLeod <Randy.MacLeod@windriver.com> |
| 24 | --- |
| 25 | Makefile | 2 +- |
| 26 | utils.c | 102 +++++++++++++++++++++++++++++++++++++++++++++++++------ |
| 27 | 2 files changed, 92 insertions(+), 12 deletions(-) |
| 28 | |
| 29 | diff --git a/Makefile b/Makefile |
| 30 | index 1bde7be..439eb79 100644 |
| 31 | --- a/Makefile |
| 32 | +++ b/Makefile |
| 33 | @@ -29,7 +29,7 @@ TEST_DATA=$(shell echo `pwd`/tests/data) |
| 34 | all: $(SOURCES) $(EXECUTABLE) |
| 35 | |
| 36 | $(EXECUTABLE): $(OBJECTS) |
| 37 | - $(CC) $(LDFLAGS) $(OBJECTS) -o $@ |
| 38 | + $(CC) $(LDFLAGS) $(OBJECTS) -lutil -o $@ |
| 39 | |
| 40 | tests: $(TEST_SOURCES) $(TEST_EXECUTABLE) |
| 41 | |
| 42 | diff --git a/utils.c b/utils.c |
| 43 | index ad737c2..f11ce39 100644 |
| 44 | --- a/utils.c |
| 45 | +++ b/utils.c |
| 46 | @@ -1,5 +1,6 @@ |
| 47 | /** |
| 48 | * Copyright (c) 2016 Intel Corporation |
| 49 | + * Copyright (C) 2019 Wind River Systems, Inc. |
| 50 | * |
| 51 | * This program is free software; you can redistribute it and/or |
| 52 | * modify it under the terms of the GNU General Public License |
| 53 | @@ -22,23 +23,27 @@ |
| 54 | */ |
| 55 | |
| 56 | #define _GNU_SOURCE |
| 57 | + |
| 58 | #include <stdio.h> |
| 59 | |
| 60 | +#include <dirent.h> |
| 61 | +#include <errno.h> |
| 62 | +#include <fcntl.h> |
| 63 | +#include <grp.h> |
| 64 | #include <libgen.h> |
| 65 | -#include <signal.h> |
| 66 | #include <poll.h> |
| 67 | -#include <fcntl.h> |
| 68 | +#include <pty.h> |
| 69 | +#include <signal.h> |
| 70 | +#include <stdlib.h> |
| 71 | +#include <string.h> |
| 72 | #include <time.h> |
| 73 | -#include <dirent.h> |
| 74 | +#include <unistd.h> |
| 75 | + |
| 76 | +#include <sys/ioctl.h> |
| 77 | #include <sys/resource.h> |
| 78 | +#include <sys/stat.h> |
| 79 | #include <sys/types.h> |
| 80 | #include <sys/wait.h> |
| 81 | -#include <sys/stat.h> |
| 82 | -#include <unistd.h> |
| 83 | -#include <string.h> |
| 84 | -#include <stdlib.h> |
| 85 | - |
| 86 | -#include <errno.h> |
| 87 | |
| 88 | #include "ptest_list.h" |
| 89 | #include "utils.h" |
| 90 | @@ -346,6 +351,53 @@ wait_child(const char *ptest_dir, const char *run_ptest, pid_t pid, |
| 91 | return status; |
| 92 | } |
| 93 | |
| 94 | +/* Returns an integer file descriptor. |
| 95 | + * If it returns < 0, an error has occurred. |
| 96 | + * Otherwise, it has returned the slave pty file descriptor. |
| 97 | + * fp should be writable, likely stdout/err. |
| 98 | + */ |
| 99 | +static int |
| 100 | +setup_slave_pty(FILE *fp) { |
| 101 | + int pty_master = -1; |
| 102 | + int pty_slave = -1; |
| 103 | + char pty_name[256]; |
| 104 | + struct group *gptr; |
| 105 | + gid_t gid; |
| 106 | + int slave = -1; |
| 107 | + |
| 108 | + if (openpty(&pty_master, &pty_slave, pty_name, NULL, NULL) < 0) { |
| 109 | + fprintf(fp, "ERROR: openpty() failed with: %s.\n", strerror(errno)); |
| 110 | + return -1; |
| 111 | + } |
| 112 | + |
| 113 | + if ((gptr = getgrnam(pty_name)) != 0) { |
| 114 | + gid = gptr->gr_gid; |
| 115 | + } else { |
| 116 | + /* If the tty group does not exist, don't change the |
| 117 | + * group on the slave pty, only the owner |
| 118 | + */ |
| 119 | + gid = -1; |
| 120 | + } |
| 121 | + |
| 122 | + /* chown/chmod the corresponding pty, if possible. |
| 123 | + * This will only work if the process has root permissions. |
| 124 | + */ |
| 125 | + if (chown(pty_name, getuid(), gid) != 0) { |
| 126 | + fprintf(fp, "ERROR; chown() failed with: %s.\n", strerror(errno)); |
| 127 | + } |
| 128 | + |
| 129 | + /* Makes the slave read/writeable for the user. */ |
| 130 | + if (chmod(pty_name, S_IRUSR|S_IWUSR) != 0) { |
| 131 | + fprintf(fp, "ERROR: chmod() failed with: %s.\n", strerror(errno)); |
| 132 | + } |
| 133 | + |
| 134 | + if ((slave = open(pty_name, O_RDWR)) == -1) { |
| 135 | + fprintf(fp, "ERROR: open() failed with: %s.\n", strerror(errno)); |
| 136 | + } |
| 137 | + return (slave); |
| 138 | +} |
| 139 | + |
| 140 | + |
| 141 | int |
| 142 | run_ptests(struct ptest_list *head, const struct ptest_options opts, |
| 143 | const char *progname, FILE *fp, FILE *fp_stderr) |
| 144 | @@ -362,6 +414,8 @@ run_ptests(struct ptest_list *head, const struct ptest_options opts, |
| 145 | int timeouted; |
| 146 | time_t sttime, entime; |
| 147 | int duration; |
| 148 | + int slave; |
| 149 | + int pgid = -1; |
| 150 | |
| 151 | if (opts.xml_filename) { |
| 152 | xh = xml_create(ptest_list_length(head), opts.xml_filename); |
| 153 | @@ -379,7 +433,6 @@ run_ptests(struct ptest_list *head, const struct ptest_options opts, |
| 154 | close(pipefd_stdout[1]); |
| 155 | break; |
| 156 | } |
| 157 | - |
| 158 | fprintf(fp, "START: %s\n", progname); |
| 159 | PTEST_LIST_ITERATE_START(head, p); |
| 160 | char *ptest_dir = strdup(p->run_ptest); |
| 161 | @@ -388,6 +441,13 @@ run_ptests(struct ptest_list *head, const struct ptest_options opts, |
| 162 | break; |
| 163 | } |
| 164 | dirname(ptest_dir); |
| 165 | + if (ioctl(0, TIOCNOTTY) == -1) { |
| 166 | + fprintf(fp, "ERROR: Unable to detach from controlling tty, %s\n", strerror(errno)); |
| 167 | + } |
| 168 | + |
| 169 | + if ((pgid = getpgid(0)) == -1) { |
| 170 | + fprintf(fp, "ERROR: getpgid() failed, %s\n", strerror(errno)); |
| 171 | + } |
| 172 | |
| 173 | child = fork(); |
| 174 | if (child == -1) { |
| 175 | @@ -395,13 +455,33 @@ run_ptests(struct ptest_list *head, const struct ptest_options opts, |
| 176 | rc = -1; |
| 177 | break; |
| 178 | } else if (child == 0) { |
| 179 | - setsid(); |
| 180 | + close(0); |
| 181 | + if ((slave = setup_slave_pty(fp)) < 0) { |
| 182 | + fprintf(fp, "ERROR: could not setup pty (%d).", slave); |
| 183 | + } |
| 184 | + if (setpgid(0,pgid) == -1) { |
| 185 | + fprintf(fp, "ERROR: setpgid() failed, %s\n", strerror(errno)); |
| 186 | + } |
| 187 | + |
| 188 | + if (setsid() == -1) { |
| 189 | + fprintf(fp, "ERROR: setsid() failed, %s\n", strerror(errno)); |
| 190 | + } |
| 191 | + |
| 192 | + if (ioctl(0, TIOCSCTTY, NULL) == -1) { |
| 193 | + fprintf(fp, "ERROR: Unable to attach to controlling tty, %s\n", strerror(errno)); |
| 194 | + } |
| 195 | + |
| 196 | run_child(p->run_ptest, pipefd_stdout[1], pipefd_stderr[1]); |
| 197 | + |
| 198 | } else { |
| 199 | int status; |
| 200 | int fds[2]; fds[0] = pipefd_stdout[0]; fds[1] = pipefd_stderr[0]; |
| 201 | FILE *fps[2]; fps[0] = fp; fps[1] = fp_stderr; |
| 202 | |
| 203 | + if (setpgid(child, pgid) == -1) { |
| 204 | + fprintf(fp, "ERROR: setpgid() failed, %s\n", strerror(errno)); |
| 205 | + } |
| 206 | + |
| 207 | sttime = time(NULL); |
| 208 | fprintf(fp, "%s\n", get_stime(stime, GET_STIME_BUF_SIZE, sttime)); |
| 209 | fprintf(fp, "BEGIN: %s\n", ptest_dir); |
| 210 | -- |
| 211 | 2.17.0 |
| 212 | |