Fix tests failing with setsid
v3 cover Fix tests failing with setsid
Following tests are failing when running on a new session started with setsid command/syscall: pty01, ptem01, setpgid01.
Tihs patch-set refactor them in order to fix this issue. Some tests like pty01 and ptem01 have been split into multiple files due to their complexity.
---
Changes in v3:
- move slave/master opening in common.h
- check if master device exists inside open_master()
- check if slave device has right permissions inside open_slave()
- add TST_EXP_EQ_STRN macro
- fix SAFE_PTSNAME macro
- move master handling inside setup/cleanup in most of the test
- Link to v2: https://lore.kernel.org/r/[email protected]
Changes in v2:
- add SAFE_PTSNAME macro
- ptem01: check ptsname() with TBROK
- Link to v1: https://lore.kernel.org/r/[email protected]
---
Andrea Cervesato (12):
Refactor setpgid01 test
Add SAFE_PTSNAME macro
Refactor ptem01 test
Add ptem02 test
Add ptem03 test
Add ptem04 test
Add ptem05 test
Add ptem06 test
Add TST_EXP_EQ_STRN macro
Refactor pty01 test
Add pty08 test
Add pty09 test
include/tst_safe_macros.h | 4 + include/tst_test_macros.h | 21 ++ lib/tst_safe_macros.c | 14 + runtest/pty | 7 + testcases/kernel/pty/.gitignore | 7 + testcases/kernel/pty/common.h | 68 ++++ testcases/kernel/pty/ptem01.c | 456 +++----------------------- testcases/kernel/pty/ptem02.c | 71 ++++ testcases/kernel/pty/ptem03.c | 49 +++ testcases/kernel/pty/ptem04.c | 49 +++ testcases/kernel/pty/ptem05.c | 55 ++++ testcases/kernel/pty/ptem06.c | 52 +++ testcases/kernel/pty/pty01.c | 409 +++-------------------- testcases/kernel/pty/pty08.c | 38 +++ testcases/kernel/pty/pty09.c | 74 +++++ testcases/kernel/syscalls/setpgid/setpgid01.c | 152 ++------- 16 files changed, 610 insertions(+), 916 deletions(-)
---
base-commit: 546ad9ccc9c61bf848cb0eadc24203202b20b08c
change-id: 20241204-fix_setsid_tests-876a46267285
Best regards,
Signature · 5 lines
Andrea Cervesato <[email protected]> -- Mailing list info: https://lists.linux.it/listinfo/ltp
PATCH v3 1/12 Refactor setpgid01 test
From: Andrea Cervesato <[email protected]>
Test used to fail when running inside an executor that is setting a new session via setsid(). By using the new LTP library, we fix this issue since it forks parent first, then execute it, enabling any setsid() to be run in the main process before execution.
--- testcases/kernel/syscalls/setpgid/setpgid01.c | 152 +++++--------------------- 1 file changed, 25 insertions(+), 127 deletions(-)
testcases/kernel/syscalls/setpgid/setpgid01.c +25 -127
@@ -1,150 +1,48 @@ +// SPDX-License-Identifier: GPL-2.0-or-later /* * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved. -* -* This program is free software; you can redistribute it and/or modify it -* under the terms of version 2 of the GNU General Public License as -* published by the Free Software Foundation. -* -* This program is distributed in the hope that it would be useful, but -* WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. -* -* Further, this software is distributed without any warranty that it is -* free of the rightful claim of any third person regarding infringement -* or the like. Any license provided herein, whether implied or -* otherwise, applies only to this software file. Patent licenses, if -* any, provided herein do not apply to combinations of this program with -* other software, or any other product whatsoever. -* -* You should have received a copy of the GNU General Public License along -* with this program; if not, write the Free Software Foundation, Inc., -* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. -* -* Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy, -* Mountain View, CA 94043, or: -* -* http://www.sgi.com -* -* For further information regarding this notice, see: -* -* http://oss.sgi.com/projects/GenInfo/NoticeExplan/ -* +* Copyright (C) 2024 SUSE LLC Andrea Cervesato <[email protected]> */ -/* $Id: setpgid01.c,v 1.7 2009/11/02 13:57:18 subrata_modak Exp $ */ -/* -* Description: -* Verify that: -* 1. Basic functionality test for setpgid(2). -* 2. Check functioning of setpgid(2) with pid = 0 and pgid = 0. +/*\ +* [Description] +* +* Verify basic setpgid() functionality, re-setting group ID inside both parent +* and child. In the first case, we obtain getpgrp() and set it. In the second +* case, we use setpgid(0, 0). */ -#include <errno.h> -#include <string.h> -#include <signal.h> -#include <stdlib.h> -#include <sys/wait.h> -#include "test.h" - -static void setup (void ); -static void cleanup (void ); - -char *TCID ="setpgid01" ; - -static void setpgid_test1 (void ); -static void setpgid_test2 (void ); -static void (*testfunc[])(void ) = { setpgid_test1, setpgid_test2}; -int TST_TOTAL =ARRAY_SIZE (testfunc); - -int main (int ac,char **av) -{ -int i, lc; - -tst_parse_opts (ac, av,NULL ,NULL ); - -setup (); - -for (lc =0 ;TEST_LOOPING (lc); lc++) { - tst_count =0 ; - -for (i =0 ; i <TST_TOTAL ; i++) - (*testfunc[i])(); - } - -cleanup (); -tst_exit (); -} +#include "tst_test.h" static void setpgid_test1 (void ) {pid_t pgid, pid; - pgid =getpgrp (); - pid =getpid (); + pgid =TST_EXP_PID (getpgrp ()); + pid =TST_EXP_PID (getpid ()); -TEST (setpgid (pid, pgid)); -if (TEST_RETURN ==-1 ||getpgrp () != pgid) { -tst_resm (TFAIL |TTERRNO ,"test setpgid(%d, %d) fail" , - pid, pgid); - }else { -tst_resm (TPASS ,"test setpgid(%d, %d) success" , pid, pgid); - } -} - -static int wait4child (pid_t child) -{ -int status; - -if (waitpid (child, &status,0 ) ==-1 ) -tst_resm (TBROK |TERRNO ,"waitpid" ); -if (WIFEXITED (status)) -return WEXITSTATUS (status); -else -return status; +TST_EXP_PASS (setpgid (pid, pgid)); +TST_EXP_EQ_LI (pgid,getpgrp ()); }static void setpgid_test2 (void ) { -int ret; -pid_t pgid, pid; +pid_t pgid; - pid =tst_fork (); -if (pid ==-1 ) -tst_brkm (TBROK |TERRNO , cleanup,"fork()" ); - -if (pid !=0 ) { - ret =wait4child (pid); - }else { - pid =getpid (); -TEST (setpgid (0 ,0 )); - pgid =getpgrp (); -if (TEST_RETURN ==-1 ) { -fprintf (stderr,"setpgid(0, 0) fails in " -"child process: %s\n" ,strerror (TEST_ERRNO )); -exit (1 ); - }else if (pgid != pid) { -fprintf (stderr,"setpgid(0, 0) fails to make PGID" -"equal to PID\n" ); -exit (1 ); - }else { -exit (0 ); - } +if (!SAFE_FORK ()) { + pgid =TST_EXP_PID (getpid ()); +TST_EXP_PASS (setpgid (0 ,0 )); +TST_EXP_EQ_LI (pgid,getpgrp ()); } - -if (ret ==0 ) -tst_resm (TPASS ,"test setpgid(0, 0) success" ); -else -tst_resm (TFAIL ,"test setpgid(0, 0) fail" ); } - -static void setup (void ) +static void run (void ) { -tst_sig (FORK ,DEF_HANDLER , cleanup); - -TEST_PAUSE ; +setpgid_test1 (); +setpgid_test2 (); } -static void cleanup (void ) -{ -} +static struct tst_test test = { + .test_all = run, + .forks_child =1 , +};
Signature · 5 lines
2.43.0 -- Mailing list info: https://lists.linux.it/listinfo/ltp
PATCH v3 2/12 Add SAFE_PTSNAME macro
In reply to Andrea CervesatoFrom: Andrea Cervesato <[email protected]>
--- include/tst_safe_macros.h | 4 ++++ lib/tst_safe_macros.c | 14 ++++++++++++++ 2 files changed, 18 insertions(+)
include/tst_safe_macros.h +4 -0
@@ -528,4 +528,8 @@ ssize_t safe_writev(const char *file, const int lineno, char len_strict,safe_writev (__FILE__, __LINE__, (len_strict), (fildes), \ (iov), (iovcnt)) +char *safe_ptsname (const char *const file,const int lineno,int masterfd); +#define SAFE_PTSNAME (masterfd) \ + safe_ptsname(__FILE__, __LINE__, (masterfd)) +#endif /* TST_SAFE_MACROS_H__ */
lib/tst_safe_macros.c +14 -0
@@ -796,3 +796,17 @@ ssize_t safe_writev(const char *file, const int lineno, char len_strict,return rval; } + +char *safe_ptsname (const char *const file,const int lineno,int masterfd) +{ +char *name; + + name =ptsname (masterfd); + +if (!name) { +tst_brk_ (file, lineno,TBROK |TERRNO , +"ptsname(%d) failed" , masterfd); + } + +return name; +}
Signature · 5 lines
2.43.0 -- Mailing list info: https://lists.linux.it/listinfo/ltp
PATCH v3 3/12 Refactor ptem01 test
In reply to Andrea CervesatoFrom: Andrea Cervesato <[email protected]>
Fix test failure when running inside a new session via setsid() and start spliting its internal tests cases implementations into multiple files.
--- testcases/kernel/pty/common.h | 68 +++++++ testcases/kernel/pty/ptem01.c | 456 ++++-------------------------------------- 2 files changed, 105 insertions(+), 419 deletions(-)
testcases/kernel/pty/common.h new file +68 -0
@@ -0,0 +1,68 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* +* Copyright (C) 2024 SUSE LLC Andrea Cervesato <[email protected]> +*/ + +#ifndef PTEM_H +#define PTEM_H + +#define _GNU_SOURCE + +#include "tst_test.h" + +#define MASTERCLONE "/dev/ptmx" + +static inline int open_master (void ) +{ +int masterfd; + +if (access (MASTERCLONE ,F_OK )) +tst_brk (TCONF ,"%s device doesn't exist" ,MASTERCLONE ); + +tst_res (TINFO ,"opening master %s" ,MASTERCLONE ); + + masterfd =SAFE_OPEN (MASTERCLONE ,O_RDWR ); + +if (grantpt (masterfd) ==-1 ) +tst_brk (TBROK |TERRNO ,"grantpt() error" ); + +if (unlockpt (masterfd) ==-1 ) +tst_brk (TBROK |TERRNO ,"unlockpt() error" ); + +return masterfd; +} + +static inline int open_slave (const int masterfd) +{ +int slavefd; +char *slavename; + + slavename =SAFE_PTSNAME (masterfd); + +tst_res (TINFO ,"opening slave %s" , slavename); + +#ifndef __BIONIC__ +/* grantpt() is a no-op in bionic. */ +struct stat st; + +SAFE_STAT (slavename, &st); + +uid_t uid =getuid (); + +if (st.st_uid != uid) { +tst_brk (TBROK ,"uid mismatch st.st_uid(%d) != getuid(%d)" , + st.st_uid, uid); + } + +if (st.st_mode != (S_IFCHR |0620 )) { +tst_brk (TBROK ,"unexpected slave device permission: %o" , + st.st_mode); + } +#endif + + slavefd =SAFE_OPEN (slavename,O_RDWR ); + +return slavefd; +} + +#endif
testcases/kernel/pty/ptem01.c +37 -419
478 lines not on this page · raw message
Signature · 5 lines
2.43.0 -- Mailing list info: https://lists.linux.it/listinfo/ltp
PATCH v3 4/12 Add ptem02 test
In reply to Andrea CervesatoFrom: Andrea Cervesato <[email protected]>
Verify that it's possible to open a pseudo-terminal via /dev/ptmx, obtain a slave device and set/get window size.
--- runtest/pty | 1 + testcases/kernel/pty/.gitignore | 1 + testcases/kernel/pty/ptem02.c | 71 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 73 insertions(+)
runtest/pty +1 -0
@@ -7,5 +7,6 @@ pty05 pty05 pty06 pty06 pty07 pty07 ptem01 ptem01 +ptem02 ptem02 hangup01 hangup01
testcases/kernel/pty/.gitignore +1 -0
@@ -1,5 +1,6 @@ /hangup01 /ptem01 +/ptem02 /pty01 /pty02 /pty03
testcases/kernel/pty/ptem02.c new file +71 -0
71 lines not on this page · raw message
Signature · 5 lines
2.43.0 -- Mailing list info: https://lists.linux.it/listinfo/ltp
PATCH v3 5/12 Add ptem03 test
In reply to Andrea CervesatoFrom: Andrea Cervesato <[email protected]>
Verify that it's possible to open a pseudo-terminal via /dev/ptmx, obtain a slave device and to send a break to both master and slave.
--- runtest/pty | 1 + testcases/kernel/pty/.gitignore | 1 + testcases/kernel/pty/ptem03.c | 49 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+)
runtest/pty +1 -0
@@ -8,5 +8,6 @@ pty06 pty06 pty07 pty07 ptem01 ptem01 ptem02 ptem02 +ptem03 ptem03 hangup01 hangup01
testcases/kernel/pty/.gitignore +1 -0
@@ -1,6 +1,7 @@ /hangup01 /ptem01 /ptem02 +/ptem03 /pty01 /pty02 /pty03
testcases/kernel/pty/ptem03.c new file +49 -0
49 lines not on this page · raw message
Signature · 5 lines
2.43.0 -- Mailing list info: https://lists.linux.it/listinfo/ltp
PATCH v3 6/12 Add ptem04 test
In reply to Andrea CervesatoFrom: Andrea Cervesato <[email protected]>
Verify that it's possible to open a pseudo-terminal via /dev/ptmx, obtain a slave device and to check if it's possible to open it multiple times.
--- runtest/pty | 1 + testcases/kernel/pty/.gitignore | 1 + testcases/kernel/pty/ptem04.c | 49 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+)
runtest/pty +1 -0
@@ -9,5 +9,6 @@ pty07 pty07 ptem01 ptem01 ptem02 ptem02 ptem03 ptem03 +ptem04 ptem04 hangup01 hangup01
testcases/kernel/pty/.gitignore +1 -0
@@ -2,6 +2,7 @@ /ptem01 /ptem02 /ptem03 +/ptem04 /pty01 /pty02 /pty03
testcases/kernel/pty/ptem04.c new file +49 -0
49 lines not on this page · raw message
Signature · 5 lines
2.43.0 -- Mailing list info: https://lists.linux.it/listinfo/ltp
PATCH v3 7/12 Add ptem05 test
In reply to Andrea CervesatoFrom: Andrea Cervesato <[email protected]>
Verify that it's possible to open a pseudo-terminal via /dev/ptmx, to obtain a master + slave pair and to open them multiple times.
--- runtest/pty | 1 + testcases/kernel/pty/.gitignore | 1 + testcases/kernel/pty/ptem05.c | 55 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+)
runtest/pty +1 -0
@@ -10,5 +10,6 @@ ptem01 ptem01 ptem02 ptem02 ptem03 ptem03 ptem04 ptem04 +ptem05 ptem05 hangup01 hangup01
testcases/kernel/pty/.gitignore +1 -0
@@ -3,6 +3,7 @@ /ptem02 /ptem03 /ptem04 +/ptem05 /pty01 /pty02 /pty03
testcases/kernel/pty/ptem05.c new file +55 -0
55 lines not on this page · raw message
Signature · 5 lines
2.43.0 -- Mailing list info: https://lists.linux.it/listinfo/ltp
PATCH v3 8/12 Add ptem06 test
In reply to Andrea CervesatoFrom: Andrea Cervesato <[email protected]>
Verify that it's possible to open a pseudo-terminal via /dev/ptmx, to obtain a slave device and to set baudrate to B0 (which means hang up).
--- runtest/pty | 1 + testcases/kernel/pty/.gitignore | 1 + testcases/kernel/pty/ptem06.c | 52 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+)
runtest/pty +1 -0
@@ -11,5 +11,6 @@ ptem02 ptem02 ptem03 ptem03 ptem04 ptem04 ptem05 ptem05 +ptem06 ptem06 hangup01 hangup01
testcases/kernel/pty/.gitignore +1 -0
@@ -4,6 +4,7 @@ /ptem03 /ptem04 /ptem05 +/ptem06 /pty01 /pty02 /pty03
testcases/kernel/pty/ptem06.c new file +52 -0
52 lines not on this page · raw message
Signature · 5 lines
2.43.0 -- Mailing list info: https://lists.linux.it/listinfo/ltp
PATCH v3 9/12 Add TST_EXP_EQ_STRN macro
In reply to Andrea CervesatoFrom: Andrea Cervesato <[email protected]>
New TST_EXP_EQ_STRN macro can be used to compare two strings, passing their length if needed.
--- include/tst_test_macros.h | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+)
include/tst_test_macros.h +21 -0
@@ -425,4 +425,25 @@ const char *tst_errno_names(char *buf, const int *exp_errs, int exp_errs_cnt); } \ }while (0 ) +/** +* TST_EXP_EQ_STRN() - Compare two strings, providing length as well. +* +* @STR_A: string to compare. +* @STR_B: string to compare. +* @LEN: length of the string. + */ +#define TST_EXP_EQ_STRN (STR_A ,STR_B ,LEN ) do { \ + TST_PASS = strncmp(STR_A, STR_B, LEN) == 0; \ + \ + if (TST_PASS) { \ + tst_res_(__FILE__, __LINE__, TPASS, \ + "%s == %s (%s)", \ + #STR_A, #STR_B, STR_B); \ + } else { \ + tst_res_(__FILE__, __LINE__, TFAIL, \ + "%s (%s) != %s (%s)", \ + #STR_A, STR_A, #STR_B, STR_B); \ + } \ +} while (0) +#endif /* TST_TEST_MACROS_H__ */
Signature · 5 lines
2.43.0 -- Mailing list info: https://lists.linux.it/listinfo/ltp
Re: [LTP] [PATCH v3 09/12] Add TST_EXP_EQ_STRN macro
Hi!
include/tst_test_macros.h const char *tst_errno_names(char *buf, const int *exp_errs, int exp_errs_cnt); · line 445
Quoted 11 lines
include/tst_test_macros.h +11
-0+#define TST_EXP_EQ_STRN (STR_A ,STR_B ,LEN ) do { \ + TST_PASS = strncmp(STR_A, STR_B, LEN) == 0; \ + \ + if (TST_PASS) { \ + tst_res_(__FILE__, __LINE__, TPASS, \ + "%s == %s (%s)", \ + #STR_A, #STR_B, STR_B); \ + } else { \ + tst_res_(__FILE__, __LINE__, TFAIL, \ + "%s (%s) != %s (%s)", \ + #STR_A, STR_A, #STR_B, STR_B); \
Passing these strings to printf-like function is not safe at all, since they are possibly not nul terminated. If we realy wanted to print them we would have to copy them and nul terminated them.
e.g.
char str_a_cpy[LEN+1], str_b_cpy[LEN+1]; strncpy(str_a_cpy, STR_A, LEN); str_a_cpy[LEN] = 0; strncpy(str_b_cpy, STR_B, LEN); str_b_cpy[LEN] = 0; ... tst_res_(...., str_b_cpy, ...);
Quoted 11 lines
include/tst_test_macros.h +3
-0+ } \ +}while (0 ) + #endif /* TST_TEST_MACROS_H__ */Signature · 6 lines
-- 2.43.0 -- Mailing list info: https://lists.linux.it/listinfo/ltp
Signature · 5 lines
Cyril Hrubis [email protected] -- Mailing list info: https://lists.linux.it/listinfo/ltp
Re: [LTP] [PATCH v3 09/12] Add TST_EXP_EQ_STRN macro
Hi Cyril,
I guess this is affecting also TST_EXP_EQ_STR then.. I will need to fix that one as well.
Andrea
Quoted 40 lines · Cyril Hrubis
Hi!
include/tst_test_macros.h const char *tst_errno_names(char *buf, const int *exp_errs, int exp_errs_cnt); · line 445
Quoted 11 lines
include/tst_test_macros.h +11
-0+#define TST_EXP_EQ_STRN (STR_A ,STR_B ,LEN ) do { \ + TST_PASS = strncmp(STR_A, STR_B, LEN) == 0; \ + \ + if (TST_PASS) { \ + tst_res_(__FILE__, __LINE__, TPASS, \ + "%s == %s (%s)", \ + #STR_A, #STR_B, STR_B); \ + } else { \ + tst_res_(__FILE__, __LINE__, TFAIL, \ + "%s (%s) != %s (%s)", \ + #STR_A, STR_A, #STR_B, STR_B); \Passing these strings to printf-like function is not safe at all, since they are possibly not nul terminated. If we realy wanted to print them we would have to copy them and nul terminated them.
e.g.
char str_a_cpy[LEN+1], str_b_cpy[LEN+1]; strncpy(str_a_cpy, STR_A, LEN); str_a_cpy[LEN] = 0; strncpy(str_b_cpy, STR_B, LEN); str_b_cpy[LEN] = 0; ... tst_res_(...., str_b_cpy, ...);Quoted 11 lines
include/tst_test_macros.h +3
-0+ } \ +}while (0 ) + #endif /* TST_TEST_MACROS_H__ */Signature · 6 lines
-- 2.43.0 -- Mailing list info: https://lists.linux.it/listinfo/ltp
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
Re: [LTP] [PATCH v3 09/12] Add TST_EXP_EQ_STRN macro
Hi!
Quoted 2 lines
I guess this is affecting also
TST_EXP_EQ_STRthen.. I will need to fix that one as well.
Not really, because if you do not pass string lenght the string must be nul terminated.
Signature · 5 lines
Cyril Hrubis [email protected] -- Mailing list info: https://lists.linux.it/listinfo/ltp
PATCH v3 10/12 Refactor pty01 test
In reply to Andrea CervesatoFrom: Andrea Cervesato <[email protected]>
Rewrite part of the code using the new LTP library and fix the execution of the test inside a new session via setsid(). The test is now split into multiple files, instead of having multiple test* functions executing all in one file.
--- testcases/kernel/pty/pty01.c | 409 +++++-------------------------------------- 1 file changed, 39 insertions(+), 370 deletions(-)
testcases/kernel/pty/pty01.c +39 -370
@@ -1,395 +1,64 @@ +// SPDX-License-Identifier: GPL-2.0-or-later /* -* -* Copyright (c) International Business Machines Corp., 2002 -* -* This program is free software; you can redistribute it and/or modify -* it under the terms of the GNU General Public License as published by -* the Free Software Foundation; either version 2 of the License, or -* (at your option) any later version. -* -* This program is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See -* the GNU General Public License for more details. -* -* You should have received a copy of the GNU General Public License -* along with this program; if not, write to the Free Software -* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +* Copyright (c) International Business Machines Corp., 2002 +* Copyright (C) 2024 SUSE LLC Andrea Cervesato <[email protected]> */ -/* 12/23/2002 Port to LTP [email protected] */ -/* 06/30/2001 Port to Linux [email protected] */ +/*\ +* [Description] +* +* Verify that write/read is properly working when master and slave +* pseudo terminals communicate with each other. +*/ #define _GNU_SOURCE -#include <sys/types.h> -#include <sys/stat.h> -#include <sys/wait.h> -#include <errno.h> -#include <fcntl.h> -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <termios.h> -#include <unistd.h> - -#include "test.h" -#include "safe_macros.h" -#include "lapi/ioctl.h" - -char *TCID ="pty01" ;/* Test program identifier. */ -int TST_TOTAL =5 ;/* Total number of test cases. */ -/**************/ -/* -* pty master clone device -*/ -#define MASTERCLONE "/dev/ptmx" +#include "common.h" -/* -* string for testing read/write on ptys -*/ #define STRING "Linux Test Project\n" -/* -* test buffer size -*/ -#define TESTSIZE 1024 - -/* -* mode we expect grantpt() to leave pty as -*/ -#define PTY_MODE 020622 - -/* -* number of procs for parallel test -*/ -#define NUMPROCS 15 - -/* -* test slave locking -*/ -static int test1 (void ) -{ -int masterfd;/* master pty fd */ -int slavefd;/* slave pty fd */ -char *slavename; -struct stat st; -char buf[TESTSIZE ]; - - masterfd =SAFE_OPEN (NULL ,MASTERCLONE ,O_RDWR ); - - slavename =ptsname (masterfd); -if (slavename ==NULL ) { -tst_brkm (TBROK |TERRNO ,NULL ,"ptsname() call failed" ); - } - -if (grantpt (masterfd) !=0 ) { -tst_brkm (TBROK |TERRNO ,NULL ,"grantpt() call failed" ); - } - -if (stat (slavename, &st) !=0 ) { -tst_brkm (TBROK |TERRNO ,NULL ,"stat(%s) failed" , slavename); - } -if (st.st_uid !=getuid ()) { -tst_brkm (TBROK ,NULL ,"uid mismatch" ); - } - -/* grantpt() is a no-op in bionic. */ -#ifndef __BIONIC__ -if (st.st_mode != (S_IFCHR |S_IRUSR |S_IWUSR |S_IWGRP )) { -tst_brkm (TBROK ,NULL ,"mode mismatch (mode=%o)" , st.st_mode); - } -#endif - - slavefd =open (slavename,O_RDWR ); -if (slavefd >=0 ) { -tst_brkm (TBROK ,NULL ,"open didn't fail as expected!" ); - } - -if (unlockpt (masterfd) !=0 ) { -tst_brkm (TBROK |TERRNO ,NULL ,"unlockpt() failed" ); - } - - slavefd =SAFE_OPEN (NULL , slavename,O_RDWR ); - -/* -* test writing to the master / reading from the slave -*/ -if (write (masterfd,STRING ,strlen (STRING )) !=strlen (STRING )) { -/* -* XXX: the errno printout might be garbage, but better to be -* safe than sorry.. -*/ -tst_brkm (TFAIL |TERRNO ,NULL ,"write to master" ); - } - -if (read (slavefd, buf,strlen (STRING )) !=strlen (STRING )) { -/* XXX: Same as write above.. */ -tst_brkm (TFAIL |TERRNO ,NULL ,"read from slave" ); - } -if (strncmp (STRING , buf,strlen (STRING ) -1 ) !=0 ) { -tst_brkm (TFAIL ,NULL , -"strings are different (STRING = '%s' != buf = '%s')" , -STRING , buf); - } - -/* -* test writing to the slave / reading from the master -*/ -if (write (slavefd,STRING ,strlen (STRING )) !=strlen (STRING )) { -/* XXX: Same as write above.. */ -tst_brkm (TFAIL |TERRNO ,NULL ,"write to slave" ); - } - -if (read (masterfd, buf,strlen (STRING )) !=strlen (STRING )) { -/* XXX: Same as write above.. */ -tst_brkm (TFAIL |TERRNO ,NULL ,"read from master" ); - } -if (strncmp (STRING , buf,strlen (STRING ) -1 ) !=0 ) { -tst_brkm (TFAIL ,NULL , -"strings are different (STRING = '%s' != buf = '%s')." , -STRING , buf); - } - -/* -* try an invalid ioctl on the slave... -*/ -if (ioctl (slavefd,TIOCGWINSZ ,NULL ) ==0 ) { -tst_brkm (TFAIL ,NULL , -"invalid slave TIOCGWINSZ ioctl succeeded.. it should " -"have failed" ); - } +static size_t string_len; +static int masterfd =-1 ; +static int slavefd =-1 ; -/* -* try an invalid ioctl on the master... -*/ -if (ioctl (masterfd,TIOCGWINSZ ,NULL ) ==0 ) { -tst_brkm (TFAIL ,NULL , -"invalid master TIOCGWINSZ ioctl succeeded.. it should " -"have failed" ); - } - -/* -* close pty fds -*/ -if (close (slavefd) !=0 ) { -tst_brkm (TBROK |TERRNO ,NULL ,"close of slave" ); - } -if (close (masterfd) !=0 ) { -tst_brkm (TBROK |TERRNO ,NULL ,"close of master" ); - } -tst_resm (TPASS ,"test1" ); -/** NOTREACHED **/ -return 0 ; -} - -/* -* test slave operations with closed master -*/ -static void test2 (void ) +static void run (void ) { -int masterfd;/* master pty fd */ -int slavefd;/* slave pty fd */ -int i; -char *slavename; -char c; - - masterfd =SAFE_OPEN (NULL ,MASTERCLONE ,O_RDWR ); - - slavename =ptsname (masterfd); -if (slavename ==NULL ) { -tst_brkm (TBROK |TERRNO ,NULL ,"ptsname() call failed" ); - } - -if (grantpt (masterfd) !=0 ) { -tst_brkm (TBROK |TERRNO ,NULL ,"grantpt() call failed" ); - } - -if (unlockpt (masterfd) !=0 ) { -tst_brkm (TBROK |TERRNO ,NULL ,"unlockpt() call failed" ); - } - - slavefd =SAFE_OPEN (NULL , slavename,O_RDWR ); +char buf[BUFSIZ ]; -/* -* close pty fds. See what happens when we close the master -* first. -*/ -if (close (masterfd) !=0 ) { -tst_brkm (TBROK |TERRNO ,NULL ,"close()" ); - } +tst_res (TINFO ,"Send message to master and read it from slave" ); - errno =0 ; -if ((i =read (slavefd, &c,1 )) ==1 ) { -tst_brkm (TFAIL ,NULL , -"reading from slave fd should have failed, but didn't" -"(read '%c')" , c); - } +memset (buf,0 ,BUFSIZ ); +SAFE_WRITE (SAFE_WRITE_ALL , masterfd,STRING , string_len); +SAFE_READ (0 , slavefd, buf, string_len +1 ); +TST_EXP_EQ_STRN (STRING , buf, string_len -1 ); -if ((i =write (slavefd, &c,1 )) ==1 ) { -tst_brkm (TFAIL ,NULL , -"writing to slave fd should have failed, but didn't" ); - } +tst_res (TINFO ,"Send message to slave and read it from master" ); -if (ioctl (slavefd,TIOCGWINSZ ,NULL ) ==0 ) { -tst_brkm (TFAIL ,NULL , -"trying TIOCGWINSZ on slave fd should have failed, " -"but didn't" ); - } - -if (close (slavefd) !=0 ) { -tst_brkm (TBROK ,NULL ,"close" ); - } -tst_resm (TPASS ,"test2" ); +memset (buf,0 ,BUFSIZ ); +SAFE_WRITE (SAFE_WRITE_ALL , slavefd,STRING , string_len); +SAFE_READ (0 , masterfd, buf, string_len +1 ); +TST_EXP_EQ_STRN (STRING , buf, string_len -1 ); } -/* -* test operations on master with closed slave -*/ -static void test3 (void ) +static void setup (void ) { -int masterfd;/* master pty fd */ + masterfd =open_master (); + slavefd =open_slave (masterfd); - masterfd =SAFE_OPEN (NULL ,MASTERCLONE ,O_RDWR ); - -if (ioctl (masterfd,TIOCGWINSZ ,NULL ) ==0 ) { -tst_brkm (TFAIL |TERRNO ,NULL , -"trying TIOCGWINSZ on master with no open slave " -"succeeded unexpectedly" ); - } -tst_resm (TPASS ,"test3" ); + string_len =strlen (STRING ); } -/* -* test multiple opens on slave side of pty -*/ -static void test4 (void ) +static void cleanup (void ) { -int masterfd;/* master pty fd */ -int slavefd;/* slave pty fd */ -int slavefd2; -int slavefd3; -char *slavename; - - masterfd =SAFE_OPEN (NULL ,MASTERCLONE ,O_RDWR ); - - slavename =ptsname (masterfd); -if (slavename ==NULL ) { -tst_brkm (TBROK ,NULL ,"ptsname() call failed" ); - } - -if (grantpt (masterfd) !=0 ) { -tst_brkm (TBROK ,NULL ,"grantpt() call failed" ); - } +if (masterfd !=-1 ) +SAFE_CLOSE (masterfd); -if (unlockpt (masterfd) !=0 ) { -tst_brkm (TBROK |TERRNO ,NULL ,"unlockpt() call failed" ); - } - - slavefd =SAFE_OPEN (NULL , slavename,O_RDWR ); - - slavefd2 =open (slavename,O_RDWR ); -if (slavefd <0 ) { -tst_brkm (TFAIL |TERRNO ,NULL ,"Could not open %s (again)" , - slavename); - } - - slavefd3 =open (slavename,O_RDWR ); -if (slavefd <0 ) { -tst_brkm (TFAIL |TERRNO ,NULL ,"Could not open %s (once more)" , - slavename); - } - -/* -* close pty fds. -*/ -if (close (slavefd) !=0 ) { -tst_brkm (TBROK |TERRNO ,NULL ,"close slave" ); - } - -if (close (slavefd2) !=0 ) { -tst_brkm (TBROK ,NULL ,"close slave again" ); - } - -if (close (slavefd3) !=0 ) { -tst_brkm (TBROK ,NULL ,"close slave once more" ); - } - -if (close (masterfd) !=0 ) { -tst_brkm (TBROK ,NULL ,"close master" ); - } -tst_resm (TPASS ,"test4" ); -} - -/* -* test opening/closing lots of ptys in parallel. We may run out -* of ptys for this test depending on how the system is configured, -* but that's not a fatal error. -*/ -static void test5 (void ) -{ -int masterfd;/* master pty fd */ -char *slavename; -int status; -int i; - -for (i =0 ; i <NUMPROCS ; ++i) { -switch (fork ()) { -case -1 : -tst_brkm (TBROK ,NULL ,"fork()" ); -break ; -case 0 : - masterfd =open (MASTERCLONE ,O_RDWR ); -if (masterfd <0 ) { -printf ("proc %d: opening %s failed: %s" , - i,MASTERCLONE ,strerror (errno)); -exit (1 ); - } -if (grantpt (masterfd) !=0 ) { -printf ("proc %d: grantpt() call failed: %s" , - i,strerror (errno)); -exit (1 ); - } - slavename =ptsname (masterfd); -if (slavename ==NULL ) { -printf ("proc %d: ptsname() call failed: %s" , - i,strerror (errno)); -exit (1 ); - } -sleep (10 ); -if (close (masterfd) !=0 ) { -printf ("proc %d: close failed: %s" , - i,strerror (errno)); -exit (1 ); - } -exit (0 ); -default : -break ; - } - } -while (wait (&status) >0 ) { -if (status) { -tst_brkm (TFAIL ,NULL , -"child exited with non-zero status %d" , - status); - } - } -tst_resm (TPASS ,"test5" ); +if (slavefd !=-1 ) +SAFE_CLOSE (slavefd); } -/* -* main test driver -*/ -int main (void ) -{ -test1 (); -test2 (); -test3 (); -test4 (); -test5 (); - -/* -* all done -*/ -tst_exit (); -} +static struct tst_test test = { + .test_all = run, + .setup = setup, + .cleanup = cleanup, +};
Signature · 5 lines
2.43.0 -- Mailing list info: https://lists.linux.it/listinfo/ltp
PATCH v3 11/12 Add pty08 test
In reply to Andrea CervesatoFrom: Andrea Cervesato <[email protected]>
Verify that slave pseudo-terminal fails reading/writing if master has been closed.
--- runtest/pty | 1 + testcases/kernel/pty/.gitignore | 1 + testcases/kernel/pty/pty08.c | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 40 insertions(+)
runtest/pty +1 -0
@@ -6,6 +6,7 @@ pty04 pty04 pty05 pty05 pty06 pty06 pty07 pty07 +pty08 pty08 ptem01 ptem01 ptem02 ptem02 ptem03 ptem03
testcases/kernel/pty/.gitignore +1 -0
@@ -12,3 +12,4 @@ /pty05 /pty06 /pty07 +/pty08
testcases/kernel/pty/pty08.c new file +38 -0
@@ -0,0 +1,38 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* +* Copyright (c) International Business Machines Corp., 2002 +* Copyright (C) 2024 SUSE LLC Andrea Cervesato <[email protected]> +*/ + +/*\ +* [Description] +* +* Verify that slave pseudo-terminal fails reading/writing if master has been +* closed. +*/ + +#define _GNU_SOURCE + +#include "common.h" + +static void run (void ) +{ +int slavefd; +int masterfd; +char buf; + + masterfd =open_master (); + slavefd =open_slave (masterfd); + +tst_res (TINFO ,"Closing master communication" ); +SAFE_CLOSE (masterfd); + +TST_EXP_EQ_LI (read (slavefd, &buf,1 ),0 ); +TST_EXP_FAIL (write (slavefd, &buf,1 ),EIO ); + +SAFE_CLOSE (slavefd); +} + +static struct tst_test test = { + .test_all = run, +};
Signature · 5 lines
2.43.0 -- Mailing list info: https://lists.linux.it/listinfo/ltp
PATCH v3 12/12 Add pty09 test
In reply to Andrea CervesatoFrom: Andrea Cervesato <[email protected]>
Verify that slave pseudo-terminal can be opened multiple times in parallel.
--- runtest/pty | 1 + testcases/kernel/pty/.gitignore | 1 + testcases/kernel/pty/pty09.c | 74 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+)
runtest/pty +1 -0
@@ -7,6 +7,7 @@ pty05 pty05 pty06 pty06 pty07 pty07 pty08 pty08 +pty09 pty09 ptem01 ptem01 ptem02 ptem02 ptem03 ptem03
testcases/kernel/pty/.gitignore +1 -0
@@ -13,3 +13,4 @@ /pty06 /pty07 /pty08 +/pty09
testcases/kernel/pty/pty09.c new file +74 -0
74 lines not on this page · raw message
Signature · 5 lines
2.43.0 -- Mailing list info: https://lists.linux.it/listinfo/ltp