diff options
author | ngie <ngie@FreeBSD.org> | 2014-10-02 23:26:49 +0000 |
---|---|---|
committer | ngie <ngie@FreeBSD.org> | 2014-10-02 23:26:49 +0000 |
commit | 3f09b8d0af642c2aeb96a4d667cefb7fe3bce443 (patch) | |
tree | 544932e2a2c5a5a202b752beefba0b3e327b3858 /contrib/netbsd-tests/lib/libc/gen/posix_spawn | |
parent | b941fec92da62b0eab650295f4e8a381dbbc04b4 (diff) | |
parent | e1f2d32c0e0678782c353c48364cddedfae58b0a (diff) | |
download | FreeBSD-src-3f09b8d0af642c2aeb96a4d667cefb7fe3bce443.zip FreeBSD-src-3f09b8d0af642c2aeb96a4d667cefb7fe3bce443.tar.gz |
Import the NetBSD test suite from ^/vendor/NetBSD/tests/09.30.2014_20.45 ,
minus the vendor Makefiles
Provide directions for how to bootstrap the vendor sources in
FREEBSD-upgrade
MFC after 2 weeks
Discussed with: rpaulo
Sponsored by: EMC / Isilon Storage Division
Diffstat (limited to 'contrib/netbsd-tests/lib/libc/gen/posix_spawn')
7 files changed, 989 insertions, 0 deletions
diff --git a/contrib/netbsd-tests/lib/libc/gen/posix_spawn/h_fileactions.c b/contrib/netbsd-tests/lib/libc/gen/posix_spawn/h_fileactions.c new file mode 100644 index 0000000..d923370 --- /dev/null +++ b/contrib/netbsd-tests/lib/libc/gen/posix_spawn/h_fileactions.c @@ -0,0 +1,104 @@ +/* $NetBSD: h_fileactions.c,v 1.1 2012/02/13 21:03:08 martin Exp $ */ + +/*- + * Copyright (c) 2012 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Charles Zhang <charles@NetBSD.org> and + * Martin Husemann <martin@NetBSD.org>. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <errno.h> +#include <sys/stat.h> + +#define BUFSIZE 16 + +/* + * This checks (hardcoded) the assumptions that are setup from the + * main test program via posix spawn file actions. + * Program exits with EXIT_SUCCESS or EXIT_FAILURE accordingly + * (and does some stderr diagnostics in case of errors). + */ +int +main(int argc, char **argv) +{ + int res = EXIT_SUCCESS; + char buf[BUFSIZE]; + struct stat sb0, sb1; + + strcpy(buf, "test..."); + /* file desc 3 should be closed via addclose */ + if (read(3, buf, BUFSIZE) != -1 || errno != EBADF) { + fprintf(stderr, "%s: filedesc 3 is not closed\n", + getprogname()); + res = EXIT_FAILURE; + } + /* file desc 4 should be closed via closeonexec */ + if (read(4, buf, BUFSIZE) != -1 || errno != EBADF) { + fprintf(stderr, "%s: filedesc 4 is not closed\n", + getprogname()); + res = EXIT_FAILURE; + } + /* file desc 5 remains open */ + if (write(5, buf, BUFSIZE) <= 0) { + fprintf(stderr, "%s: could not write to filedesc 5\n", + getprogname()); + res = EXIT_FAILURE; + } + /* file desc 6 should be open (via addopen) */ + if (write(6, buf, BUFSIZE) <= 0) { + fprintf(stderr, "%s: could not write to filedesc 6\n", + getprogname()); + res = EXIT_FAILURE; + } + /* file desc 7 should refer to stdout */ + fflush(stdout); + if (fstat(fileno(stdout), &sb0) != 0) { + fprintf(stderr, "%s: could not fstat stdout\n", + getprogname()); + res = EXIT_FAILURE; + } + if (fstat(7, &sb1) != 0) { + fprintf(stderr, "%s: could not fstat filedesc 7\n", + getprogname()); + res = EXIT_FAILURE; + } + if (write(7, buf, strlen(buf)) <= 0) { + fprintf(stderr, "%s: could not write to filedesc 7\n", + getprogname()); + res = EXIT_FAILURE; + } + if (memcmp(&sb0, &sb1, sizeof sb0) != 0) { + fprintf(stderr, "%s: stat results differ\n", getprogname()); + res = EXIT_FAILURE; + } + + return res; +} + diff --git a/contrib/netbsd-tests/lib/libc/gen/posix_spawn/h_nonexec.sh b/contrib/netbsd-tests/lib/libc/gen/posix_spawn/h_nonexec.sh new file mode 100755 index 0000000..deee6fe --- /dev/null +++ b/contrib/netbsd-tests/lib/libc/gen/posix_spawn/h_nonexec.sh @@ -0,0 +1,3 @@ +#! /nonexistent + +# this is just a dummy script, trying to be non-executable diff --git a/contrib/netbsd-tests/lib/libc/gen/posix_spawn/h_spawn.c b/contrib/netbsd-tests/lib/libc/gen/posix_spawn/h_spawn.c new file mode 100644 index 0000000..dbf5da5 --- /dev/null +++ b/contrib/netbsd-tests/lib/libc/gen/posix_spawn/h_spawn.c @@ -0,0 +1,50 @@ +/* $NetBSD: h_spawn.c,v 1.1 2012/02/13 21:03:08 martin Exp $ */ + +/*- + * Copyright (c) 2012 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Charles Zhang <charles@NetBSD.org> and + * Martin Husemann <martin@NetBSD.org>. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include <stdio.h> +#include <stdlib.h> + +int +main(int argc, char **argv) +{ + unsigned long ret; + char *endp; + + if (argc < 2) { + fprintf(stderr, "usage:\n\t%s (retcode)\n", getprogname()); + exit(255); + } + ret = strtoul(argv[1], &endp, 10); + + fprintf(stderr, "%s exiting with status %lu\n", getprogname(), ret); + return ret; +} diff --git a/contrib/netbsd-tests/lib/libc/gen/posix_spawn/h_spawnattr.c b/contrib/netbsd-tests/lib/libc/gen/posix_spawn/h_spawnattr.c new file mode 100644 index 0000000..1f13c54 --- /dev/null +++ b/contrib/netbsd-tests/lib/libc/gen/posix_spawn/h_spawnattr.c @@ -0,0 +1,90 @@ +/* $NetBSD: h_spawnattr.c,v 1.1 2012/02/13 21:03:08 martin Exp $ */ + +/*- + * Copyright (c) 2012 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Charles Zhang <charles@NetBSD.org> and + * Martin Husemann <martin@NetBSD.org>. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include <errno.h> +#include <stdio.h> +#include <stdlib.h> +#include <signal.h> +#include <unistd.h> + +/* + * Helper to test the hardcoded assumptions from t_spawnattr.c + * Exit with apropriate exit status and print diagnostics to + * stderr explaining what is wrong. + */ +int +main(int argc, char **argv) +{ + int parent_pipe, res = EXIT_SUCCESS; + sigset_t sig; + struct sigaction act; + ssize_t rd; + char tmp; + + sigemptyset(&sig); + if (sigprocmask(0, NULL, &sig) < 0) { + fprintf(stderr, "%s: sigprocmask error\n", getprogname()); + res = EXIT_FAILURE; + } + if (!sigismember(&sig, SIGUSR1)) { + fprintf(stderr, "%s: SIGUSR not in procmask\n", getprogname()); + res = EXIT_FAILURE; + } + if (sigaction(SIGUSR1, NULL, &act) < 0) { + fprintf(stderr, "%s: sigaction error\n", getprogname()); + res = EXIT_FAILURE; + } + if (act.sa_sigaction != (void *)SIG_DFL) { + fprintf(stderr, "%s: SIGUSR1 action != SIG_DFL\n", + getprogname()); + res = EXIT_FAILURE; + } + + if (argc >= 2) { + parent_pipe = atoi(argv[1]); + if (parent_pipe > 2) { + printf("%s: waiting for command from parent on pipe " + "%d\n", getprogname(), parent_pipe); + rd = read(parent_pipe, &tmp, 1); + if (rd == 1) { + printf("%s: got command %c from parent\n", + getprogname(), tmp); + } else if (rd == -1) { + printf("%s: %d is no pipe, errno %d\n", + getprogname(), parent_pipe, errno); + res = EXIT_FAILURE; + } + } + } + + return res; +} diff --git a/contrib/netbsd-tests/lib/libc/gen/posix_spawn/t_fileactions.c b/contrib/netbsd-tests/lib/libc/gen/posix_spawn/t_fileactions.c new file mode 100644 index 0000000..1cf6433 --- /dev/null +++ b/contrib/netbsd-tests/lib/libc/gen/posix_spawn/t_fileactions.c @@ -0,0 +1,385 @@ +/* $NetBSD: t_fileactions.c,v 1.5 2012/04/09 19:42:07 martin Exp $ */ + +/*- + * Copyright (c) 2012 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Charles Zhang <charles@NetBSD.org> and + * Martin Husemann <martin@NetBSD.org>. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + + +#include <atf-c.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <errno.h> +#include <fcntl.h> +#include <spawn.h> +#include <unistd.h> +#include <sys/wait.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"); +} + +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); + + /* ok, wait for the child to finish */ + waitpid(pid, &status, 0); + ATF_REQUIRE(WIFEXITED(status) && WEXITSTATUS(status) == EXIT_FAILURE); + + /* 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_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; + pid_t pid; + char * const args[2] = { __UNCONST("cat"), NULL }; + posix_spawn_file_actions_t fa; + + /* + * make sure stdin is open in the parent + */ + freopen("/dev/zero", "r", stdin); + /* + * 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); +} + +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; + pid_t pid; + 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); + err = posix_spawn(&pid, "/bin/cat", &fa, NULL, args, NULL); + if (err == 0) { + /* + * The child has been created - it should fail and + * return exit code 127 + */ + waitpid(pid, &status, 0); + ATF_REQUIRE(WIFEXITED(status) && WEXITSTATUS(status) == 127); + } else { + /* + * The error has been noticed early enough, no child has + * been run + */ + ATF_REQUIRE(err == ENOENT); + } + 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", + "posix_spawn fails when a file to open does not exist " + "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; + pid_t pid; + char * const args[2] = { __UNCONST("cat"), NULL }; + posix_spawnattr_t attr; + posix_spawn_file_actions_t fa; + + posix_spawnattr_init(&attr); + /* + * POSIX_SPAWN_RETURNERROR is a NetBSD specific flag that + * will cause a "proper" return value from posix_spawn(2) + * instead of a (potential) success there and a 127 exit + * 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); + 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_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; + 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); + + closefrom(fileno(stderr)+1); + + fd1 = open("/dev/null", O_RDONLY); + ATF_REQUIRE(fd1 == 3); + + fd2 = open("/dev/null", O_WRONLY, O_CLOEXEC); + ATF_REQUIRE(fd2 == 4); + + 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); + + 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); + + ATF_REQUIRE(err == 0); + + waitpid(pid, &status, 0); + 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_open_nonexistent_diag); + 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(); +} diff --git a/contrib/netbsd-tests/lib/libc/gen/posix_spawn/t_spawn.c b/contrib/netbsd-tests/lib/libc/gen/posix_spawn/t_spawn.c new file mode 100644 index 0000000..178374b --- /dev/null +++ b/contrib/netbsd-tests/lib/libc/gen/posix_spawn/t_spawn.c @@ -0,0 +1,184 @@ +/* $NetBSD: t_spawn.c,v 1.1 2012/02/13 21:03:08 martin Exp $ */ + +/*- + * Copyright (c) 2012 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Charles Zhang <charles@NetBSD.org> and + * Martin Husemann <martin@NetBSD.org>. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + + +#include <atf-c.h> +#include <spawn.h> +#include <string.h> +#include <stdio.h> +#include <stdlib.h> +#include <errno.h> +#include <sys/wait.h> + +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); +} + +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); +} + +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) +{ + char buf[FILENAME_MAX]; + char * const args[] = { __UNCONST("h_zero"), NULL }; + int err; + + snprintf(buf, sizeof buf, "%s/h_zero", atf_tc_get_config_var(tc, "srcdir")); + err = posix_spawn(NULL, buf, NULL, NULL, args, NULL); + ATF_REQUIRE_MSG(err == ENOEXEC, "expected error %d, got %d when spawning %s", ENOEXEC, err, buf); +} + +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"); +} + +ATF_TC_BODY(t_spawn_missing, tc) +{ + char buf[FILENAME_MAX]; + char * const args[] = { __UNCONST("h_nonexist"), NULL }; + int err; + + snprintf(buf, sizeof buf, "%s/h_nonexist", + atf_tc_get_config_var(tc, "srcdir")); + err = posix_spawn(NULL, buf, NULL, NULL, args, NULL); + ATF_REQUIRE_MSG(err == ENOENT, "expected error %d, got %d when spawning %s", ENOENT, err, buf); +} + +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) +{ + char buf[FILENAME_MAX]; + char * const args[] = { __UNCONST("h_nonexec"), NULL }; + int err; + + snprintf(buf, sizeof buf, "%s/h_nonexec", + atf_tc_get_config_var(tc, "srcdir")); + err = posix_spawn(NULL, buf, NULL, NULL, args, NULL); + ATF_REQUIRE_MSG(err == ENOENT, "expected error %d, got %d when spawning %s", ENOENT, err, buf); +} + +ATF_TC(t_spawn_child); + +ATF_TC_HEAD(t_spawn_child, tc) +{ + atf_tc_set_md_var(tc, "descr", + "posix_spawn a child and get it's return code"); +} + +ATF_TC_BODY(t_spawn_child, tc) +{ + char buf[FILENAME_MAX]; + char * const args0[] = { __UNCONST("h_spawn"), __UNCONST("0"), NULL }; + char * const args1[] = { __UNCONST("h_spawn"), __UNCONST("1"), NULL }; + char * const args7[] = { __UNCONST("h_spawn"), __UNCONST("7"), NULL }; + int err, status; + pid_t pid; + + snprintf(buf, sizeof buf, "%s/h_spawn", + atf_tc_get_config_var(tc, "srcdir")); + + 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) == 0); + + err = posix_spawn(&pid, buf, NULL, NULL, args1, NULL); + ATF_REQUIRE(err == 0); + ATF_REQUIRE(pid > 0); + waitpid(pid, &status, 0); + ATF_REQUIRE(WIFEXITED(status) && WEXITSTATUS(status) == 1); + + err = posix_spawn(&pid, buf, NULL, NULL, args7, NULL); + ATF_REQUIRE(err == 0); + ATF_REQUIRE(pid > 0); + waitpid(pid, &status, 0); + ATF_REQUIRE(WIFEXITED(status) && WEXITSTATUS(status) == 7); +} + +ATF_TP_ADD_TCS(tp) +{ + ATF_TP_ADD_TC(tp, t_spawn_ls); + ATF_TP_ADD_TC(tp, t_spawnp_ls); + ATF_TP_ADD_TC(tp, t_spawn_zero); + ATF_TP_ADD_TC(tp, t_spawn_missing); + ATF_TP_ADD_TC(tp, t_spawn_nonexec); + ATF_TP_ADD_TC(tp, t_spawn_child); + + return atf_no_error(); +} diff --git a/contrib/netbsd-tests/lib/libc/gen/posix_spawn/t_spawnattr.c b/contrib/netbsd-tests/lib/libc/gen/posix_spawn/t_spawnattr.c new file mode 100644 index 0000000..eb99c41 --- /dev/null +++ b/contrib/netbsd-tests/lib/libc/gen/posix_spawn/t_spawnattr.c @@ -0,0 +1,173 @@ +/* $NetBSD: t_spawnattr.c,v 1.1 2012/02/13 21:03:08 martin Exp $ */ + +/*- + * Copyright (c) 2012 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Charles Zhang <charles@NetBSD.org> and + * Martin Husemann <martin@NetBSD.org>. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#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 <unistd.h> +#include <sys/wait.h> + +#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() +{ + int scheduler, max, min, new; + + max = MAX(MAX(SCHED_FIFO, SCHED_OTHER), SCHED_RR); + min = MIN(MIN(SCHED_FIFO, SCHED_OTHER), SCHED_RR); + + /* get current schedule policy */ + scheduler = sched_getscheduler(0); + + /* new scheduler */ + new = (scheduler + 1); + if (new > max) + new = min; + + return new; +} + +static int +get_different_priority() +{ + int scheduler, max, min, new, priority; + struct sched_param param; + + /* get current schedule policy */ + scheduler = sched_getscheduler(0); + + max = sched_get_priority_max(scheduler); + min = sched_get_priority_min(scheduler); + + sched_getparam(0, ¶m); + priority = param.sched_priority; + + /* new schedule policy */ + new = (priority + 1); + if (new > max) + new = min; + + return new; +} + +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]; + char helper_arg[128]; + char * const args[] = { __UNCONST("h_spawnattr"), helper_arg, NULL }; + struct sched_param sp, child_sp; + sigset_t sig; + posix_spawnattr_t attr; + char helper[FILENAME_MAX]; + + /* + * create a pipe to controll the child + */ + err = pipe(pfd); + ATF_REQUIRE_MSG(err == 0, "could not create pipe, errno %d", errno); + sprintf(helper_arg, "%d", pfd[0]); + + posix_spawnattr_init(&attr); + + scheduler = get_different_scheduler(); + priority = get_different_priority(); + sp.sched_priority = priority; + + sigemptyset(&sig); + sigaddset(&sig, SIGUSR1); + + 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); + + sprintf(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); + + 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); + ATF_REQUIRE_MSG(child_sp.sched_priority == sp.sched_priority, + "priority is: %d, but we requested: %d", + child_sp.sched_priority, sp.sched_priority); + + ATF_REQUIRE_MSG(pid == getpgid(pid), "child pid: %d, child pgid: %d", + pid, getpgid(pid)); + + /* ready, let child go */ + write(pfd[1], "q", 1); + close(pfd[0]); + close(pfd[1]); + + /* wait and check result from child */ + waitpid(pid, &status, 0); + ATF_REQUIRE(WIFEXITED(status) && WEXITSTATUS(status) == EXIT_SUCCESS); + + posix_spawnattr_destroy(&attr); +} + +ATF_TP_ADD_TCS(tp) +{ + ATF_TP_ADD_TC(tp, t_spawnattr); + + return atf_no_error(); +} |