All of lore.kernel.org
 help / color / mirror / Atom feed
From: Anibal Limon <anibal@limonsoftware.com>
To: yocto-patches@lists.yoctoproject.org
Cc: randy.macleod@windriver.com, changqing.li@windriver.com,
	Anibal Limon <anibal@limonsoftware.com>
Subject: [ptest-runner 1/2] utils.c: run-ptests improve pseudo-terminal handling
Date: Mon,  6 May 2024 09:40:41 -0600	[thread overview]
Message-ID: <20240506154042.3395390-1-anibal@limonsoftware.com> (raw)

Setup master and slave pty before run ptest, on child attach to the
slave pty and set it as controlling tty and stdin.

See rev 59381a643e1fcce9b6920cb59af9402dd5eb0dfb .

Co-authored-by: Changqing Li <changqing.li@windriver.com>
Signed-off-by: Anibal Limon <anibal@limonsoftware.com>
---
 utils.c | 90 ++++++++++++---------------------------------------------
 1 file changed, 19 insertions(+), 71 deletions(-)

diff --git a/utils.c b/utils.c
index 46918f4..3a35313 100644
--- a/utils.c
+++ b/utils.c
@@ -341,53 +341,6 @@ run_child(char *run_ptest, int fd_stdout, int fd_stderr)
 	/* exit(1); not needed? */
 }
 
-/* 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];
-	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 {
-		/* If the tty group does not exist, don't change the
-		 * group on the slave pty, only the owner
-		 */
-		gid = (gid_t)-1;
-	}
-
-	/* chown/chmod the corresponding pty, if possible.
-	 * This will only work if the process has root permissions.
-	 */
-	if (chown(pty_name, getuid(), gid) != 0) {
-		fprintf(fp, "ERROR; chown() failed with: %s.\n", strerror(errno));
-	}
-
-	/* Makes the slave read/writeable for the user. */
-	if (chmod(pty_name, S_IRUSR|S_IWUSR) != 0) {
-		fprintf(fp, "ERROR: chmod() failed with: %s.\n", strerror(errno));
-	}
-
-	if ((slave = open(pty_name, O_RDWR)) == -1) {
-		fprintf(fp, "ERROR: open() failed with: %s.\n", strerror(errno));
-	}
-	return (slave);
-}
-
-
 int
 run_ptests(struct ptest_list *head, const struct ptest_options opts,
 		const char *progname, FILE *fp, FILE *fp_stderr)
@@ -406,16 +359,12 @@ 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[2] = {-1, -1};
 
 			if (pipe2(pipefd_stdout, 0) == -1) {
 				rc = -1;
@@ -428,6 +377,16 @@ run_ptests(struct ptest_list *head, const struct ptest_options opts,
 				break;
 			}
 
+			if (openpty(&pty[0], &pty[1], NULL, NULL, NULL) < 0) {
+				fprintf(fp, "ERROR: openpty() failed with: %s.\n", strerror(errno));
+				close(pipefd_stderr[PIPE_READ]);
+				close(pipefd_stderr[PIPE_WRITE]);
+				close(pipefd_stdout[PIPE_READ]);
+				close(pipefd_stdout[PIPE_WRITE]);
+				rc = -1;
+				break;
+			}
+
 			char *ptest_dir = strdup(p->run_ptest);
 			if (ptest_dir == NULL) {
 				rc = -1;
@@ -435,38 +394,31 @@ 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));
-			}
-
 			pid_t child = fork();
 			if (child == -1) {
 				fprintf(fp, "ERROR: Fork %s\n", strerror(errno));
 				rc = -1;
 				break;
 			} 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]);
 
-				if ((slave = setup_slave_pty(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));
-				}
+				/* Close master pty and set slave pty as stdin */
+				do_close(&pty[0]);
 
 				if (setsid() ==  -1) {
 					fprintf(fp, "ERROR: setsid() failed, %s\n", strerror(errno));
 				}
-
-				if (ioctl(0, TIOCSCTTY, NULL) == -1) {
+				if (ioctl(pty[1], TIOCSCTTY, NULL) == -1) {
 					fprintf(fp, "ERROR: Unable to attach to controlling tty, %s\n", strerror(errno));
 				}
 
+				if (dup2(pty[1], STDIN_FILENO) < 0) {
+					fprintf(fp, "ERROR: Unable to dup slave pty to stdin, %s\n", strerror(errno));
+				}
+				do_close(&pty[1]);
+
 				if (chdir(ptest_dir) == -1) {
 					fprintf(fp, "ERROR: Unable to chdir(%s), %s\n", ptest_dir, strerror(errno));
 				} else {
@@ -481,10 +433,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);
-- 
2.43.0



             reply	other threads:[~2024-05-06 15:40 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-05-06 15:40 Anibal Limon [this message]
2024-05-06 15:40 ` [ptest-runner 2/2] utils.c: run_ptests improve error handling on ptests iteration Anibal Limon

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240506154042.3395390-1-anibal@limonsoftware.com \
    --to=anibal@limonsoftware.com \
    --cc=changqing.li@windriver.com \
    --cc=randy.macleod@windriver.com \
    --cc=yocto-patches@lists.yoctoproject.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.