xref: /freebsd/contrib/netbsd-tests/lib/libc/gen/t_nice.c (revision c6db8143eda5c775467145ac73e8ebec47afdd8f)
1 /*	$NetBSD: t_nice.c,v 1.8 2012/03/18 07:00:51 jruoho Exp $ */
2 
3 /*-
4  * Copyright (c) 2011 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jukka Ruohonen.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
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
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
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
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
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: t_nice.c,v 1.8 2012/03/18 07:00:51 jruoho Exp $");
33 
34 #include <sys/resource.h>
35 #include <sys/wait.h>
36 
37 #include <atf-c.h>
38 #include <errno.h>
39 #include <limits.h>
40 #include <pthread.h>
41 #include <stdlib.h>
42 #include <unistd.h>
43 
44 static void	*threadfunc(void *);
45 
46 static void *
47 threadfunc(void *arg)
48 {
49 	int pri, val;
50 
51 	val = *(int *)arg;
52 
53 	errno = 0;
54 	pri = getpriority(PRIO_PROCESS, 0);
55 	ATF_REQUIRE(errno == 0);
56 
57 	if (pri != val)
58 		atf_tc_fail("nice(3) value was not propagated to threads");
59 
60 	return NULL;
61 }
62 
63 ATF_TC(nice_err);
64 ATF_TC_HEAD(nice_err, tc)
65 {
66 	atf_tc_set_md_var(tc, "descr",
67 	    "Test nice(3) for invalid parameters (PR lib/42587)");
68 	atf_tc_set_md_var(tc, "require.user", "unprivileged");
69 }
70 
71 ATF_TC_BODY(nice_err, tc)
72 {
73 	int i;
74 
75 #ifdef __FreeBSD__
76 	atf_tc_expect_fail("nice(incr) with incr < 0 fails with unprivileged "
77 	   "users and sets errno == EPERM; see PR # 189821 for more details");
78 #endif
79 
80 	/*
81 	 * The call should fail with EPERM if the
82 	 * supplied parameter is negative and the
83 	 * caller does not have privileges.
84 	 */
85 	for (i = -20; i < 0; i++) {
86 
87 		errno = 0;
88 
89 		ATF_REQUIRE_ERRNO(EPERM, nice(i) == -1);
90 	}
91 }
92 
93 ATF_TC(nice_priority);
94 ATF_TC_HEAD(nice_priority, tc)
95 {
96 	atf_tc_set_md_var(tc, "descr", "Test nice(3) vs. getpriority(2)");
97 }
98 
99 ATF_TC_BODY(nice_priority, tc)
100 {
101 #ifdef __FreeBSD__
102 	int i, pri, pri2, nic;
103 #else
104 	int i, pri, nic;
105 #endif
106 	pid_t pid;
107 	int sta;
108 
109 	for (i = 0; i <= 20; i++) {
110 
111 		nic = nice(i);
112 		ATF_REQUIRE(nic != -1);
113 
114 		errno = 0;
115 		pri = getpriority(PRIO_PROCESS, 0);
116 		ATF_REQUIRE(errno == 0);
117 
118 #ifdef __NetBSD__
119 		if (nic != pri)
120 			atf_tc_fail("nice(3) and getpriority(2) conflict");
121 #endif
122 
123 		/*
124 		 * Also verify that the nice(3) values
125 		 * are inherited by child processes.
126 		 */
127 		pid = fork();
128 		ATF_REQUIRE(pid >= 0);
129 
130 		if (pid == 0) {
131 
132 			errno = 0;
133 #ifdef __FreeBSD__
134 			pri = getpriority(PRIO_PROCESS, 0);
135 #else
136 			pri2 = getpriority(PRIO_PROCESS, 0);
137 #endif
138 			ATF_REQUIRE(errno == 0);
139 
140 #ifdef __FreeBSD__
141 			if (pri != pri2)
142 #else
143 			if (nic != pri)
144 #endif
145 				_exit(EXIT_FAILURE);
146 
147 			_exit(EXIT_SUCCESS);
148 		}
149 
150 		(void)wait(&sta);
151 
152 		if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS)
153 			atf_tc_fail("nice(3) value was not inherited");
154 	}
155 }
156 
157 ATF_TC(nice_root);
158 ATF_TC_HEAD(nice_root, tc)
159 {
160 	atf_tc_set_md_var(tc, "descr", "Test that nice(3) works");
161 	atf_tc_set_md_var(tc, "require.user", "root");
162 }
163 
164 ATF_TC_BODY(nice_root, tc)
165 {
166 	int i;
167 
168 	for (i = -20; i <= 20; i++) {
169 
170 		ATF_REQUIRE(nice(i) != -1);
171 	}
172 }
173 
174 ATF_TC(nice_thread);
175 ATF_TC_HEAD(nice_thread, tc)
176 {
177 	atf_tc_set_md_var(tc, "descr", "Test nice(3) with threads");
178 }
179 
180 ATF_TC_BODY(nice_thread, tc)
181 {
182 	pthread_t tid[5];
183 #ifdef __FreeBSD__
184 	int pri, rv, val;
185 #else
186 	int rv, val;
187 #endif
188 	size_t i;
189 
190 	/*
191 	 * Test that the scheduling priority is
192 	 * propagated to all system scope threads.
193 	 */
194 	for (i = 0; i < __arraycount(tid); i++) {
195 
196 		val = nice(i);
197 		ATF_REQUIRE(val != -1);
198 
199 #ifdef __FreeBSD__
200 		pri = getpriority(PRIO_PROCESS, 0);
201 		rv = pthread_create(&tid[i], NULL, threadfunc, &pri);
202 #else
203 		rv = pthread_create(&tid[i], NULL, threadfunc, &val);
204 #endif
205 		ATF_REQUIRE(rv == 0);
206 
207 		rv = pthread_join(tid[i], NULL);
208 		ATF_REQUIRE(rv == 0);
209 	}
210 }
211 
212 ATF_TP_ADD_TCS(tp)
213 {
214 
215 	ATF_TP_ADD_TC(tp, nice_err);
216 	ATF_TP_ADD_TC(tp, nice_priority);
217 	ATF_TP_ADD_TC(tp, nice_root);
218 	ATF_TP_ADD_TC(tp, nice_thread);
219 
220 	return atf_no_error();
221 }
222