1 | /* Test posix_spawn setsid attribute. |
2 | Copyright (C) 2017-2022 Free Software Foundation, Inc. |
3 | This file is part of the GNU C Library. |
4 | |
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. |
9 | |
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. |
14 | |
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/>. */ |
18 | |
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> |
26 | |
27 | #include <support/check.h> |
28 | |
29 | static void |
30 | do_test_setsid (bool test_setsid) |
31 | { |
32 | pid_t sid, child_sid; |
33 | int res; |
34 | |
35 | /* Current session ID. */ |
36 | sid = getsid(0); |
37 | if (sid == (pid_t) -1) |
38 | FAIL_EXIT1 ("getsid (0): %m" ); |
39 | |
40 | posix_spawnattr_t attrp; |
41 | /* posix_spawnattr_init should not fail (it basically memset the |
42 | attribute). */ |
43 | posix_spawnattr_init (attr: &attrp); |
44 | if (test_setsid) |
45 | { |
46 | res = posix_spawnattr_setflags (attr: &attrp, POSIX_SPAWN_SETSID); |
47 | if (res != 0) |
48 | { |
49 | errno = res; |
50 | FAIL_EXIT1 ("posix_spawnattr_setflags: %m" ); |
51 | } |
52 | } |
53 | |
54 | /* Program to run. */ |
55 | char *args[2] = { (char *) "true" , NULL }; |
56 | pid_t child; |
57 | |
58 | res = posix_spawnp (pid: &child, file: "true" , NULL, attrp: &attrp, argv: args, envp: environ); |
59 | /* posix_spawnattr_destroy is noop. */ |
60 | posix_spawnattr_destroy (attr: &attrp); |
61 | |
62 | if (res != 0) |
63 | { |
64 | errno = res; |
65 | FAIL_EXIT1 ("posix_spawnp: %m" ); |
66 | } |
67 | |
68 | /* Child should have a different session ID than parent. */ |
69 | child_sid = getsid (child); |
70 | |
71 | if (child_sid == (pid_t) -1) |
72 | FAIL_EXIT1 ("getsid (%i): %m" , child); |
73 | |
74 | if (test_setsid) |
75 | { |
76 | if (child_sid == sid) |
77 | FAIL_EXIT1 ("child session ID matched parent one" ); |
78 | } |
79 | else |
80 | { |
81 | if (child_sid != sid) |
82 | FAIL_EXIT1 ("child session ID did not match parent one" ); |
83 | } |
84 | } |
85 | |
86 | static int |
87 | do_test (void) |
88 | { |
89 | do_test_setsid (false); |
90 | do_test_setsid (true); |
91 | |
92 | return 0; |
93 | } |
94 | |
95 | #include <support/test-driver.c> |
96 | |