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