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