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