CVS commit: src/tests/lib/libc/gen/posix_spawn
Module Name:src Committed By: martin Date: Sun May 2 11:18:11 UTC 2021 Modified Files: src/tests/lib/libc/gen/posix_spawn: h_spawn.c t_spawnattr.c Log Message: Add test for POSIX_SPAWN_RESETIDS flag To generate a diff of this commit: cvs rdiff -u -r1.1 -r1.2 src/tests/lib/libc/gen/posix_spawn/h_spawn.c cvs rdiff -u -r1.3 -r1.4 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/h_spawn.c diff -u src/tests/lib/libc/gen/posix_spawn/h_spawn.c:1.1 src/tests/lib/libc/gen/posix_spawn/h_spawn.c:1.2 --- src/tests/lib/libc/gen/posix_spawn/h_spawn.c:1.1 Mon Feb 13 21:03:08 2012 +++ src/tests/lib/libc/gen/posix_spawn/h_spawn.c Sun May 2 11:18:11 2021 @@ -1,4 +1,4 @@ -/* $NetBSD: h_spawn.c,v 1.1 2012/02/13 21:03:08 martin Exp $ */ +/* $NetBSD: h_spawn.c,v 1.2 2021/05/02 11:18:11 martin Exp $ */ /*- * Copyright (c) 2012 The NetBSD Foundation, Inc. @@ -32,6 +32,8 @@ #include #include +#include +#include int main(int argc, char **argv) @@ -39,11 +41,25 @@ main(int argc, char **argv) unsigned long ret; char *endp; - if (argc < 2) { + if (argc == 2 && strcmp(argv[1], "--resetids") == 0) { + if (getuid() != geteuid() || getgid() != getegid()) { + fprintf(stderr, "uid/gid do not match effective ids, " + "uid: %d euid: %d gid: %d egid: %d\n", + getuid(), geteuid(), getgid(), getegid()); + exit(255); + } + return 0; + } else if (argc != 2) { fprintf(stderr, "usage:\n\t%s (retcode)\n", getprogname()); exit(255); } ret = strtoul(argv[1], &endp, 10); + if (*endp != 0) { + fprintf(stderr, + "invalid arg: %s\n" + "usage:\n\t%s (retcode)\n", endp, getprogname()); + exit(255); + } fprintf(stderr, "%s exiting with status %lu\n", getprogname(), ret); return ret; Index: src/tests/lib/libc/gen/posix_spawn/t_spawnattr.c diff -u src/tests/lib/libc/gen/posix_spawn/t_spawnattr.c:1.3 src/tests/lib/libc/gen/posix_spawn/t_spawnattr.c:1.4 --- src/tests/lib/libc/gen/posix_spawn/t_spawnattr.c:1.3 Thu Dec 21 03:31:43 2017 +++ src/tests/lib/libc/gen/posix_spawn/t_spawnattr.c Sun May 2 11:18:11 2021 @@ -1,4 +1,4 @@ -/* $NetBSD: t_spawnattr.c,v 1.3 2017/12/21 03:31:43 christos Exp $ */ +/* $NetBSD: t_spawnattr.c,v 1.4 2021/05/02 11:18:11 martin Exp $ */ /*- * Copyright (c) 2012 The NetBSD Foundation, Inc. @@ -165,9 +165,43 @@ ATF_TC_BODY(t_spawnattr, tc) posix_spawnattr_destroy(&attr); } +ATF_TC(t_spawn_resetids); + +ATF_TC_HEAD(t_spawn_resetids, tc) +{ + atf_tc_set_md_var(tc, "descr", + "posix_spawn a child and with POSIX_SPAWN_RESETIDS flag"); +} + +ATF_TC_BODY(t_spawn_resetids, tc) +{ + char buf[FILENAME_MAX]; + char * const args[] = { + __UNCONST("h_spawn"), __UNCONST("--resetids"), NULL + }; + posix_spawnattr_t attr; + int err, status; + pid_t pid; + + posix_spawnattr_init(&attr); + posix_spawnattr_setflags(&attr, POSIX_SPAWN_RESETIDS); + + 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); + + posix_spawnattr_destroy(&attr); +} + ATF_TP_ADD_TCS(tp) { ATF_TP_ADD_TC(tp, t_spawnattr); + ATF_TP_ADD_TC(tp, t_spawn_resetids); return atf_no_error(); }
CVS commit: src/tests/lib/libc/gen/posix_spawn
Module Name:src Committed By: christos Date: Thu Dec 21 03:31:43 UTC 2017 Modified Files: src/tests/lib/libc/gen/posix_spawn: t_spawnattr.c Log Message: Fix broken test: we can't assume that the current schedule priority range will overlap with the requested scheduler range, so get the new scheduler range, and then try to find a different priority. If that fails (to find a different scheduling range), give up here. To generate a diff of this commit: cvs rdiff -u -r1.2 -r1.3 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/t_spawnattr.c diff -u src/tests/lib/libc/gen/posix_spawn/t_spawnattr.c:1.2 src/tests/lib/libc/gen/posix_spawn/t_spawnattr.c:1.3 --- src/tests/lib/libc/gen/posix_spawn/t_spawnattr.c:1.2 Mon Dec 18 08:18:23 2017 +++ src/tests/lib/libc/gen/posix_spawn/t_spawnattr.c Wed Dec 20 22:31:43 2017 @@ -1,4 +1,4 @@ -/* $NetBSD: t_spawnattr.c,v 1.2 2017/12/18 13:18:23 christos Exp $ */ +/* $NetBSD: t_spawnattr.c,v 1.3 2017/12/21 03:31:43 christos Exp $ */ /*- * Copyright (c) 2012 The NetBSD Foundation, Inc. @@ -45,11 +45,8 @@ #define MAX(a, b) (a) > (b) ? (a) : (b) #define MIN(a, b) (a) > (b) ? (b) : (a) -static int get_different_scheduler(void); -static int get_different_priority(void); - static int -get_different_scheduler() +get_different_scheduler(void) { /* * We don't want to use SCHED_OTHER because it does not have @@ -69,14 +66,12 @@ get_different_scheduler() } static int -get_different_priority() +get_different_priority(int scheduler) { - int scheduler, max, min, new, priority; + int min, max, new, priority; struct sched_param param; - /* get current schedule policy */ - scheduler = sched_getscheduler(0); - + /* Get the priority range for the new scheduler */ max = sched_get_priority_max(scheduler); min = sched_get_priority_min(scheduler); @@ -84,10 +79,13 @@ get_different_priority() priority = param.sched_priority; /* new schedule policy */ - new = (priority + 1); - if (new > max) - new = min; + 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); return new; } @@ -120,8 +118,9 @@ ATF_TC_BODY(t_spawnattr, tc) posix_spawnattr_init(&attr); scheduler = get_different_scheduler(); - priority = get_different_priority(); + priority = get_different_priority(scheduler); sp.sched_priority = priority; + printf("using scheduler %d, priority %d\n", scheduler, priority); sigemptyset(&sig); sigaddset(&sig, SIGUSR1);
CVS commit: src/tests/lib/libc/gen/posix_spawn
Module Name:src Committed By: christos Date: Mon Dec 18 13:18:23 UTC 2017 Modified Files: src/tests/lib/libc/gen/posix_spawn: t_spawnattr.c Log Message: Don't use SCHED_OTHER. To generate a diff of this commit: cvs rdiff -u -r1.1 -r1.2 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/t_spawnattr.c diff -u src/tests/lib/libc/gen/posix_spawn/t_spawnattr.c:1.1 src/tests/lib/libc/gen/posix_spawn/t_spawnattr.c:1.2 --- src/tests/lib/libc/gen/posix_spawn/t_spawnattr.c:1.1 Mon Feb 13 16:03:08 2012 +++ src/tests/lib/libc/gen/posix_spawn/t_spawnattr.c Mon Dec 18 08:18:23 2017 @@ -1,4 +1,4 @@ -/* $NetBSD: t_spawnattr.c,v 1.1 2012/02/13 21:03:08 martin Exp $ */ +/* $NetBSD: t_spawnattr.c,v 1.2 2017/12/18 13:18:23 christos Exp $ */ /*- * Copyright (c) 2012 The NetBSD Foundation, Inc. @@ -51,20 +51,21 @@ static int get_different_priority(void); static int get_different_scheduler() { - int scheduler, max, min, new; - - max = MAX(MAX(SCHED_FIFO, SCHED_OTHER), SCHED_RR); - min = MIN(MIN(SCHED_FIFO, SCHED_OTHER), SCHED_RR); + /* + * We don't want to use SCHED_OTHER because it does not have + * different priorities. + */ /* get current schedule policy */ - scheduler = sched_getscheduler(0); - - /* new scheduler */ - new = (scheduler + 1); - if (new > max) - new = min; - - return new; + switch (sched_getscheduler(0)) { + case SCHED_RR: + return SCHED_FIFO; + case SCHED_FIFO: + case SCHED_OTHER: + return SCHED_RR; + default: + abort(); + } } static int
CVS commit: src/tests/lib/libc/gen/posix_spawn
Module Name:src Committed By: martin Date: Mon Feb 20 12:22:40 UTC 2012 Modified Files: src/tests/lib/libc/gen/posix_spawn: t_fileactions.c Log Message: Add a test case to call posix_spawn with empty file actions, which reproduced the (now fixed) PR kern/46038. To generate a diff of this commit: cvs rdiff -u -r1.2 -r1.3 src/tests/lib/libc/gen/posix_spawn/t_fileactions.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/t_fileactions.c diff -u src/tests/lib/libc/gen/posix_spawn/t_fileactions.c:1.2 src/tests/lib/libc/gen/posix_spawn/t_fileactions.c:1.3 --- src/tests/lib/libc/gen/posix_spawn/t_fileactions.c:1.2 Tue Feb 14 00:13:54 2012 +++ src/tests/lib/libc/gen/posix_spawn/t_fileactions.c Mon Feb 20 12:22:40 2012 @@ -1,4 +1,4 @@ -/* $NetBSD: t_fileactions.c,v 1.2 2012/02/14 00:13:54 martin Exp $ */ +/* $NetBSD: t_fileactions.c,v 1.3 2012/02/20 12:22:40 martin Exp $ */ /*- * Copyright (c) 2012 The NetBSD Foundation, Inc. @@ -279,12 +279,57 @@ ATF_TC_BODY(t_spawn_fileactions, tc) ATF_REQUIRE(WIFEXITED(status) && WEXITSTATUS(status) == EXIT_SUCCESS); } +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; + pid_t pid; + char * const args[2] = { __UNCONST("cat"), NULL }; + posix_spawn_file_actions_t fa; + size_t insize, outsize; + + /* + * try a "cat < testfile > checkfile", but set up stdin/stdout + * 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); + + 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); + + /* ok, wait for the child to finish */ + waitpid(pid, &status, 0); + ATF_REQUIRE(WIFEXITED(status) && WEXITSTATUS(status) == EXIT_SUCCESS); + + /* 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_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_reopen); ATF_TP_ADD_TC(tp, t_spawn_openmode); + ATF_TP_ADD_TC(tp, t_spawn_empty_fileactions); return atf_no_error(); }
CVS commit: src/tests/lib/libc/gen/posix_spawn
Module Name:src Committed By: martin Date: Tue Feb 14 00:13:54 UTC 2012 Modified Files: src/tests/lib/libc/gen/posix_spawn: Makefile t_fileactions.c Log Message: Add a few more posix_spawn testcases, which should detect everything that was wrong in the initial version, causing the failure reported in PR kern/45991. To generate a diff of this commit: cvs rdiff -u -r1.1 -r1.2 src/tests/lib/libc/gen/posix_spawn/Makefile \ src/tests/lib/libc/gen/posix_spawn/t_fileactions.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/Makefile diff -u src/tests/lib/libc/gen/posix_spawn/Makefile:1.1 src/tests/lib/libc/gen/posix_spawn/Makefile:1.2 --- src/tests/lib/libc/gen/posix_spawn/Makefile:1.1 Mon Feb 13 21:03:08 2012 +++ src/tests/lib/libc/gen/posix_spawn/Makefile Tue Feb 14 00:13:54 2012 @@ -1,6 +1,7 @@ -# $NetBSD: Makefile,v 1.1 2012/02/13 21:03:08 martin Exp $ +# $NetBSD: Makefile,v 1.2 2012/02/14 00:13:54 martin Exp $ NOMAN= # defined +WARNS=4 .include Index: src/tests/lib/libc/gen/posix_spawn/t_fileactions.c diff -u src/tests/lib/libc/gen/posix_spawn/t_fileactions.c:1.1 src/tests/lib/libc/gen/posix_spawn/t_fileactions.c:1.2 --- src/tests/lib/libc/gen/posix_spawn/t_fileactions.c:1.1 Mon Feb 13 21:03:08 2012 +++ src/tests/lib/libc/gen/posix_spawn/t_fileactions.c Tue Feb 14 00:13:54 2012 @@ -1,4 +1,4 @@ -/* $NetBSD: t_fileactions.c,v 1.1 2012/02/13 21:03:08 martin Exp $ */ +/* $NetBSD: t_fileactions.c,v 1.2 2012/02/14 00:13:54 martin Exp $ */ /*- * Copyright (c) 2012 The NetBSD Foundation, Inc. @@ -35,17 +35,212 @@ #include #include #include +#include #include #include #include #include + +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"); +} + +static off_t +filesize(const char * restrict fname) +{ + struct stat st; + int err; + + err = stat(fname, &st); + ATF_REQUIRE(err == 0); + return st.st_size; +} + +#define TESTFILE "./the_input_data" +#define CHECKFILE "./the_output_data" +#define TESTCONTENT "marry has a little lamb" + +static void +make_testfile(const char *restrict file) +{ + FILE *f; + size_t written; + + f = fopen(file, "w"); + ATF_REQUIRE(f != NULL); + written = fwrite(TESTCONTENT, 1, strlen(TESTCONTENT), f); + fclose(f); + ATF_REQUIRE(written == strlen(TESTCONTENT)); +} + +static void +empty_outfile(const char *restrict filename) +{ + FILE *f; + + f = fopen(filename, "w"); + ATF_REQUIRE(f != NULL); + fclose(f); +} + +ATF_TC_BODY(t_spawn_openmode, tc) +{ + int status, err; + pid_t pid; + size_t insize, outsize; + char * const args[2] = { __UNCONST("cat"), NULL }; + posix_spawn_file_actions_t fa; + + /* + * 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); + + /* ok, wait for the child to finish */ + waitpid(pid, &status, 0); + ATF_REQUIRE(WIFEXITED(status) && WEXITSTATUS(status) == EXIT_SUCCESS); + + /* 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); + + /* + * try a "cat < testfile >> checkfile" + */ + 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); + + /* ok, wait for the child to finish */ + waitpid(pid, &status, 0); + ATF_REQUIRE(WIFEXITED(status) && WEXITSTATUS(status) == EXIT_SUCCESS); + + /* 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); + + /* + * try a "cat < testfile > checkfile" with input and output swapped + */ + 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); + + /*