Module Name: src Committed By: riastradh Date: Sun Mar 16 15:35:37 UTC 2025
Modified Files: src/tests/lib/libc/gen/posix_spawn: fa_spawn_utils.c fa_spawn_utils.h t_fileactions.c t_spawn.c t_spawnattr.c Log Message: tests/lib/libc/gen/posix_spawn: Spruce up a bit. - KNF - Don't touch files outside the test working directory. - No need to pre-clean files -- atf gives us an empty tempdir. - Use h_macros.h to simplify a lot of checks. - Use names for enumerated arguments, not magic numbers. - Check some more syscalls. - Add include guard. To generate a diff of this commit: cvs rdiff -u -r1.1 -r1.2 src/tests/lib/libc/gen/posix_spawn/fa_spawn_utils.c \ src/tests/lib/libc/gen/posix_spawn/fa_spawn_utils.h cvs rdiff -u -r1.7 -r1.8 src/tests/lib/libc/gen/posix_spawn/t_fileactions.c cvs rdiff -u -r1.11 -r1.12 src/tests/lib/libc/gen/posix_spawn/t_spawn.c cvs rdiff -u -r1.6 -r1.7 src/tests/lib/libc/gen/posix_spawn/t_spawnattr.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/tests/lib/libc/gen/posix_spawn/fa_spawn_utils.c diff -u src/tests/lib/libc/gen/posix_spawn/fa_spawn_utils.c:1.1 src/tests/lib/libc/gen/posix_spawn/fa_spawn_utils.c:1.2 --- src/tests/lib/libc/gen/posix_spawn/fa_spawn_utils.c:1.1 Sun Nov 7 15:46:20 2021 +++ src/tests/lib/libc/gen/posix_spawn/fa_spawn_utils.c Sun Mar 16 15:35:36 2025 @@ -1,4 +1,4 @@ -/* $NetBSD: fa_spawn_utils.c,v 1.1 2021/11/07 15:46:20 christos Exp $ */ +/* $NetBSD: fa_spawn_utils.c,v 1.2 2025/03/16 15:35:36 riastradh Exp $ */ /*- * Copyright (c) 2012 The NetBSD Foundation, Inc. @@ -29,34 +29,34 @@ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ + #include <sys/cdefs.h> -__RCSID("$NetBSD: fa_spawn_utils.c,v 1.1 2021/11/07 15:46:20 christos Exp $"); +__RCSID("$NetBSD: fa_spawn_utils.c,v 1.2 2025/03/16 15:35:36 riastradh Exp $"); + +#include <sys/stat.h> #include <atf-c.h> -#include <stdio.h> #include <errno.h> +#include <stdio.h> #include <string.h> -#include <sys/stat.h> #include "fa_spawn_utils.h" +#include "h_macros.h" off_t -filesize(const char * restrict fname) +filesize(const char *fname) { struct stat st; - int err; - err = stat(fname, &st); - ATF_REQUIRE_MSG(err == 0, "Can't stat %s (%s)", fname, strerror(errno)); + RL(stat(fname, &st)); return st.st_size; } void -empty_outfile(const char * restrict fname) +empty_outfile(const char *fname) { FILE *f; - f = fopen(fname, "w"); - ATF_REQUIRE_MSG(f != NULL, "Can't open %s (%s)", fname, strerror(errno)); - fclose(f); + REQUIRE_LIBC(f = fopen(fname, "w"), NULL); + REQUIRE_LIBC(fclose(f), EOF); } Index: src/tests/lib/libc/gen/posix_spawn/fa_spawn_utils.h diff -u src/tests/lib/libc/gen/posix_spawn/fa_spawn_utils.h:1.1 src/tests/lib/libc/gen/posix_spawn/fa_spawn_utils.h:1.2 --- src/tests/lib/libc/gen/posix_spawn/fa_spawn_utils.h:1.1 Sun Nov 7 15:46:20 2021 +++ src/tests/lib/libc/gen/posix_spawn/fa_spawn_utils.h Sun Mar 16 15:35:36 2025 @@ -1,3 +1,5 @@ +/* $NetBSD: fa_spawn_utils.h,v 1.2 2025/03/16 15:35:36 riastradh Exp $ */ + /*- * Copyright (c) 2012 The NetBSD Foundation, Inc. * All rights reserved. @@ -27,5 +29,11 @@ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ -off_t filesize(const char * restrict); -void empty_outfile(const char * restrict); + +#ifndef FA_SPAWN_UTILS_H +#define FA_SPAWN_UTILS_H + +off_t filesize(const char *); +void empty_outfile(const char *); + +#endif Index: src/tests/lib/libc/gen/posix_spawn/t_fileactions.c diff -u src/tests/lib/libc/gen/posix_spawn/t_fileactions.c:1.7 src/tests/lib/libc/gen/posix_spawn/t_fileactions.c:1.8 --- src/tests/lib/libc/gen/posix_spawn/t_fileactions.c:1.7 Sun Nov 7 15:46:20 2021 +++ src/tests/lib/libc/gen/posix_spawn/t_fileactions.c Sun Mar 16 15:35:36 2025 @@ -1,4 +1,4 @@ -/* $NetBSD: t_fileactions.c,v 1.7 2021/11/07 15:46:20 christos Exp $ */ +/* $NetBSD: t_fileactions.c,v 1.8 2025/03/16 15:35:36 riastradh Exp $ */ /*- * Copyright (c) 2012 The NetBSD Foundation, Inc. @@ -29,34 +29,24 @@ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ -#include <sys/cdefs.h> -__RCSID("$NetBSD: t_fileactions.c,v 1.7 2021/11/07 15:46:20 christos Exp $"); - -#include <atf-c.h> +#include <sys/cdefs.h> +__RCSID("$NetBSD: t_fileactions.c,v 1.8 2025/03/16 15:35:36 riastradh Exp $"); -#include <sys/wait.h> #include <sys/stat.h> +#include <sys/wait.h> -#include <stdio.h> -#include <stdlib.h> -#include <string.h> +#include <atf-c.h> #include <errno.h> #include <fcntl.h> #include <spawn.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> #include <unistd.h> #include "fa_spawn_utils.h" - - -ATF_TC(t_spawn_openmode); - -ATF_TC_HEAD(t_spawn_openmode, tc) -{ - atf_tc_set_md_var(tc, "descr", - "Test the proper handling of 'mode' for 'open' fileactions"); - atf_tc_set_md_var(tc, "require.progs", "/bin/cat"); -} +#include "h_macros.h" #define TESTFILE "./the_input_data" #define CHECKFILE "./the_output_data" @@ -66,18 +56,24 @@ static void make_testfile(const char *restrict file) { FILE *f; - size_t written; + ssize_t written; - f = fopen(file, "w"); - ATF_REQUIRE(f != NULL); - written = fwrite(TESTCONTENT, 1, strlen(TESTCONTENT), f); - fclose(f); - ATF_REQUIRE(written == strlen(TESTCONTENT)); + REQUIRE_LIBC(f = fopen(file, "w"), NULL); + RL(written = fwrite(TESTCONTENT, 1, strlen(TESTCONTENT), f)); + REQUIRE_LIBC(fclose(f), EOF); + ATF_REQUIRE((size_t)written == strlen(TESTCONTENT)); } +ATF_TC(t_spawn_openmode); +ATF_TC_HEAD(t_spawn_openmode, tc) +{ + atf_tc_set_md_var(tc, "descr", + "Test the proper handling of 'mode' for 'open' fileactions"); + atf_tc_set_md_var(tc, "require.progs", "/bin/cat"); +} ATF_TC_BODY(t_spawn_openmode, tc) { - int status, err; + int status; pid_t pid; size_t insize, outsize; char * const args[2] = { __UNCONST("cat"), NULL }; @@ -87,27 +83,28 @@ ATF_TC_BODY(t_spawn_openmode, tc) * try a "cat < testfile > checkfile" */ make_testfile(TESTFILE); - unlink(CHECKFILE); - posix_spawn_file_actions_init(&fa); - posix_spawn_file_actions_addopen(&fa, fileno(stdin), - TESTFILE, O_RDONLY, 0); - posix_spawn_file_actions_addopen(&fa, fileno(stdout), - CHECKFILE, O_WRONLY|O_CREAT, 0600); - err = posix_spawn(&pid, "/bin/cat", &fa, NULL, args, NULL); - posix_spawn_file_actions_destroy(&fa); - - ATF_REQUIRE(err == 0); + RZ(posix_spawn_file_actions_init(&fa)); + RZ(posix_spawn_file_actions_addopen(&fa, fileno(stdin), + TESTFILE, O_RDONLY, 0)); + RZ(posix_spawn_file_actions_addopen(&fa, fileno(stdout), + CHECKFILE, O_WRONLY|O_CREAT, 0600)); + RZ(posix_spawn(&pid, "/bin/cat", &fa, NULL, args, NULL)); + RZ(posix_spawn_file_actions_destroy(&fa)); /* ok, wait for the child to finish */ - waitpid(pid, &status, 0); - ATF_REQUIRE(WIFEXITED(status) && WEXITSTATUS(status) == EXIT_SUCCESS); + RL(waitpid(pid, &status, 0)); + ATF_REQUIRE_MSG((WIFEXITED(status) && + WEXITSTATUS(status) == EXIT_SUCCESS), + "status=0x%x", status); /* now check that input and output have the same size */ insize = filesize(TESTFILE); outsize = filesize(CHECKFILE); - ATF_REQUIRE(insize == strlen(TESTCONTENT)); - ATF_REQUIRE(insize == outsize); + ATF_CHECK_MSG(insize == strlen(TESTCONTENT), + "insize=%zu strlen(TESTCONTENT)=%zu", insize, strlen(TESTCONTENT)); + ATF_CHECK_MSG(insize == outsize, + "insize=%zu outsize=%zu", insize, outsize); /* * try a "cat < testfile >> checkfile" @@ -115,25 +112,27 @@ ATF_TC_BODY(t_spawn_openmode, tc) make_testfile(TESTFILE); make_testfile(CHECKFILE); - posix_spawn_file_actions_init(&fa); - posix_spawn_file_actions_addopen(&fa, fileno(stdin), - TESTFILE, O_RDONLY, 0); - posix_spawn_file_actions_addopen(&fa, fileno(stdout), - CHECKFILE, O_WRONLY|O_APPEND, 0); - err = posix_spawn(&pid, "/bin/cat", &fa, NULL, args, NULL); - posix_spawn_file_actions_destroy(&fa); - - ATF_REQUIRE(err == 0); + RZ(posix_spawn_file_actions_init(&fa)); + RZ(posix_spawn_file_actions_addopen(&fa, fileno(stdin), + TESTFILE, O_RDONLY, 0)); + RZ(posix_spawn_file_actions_addopen(&fa, fileno(stdout), + CHECKFILE, O_WRONLY|O_APPEND, 0)); + RZ(posix_spawn(&pid, "/bin/cat", &fa, NULL, args, NULL)); + RZ(posix_spawn_file_actions_destroy(&fa)); /* ok, wait for the child to finish */ - waitpid(pid, &status, 0); - ATF_REQUIRE(WIFEXITED(status) && WEXITSTATUS(status) == EXIT_SUCCESS); + RL(waitpid(pid, &status, 0)); + ATF_REQUIRE_MSG((WIFEXITED(status) && + WEXITSTATUS(status) == EXIT_SUCCESS), + "status=0x%x", status); /* now check that output is twice as long as input */ insize = filesize(TESTFILE); outsize = filesize(CHECKFILE); - ATF_REQUIRE(insize == strlen(TESTCONTENT)); - ATF_REQUIRE(insize*2 == outsize); + ATF_CHECK_MSG(insize == strlen(TESTCONTENT), + "insize=%zu strlen(TESTCONTENT)=%zu", insize, strlen(TESTCONTENT)); + ATF_CHECK_MSG(insize*2 == outsize, + "insize*2=%zu outsize=%zu", insize*2, outsize); /* * try a "cat < testfile > checkfile" with input and output swapped @@ -141,39 +140,39 @@ ATF_TC_BODY(t_spawn_openmode, tc) make_testfile(TESTFILE); empty_outfile(CHECKFILE); - posix_spawn_file_actions_init(&fa); - posix_spawn_file_actions_addopen(&fa, fileno(stdout), - TESTFILE, O_RDONLY, 0); - posix_spawn_file_actions_addopen(&fa, fileno(stdin), - CHECKFILE, O_WRONLY, 0); - err = posix_spawn(&pid, "/bin/cat", &fa, NULL, args, NULL); - posix_spawn_file_actions_destroy(&fa); - - ATF_REQUIRE(err == 0); + RZ(posix_spawn_file_actions_init(&fa)); + RZ(posix_spawn_file_actions_addopen(&fa, fileno(stdout), + TESTFILE, O_RDONLY, 0)); + RZ(posix_spawn_file_actions_addopen(&fa, fileno(stdin), + CHECKFILE, O_WRONLY, 0)); + RZ(posix_spawn(&pid, "/bin/cat", &fa, NULL, args, NULL)); + RZ(posix_spawn_file_actions_destroy(&fa)); /* ok, wait for the child to finish */ - waitpid(pid, &status, 0); - ATF_REQUIRE(WIFEXITED(status) && WEXITSTATUS(status) == EXIT_FAILURE); + RL(waitpid(pid, &status, 0)); + ATF_REQUIRE_MSG((WIFEXITED(status) && + WEXITSTATUS(status) == EXIT_FAILURE), + "status=0x%x", status); /* now check that input and output are still the same size */ insize = filesize(TESTFILE); outsize = filesize(CHECKFILE); - ATF_REQUIRE(insize == strlen(TESTCONTENT)); - ATF_REQUIRE(outsize == 0); + ATF_CHECK_MSG(insize == strlen(TESTCONTENT), + "insize=%zu strlen(TESTCONTENT)=%zu", insize, strlen(TESTCONTENT)); + ATF_CHECK_MSG(outsize == 0, + "outsize=%zu", outsize); } ATF_TC(t_spawn_reopen); - ATF_TC_HEAD(t_spawn_reopen, tc) { atf_tc_set_md_var(tc, "descr", "an open filehandle can be replaced by a 'open' fileaction"); atf_tc_set_md_var(tc, "require.progs", "/bin/cat"); } - ATF_TC_BODY(t_spawn_reopen, tc) { - int status, err; + int status; pid_t pid; char * const args[2] = { __UNCONST("cat"), NULL }; posix_spawn_file_actions_t fa; @@ -181,31 +180,29 @@ ATF_TC_BODY(t_spawn_reopen, tc) /* * make sure stdin is open in the parent */ - freopen("/dev/zero", "r", stdin); + REQUIRE_LIBC(freopen("/dev/zero", "r", stdin), NULL); /* * now request an open for this fd again in the child */ - posix_spawn_file_actions_init(&fa); - posix_spawn_file_actions_addopen(&fa, fileno(stdin), - "/dev/null", O_RDONLY, 0); - err = posix_spawn(&pid, "/bin/cat", &fa, NULL, args, NULL); - posix_spawn_file_actions_destroy(&fa); - - ATF_REQUIRE(err == 0); - - waitpid(pid, &status, 0); - ATF_REQUIRE(WIFEXITED(status) && WEXITSTATUS(status) == EXIT_SUCCESS); + RZ(posix_spawn_file_actions_init(&fa)); + RZ(posix_spawn_file_actions_addopen(&fa, fileno(stdin), + "/dev/null", O_RDONLY, 0)); + RZ(posix_spawn(&pid, "/bin/cat", &fa, NULL, args, NULL)); + RZ(posix_spawn_file_actions_destroy(&fa)); + + RL(waitpid(pid, &status, 0)); + ATF_REQUIRE_MSG((WIFEXITED(status) && + WEXITSTATUS(status) == EXIT_SUCCESS), + "status=0x%x", status); } ATF_TC(t_spawn_open_nonexistent); - ATF_TC_HEAD(t_spawn_open_nonexistent, tc) { atf_tc_set_md_var(tc, "descr", "posix_spawn fails when a file to open does not exist"); atf_tc_set_md_var(tc, "require.progs", "/bin/cat"); } - ATF_TC_BODY(t_spawn_open_nonexistent, tc) { int err, status; @@ -213,9 +210,9 @@ ATF_TC_BODY(t_spawn_open_nonexistent, tc char * const args[2] = { __UNCONST("cat"), NULL }; posix_spawn_file_actions_t fa; - posix_spawn_file_actions_init(&fa); - posix_spawn_file_actions_addopen(&fa, STDIN_FILENO, - "./non/ex/ist/ent", O_RDONLY, 0); + RZ(posix_spawn_file_actions_init(&fa)); + RZ(posix_spawn_file_actions_addopen(&fa, STDIN_FILENO, + "./non/ex/ist/ent", O_RDONLY, 0)); err = posix_spawn(&pid, "/bin/cat", &fa, NULL, args, NULL); if (err == 0) { /* @@ -229,13 +226,13 @@ ATF_TC_BODY(t_spawn_open_nonexistent, tc * The error has been noticed early enough, no child has * been run */ - ATF_REQUIRE(err == ENOENT); + ATF_REQUIRE_MSG(err == ENOENT, "err=%d (%s)", + err, strerror(err)); } - posix_spawn_file_actions_destroy(&fa); + RZ(posix_spawn_file_actions_destroy(&fa)); } ATF_TC(t_spawn_open_nonexistent_diag); - ATF_TC_HEAD(t_spawn_open_nonexistent_diag, tc) { atf_tc_set_md_var(tc, "descr", @@ -243,7 +240,6 @@ ATF_TC_HEAD(t_spawn_open_nonexistent_dia "and delivers proper diagnostic"); atf_tc_set_md_var(tc, "require.progs", "/bin/cat"); } - ATF_TC_BODY(t_spawn_open_nonexistent_diag, tc) { int err; @@ -252,7 +248,7 @@ ATF_TC_BODY(t_spawn_open_nonexistent_dia posix_spawnattr_t attr; posix_spawn_file_actions_t fa; - posix_spawnattr_init(&attr); + RZ(posix_spawnattr_init(&attr)); /* * POSIX_SPAWN_RETURNERROR is a NetBSD specific flag that * will cause a "proper" return value from posix_spawn(2) @@ -260,72 +256,68 @@ ATF_TC_BODY(t_spawn_open_nonexistent_dia * status from the child process (c.f. the non-diag variant * of this test). */ - posix_spawnattr_setflags(&attr, POSIX_SPAWN_RETURNERROR); - posix_spawn_file_actions_init(&fa); - posix_spawn_file_actions_addopen(&fa, STDIN_FILENO, - "./non/ex/ist/ent", O_RDONLY, 0); + RZ(posix_spawnattr_setflags(&attr, POSIX_SPAWN_RETURNERROR)); + RZ(posix_spawn_file_actions_init(&fa)); + RZ(posix_spawn_file_actions_addopen(&fa, STDIN_FILENO, + "./non/ex/ist/ent", O_RDONLY, 0)); err = posix_spawn(&pid, "/bin/cat", &fa, &attr, args, NULL); - ATF_REQUIRE(err == ENOENT); - posix_spawn_file_actions_destroy(&fa); - posix_spawnattr_destroy(&attr); + ATF_REQUIRE_MSG(err == ENOENT, "err=%d (%s)", err, strerror(err)); + RZ(posix_spawn_file_actions_destroy(&fa)); + RZ(posix_spawnattr_destroy(&attr)); } ATF_TC(t_spawn_fileactions); - ATF_TC_HEAD(t_spawn_fileactions, tc) { atf_tc_set_md_var(tc, "descr", "Tests various complex fileactions"); } - ATF_TC_BODY(t_spawn_fileactions, tc) { - int fd1, fd2, fd3, status, err; + int fd1, fd2, fd3, status; pid_t pid; char * const args[2] = { __UNCONST("h_fileactions"), NULL }; char helper[FILENAME_MAX]; posix_spawn_file_actions_t fa; - posix_spawn_file_actions_init(&fa); + RZ(posix_spawn_file_actions_init(&fa)); - closefrom(fileno(stderr)+1); + RL(closefrom(fileno(stderr) + 1)); - fd1 = open("/dev/null", O_RDONLY); + RL(fd1 = open("/dev/null", O_RDONLY)); ATF_REQUIRE(fd1 == 3); - fd2 = open("/dev/null", O_WRONLY, O_CLOEXEC); + RL(fd2 = open("/dev/null", O_WRONLY, O_CLOEXEC)); ATF_REQUIRE(fd2 == 4); - fd3 = open("/dev/null", O_WRONLY); + RL(fd3 = open("/dev/null", O_WRONLY)); ATF_REQUIRE(fd3 == 5); - posix_spawn_file_actions_addclose(&fa, fd1); - posix_spawn_file_actions_addopen(&fa, 6, "/dev/null", O_RDWR, 0); - posix_spawn_file_actions_adddup2(&fa, 1, 7); + RZ(posix_spawn_file_actions_addclose(&fa, fd1)); + RZ(posix_spawn_file_actions_addopen(&fa, 6, "/dev/null", O_RDWR, 0)); + RZ(posix_spawn_file_actions_adddup2(&fa, 1, 7)); snprintf(helper, sizeof helper, "%s/h_fileactions", atf_tc_get_config_var(tc, "srcdir")); - err = posix_spawn(&pid, helper, &fa, NULL, args, NULL); - posix_spawn_file_actions_destroy(&fa); + RZ(posix_spawn(&pid, helper, &fa, NULL, args, NULL)); + RZ(posix_spawn_file_actions_destroy(&fa)); - ATF_REQUIRE(err == 0); - - waitpid(pid, &status, 0); - ATF_REQUIRE(WIFEXITED(status) && WEXITSTATUS(status) == EXIT_SUCCESS); + RL(waitpid(pid, &status, 0)); + ATF_REQUIRE_MSG((WIFEXITED(status) && + WEXITSTATUS(status) == EXIT_SUCCESS), + "status=0x%x", status); } ATF_TC(t_spawn_empty_fileactions); - ATF_TC_HEAD(t_spawn_empty_fileactions, tc) { atf_tc_set_md_var(tc, "descr", "posix_spawn with empty fileactions (PR kern/46038)"); atf_tc_set_md_var(tc, "require.progs", "/bin/cat"); } - ATF_TC_BODY(t_spawn_empty_fileactions, tc) { - int status, err; + int status; pid_t pid; char * const args[2] = { __UNCONST("cat"), NULL }; posix_spawn_file_actions_t fa; @@ -336,30 +328,32 @@ ATF_TC_BODY(t_spawn_empty_fileactions, t * already in the parent and pass empty file actions to the child. */ make_testfile(TESTFILE); - unlink(CHECKFILE); - freopen(TESTFILE, "r", stdin); - freopen(CHECKFILE, "w", stdout); + REQUIRE_LIBC(freopen(TESTFILE, "r", stdin), NULL); + REQUIRE_LIBC(freopen(CHECKFILE, "w", stdout), NULL); - posix_spawn_file_actions_init(&fa); - err = posix_spawn(&pid, "/bin/cat", &fa, NULL, args, NULL); - posix_spawn_file_actions_destroy(&fa); - - ATF_REQUIRE(err == 0); + RZ(posix_spawn_file_actions_init(&fa)); + RZ(posix_spawn(&pid, "/bin/cat", &fa, NULL, args, NULL)); + RZ(posix_spawn_file_actions_destroy(&fa)); /* ok, wait for the child to finish */ - waitpid(pid, &status, 0); - ATF_REQUIRE(WIFEXITED(status) && WEXITSTATUS(status) == EXIT_SUCCESS); + RL(waitpid(pid, &status, 0)); + ATF_REQUIRE_MSG((WIFEXITED(status) && + WEXITSTATUS(status) == EXIT_SUCCESS), + "status=0x%x", status); /* now check that input and output have the same size */ insize = filesize(TESTFILE); outsize = filesize(CHECKFILE); - ATF_REQUIRE(insize == strlen(TESTCONTENT)); - ATF_REQUIRE(insize == outsize); + ATF_CHECK_MSG(insize == strlen(TESTCONTENT), + "insize=%zu strlen(TESTCONTENT)=%zu", insize, strlen(TESTCONTENT)); + ATF_CHECK_MSG(insize == outsize, + "insize=%zu outsize=%zu", insize, outsize); } ATF_TP_ADD_TCS(tp) { + ATF_TP_ADD_TC(tp, t_spawn_fileactions); ATF_TP_ADD_TC(tp, t_spawn_open_nonexistent); ATF_TP_ADD_TC(tp, t_spawn_open_nonexistent_diag); Index: src/tests/lib/libc/gen/posix_spawn/t_spawn.c diff -u src/tests/lib/libc/gen/posix_spawn/t_spawn.c:1.11 src/tests/lib/libc/gen/posix_spawn/t_spawn.c:1.12 --- src/tests/lib/libc/gen/posix_spawn/t_spawn.c:1.11 Sat Mar 15 12:09:41 2025 +++ src/tests/lib/libc/gen/posix_spawn/t_spawn.c Sun Mar 16 15:35:36 2025 @@ -1,4 +1,4 @@ -/* $NetBSD: t_spawn.c,v 1.11 2025/03/15 12:09:41 riastradh Exp $ */ +/* $NetBSD: t_spawn.c,v 1.12 2025/03/16 15:35:36 riastradh Exp $ */ /*- * Copyright (c) 2012, 2021 The NetBSD Foundation, Inc. @@ -31,21 +31,19 @@ */ #include <sys/cdefs.h> -__RCSID("$NetBSD: t_spawn.c,v 1.11 2025/03/15 12:09:41 riastradh Exp $"); - -#include <atf-c.h> +__RCSID("$NetBSD: t_spawn.c,v 1.12 2025/03/16 15:35:36 riastradh Exp $"); #include <sys/fcntl.h> #include <sys/stat.h> #include <sys/types.h> #include <sys/wait.h> +#include <atf-c.h> #include <errno.h> #include <fcntl.h> #include <limits.h> #include <signal.h> #include <spawn.h> -#include <stdarg.h> #include <stdio.h> #include <stdlib.h> #include <string.h> @@ -55,50 +53,42 @@ __RCSID("$NetBSD: t_spawn.c,v 1.11 2025/ #include "fa_spawn_utils.h" #include "h_macros.h" -static void check_success(const char *, int, ...); +static void check_success(const char *, const char *); ATF_TC(t_spawn_ls); - ATF_TC_HEAD(t_spawn_ls, tc) { atf_tc_set_md_var(tc, "descr", "Tests a simple posix_spawn executing /bin/ls"); } - ATF_TC_BODY(t_spawn_ls, tc) { char * const args[] = { __UNCONST("ls"), __UNCONST("-la"), NULL }; - int err; - err = posix_spawn(NULL, "/bin/ls", NULL, NULL, args, NULL); - ATF_REQUIRE(err == 0); + RZ(posix_spawn(NULL, "/bin/ls", NULL, NULL, args, NULL)); } ATF_TC(t_spawnp_ls); - ATF_TC_HEAD(t_spawnp_ls, tc) { atf_tc_set_md_var(tc, "descr", "Tests a simple posix_spawnp executing ls via $PATH"); } - ATF_TC_BODY(t_spawnp_ls, tc) { char * const args[] = { __UNCONST("ls"), __UNCONST("-la"), NULL }; - int err; - err = posix_spawnp(NULL, "ls", NULL, NULL, args, NULL); - ATF_REQUIRE(err == 0); + RZ(posix_spawnp(NULL, "ls", NULL, NULL, args, NULL)); } static void spawn_error(const atf_tc_t *tc, const char *name, int error) { - char buf[FILENAME_MAX]; + char buf[PATH_MAX]; char * const args[] = { __UNCONST(name), NULL }; int err; - snprintf(buf, sizeof buf, "%s/%s", + snprintf(buf, sizeof(buf), "%s/%s", atf_tc_get_config_var(tc, "srcdir"), name); err = posix_spawn(NULL, buf, NULL, NULL, args, NULL); ATF_REQUIRE_MSG(err == error, "expected error %d, " @@ -106,46 +96,39 @@ spawn_error(const atf_tc_t *tc, const ch } ATF_TC(t_spawn_zero); - ATF_TC_HEAD(t_spawn_zero, tc) { atf_tc_set_md_var(tc, "descr", "posix_spawn an invalid binary"); } - ATF_TC_BODY(t_spawn_zero, tc) { spawn_error(tc, "h_zero", ENOEXEC); } ATF_TC(t_spawn_missing); - ATF_TC_HEAD(t_spawn_missing, tc) { atf_tc_set_md_var(tc, "descr", - "posix_spawn a non existant binary"); + "posix_spawn a nonexistent binary"); } - ATF_TC_BODY(t_spawn_missing, tc) { spawn_error(tc, "h_nonexist", ENOENT); } ATF_TC(t_spawn_nonexec); - ATF_TC_HEAD(t_spawn_nonexec, tc) { atf_tc_set_md_var(tc, "descr", "posix_spawn a script with non existing interpreter"); } - ATF_TC_BODY(t_spawn_nonexec, tc) { spawn_error(tc, "h_nonexec", ENOENT); } ATF_TC(t_spawn_child); - ATF_TC_HEAD(t_spawn_child, tc) { atf_tc_set_md_var(tc, "descr", @@ -154,92 +137,75 @@ ATF_TC_HEAD(t_spawn_child, tc) ATF_TC_BODY(t_spawn_child, tc) { - char buf[FILENAME_MAX]; + char buf[PATH_MAX]; char rv[2] = { '0', '\0' }; char * const args0[] = { __UNCONST("h_spawn"), rv, NULL }; int rets[] = { 0, 1, 7 }; - int err, status; + int status; pid_t pid; - snprintf(buf, sizeof buf, "%s/h_spawn", + snprintf(buf, sizeof(buf), "%s/h_spawn", atf_tc_get_config_var(tc, "srcdir")); for (size_t i = 0; i < __arraycount(rets); i++) { rv[0] = rets[i] + '0'; - err = posix_spawn(&pid, buf, NULL, NULL, args0, NULL); - ATF_REQUIRE(err == 0); - ATF_REQUIRE(pid > 0); - waitpid(pid, &status, 0); - ATF_REQUIRE(WIFEXITED(status) && - WEXITSTATUS(status) == rets[i]); + RZ(posix_spawn(&pid, buf, NULL, NULL, args0, NULL)); + ATF_REQUIRE_MSG(pid > 0, "pid=%lld", (long long)pid); + RL(waitpid(pid, &status, 0)); + ATF_REQUIRE_MSG((WIFEXITED(status) && + WEXITSTATUS(status) == rets[i]), + "status=0x%x", status); } } -#define CHDIRPATH "/tmp" #define FILENAME "output" -#define FILEPATH "/tmp/output" -#define CHDIR 1 -#define FCHDIR 2 +enum chdirop { + OP_CHDIR = 1, + OP_FCHDIR = 2, +}; static void -check_success(const char *file, int argc, ...) +check_success(const char *dir, const char *file) { - va_list ap; - ssize_t bytesRead; + ssize_t bytes_read; int fd; - size_t sizeOfFile = (size_t)filesize(file); - size_t sizeOfStr; + size_t sizeof_file = (size_t)filesize(file); + size_t sizeof_str; char *contents; - const char *dir; - contents = malloc(sizeOfFile); - ATF_REQUIRE(contents != NULL); + REQUIRE_LIBC(contents = malloc(sizeof_file), NULL); - /* - * for now only 1 variadic argument expected - * only from t_spawn_[f]chdir_rel - */ - if (argc != 0) { - va_start(ap, argc); - dir = va_arg(ap, char *); - ATF_REQUIRE(dir != NULL); - va_end(ap); - } else - dir = CHDIRPATH; - - fd = open(file, O_RDONLY); - ATF_REQUIRE_MSG(fd != -1, "Can't open `%s' (%s)", file, - strerror(errno)); + RL(fd = open(file, O_RDONLY)); /* * file contains form feed i.e ASCII - 10 at the end. - * Therefore sizeOfFile - 1 + * Therefore sizeof_file - 1 */ - sizeOfStr = strlen(dir); - ATF_CHECK_MSG(sizeOfStr == sizeOfFile - 1, "%zu (%s) != %zu (%s)", - sizeOfStr, dir, sizeOfFile - 1, file); + sizeof_str = strlen(dir); + ATF_CHECK_MSG(sizeof_str == sizeof_file - 1, "%zu (%s) != %zu (%s)", + sizeof_str, dir, sizeof_file - 1, file); - bytesRead = read(fd, contents, sizeOfFile - 1); - contents[sizeOfFile - 1] = '\0'; + bytes_read = read(fd, contents, sizeof_file - 1); + contents[sizeof_file - 1] = '\0'; ATF_REQUIRE_MSG(strcmp(dir, contents) == 0, - "[%s] != [%s] Directories dont match", dir, contents); + "[%s] != [%s] Directories don't match", dir, contents); - fd = close(fd); - ATF_REQUIRE(fd == 0); + RL(close(fd)); - unlink(file); + RL(unlink(file)); free(contents); /* XXX not really required */ - ATF_REQUIRE((size_t)bytesRead == sizeOfStr); + ATF_REQUIRE_MSG((size_t)bytes_read == sizeof_str, + "bytes_read=%zu sizeof_str=%zu", bytes_read, sizeof_str); } static void -spawn_chdir(const char *dirpath, const char *filepath, int operation, +spawn_chdir(const char *dirpath, const char *filepath, enum chdirop operation, int expected_error) { - int error, fd=-1, status; + int error, fd = -1, status; char * const args[2] = { __UNCONST("pwd"), NULL }; pid_t pid; posix_spawnattr_t attr, *attr_p; @@ -248,35 +214,29 @@ spawn_chdir(const char *dirpath, const c if (filepath) empty_outfile(filepath); - error = posix_spawn_file_actions_init(&fa); - ATF_REQUIRE(error == 0); + RZ(posix_spawn_file_actions_init(&fa)); - switch(operation) { - case CHDIR: - error = posix_spawn_file_actions_addchdir(&fa, dirpath); + switch (operation) { + case OP_CHDIR: + RZ(posix_spawn_file_actions_addchdir(&fa, dirpath)); break; - case FCHDIR: - fd = open(dirpath, O_RDONLY); - ATF_REQUIRE(fd != -1); - - error = posix_spawn_file_actions_addfchdir(&fa, fd); + case OP_FCHDIR: + RL(fd = open(dirpath, O_RDONLY)); + RZ(posix_spawn_file_actions_addfchdir(&fa, fd)); break; } - ATF_REQUIRE(error == 0); /* * if POSIX_SPAWN_RETURNERROR is expected, then no need to open the * file */ if (expected_error == 0) { - error = posix_spawn_file_actions_addopen(&fa, STDOUT_FILENO, - FILENAME, O_WRONLY, 0); - ATF_REQUIRE(error == 0); + RZ(posix_spawn_file_actions_addopen(&fa, STDOUT_FILENO, + FILENAME, O_WRONLY, 0)); attr_p = NULL; } else { - error = posix_spawnattr_init(&attr); - ATF_REQUIRE(error == 0); + RZ(posix_spawnattr_init(&attr)); /* * POSIX_SPAWN_RETURNERROR is a NetBSD specific flag that @@ -285,52 +245,56 @@ spawn_chdir(const char *dirpath, const c * status from the child process (c.f. the non-diag variant * of this test). */ - error = posix_spawnattr_setflags(&attr, - POSIX_SPAWN_RETURNERROR); - ATF_REQUIRE(error == 0); + RZ(posix_spawnattr_setflags(&attr, POSIX_SPAWN_RETURNERROR)); attr_p = &attr; } error = posix_spawn(&pid, "/bin/pwd", &fa, attr_p, args, NULL); - ATF_REQUIRE(error == expected_error); + ATF_REQUIRE_MSG(error == expected_error, "error=%d expected_error=%d", + error, expected_error); /* wait for the child to finish only when no spawnattr */ if (attr_p) { - posix_spawnattr_destroy(&attr); + RZ(posix_spawnattr_destroy(&attr)); } else { - waitpid(pid, &status, 0); - ATF_REQUIRE_MSG(WIFEXITED(status) && - WEXITSTATUS(status) == EXIT_SUCCESS, - "%s", "[f]chdir failed"); + RL(waitpid(pid, &status, 0)); + ATF_REQUIRE_MSG((WIFEXITED(status) && + WEXITSTATUS(status) == EXIT_SUCCESS), + "[f]chdir failed"); } - posix_spawn_file_actions_destroy(&fa); + RZ(posix_spawn_file_actions_destroy(&fa)); /* * The file incase of fchdir(), * should be closed before reopening in 'check_success' */ if (fd != -1) { - error = close(fd); - ATF_REQUIRE(error == 0); + RL(close(fd)); } } ATF_TC(t_spawn_chdir_abs); - ATF_TC_HEAD(t_spawn_chdir_abs, tc) { atf_tc_set_md_var(tc, "descr", "Test posix_spawn_fa_addchdir for absolute path"); atf_tc_set_md_var(tc, "require.progs", "/bin/pwd"); } - ATF_TC_BODY(t_spawn_chdir_abs, tc) { - spawn_chdir(CHDIRPATH, FILEPATH, 1, 0); + char chdirpath[PATH_MAX], filepath[PATH_MAX]; + + REQUIRE_LIBC(getcwd(chdirpath, sizeof(chdirpath)), NULL); + RL(chdir("/")); + if (snprintf(filepath, sizeof(filepath), "%s/%s", chdirpath, FILENAME) + >= (int)sizeof(filepath)) + atf_tc_fail("too deep: %s", chdirpath); + + spawn_chdir(chdirpath, filepath, OP_CHDIR, 0); /* finally cross check the output of "pwd" directory */ - check_success(FILEPATH, 0); + check_success(chdirpath, filepath); } ATF_TC(t_spawn_chdir_rel); @@ -342,30 +306,15 @@ ATF_TC_HEAD(t_spawn_chdir_rel, tc) atf_tc_set_md_var(tc, "require.progs", "/bin/pwd"); } - ATF_TC_BODY(t_spawn_chdir_rel, tc) { - int error; const char *relative_dir = "ch-dir"; const char *testdir = getcwd(NULL, 0); char *chdirwd, *filepath; - /* cleanup previous */ - error = asprintf(&filepath, "%s/%s", relative_dir, FILENAME); - ATF_CHECK(error != -1); - unlink(filepath); - free(filepath); - - rmdir(relative_dir); - error = mkdir(relative_dir, 0777); - ATF_REQUIRE_MSG(error == 0, "mkdir `%s' (%s)", relative_dir, - strerror(errno)); - - error = asprintf(&chdirwd, "%s/%s", testdir, relative_dir); - ATF_CHECK(error != -1); - - error = asprintf(&filepath, "%s/%s", chdirwd, FILENAME); - ATF_CHECK(error != -1); + RL(mkdir(relative_dir, 0777)); + RL(asprintf(&chdirwd, "%s/%s", testdir, relative_dir)); + RL(asprintf(&filepath, "%s/%s", chdirwd, FILENAME)); #ifdef DEBUG printf("cwd: %s\n", testdir); @@ -376,44 +325,43 @@ ATF_TC_BODY(t_spawn_chdir_rel, tc) spawn_chdir(relative_dir, filepath, 1, 0); /* finally cross check the directory */ - check_success(filepath, 1, chdirwd); + check_success(chdirwd, filepath); free(chdirwd); free(filepath); } ATF_TC(t_spawn_chdir_file); - ATF_TC_HEAD(t_spawn_chdir_file, tc) { atf_tc_set_md_var(tc, "descr", "Test posix_spawn_fa_addchdir on plain file (not a directory)"); atf_tc_set_md_var(tc, "require.progs", "/bin/pwd"); } - ATF_TC_BODY(t_spawn_chdir_file, tc) { - spawn_chdir(FILEPATH, FILEPATH, 1, ENOTDIR); + char cwd[PATH_MAX], filepath[PATH_MAX]; + + REQUIRE_LIBC(getcwd(cwd, sizeof(cwd)), NULL); + if (snprintf(filepath, sizeof(filepath), "%s/%s", cwd, FILENAME) + >= (int)sizeof(filepath)) + atf_tc_fail("too deep: %s", cwd); - unlink(FILEPATH); + spawn_chdir(filepath, filepath, 1, ENOTDIR); } ATF_TC(t_spawn_chdir_invalid); - ATF_TC_HEAD(t_spawn_chdir_invalid, tc) { atf_tc_set_md_var(tc, "descr", "Test posix_spawn_fa_addchdir for an invalid dir"); atf_tc_set_md_var(tc, "require.progs", "/bin/pwd"); } - ATF_TC_BODY(t_spawn_chdir_invalid, tc) { spawn_chdir("/not/a/valid/dir", NULL, 1, ENOENT); - } ATF_TC(t_spawn_chdir_permissions); - ATF_TC_HEAD(t_spawn_chdir_permissions, tc) { atf_tc_set_md_var(tc, "descr", @@ -421,38 +369,38 @@ ATF_TC_HEAD(t_spawn_chdir_permissions, t atf_tc_set_md_var(tc, "require.progs", "/bin/pwd"); atf_tc_set_md_var(tc, "require.user", "unprivileged"); } - ATF_TC_BODY(t_spawn_chdir_permissions, tc) { - int error; - const char *restrRelDir = "prohibited"; + const char *restricted_dir = "prohibited"; - rmdir(restrRelDir); - error = mkdir(restrRelDir, 0055); - ATF_REQUIRE(error == 0); + RL(mkdir(restricted_dir, 0055)); - spawn_chdir(restrRelDir, NULL, 1, EACCES); + spawn_chdir(restricted_dir, NULL, 1, EACCES); } - ATF_TC(t_spawn_fchdir_abs); - ATF_TC_HEAD(t_spawn_fchdir_abs, tc) { atf_tc_set_md_var(tc, "descr", "Test posix_spawn_fa_fchdir"); atf_tc_set_md_var(tc, "require.progs", "/bin/pwd"); } - ATF_TC_BODY(t_spawn_fchdir_abs, tc) { - spawn_chdir(CHDIRPATH, FILEPATH, 2, 0); + char chdirpath[PATH_MAX], filepath[PATH_MAX]; + + REQUIRE_LIBC(getcwd(chdirpath, sizeof(chdirpath)), NULL); + RL(chdir("/")); + if (snprintf(filepath, sizeof(filepath), "%s/%s", chdirpath, FILENAME) + >= (int)sizeof(filepath)) + atf_tc_fail("too deep: %s", chdirpath); + + spawn_chdir(chdirpath, filepath, OP_FCHDIR, 0); /* finally cross check the directory */ - check_success(FILEPATH, 0); + check_success(chdirpath, filepath); } ATF_TC(t_spawn_fchdir_rel); - ATF_TC_HEAD(t_spawn_fchdir_rel, tc) { atf_tc_set_md_var(tc, "descr", @@ -460,39 +408,31 @@ ATF_TC_HEAD(t_spawn_fchdir_rel, tc) "directory"); atf_tc_set_md_var(tc, "require.progs", "/bin/pwd"); } - ATF_TC_BODY(t_spawn_fchdir_rel, tc) { - int error; const char *relative_dir = "ch-dir"; const char *testdir = getcwd(NULL, 0); char *chdirwd, *filepath; - rmdir(relative_dir); - error = mkdir(relative_dir, 0755); - ATF_REQUIRE(error == 0); + RL(mkdir(relative_dir, 0755)); /* * This is done in parts purposely. * It enables the abs path of the relative dir * to be passed to 'check_success()' for comparing */ - error = asprintf(&chdirwd, "%s/%s", testdir, relative_dir); - ATF_CHECK(error != -1); - - error = asprintf(&filepath, "%s/%s", chdirwd, FILENAME); - ATF_CHECK(error != -1); + RL(asprintf(&chdirwd, "%s/%s", testdir, relative_dir)); + RL(asprintf(&filepath, "%s/%s", chdirwd, FILENAME)); spawn_chdir(relative_dir, filepath, 2, 0); /* finally cross check the directory */ - check_success(filepath, 1, chdirwd); + check_success(chdirwd, filepath); free(chdirwd); free(filepath); } ATF_TC(t_spawn_fchdir_file); - ATF_TC_HEAD(t_spawn_fchdir_file, tc) { atf_tc_set_md_var(tc, "descr", @@ -500,26 +440,22 @@ ATF_TC_HEAD(t_spawn_fchdir_file, tc) "regular file (not a directory)"); atf_tc_set_md_var(tc, "require.progs", "/bin/pwd"); } - ATF_TC_BODY(t_spawn_fchdir_file, tc) { - int error, fd; - - fd = open(FILEPATH, O_WRONLY | O_CREAT | O_TRUNC, 0644); - ATF_REQUIRE_MSG(fd != -1, "Can't open `%s' (%s)", FILEPATH, - strerror(errno)); - - error = close(fd); - ATF_REQUIRE(error == 0); - - spawn_chdir(FILEPATH, NULL, 2, ENOTDIR); + char cwd[PATH_MAX], filepath[PATH_MAX]; + int fd; - unlink(FILEPATH); + REQUIRE_LIBC(getcwd(cwd, sizeof(cwd)), NULL); + if (snprintf(filepath, sizeof(filepath), "%s/%s", cwd, FILENAME) + >= (int)sizeof(filepath)) + atf_tc_fail("too deep: %s", cwd); + RL(fd = open(FILENAME, O_WRONLY | O_CREAT | O_TRUNC, 0644)); + RL(close(fd)); + spawn_chdir(filepath, NULL, 2, ENOTDIR); } ATF_TC(t_spawn_fchdir_neg_fd); - ATF_TC_HEAD(t_spawn_fchdir_neg_fd, tc) { atf_tc_set_md_var(tc, "descr", @@ -527,7 +463,6 @@ ATF_TC_HEAD(t_spawn_fchdir_neg_fd, tc) "descriptor"); atf_tc_set_md_var(tc, "require.progs", "/bin/pwd"); } - ATF_TC_BODY(t_spawn_fchdir_neg_fd, tc) { int error, fd; @@ -535,24 +470,21 @@ ATF_TC_BODY(t_spawn_fchdir_neg_fd, tc) fd = -1; - error = posix_spawn_file_actions_init(&fa); - ATF_REQUIRE(error == 0); + RZ(posix_spawn_file_actions_init(&fa)); error = posix_spawn_file_actions_addfchdir(&fa, fd); - ATF_REQUIRE(error == EBADF); + ATF_REQUIRE_MSG(error == EBADF, "error=%d", error); - posix_spawn_file_actions_destroy(&fa); + RZ(posix_spawn_file_actions_destroy(&fa)); } ATF_TC(t_spawn_fchdir_closed); - ATF_TC_HEAD(t_spawn_fchdir_closed, tc) { atf_tc_set_md_var(tc, "descr", "Testing posix_spawn_file_actions_addfchdir for a closed fd"); atf_tc_set_md_var(tc, "require.progs", "/bin/pwd"); } - ATF_TC_BODY(t_spawn_fchdir_closed, tc) { int error, fd; @@ -562,8 +494,7 @@ ATF_TC_BODY(t_spawn_fchdir_closed, tc) posix_spawn_file_actions_t fa; fd = 3; - error = posix_spawnattr_init(&attr); - ATF_CHECK(error == 0); + RZ(posix_spawnattr_init(&attr)); /* * POSIX_SPAWN_RETURNERROR is a NetBSD specific flag that * will cause a "proper" return value from posix_spawn(2) @@ -571,20 +502,15 @@ ATF_TC_BODY(t_spawn_fchdir_closed, tc) * status from the child process (c.f. the non-diag variant * of this test). */ - error = posix_spawnattr_setflags(&attr, POSIX_SPAWN_RETURNERROR); - ATF_REQUIRE(error == 0); - - error = posix_spawn_file_actions_init(&fa); - ATF_REQUIRE(error == 0); - - error = posix_spawn_file_actions_addfchdir(&fa, fd); - ATF_REQUIRE(error == 0); + RZ(posix_spawnattr_setflags(&attr, POSIX_SPAWN_RETURNERROR)); + RZ(posix_spawn_file_actions_init(&fa)); + RZ(posix_spawn_file_actions_addfchdir(&fa, fd)); error = posix_spawn(&pid, "/bin/pwd", &fa, &attr, args, NULL); - ATF_REQUIRE(error == EBADF); + ATF_REQUIRE_MSG(error == EBADF, "error=%d", error); - posix_spawn_file_actions_destroy(&fa); - posix_spawnattr_destroy(&attr); + RZ(posix_spawn_file_actions_destroy(&fa)); + RZ(posix_spawnattr_destroy(&attr)); } ATF_TC(t_spawn_sig); @@ -628,12 +554,6 @@ ATF_TC_BODY(t_spawn_sig, tc) } } -#undef CHDIR -#undef FCHDIR -#undef CHDIRPATH -#undef FILENAME -#undef FILEPATH - ATF_TP_ADD_TCS(tp) { ATF_TP_ADD_TC(tp, t_spawn_ls); Index: src/tests/lib/libc/gen/posix_spawn/t_spawnattr.c diff -u src/tests/lib/libc/gen/posix_spawn/t_spawnattr.c:1.6 src/tests/lib/libc/gen/posix_spawn/t_spawnattr.c:1.7 --- src/tests/lib/libc/gen/posix_spawn/t_spawnattr.c:1.6 Mon May 23 21:46:12 2022 +++ src/tests/lib/libc/gen/posix_spawn/t_spawnattr.c Sun Mar 16 15:35:36 2025 @@ -1,4 +1,4 @@ -/* $NetBSD: t_spawnattr.c,v 1.6 2022/05/23 21:46:12 andvar Exp $ */ +/* $NetBSD: t_spawnattr.c,v 1.7 2025/03/16 15:35:36 riastradh Exp $ */ /*- * Copyright (c) 2012 The NetBSD Foundation, Inc. @@ -29,32 +29,39 @@ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ + #include <sys/cdefs.h> -__RCSID("$NetBSD: t_spawnattr.c,v 1.6 2022/05/23 21:46:12 andvar Exp $"); +__RCSID("$NetBSD: t_spawnattr.c,v 1.7 2025/03/16 15:35:36 riastradh Exp $"); #include <sys/param.h> +#include <sys/wait.h> + #include <atf-c.h> -#include <stdio.h> -#include <stdlib.h> -#include <string.h> #include <errno.h> #include <fcntl.h> #include <sched.h> #include <signal.h> #include <spawn.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> #include <unistd.h> -#include <sys/wait.h> + +#include "h_macros.h" static int get_different_scheduler(void) { + int sched; + /* * We don't want to use SCHED_OTHER because it does not have * different priorities. */ /* get current schedule policy */ - switch (sched_getscheduler(0)) { + RL(sched = sched_getscheduler(0)); + switch (sched) { case SCHED_RR: return SCHED_FIFO; case SCHED_FIFO: @@ -72,17 +79,17 @@ get_different_priority(int scheduler) struct sched_param param; /* Get the priority range for the new scheduler */ - max = sched_get_priority_max(scheduler); - min = sched_get_priority_min(scheduler); + RL(max = sched_get_priority_max(scheduler)); + RL(min = sched_get_priority_min(scheduler)); - sched_getparam(0, ¶m); + RL(sched_getparam(0, ¶m)); priority = param.sched_priority; - + /* new schedule policy */ for (new = min; new <= max; new++) if (priority != new) break; - + ATF_REQUIRE_MSG(priority != new, "could not find different priority"); printf("min %d max %d for scheduler %d, returning %d\n", min, max, scheduler, new); @@ -90,17 +97,15 @@ get_different_priority(int scheduler) } ATF_TC(t_spawnattr); - ATF_TC_HEAD(t_spawnattr, tc) { atf_tc_set_md_var(tc, "require.user", "root"); atf_tc_set_md_var(tc, "descr", "Tests posix_spawn with scheduler attributes"); } - ATF_TC_BODY(t_spawnattr, tc) { - int pid, scheduler, child_scheduler, priority, status, err, pfd[2]; + int pid, scheduler, child_scheduler, priority, status, pfd[2]; char helper_arg[128]; char * const args[] = { __UNCONST("h_spawnattr"), helper_arg, NULL }; struct sched_param sp, child_sp; @@ -111,41 +116,40 @@ ATF_TC_BODY(t_spawnattr, tc) /* * create a pipe to control the child */ - err = pipe(pfd); - ATF_REQUIRE_MSG(err == 0, "could not create pipe, errno %d", errno); - sprintf(helper_arg, "%d", pfd[0]); + RL(pipe(pfd)); + snprintf(helper_arg, sizeof(helper_arg), "%d", pfd[0]); - posix_spawnattr_init(&attr); + RZ(posix_spawnattr_init(&attr)); scheduler = get_different_scheduler(); priority = get_different_priority(scheduler); sp.sched_priority = priority; printf("using scheduler %d, priority %d\n", scheduler, priority); - - sigemptyset(&sig); - sigaddset(&sig, SIGUSR1); - posix_spawnattr_setflags(&attr, POSIX_SPAWN_SETSCHEDULER | + RL(sigemptyset(&sig)); + RL(sigaddset(&sig, SIGUSR1)); + + RZ(posix_spawnattr_setflags(&attr, POSIX_SPAWN_SETSCHEDULER | POSIX_SPAWN_SETSCHEDPARAM | POSIX_SPAWN_SETPGROUP | POSIX_SPAWN_SETSIGMASK | POSIX_SPAWN_SETSIGDEF | - POSIX_SPAWN_SETSIGDEF); - posix_spawnattr_setpgroup(&attr, 0); - posix_spawnattr_setschedparam(&attr, &sp); - posix_spawnattr_setschedpolicy(&attr, scheduler); - posix_spawnattr_setsigmask(&attr, &sig); - posix_spawnattr_setsigdefault(&attr, &sig); + POSIX_SPAWN_SETSIGDEF)); + RZ(posix_spawnattr_setpgroup(&attr, 0)); + RZ(posix_spawnattr_setschedparam(&attr, &sp)); + RZ(posix_spawnattr_setschedpolicy(&attr, scheduler)); + RZ(posix_spawnattr_setsigmask(&attr, &sig)); + RZ(posix_spawnattr_setsigdefault(&attr, &sig)); - sprintf(helper, "%s/h_spawnattr", + snprintf(helper, sizeof(helper), "%s/h_spawnattr", atf_tc_get_config_var(tc, "srcdir")); - err = posix_spawn(&pid, helper, NULL, &attr, args, NULL); - ATF_REQUIRE_MSG(err == 0, "error %d", err); + RZ(posix_spawn(&pid, helper, NULL, &attr, args, NULL)); + ATF_REQUIRE_MSG(pid > 0, "pid=%lld", (long long)pid); - child_scheduler = sched_getscheduler(pid); + RL(child_scheduler = sched_getscheduler(pid)); ATF_REQUIRE_MSG(scheduler == child_scheduler, "scheduler = %d, child_scheduler = %d, pid %d, errno %d", scheduler, child_scheduler, pid, errno); - sched_getparam(pid, &child_sp); + RL(sched_getparam(pid, &child_sp)); ATF_REQUIRE_MSG(child_sp.sched_priority == sp.sched_priority, "priority is: %d, but we requested: %d", child_sp.sched_priority, sp.sched_priority); @@ -154,15 +158,15 @@ ATF_TC_BODY(t_spawnattr, tc) pid, getpgid(pid)); /* ready, let child go */ - write(pfd[1], "q", 1); - close(pfd[0]); - close(pfd[1]); + RL(write(pfd[1], "q", 1)); + RL(close(pfd[0])); + RL(close(pfd[1])); /* wait and check result from child */ - waitpid(pid, &status, 0); + RL(waitpid(pid, &status, 0)); ATF_REQUIRE(WIFEXITED(status) && WEXITSTATUS(status) == EXIT_SUCCESS); - posix_spawnattr_destroy(&attr); + RZ(posix_spawnattr_destroy(&attr)); } ATF_TC(t_spawn_resetids); @@ -180,22 +184,22 @@ ATF_TC_BODY(t_spawn_resetids, tc) __UNCONST("h_spawn"), __UNCONST("--resetids"), NULL }; posix_spawnattr_t attr; - int err, status; + int status; pid_t pid; - posix_spawnattr_init(&attr); - posix_spawnattr_setflags(&attr, POSIX_SPAWN_RESETIDS); + RZ(posix_spawnattr_init(&attr)); + RZ(posix_spawnattr_setflags(&attr, POSIX_SPAWN_RESETIDS)); - snprintf(buf, sizeof buf, "%s/h_spawn", + snprintf(buf, sizeof(buf), "%s/h_spawn", atf_tc_get_config_var(tc, "srcdir")); - err = posix_spawn(&pid, buf, NULL, &attr, args, NULL); - ATF_REQUIRE(err == 0); - ATF_REQUIRE(pid > 0); - waitpid(pid, &status, 0); - ATF_REQUIRE(WIFEXITED(status) && WEXITSTATUS(status) == 0); + RZ(posix_spawn(&pid, buf, NULL, &attr, args, NULL)); + ATF_REQUIRE_MSG(pid > 0, "pid=%lld", (long long)pid); + RL(waitpid(pid, &status, 0)); + ATF_REQUIRE_MSG(WIFEXITED(status) && WEXITSTATUS(status) == 0, + "status=0x%x", status); - posix_spawnattr_destroy(&attr); + RZ(posix_spawnattr_destroy(&attr)); } ATF_TP_ADD_TCS(tp)