| @@ -1,198 +1,195 @@ | | | @@ -1,198 +1,195 @@ |
1 | /* $NetBSD: t_nice.c,v 1.5 2011/04/17 06:18:23 jruoho Exp $ */ | | 1 | /* $NetBSD: t_nice.c,v 1.6 2011/04/25 20:55:06 njoly Exp $ */ |
2 | | | 2 | |
3 | /*- | | 3 | /*- |
4 | * Copyright (c) 2011 The NetBSD Foundation, Inc. | | 4 | * Copyright (c) 2011 The NetBSD Foundation, Inc. |
5 | * All rights reserved. | | 5 | * All rights reserved. |
6 | * | | 6 | * |
7 | * This code is derived from software contributed to The NetBSD Foundation | | 7 | * This code is derived from software contributed to The NetBSD Foundation |
8 | * by Jukka Ruohonen. | | 8 | * by Jukka Ruohonen. |
9 | * | | 9 | * |
10 | * Redistribution and use in source and binary forms, with or without | | 10 | * Redistribution and use in source and binary forms, with or without |
11 | * modification, are permitted provided that the following conditions | | 11 | * modification, are permitted provided that the following conditions |
12 | * are met: | | 12 | * are met: |
13 | * 1. Redistributions of source code must retain the above copyright | | 13 | * 1. Redistributions of source code must retain the above copyright |
14 | * notice, this list of conditions and the following disclaimer. | | 14 | * notice, this list of conditions and the following disclaimer. |
15 | * 2. Redistributions in binary form must reproduce the above copyright | | 15 | * 2. Redistributions in binary form must reproduce the above copyright |
16 | * notice, this list of conditions and the following disclaimer in the | | 16 | * notice, this list of conditions and the following disclaimer in the |
17 | * documentation and/or other materials provided with the distribution. | | 17 | * documentation and/or other materials provided with the distribution. |
18 | * | | 18 | * |
19 | * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS | | 19 | * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS |
20 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | | 20 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
21 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | | 21 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
22 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS | | 22 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS |
23 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | | 23 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | | 24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | | 25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | | 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | | 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | | 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
29 | * POSSIBILITY OF SUCH DAMAGE. | | 29 | * POSSIBILITY OF SUCH DAMAGE. |
30 | */ | | 30 | */ |
31 | #include <sys/cdefs.h> | | 31 | #include <sys/cdefs.h> |
32 | __RCSID("$NetBSD: t_nice.c,v 1.5 2011/04/17 06:18:23 jruoho Exp $"); | | 32 | __RCSID("$NetBSD: t_nice.c,v 1.6 2011/04/25 20:55:06 njoly Exp $"); |
33 | | | 33 | |
34 | #include <sys/resource.h> | | 34 | #include <sys/resource.h> |
35 | #include <sys/wait.h> | | 35 | #include <sys/wait.h> |
36 | | | 36 | |
37 | #include <atf-c.h> | | 37 | #include <atf-c.h> |
38 | #include <errno.h> | | 38 | #include <errno.h> |
39 | #include <limits.h> | | 39 | #include <limits.h> |
40 | #include <pthread.h> | | 40 | #include <pthread.h> |
41 | #include <stdlib.h> | | 41 | #include <stdlib.h> |
42 | #include <unistd.h> | | 42 | #include <unistd.h> |
43 | | | 43 | |
44 | static void *threadfunc(void *); | | 44 | static void *threadfunc(void *); |
45 | | | 45 | |
46 | static void * | | 46 | static void * |
47 | threadfunc(void *arg) | | 47 | threadfunc(void *arg) |
48 | { | | 48 | { |
49 | int pri, val; | | 49 | int pri, val; |
50 | | | 50 | |
51 | val = *(int *)arg; | | 51 | val = *(int *)arg; |
52 | | | 52 | |
53 | errno = 0; | | 53 | errno = 0; |
54 | pri = getpriority(PRIO_PROCESS, 0); | | 54 | pri = getpriority(PRIO_PROCESS, 0); |
55 | ATF_REQUIRE(errno == 0); | | 55 | ATF_REQUIRE(errno == 0); |
56 | | | 56 | |
57 | if (pri != val) | | 57 | if (pri != val) |
58 | atf_tc_fail("nice(3) value was not propagated to threads"); | | 58 | atf_tc_fail("nice(3) value was not propagated to threads"); |
59 | | | 59 | |
60 | return NULL; | | 60 | return NULL; |
61 | } | | 61 | } |
62 | | | 62 | |
63 | ATF_TC(nice_err); | | 63 | ATF_TC(nice_err); |
64 | ATF_TC_HEAD(nice_err, tc) | | 64 | ATF_TC_HEAD(nice_err, tc) |
65 | { | | 65 | { |
66 | atf_tc_set_md_var(tc, "descr", "Test nice(3) for invalid parameters"); | | 66 | atf_tc_set_md_var(tc, "descr", "Test nice(3) for invalid parameters"); |
67 | atf_tc_set_md_var(tc, "require.user", "unprivileged"); | | 67 | atf_tc_set_md_var(tc, "require.user", "unprivileged"); |
68 | } | | 68 | } |
69 | | | 69 | |
70 | ATF_TC_BODY(nice_err, tc) | | 70 | ATF_TC_BODY(nice_err, tc) |
71 | { | | 71 | { |
72 | int i; | | 72 | int i; |
73 | | | 73 | |
74 | /* | | 74 | /* |
75 | * The call should fail with EPERM if the | | 75 | * The call should fail with EPERM if the |
76 | * supplied parameter is negative and the | | 76 | * supplied parameter is negative and the |
77 | * caller does not have privileges. Note | | 77 | * caller does not have privileges. Note |
78 | * that the errno is thus "wrong" in NetBSD. | | 78 | * that the errno is thus "wrong" in NetBSD. |
79 | */ | | 79 | */ |
80 | atf_tc_expect_fail("PR lib/42587"); | | 80 | atf_tc_expect_fail("PR lib/42587"); |
81 | | | 81 | |
82 | for (i = -20; i < 0; i++) { | | 82 | for (i = -20; i < 0; i++) { |
83 | | | 83 | |
84 | errno = 0; | | 84 | errno = 0; |
85 | | | 85 | |
86 | ATF_REQUIRE(nice(i) == -1); | | 86 | ATF_REQUIRE_ERRNO(EPERM, nice(i) == -1); |
87 | | | | |
88 | if (errno != EPERM) | | | |
89 | atf_tc_fail("wrong errno"); | | | |
90 | } | | 87 | } |
91 | } | | 88 | } |
92 | | | 89 | |
93 | ATF_TC(nice_priority); | | 90 | ATF_TC(nice_priority); |
94 | ATF_TC_HEAD(nice_priority, tc) | | 91 | ATF_TC_HEAD(nice_priority, tc) |
95 | { | | 92 | { |
96 | atf_tc_set_md_var(tc, "descr", "Test nice(3) vs. getpriority(2)"); | | 93 | atf_tc_set_md_var(tc, "descr", "Test nice(3) vs. getpriority(2)"); |
97 | } | | 94 | } |
98 | | | 95 | |
99 | ATF_TC_BODY(nice_priority, tc) | | 96 | ATF_TC_BODY(nice_priority, tc) |
100 | { | | 97 | { |
101 | int i, pri, nic; | | 98 | int i, pri, nic; |
102 | pid_t pid; | | 99 | pid_t pid; |
103 | int sta; | | 100 | int sta; |
104 | | | 101 | |
105 | for (i = 0; i <= 20; i++) { | | 102 | for (i = 0; i <= 20; i++) { |
106 | | | 103 | |
107 | nic = nice(i); | | 104 | nic = nice(i); |
108 | ATF_REQUIRE(nic != -1); | | 105 | ATF_REQUIRE(nic != -1); |
109 | | | 106 | |
110 | errno = 0; | | 107 | errno = 0; |
111 | pri = getpriority(PRIO_PROCESS, 0); | | 108 | pri = getpriority(PRIO_PROCESS, 0); |
112 | ATF_REQUIRE(errno == 0); | | 109 | ATF_REQUIRE(errno == 0); |
113 | | | 110 | |
114 | if (nic != pri) | | 111 | if (nic != pri) |
115 | atf_tc_fail("nice(3) and getpriority(2) conflict"); | | 112 | atf_tc_fail("nice(3) and getpriority(2) conflict"); |
116 | | | 113 | |
117 | /* | | 114 | /* |
118 | * Also verify that the nice(3) values | | 115 | * Also verify that the nice(3) values |
119 | * are inherited by child processes. | | 116 | * are inherited by child processes. |
120 | */ | | 117 | */ |
121 | pid = fork(); | | 118 | pid = fork(); |
122 | ATF_REQUIRE(pid >= 0); | | 119 | ATF_REQUIRE(pid >= 0); |
123 | | | 120 | |
124 | if (pid == 0) { | | 121 | if (pid == 0) { |
125 | | | 122 | |
126 | errno = 0; | | 123 | errno = 0; |
127 | pri = getpriority(PRIO_PROCESS, 0); | | 124 | pri = getpriority(PRIO_PROCESS, 0); |
128 | ATF_REQUIRE(errno == 0); | | 125 | ATF_REQUIRE(errno == 0); |
129 | | | 126 | |
130 | if (nic != pri) | | 127 | if (nic != pri) |
131 | _exit(EXIT_FAILURE); | | 128 | _exit(EXIT_FAILURE); |
132 | | | 129 | |
133 | _exit(EXIT_SUCCESS); | | 130 | _exit(EXIT_SUCCESS); |
134 | } | | 131 | } |
135 | | | 132 | |
136 | (void)wait(&sta); | | 133 | (void)wait(&sta); |
137 | | | 134 | |
138 | if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS) | | 135 | if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS) |
139 | atf_tc_fail("nice(3) value was not inherited"); | | 136 | atf_tc_fail("nice(3) value was not inherited"); |
140 | } | | 137 | } |
141 | } | | 138 | } |
142 | | | 139 | |
143 | ATF_TC(nice_root); | | 140 | ATF_TC(nice_root); |
144 | ATF_TC_HEAD(nice_root, tc) | | 141 | ATF_TC_HEAD(nice_root, tc) |
145 | { | | 142 | { |
146 | atf_tc_set_md_var(tc, "descr", "Test that nice(3) works"); | | 143 | atf_tc_set_md_var(tc, "descr", "Test that nice(3) works"); |
147 | atf_tc_set_md_var(tc, "require.user", "root"); | | 144 | atf_tc_set_md_var(tc, "require.user", "root"); |
148 | } | | 145 | } |
149 | | | 146 | |
150 | ATF_TC_BODY(nice_root, tc) | | 147 | ATF_TC_BODY(nice_root, tc) |
151 | { | | 148 | { |
152 | int i; | | 149 | int i; |
153 | | | 150 | |
154 | for (i = -20; i <= 20; i++) { | | 151 | for (i = -20; i <= 20; i++) { |
155 | | | 152 | |
156 | ATF_REQUIRE(nice(i) != -1); | | 153 | ATF_REQUIRE(nice(i) != -1); |
157 | } | | 154 | } |
158 | } | | 155 | } |
159 | | | 156 | |
160 | ATF_TC(nice_thread); | | 157 | ATF_TC(nice_thread); |
161 | ATF_TC_HEAD(nice_thread, tc) | | 158 | ATF_TC_HEAD(nice_thread, tc) |
162 | { | | 159 | { |
163 | atf_tc_set_md_var(tc, "descr", "Test nice(3) with threads"); | | 160 | atf_tc_set_md_var(tc, "descr", "Test nice(3) with threads"); |
164 | } | | 161 | } |
165 | | | 162 | |
166 | ATF_TC_BODY(nice_thread, tc) | | 163 | ATF_TC_BODY(nice_thread, tc) |
167 | { | | 164 | { |
168 | pthread_t tid[5]; | | 165 | pthread_t tid[5]; |
169 | int rv, val; | | 166 | int rv, val; |
170 | size_t i; | | 167 | size_t i; |
171 | | | 168 | |
172 | /* | | 169 | /* |
173 | * Test that the scheduling priority is | | 170 | * Test that the scheduling priority is |
174 | * propagated to all system scope threads. | | 171 | * propagated to all system scope threads. |
175 | */ | | 172 | */ |
176 | for (i = 0; i < __arraycount(tid); i++) { | | 173 | for (i = 0; i < __arraycount(tid); i++) { |
177 | | | 174 | |
178 | val = nice(i); | | 175 | val = nice(i); |
179 | ATF_REQUIRE(val != -1); | | 176 | ATF_REQUIRE(val != -1); |
180 | | | 177 | |
181 | rv = pthread_create(&tid[i], NULL, threadfunc, &val); | | 178 | rv = pthread_create(&tid[i], NULL, threadfunc, &val); |
182 | ATF_REQUIRE(rv == 0); | | 179 | ATF_REQUIRE(rv == 0); |
183 | | | 180 | |
184 | rv = pthread_join(tid[i], NULL); | | 181 | rv = pthread_join(tid[i], NULL); |
185 | ATF_REQUIRE(rv == 0); | | 182 | ATF_REQUIRE(rv == 0); |
186 | } | | 183 | } |
187 | } | | 184 | } |
188 | | | 185 | |
189 | ATF_TP_ADD_TCS(tp) | | 186 | ATF_TP_ADD_TCS(tp) |
190 | { | | 187 | { |
191 | | | 188 | |
192 | ATF_TP_ADD_TC(tp, nice_err); | | 189 | ATF_TP_ADD_TC(tp, nice_err); |
193 | ATF_TP_ADD_TC(tp, nice_priority); | | 190 | ATF_TP_ADD_TC(tp, nice_priority); |
194 | ATF_TP_ADD_TC(tp, nice_root); | | 191 | ATF_TP_ADD_TC(tp, nice_root); |
195 | ATF_TP_ADD_TC(tp, nice_thread); | | 192 | ATF_TP_ADD_TC(tp, nice_thread); |
196 | | | 193 | |
197 | return atf_no_error(); | | 194 | return atf_no_error(); |
198 | } | | 195 | } |