]> CyberLeo.Net >> Repos - FreeBSD/releng/10.3.git/blob - contrib/netbsd-tests/lib/libc/gen/posix_spawn/t_spawnattr.c
- Copy stable/10@296371 to releng/10.3 in preparation for 10.3-RC1
[FreeBSD/releng/10.3.git] / contrib / netbsd-tests / lib / libc / gen / posix_spawn / t_spawnattr.c
1 /* $NetBSD: t_spawnattr.c,v 1.1 2012/02/13 21:03:08 martin 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
33 #include <sys/param.h>
34 #include <atf-c.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <errno.h>
39 #include <fcntl.h>
40 #include <sched.h>
41 #include <signal.h>
42 #include <spawn.h>
43 #include <unistd.h>
44 #include <sys/wait.h>
45
46 static int get_different_scheduler(void);
47 static int get_different_priority(int scheduler);
48
49 static const int schedulers[] = {
50         SCHED_OTHER,
51         SCHED_FIFO,
52         SCHED_RR
53 };
54
55 static int
56 get_different_scheduler(void)
57 {
58         u_int i;
59         int scheduler;
60
61         /* get current schedule policy */
62         scheduler = sched_getscheduler(0);
63         for (i = 0; i < nitems(schedulers); i++) {
64                 if (schedulers[i] == scheduler)
65                         break;
66         }
67         ATF_REQUIRE_MSG(i < nitems(schedulers),
68             "Unknown current scheduler %d", scheduler);
69                                         
70         /* new scheduler */
71         i++;
72         if (i >= nitems(schedulers))
73                 i = 0;
74         return schedulers[i];
75 }
76
77 static int
78 get_different_priority(int scheduler)
79 {
80         int max, min, new, priority;
81         struct sched_param param;
82
83         max = sched_get_priority_max(scheduler);
84         min = sched_get_priority_min(scheduler);
85
86         sched_getparam(0, &param);
87         priority = param.sched_priority;
88         
89         /*
90          * Change numerical value of the priority, to ensure that it
91          * was set for the spawned child.
92          */
93         new = priority + 1;
94         if (new > max)
95                 new = min;
96         return new;
97 }
98
99 ATF_TC(t_spawnattr);
100
101 ATF_TC_HEAD(t_spawnattr, tc)
102 {
103         atf_tc_set_md_var(tc, "require.user", "root");
104         atf_tc_set_md_var(tc, "descr",
105             "Tests posix_spawn with scheduler attributes");
106 }
107
108 ATF_TC_BODY(t_spawnattr, tc)
109 {
110         int pid, scheduler, child_scheduler, priority, status, err, pfd[2];
111         char helper_arg[128];
112         char * const args[] = { __UNCONST("h_spawnattr"), helper_arg, NULL };
113         struct sched_param sp, child_sp;
114         sigset_t sig;
115         posix_spawnattr_t attr;
116         char helper[FILENAME_MAX];
117
118         /*
119          * create a pipe to controll the child
120          */
121         err = pipe(pfd);
122         ATF_REQUIRE_MSG(err == 0, "could not create pipe, errno %d", errno);
123         sprintf(helper_arg, "%d", pfd[0]);
124
125         posix_spawnattr_init(&attr);
126
127         scheduler = get_different_scheduler();
128         priority = get_different_priority(scheduler);
129         sp.sched_priority = priority;
130         
131         sigemptyset(&sig);
132         sigaddset(&sig, SIGUSR1);
133
134         posix_spawnattr_setflags(&attr, POSIX_SPAWN_SETSCHEDULER |
135             POSIX_SPAWN_SETSCHEDPARAM | POSIX_SPAWN_SETPGROUP |
136             POSIX_SPAWN_SETSIGMASK | POSIX_SPAWN_SETSIGDEF);
137         posix_spawnattr_setpgroup(&attr, 0);
138         posix_spawnattr_setschedparam(&attr, &sp);
139         posix_spawnattr_setschedpolicy(&attr, scheduler);
140         posix_spawnattr_setsigmask(&attr, &sig);
141         posix_spawnattr_setsigdefault(&attr, &sig);
142
143         sprintf(helper, "%s/h_spawnattr",
144             atf_tc_get_config_var(tc, "srcdir"));
145         err = posix_spawn(&pid, helper, NULL, &attr, args, NULL);
146         ATF_REQUIRE_MSG(err == 0, "error %d", err);
147
148         child_scheduler = sched_getscheduler(pid);
149         ATF_REQUIRE_MSG(scheduler == child_scheduler,
150             "scheduler = %d, child_scheduler = %d, pid %d, errno %d",
151             scheduler, child_scheduler, pid, errno);
152
153         sched_getparam(pid, &child_sp);
154         ATF_REQUIRE_MSG(child_sp.sched_priority == sp.sched_priority,
155             "priority is: %d, but we requested: %d",
156             child_sp.sched_priority, sp.sched_priority);
157
158         ATF_REQUIRE_MSG(pid == getpgid(pid), "child pid: %d, child pgid: %d",
159             pid, getpgid(pid));
160
161         /* ready, let child go */
162         write(pfd[1], "q", 1);
163         close(pfd[0]);
164         close(pfd[1]);
165
166         /* wait and check result from child */
167         waitpid(pid, &status, 0);
168         ATF_REQUIRE(WIFEXITED(status) && WEXITSTATUS(status) == EXIT_SUCCESS);
169
170         posix_spawnattr_destroy(&attr);
171 }
172
173 ATF_TP_ADD_TCS(tp)
174 {
175         ATF_TP_ADD_TC(tp, t_spawnattr);
176
177         return atf_no_error();
178 }