string: Fix tester build with fortify enable with gcc 6
[glibc.git] / posix / tst-posix_spawn-setsid.c
blob124d878ce2548643e527cfefedf42c08ce235744
1 /* Test posix_spawn setsid attribute.
2 Copyright (C) 2017-2023 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
19 #include <errno.h>
20 #include <fcntl.h>
21 #include <spawn.h>
22 #include <stdbool.h>
23 #include <stdio.h>
24 #include <sys/resource.h>
25 #include <unistd.h>
27 #include <support/check.h>
29 static void
30 do_test_setsid (bool test_setsid)
32 pid_t sid, child_sid;
33 int res;
35 /* Current session ID. */
36 sid = getsid(0);
37 if (sid == (pid_t) -1)
38 FAIL_EXIT1 ("getsid (0): %m");
40 posix_spawnattr_t attrp;
41 /* posix_spawnattr_init should not fail (it basically memset the
42 attribute). */
43 posix_spawnattr_init (&attrp);
44 if (test_setsid)
46 res = posix_spawnattr_setflags (&attrp, POSIX_SPAWN_SETSID);
47 if (res != 0)
49 errno = res;
50 FAIL_EXIT1 ("posix_spawnattr_setflags: %m");
54 /* Program to run. */
55 char *args[2] = { (char *) "true", NULL };
56 pid_t child;
58 res = posix_spawnp (&child, "true", NULL, &attrp, args, environ);
59 /* posix_spawnattr_destroy is noop. */
60 posix_spawnattr_destroy (&attrp);
62 if (res != 0)
64 errno = res;
65 FAIL_EXIT1 ("posix_spawnp: %m");
68 /* Child should have a different session ID than parent. */
69 child_sid = getsid (child);
71 if (child_sid == (pid_t) -1)
72 FAIL_EXIT1 ("getsid (%i): %m", child);
74 if (test_setsid)
76 if (child_sid == sid)
77 FAIL_EXIT1 ("child session ID matched parent one");
79 else
81 if (child_sid != sid)
82 FAIL_EXIT1 ("child session ID did not match parent one");
86 static int
87 do_test (void)
89 do_test_setsid (false);
90 do_test_setsid (true);
92 return 0;
95 #include <support/test-driver.c>