t_spawnattr.c revision 1.6.4.1 1 /* $NetBSD: t_spawnattr.c,v 1.6.4.1 2025/08/02 05:58:04 perseant Exp $ */
2
3 /*-
4 * Copyright (c) 2012 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Charles Zhang <charles (at) NetBSD.org> and
9 * Martin Husemann <martin (at) NetBSD.org>.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 #include <sys/cdefs.h>
34 __RCSID("$NetBSD: t_spawnattr.c,v 1.6.4.1 2025/08/02 05:58:04 perseant Exp $");
35
36 #include <sys/param.h>
37 #include <sys/wait.h>
38
39 #include <atf-c.h>
40 #include <errno.h>
41 #include <fcntl.h>
42 #include <sched.h>
43 #include <signal.h>
44 #include <spawn.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <unistd.h>
49
50 #include "h_macros.h"
51
52 static int
53 get_different_scheduler(void)
54 {
55 int sched;
56
57 /*
58 * We don't want to use SCHED_OTHER because it does not have
59 * different priorities.
60 */
61
62 /* get current schedule policy */
63 RL(sched = sched_getscheduler(0));
64 switch (sched) {
65 case SCHED_RR:
66 return SCHED_FIFO;
67 case SCHED_FIFO:
68 case SCHED_OTHER:
69 return SCHED_RR;
70 default:
71 abort();
72 }
73 }
74
75 static int
76 get_different_priority(int scheduler)
77 {
78 int min, max, new, priority;
79 struct sched_param param;
80
81 /* Get the priority range for the new scheduler */
82 RL(max = sched_get_priority_max(scheduler));
83 RL(min = sched_get_priority_min(scheduler));
84
85 RL(sched_getparam(0, ¶m));
86 priority = param.sched_priority;
87
88 /* new schedule policy */
89 for (new = min; new <= max; new++)
90 if (priority != new)
91 break;
92
93 ATF_REQUIRE_MSG(priority != new, "could not find different priority");
94 printf("min %d max %d for scheduler %d, returning %d\n",
95 min, max, scheduler, new);
96 return new;
97 }
98
99 ATF_TC(t_spawnattr);
100 ATF_TC_HEAD(t_spawnattr, tc)
101 {
102 atf_tc_set_md_var(tc, "require.user", "root");
103 atf_tc_set_md_var(tc, "descr",
104 "Tests posix_spawn with scheduler attributes");
105 }
106 ATF_TC_BODY(t_spawnattr, tc)
107 {
108 int pid, scheduler, child_scheduler, priority, status, pfd[2];
109 char helper_arg[128];
110 char * const args[] = { __UNCONST("h_spawnattr"), helper_arg, NULL };
111 struct sched_param sp, child_sp;
112 sigset_t sig;
113 posix_spawnattr_t attr;
114 char helper[FILENAME_MAX];
115
116 /*
117 * create a pipe to control the child
118 */
119 RL(pipe(pfd));
120 snprintf(helper_arg, sizeof(helper_arg), "%d", pfd[0]);
121
122 RZ(posix_spawnattr_init(&attr));
123
124 scheduler = get_different_scheduler();
125 priority = get_different_priority(scheduler);
126 sp.sched_priority = priority;
127 printf("using scheduler %d, priority %d\n", scheduler, priority);
128
129 RL(sigemptyset(&sig));
130 RL(sigaddset(&sig, SIGUSR1));
131
132 RZ(posix_spawnattr_setflags(&attr, POSIX_SPAWN_SETSCHEDULER |
133 POSIX_SPAWN_SETSCHEDPARAM | POSIX_SPAWN_SETPGROUP |
134 POSIX_SPAWN_SETSIGMASK | POSIX_SPAWN_SETSIGDEF |
135 POSIX_SPAWN_SETSIGDEF));
136 RZ(posix_spawnattr_setpgroup(&attr, 0));
137 RZ(posix_spawnattr_setschedparam(&attr, &sp));
138 RZ(posix_spawnattr_setschedpolicy(&attr, scheduler));
139 RZ(posix_spawnattr_setsigmask(&attr, &sig));
140 RZ(posix_spawnattr_setsigdefault(&attr, &sig));
141
142 snprintf(helper, sizeof(helper), "%s/h_spawnattr",
143 atf_tc_get_config_var(tc, "srcdir"));
144 RZ(posix_spawn(&pid, helper, NULL, &attr, args, NULL));
145 ATF_REQUIRE_MSG(pid > 0, "pid=%lld", (long long)pid);
146
147 RL(child_scheduler = sched_getscheduler(pid));
148 ATF_REQUIRE_MSG(scheduler == child_scheduler,
149 "scheduler = %d, child_scheduler = %d, pid %d, errno %d",
150 scheduler, child_scheduler, pid, errno);
151
152 RL(sched_getparam(pid, &child_sp));
153 ATF_REQUIRE_MSG(child_sp.sched_priority == sp.sched_priority,
154 "priority is: %d, but we requested: %d",
155 child_sp.sched_priority, sp.sched_priority);
156
157 ATF_REQUIRE_MSG(pid == getpgid(pid), "child pid: %d, child pgid: %d",
158 pid, getpgid(pid));
159
160 /* ready, let child go */
161 RL(write(pfd[1], "q", 1));
162 RL(close(pfd[0]));
163 RL(close(pfd[1]));
164
165 /* wait and check result from child */
166 RL(waitpid(pid, &status, 0));
167 ATF_REQUIRE(WIFEXITED(status) && WEXITSTATUS(status) == EXIT_SUCCESS);
168
169 RZ(posix_spawnattr_destroy(&attr));
170 }
171
172 ATF_TC(t_spawn_resetids);
173
174 ATF_TC_HEAD(t_spawn_resetids, tc)
175 {
176 atf_tc_set_md_var(tc, "descr",
177 "posix_spawn a child and with POSIX_SPAWN_RESETIDS flag");
178 }
179
180 ATF_TC_BODY(t_spawn_resetids, tc)
181 {
182 char buf[FILENAME_MAX];
183 char * const args[] = {
184 __UNCONST("h_spawn"), __UNCONST("--resetids"), NULL
185 };
186 posix_spawnattr_t attr;
187 int status;
188 pid_t pid;
189
190 RZ(posix_spawnattr_init(&attr));
191 RZ(posix_spawnattr_setflags(&attr, POSIX_SPAWN_RESETIDS));
192
193 snprintf(buf, sizeof(buf), "%s/h_spawn",
194 atf_tc_get_config_var(tc, "srcdir"));
195
196 RZ(posix_spawn(&pid, buf, NULL, &attr, args, NULL));
197 ATF_REQUIRE_MSG(pid > 0, "pid=%lld", (long long)pid);
198 RL(waitpid(pid, &status, 0));
199 ATF_REQUIRE_MSG(WIFEXITED(status) && WEXITSTATUS(status) == 0,
200 "status=0x%x", status);
201
202 RZ(posix_spawnattr_destroy(&attr));
203 }
204
205 ATF_TP_ADD_TCS(tp)
206 {
207 ATF_TP_ADD_TC(tp, t_spawnattr);
208 ATF_TP_ADD_TC(tp, t_spawn_resetids);
209
210 return atf_no_error();
211 }
212