Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/tests/lib/libc/gen/posix_spawn Add test for POSIX_SPAWN_RESE...



details:   https://anonhg.NetBSD.org/src/rev/fda778e27c21
branches:  trunk
changeset: 1021005:fda778e27c21
user:      martin <martin%NetBSD.org@localhost>
date:      Sun May 02 11:18:11 2021 +0000

description:
Add test for POSIX_SPAWN_RESETIDS flag

diffstat:

 tests/lib/libc/gen/posix_spawn/h_spawn.c     |  20 ++++++++++++++-
 tests/lib/libc/gen/posix_spawn/t_spawnattr.c |  36 +++++++++++++++++++++++++++-
 2 files changed, 53 insertions(+), 3 deletions(-)

diffs (98 lines):

diff -r a8abfb22d0c6 -r fda778e27c21 tests/lib/libc/gen/posix_spawn/h_spawn.c
--- a/tests/lib/libc/gen/posix_spawn/h_spawn.c  Sun May 02 10:23:55 2021 +0000
+++ b/tests/lib/libc/gen/posix_spawn/h_spawn.c  Sun May 02 11:18:11 2021 +0000
@@ -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 <stdio.h>
 #include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
 
 int
 main(int argc, char **argv)
@@ -39,11 +41,25 @@
        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;
diff -r a8abfb22d0c6 -r fda778e27c21 tests/lib/libc/gen/posix_spawn/t_spawnattr.c
--- a/tests/lib/libc/gen/posix_spawn/t_spawnattr.c      Sun May 02 10:23:55 2021 +0000
+++ b/tests/lib/libc/gen/posix_spawn/t_spawnattr.c      Sun May 02 11:18:11 2021 +0000
@@ -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 @@
        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();
 }



Home | Main Index | Thread Index | Old Index