From patchwork Mon May 6 15:40:41 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anibal Limon X-Patchwork-Id: 43286 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id D9352C10F16 for ; Mon, 6 May 2024 15:40:55 +0000 (UTC) Received: from mail-oi1-f174.google.com (mail-oi1-f174.google.com [209.85.167.174]) by mx.groups.io with SMTP id smtpd.web10.24109.1715010054075232299 for ; Mon, 06 May 2024 08:40:54 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@limonsoftware-com.20230601.gappssmtp.com header.s=20230601 header.b=uT6AAT27; spf=pass (domain: limonsoftware.com, ip: 209.85.167.174, mailfrom: anibal@limonsoftware.com) Received: by mail-oi1-f174.google.com with SMTP id 5614622812f47-3c974a0647bso312237b6e.2 for ; Mon, 06 May 2024 08:40:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=limonsoftware-com.20230601.gappssmtp.com; s=20230601; t=1715010053; x=1715614853; darn=lists.yoctoproject.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=b8migmLtux8XKgcTKdJ0OOmYF0ouWScgSCUN3oqGi2Q=; b=uT6AAT27yie7H7itlALjeH7TszLu11Y7hI3s/A9oavQoUbPy1QBC/3flk2BaD7iYtc C6w8COgj6dNpK23PBlCXDv7M2Zx+nTGd0ipI8QbviG+DfkVtSjq39YM93tgACUTD8Jy/ yXxU5rp7ErKXkRBd01ed77oKByHr1gIZsxZybISj8/QAIK0C4FCTPOtwhfC6dOJZfHNO Ltq9Ar7DN9Zotdm1q8RLRCvLxeiHapIViBXIbY2IPV2uwPkPqQSllQiPAaM9dduwQyRx ROjgM/HrxhxGCgx2YXskRuUXooobSfb08Us/uXJOVjuj8z0LnMwfryBjXya3m1wEJTxL cgdA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715010053; x=1715614853; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=b8migmLtux8XKgcTKdJ0OOmYF0ouWScgSCUN3oqGi2Q=; b=TsmBhSeB05RbJo1MH4qBkua1i6yajdEkRR4IAnH+rc20L6sDk+pw5KHp/nUzlExfsO KFTe11uoa07MMqz+d00WRZ0cElme/u+dtO8uvCKoH1cl+B1FQc8Y6IzJfdhVCBKQbQMo kdrbdm4IcaReokcVjVK4eLukDXhTvnqa9IN1uvcWeU0mqB8NsisJLLC6ua8vuX7RPAIp ggjDAsMQwfJp7W24AR0Y4B8u8WevgaFUqDIBZmbAqR5DiRLLshaJgOduVTm4qG+CMjBu s7GdCrXhZdipaADVpk2njUmmi90aWXMqQKDXhkEPw3HTCPD9icxhnyzoq/PNNvkq2oQj vR2A== X-Gm-Message-State: AOJu0YxGpxcGlgWbJlbAMO5BtsA1aIH5V2CRvOcslmZ/DjA60jtJVTU0 OJpuxcwCTbsVO/6dpS1ot9AjKZltYAvF9mOq+hU41izXGqfLBx2BGwQNyLbSaqBNV7Rdv+qn+Gv vqf8= X-Google-Smtp-Source: AGHT+IF0Tc3esZT+OkSf9N4nC73dpqOUHsUG1KSyUhvUF40Pctq6P80EEc4uT67Zs7QMNHTf5Nu9mA== X-Received: by 2002:a54:400b:0:b0:3c9:68c4:d69 with SMTP id x11-20020a54400b000000b003c968c40d69mr5115380oie.34.1715010052862; Mon, 06 May 2024 08:40:52 -0700 (PDT) Received: from blackbox.admin.ts.net ([189.172.68.96]) by smtp.gmail.com with ESMTPSA id bf26-20020a056808191a00b003c9689769c7sm605780oib.8.2024.05.06.08.40.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 May 2024 08:40:52 -0700 (PDT) From: Anibal Limon To: yocto-patches@lists.yoctoproject.org Cc: randy.macleod@windriver.com, changqing.li@windriver.com, Anibal Limon Subject: [ptest-runner 1/2] utils.c: run-ptests improve pseudo-terminal handling Date: Mon, 6 May 2024 09:40:41 -0600 Message-ID: <20240506154042.3395390-1-anibal@limonsoftware.com> X-Mailer: git-send-email 2.43.0 MIME-Version: 1.0 List-Id: X-Webhook-Received: from li982-79.members.linode.com [45.33.32.79] by aws-us-west-2-korg-lkml-1.web.codeaurora.org with HTTPS for ; Mon, 06 May 2024 15:40:55 -0000 X-Groupsio-URL: https://lists.yoctoproject.org/g/yocto-patches/message/84 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 Signed-off-by: Anibal Limon --- 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); From patchwork Mon May 6 15:40:42 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anibal Limon X-Patchwork-Id: 43285 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id D9380C41513 for ; Mon, 6 May 2024 15:40:55 +0000 (UTC) Received: from mail-oi1-f170.google.com (mail-oi1-f170.google.com [209.85.167.170]) by mx.groups.io with SMTP id smtpd.web10.24110.1715010055447028757 for ; Mon, 06 May 2024 08:40:55 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@limonsoftware-com.20230601.gappssmtp.com header.s=20230601 header.b=P4bC8Pe6; spf=pass (domain: limonsoftware.com, ip: 209.85.167.170, mailfrom: anibal@limonsoftware.com) Received: by mail-oi1-f170.google.com with SMTP id 5614622812f47-3c71c7e2d40so164775b6e.3 for ; Mon, 06 May 2024 08:40:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=limonsoftware-com.20230601.gappssmtp.com; s=20230601; t=1715010054; x=1715614854; darn=lists.yoctoproject.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Y2hyCD8wt6wyHnfT+8OHQheu2DgVDKWmvxByXq6khrw=; b=P4bC8Pe68Ms2AIk6s1UTUoXjUF6ZpKHPxhyOienJgFjACbBeIw7Vx/1opqewi8Qngo QiFZti4+WkqmEMB78Tvanl3EFT3y3cCZJXVgpsY795JtV5peAZgVJploC5/DJJd3vluF U7oH2YRaQMxznUQCV5ME8POuhaCM/jw88cW7O0l2kvI6iJRwGmSmrTSOuiKfs1jv5PTV YX86Aga7DV9ZrplsZ49sdIdG8K8vfdQSnJXUGolsLE854+h228cQNX2WK1dYzLUyNCn+ ahfIz1/m3AtOQqaIGhSKujBRTyDf8XktV9TC/kyU71Jv5vJXNGoFe27jsDuDASplNI0Q o9qg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715010054; x=1715614854; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Y2hyCD8wt6wyHnfT+8OHQheu2DgVDKWmvxByXq6khrw=; b=OZP2sCOunKHZ26fEJBK1tU3gcjQm98Y+VaQ+B3QrvWQWlED0xBQcougje/2Q5g8HbW ATog1aRQLyKGTygLu/grW+U19yj5XSpzyllQBpitCuXwPzTriPv1wCs1xiIkMvhesAx9 kzURlxKwowPRo8B8Tno/nqKs5+wukZVnEKfzYdFeu0C2L++n84Wj8CSuCLfTreMn1Krl 5SW6ynmnrzVovJ4PQnZ9LXEje2zoNN5QWEPIaPbnKOxPm65iMuT24NLJA5PzrJaZdzIi y/wXg+WAWQsmr3pA9QSxltYb8jd6aNd2LLPMDVyj35oXrJbvKek1WXQNNoRCt9FEUDDL iDUg== X-Gm-Message-State: AOJu0YxizQPCZ+WNfsPmM9oy8vt4UDwDLN096uhOe4eWlL0eaX+mk/3S ymf/QDWrCEzWmbmbW53zfopoGH361zxoZbgnEmlQxdDYdAT/rI/v8e1Fstgs5YaYkYgdJqYn+X/ YQhY= X-Google-Smtp-Source: AGHT+IFateF7hbzSmfQW/1FTxgmKavTSAHqYCTlMeK6cVxKBTxZpUNCJxEpouBg0suRJJ7uo8XwDIA== X-Received: by 2002:a54:4387:0:b0:3c9:6dc9:a532 with SMTP id u7-20020a544387000000b003c96dc9a532mr3509679oiv.10.1715010054569; Mon, 06 May 2024 08:40:54 -0700 (PDT) Received: from blackbox.admin.ts.net ([189.172.68.96]) by smtp.gmail.com with ESMTPSA id bf26-20020a056808191a00b003c9689769c7sm605780oib.8.2024.05.06.08.40.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 May 2024 08:40:54 -0700 (PDT) From: Anibal Limon To: yocto-patches@lists.yoctoproject.org Cc: randy.macleod@windriver.com, changqing.li@windriver.com, Anibal Limon Subject: [ptest-runner 2/2] utils.c: run_ptests improve error handling on ptests iteration Date: Mon, 6 May 2024 09:40:42 -0600 Message-ID: <20240506154042.3395390-2-anibal@limonsoftware.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240506154042.3395390-1-anibal@limonsoftware.com> References: <20240506154042.3395390-1-anibal@limonsoftware.com> MIME-Version: 1.0 List-Id: X-Webhook-Received: from li982-79.members.linode.com [45.33.32.79] by aws-us-west-2-korg-lkml-1.web.codeaurora.org with HTTPS for ; Mon, 06 May 2024 15:40:55 -0000 X-Groupsio-URL: https://lists.yoctoproject.org/g/yocto-patches/message/85 If error on setup a new ptest use goto to clean properly and stop the loop. Signed-off-by: Anibal Limon --- utils.c | 51 ++++++++++++++++++++++++++------------------------- 1 file changed, 26 insertions(+), 25 deletions(-) diff --git a/utils.c b/utils.c index 3a35313..6cf7705 100644 --- a/utils.c +++ b/utils.c @@ -350,7 +350,6 @@ run_ptests(struct ptest_list *head, const struct ptest_options opts, struct ptest_list *p; - if (opts.xml_filename) { xh = xml_create(ptest_list_length(head), opts.xml_filename); if (!xh) @@ -362,43 +361,37 @@ run_ptests(struct ptest_list *head, const struct ptest_options opts, fprintf(fp, "START: %s\n", progname); PTEST_LIST_ITERATE_START(head, p) + char ptest_dir[PATH_MAX] = {'\0'}; int pipefd_stdout[2] = {-1, -1}; int pipefd_stderr[2] = {-1, -1}; int pty[2] = {-1, -1}; + strcpy(ptest_dir, p->run_ptest); + dirname(ptest_dir); + if (pipe2(pipefd_stdout, 0) == -1) { + fprintf(fp, "ERROR: pipe2() failed with: %s.\n", strerror(errno)); rc = -1; - break; + goto ptest_list_fail1; } + if (pipe2(pipefd_stderr, 0) == -1) { - close(pipefd_stdout[PIPE_READ]); - close(pipefd_stdout[PIPE_WRITE]); + fprintf(fp, "ERROR: pipe2() failed with: %s.\n", strerror(errno)); rc = -1; - break; + goto ptest_list_fail2; } 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; - break; + goto ptest_list_fail3; } - dirname(ptest_dir); pid_t child = fork(); if (child == -1) { fprintf(fp, "ERROR: Fork %s\n", strerror(errno)); rc = -1; - break; + goto ptest_list_fail4; } else if (child == 0) { /* Close read ends of the pipe */ do_close(&pipefd_stdout[PIPE_READ]); @@ -424,7 +417,6 @@ run_ptests(struct ptest_list *head, const struct ptest_options opts, } else { run_child(p->run_ptest, pipefd_stdout[PIPE_WRITE], pipefd_stderr[PIPE_WRITE]); } - } else { bool timedout = false; char stime[GET_STIME_BUF_SIZE]; @@ -551,22 +543,31 @@ run_ptests(struct ptest_list *head, const struct ptest_options opts, fprintf(fp, "END: %s\n", ptest_dir); fprintf(fp, "%s\n", get_stime(stime, GET_STIME_BUF_SIZE, end_time)); } - free(ptest_dir); - do_close(&pipefd_stdout[PIPE_READ]); - do_close(&pipefd_stdout[PIPE_WRITE]); + +ptest_list_fail4: + do_close(&pty[0]); + do_close(&pty[1]); + +ptest_list_fail3: do_close(&pipefd_stderr[PIPE_READ]); do_close(&pipefd_stderr[PIPE_WRITE]); +ptest_list_fail2: + do_close(&pipefd_stdout[PIPE_READ]); + do_close(&pipefd_stdout[PIPE_WRITE]); +ptest_list_fail1: fflush(fp); fflush(fp_stderr); + if (rc == -1) { + fprintf(fp_stderr, "run_ptests fails: %s", strerror(errno)); + break; + } + PTEST_LIST_ITERATE_END fprintf(fp, "STOP: %s\n", progname); } while (0); - if (rc == -1) - fprintf(fp_stderr, "run_ptests fails: %s", strerror(errno)); - if (opts.xml_filename) xml_finish(xh);