1 /* $NetBSD: t_spawnattr.c,v 1.6 2022/05/23 21:46:12 andvar 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@NetBSD.org> and
9 * Martin Husemann <martin@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 #include <sys/cdefs.h>
33 __RCSID("$NetBSD: t_spawnattr.c,v 1.6 2022/05/23 21:46:12 andvar Exp $");
34
35 #include <sys/param.h>
36 #include <atf-c.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <errno.h>
41 #include <fcntl.h>
42 #include <sched.h>
43 #include <signal.h>
44 #include <spawn.h>
45 #include <unistd.h>
46 #include <sys/wait.h>
47
48 static int
get_different_scheduler(void)49 get_different_scheduler(void)
50 {
51 /*
52 * We don't want to use SCHED_OTHER because it does not have
53 * different priorities.
54 */
55
56 /* get current schedule policy */
57 switch (sched_getscheduler(0)) {
58 case SCHED_RR:
59 return SCHED_FIFO;
60 case SCHED_FIFO:
61 case SCHED_OTHER:
62 return SCHED_RR;
63 default:
64 abort();
65 }
66 }
67
68 static int
get_different_priority(int scheduler)69 get_different_priority(int scheduler)
70 {
71 int min, max, new, priority;
72 struct sched_param param;
73
74 /* Get the priority range for the new scheduler */
75 max = sched_get_priority_max(scheduler);
76 min = sched_get_priority_min(scheduler);
77
78 sched_getparam(0, ¶m);
79 priority = param.sched_priority;
80
81 /* new schedule policy */
82 for (new = min; new <= max; new++)
83 if (priority != new)
84 break;
85
86 ATF_REQUIRE_MSG(priority != new, "could not find different priority");
87 printf("min %d max %d for scheduler %d, returning %d\n",
88 min, max, scheduler, new);
89 return new;
90 }
91
92 ATF_TC(t_spawnattr);
93
ATF_TC_HEAD(t_spawnattr,tc)94 ATF_TC_HEAD(t_spawnattr, tc)
95 {
96 atf_tc_set_md_var(tc, "require.user", "root");
97 atf_tc_set_md_var(tc, "descr",
98 "Tests posix_spawn with scheduler attributes");
99 }
100
ATF_TC_BODY(t_spawnattr,tc)101 ATF_TC_BODY(t_spawnattr, tc)
102 {
103 int pid, scheduler, child_scheduler, priority, status, err, pfd[2];
104 char helper_arg[128];
105 char * const args[] = { __UNCONST("h_spawnattr"), helper_arg, NULL };
106 struct sched_param sp, child_sp;
107 sigset_t sig;
108 posix_spawnattr_t attr;
109 char helper[FILENAME_MAX];
110
111 /*
112 * create a pipe to control the child
113 */
114 err = pipe(pfd);
115 ATF_REQUIRE_MSG(err == 0, "could not create pipe, errno %d", errno);
116 sprintf(helper_arg, "%d", pfd[0]);
117
118 posix_spawnattr_init(&attr);
119
120 scheduler = get_different_scheduler();
121 priority = get_different_priority(scheduler);
122 sp.sched_priority = priority;
123 printf("using scheduler %d, priority %d\n", scheduler, priority);
124
125 sigemptyset(&sig);
126 sigaddset(&sig, SIGUSR1);
127
128 posix_spawnattr_setflags(&attr, POSIX_SPAWN_SETSCHEDULER |
129 POSIX_SPAWN_SETSCHEDPARAM | POSIX_SPAWN_SETPGROUP |
130 POSIX_SPAWN_SETSIGMASK | POSIX_SPAWN_SETSIGDEF |
131 POSIX_SPAWN_SETSIGDEF);
132 posix_spawnattr_setpgroup(&attr, 0);
133 posix_spawnattr_setschedparam(&attr, &sp);
134 posix_spawnattr_setschedpolicy(&attr, scheduler);
135 posix_spawnattr_setsigmask(&attr, &sig);
136 posix_spawnattr_setsigdefault(&attr, &sig);
137
138 sprintf(helper, "%s/h_spawnattr",
139 atf_tc_get_config_var(tc, "srcdir"));
140 err = posix_spawn(&pid, helper, NULL, &attr, args, NULL);
141 ATF_REQUIRE_MSG(err == 0, "error %d", err);
142
143 child_scheduler = sched_getscheduler(pid);
144 ATF_REQUIRE_MSG(scheduler == child_scheduler,
145 "scheduler = %d, child_scheduler = %d, pid %d, errno %d",
146 scheduler, child_scheduler, pid, errno);
147
148 sched_getparam(pid, &child_sp);
149 ATF_REQUIRE_MSG(child_sp.sched_priority == sp.sched_priority,
150 "priority is: %d, but we requested: %d",
151 child_sp.sched_priority, sp.sched_priority);
152
153 ATF_REQUIRE_MSG(pid == getpgid(pid), "child pid: %d, child pgid: %d",
154 pid, getpgid(pid));
155
156 /* ready, let child go */
157 write(pfd[1], "q", 1);
158 close(pfd[0]);
159 close(pfd[1]);
160
161 /* wait and check result from child */
162 waitpid(pid, &status, 0);
163 ATF_REQUIRE(WIFEXITED(status) && WEXITSTATUS(status) == EXIT_SUCCESS);
164
165 posix_spawnattr_destroy(&attr);
166 }
167
168 ATF_TC(t_spawn_resetids);
169
ATF_TC_HEAD(t_spawn_resetids,tc)170 ATF_TC_HEAD(t_spawn_resetids, tc)
171 {
172 atf_tc_set_md_var(tc, "descr",
173 "posix_spawn a child and with POSIX_SPAWN_RESETIDS flag");
174 }
175
ATF_TC_BODY(t_spawn_resetids,tc)176 ATF_TC_BODY(t_spawn_resetids, tc)
177 {
178 char buf[FILENAME_MAX];
179 char * const args[] = {
180 __UNCONST("h_spawn"), __UNCONST("--resetids"), NULL
181 };
182 posix_spawnattr_t attr;
183 int err, status;
184 pid_t pid;
185
186 posix_spawnattr_init(&attr);
187 posix_spawnattr_setflags(&attr, POSIX_SPAWN_RESETIDS);
188
189 snprintf(buf, sizeof buf, "%s/h_spawn",
190 atf_tc_get_config_var(tc, "srcdir"));
191
192 err = posix_spawn(&pid, buf, NULL, &attr, args, NULL);
193 ATF_REQUIRE(err == 0);
194 ATF_REQUIRE(pid > 0);
195 waitpid(pid, &status, 0);
196 ATF_REQUIRE(WIFEXITED(status) && WEXITSTATUS(status) == 0);
197
198 posix_spawnattr_destroy(&attr);
199 }
200
ATF_TP_ADD_TCS(tp)201 ATF_TP_ADD_TCS(tp)
202 {
203 ATF_TP_ADD_TC(tp, t_spawnattr);
204 ATF_TP_ADD_TC(tp, t_spawn_resetids);
205
206 return atf_no_error();
207 }
208