t_nice.c revision 313479
1235368Sgnn/*	$NetBSD: t_nice.c,v 1.8 2012/03/18 07:00:51 jruoho Exp $ */
2235368Sgnn
3235368Sgnn/*-
4235368Sgnn * Copyright (c) 2011 The NetBSD Foundation, Inc.
5235368Sgnn * All rights reserved.
6235368Sgnn *
7235368Sgnn * This code is derived from software contributed to The NetBSD Foundation
8235368Sgnn * by Jukka Ruohonen.
9235368Sgnn *
10235368Sgnn * Redistribution and use in source and binary forms, with or without
11235368Sgnn * modification, are permitted provided that the following conditions
12235368Sgnn * are met:
13235368Sgnn * 1. Redistributions of source code must retain the above copyright
14235368Sgnn *    notice, this list of conditions and the following disclaimer.
15235368Sgnn * 2. Redistributions in binary form must reproduce the above copyright
16235368Sgnn *    notice, this list of conditions and the following disclaimer in the
17235368Sgnn *    documentation and/or other materials provided with the distribution.
18235368Sgnn *
19235368Sgnn * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20235368Sgnn * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21235368Sgnn * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22235368Sgnn * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23235368Sgnn * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24235368Sgnn * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25235368Sgnn * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26235368Sgnn * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27235368Sgnn * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28235368Sgnn * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29235368Sgnn * POSSIBILITY OF SUCH DAMAGE.
30235368Sgnn */
31235368Sgnn#include <sys/cdefs.h>
32235368Sgnn__RCSID("$NetBSD: t_nice.c,v 1.8 2012/03/18 07:00:51 jruoho Exp $");
33235368Sgnn
34235368Sgnn#include <sys/resource.h>
35235368Sgnn#include <sys/wait.h>
36235368Sgnn
37235368Sgnn#include <atf-c.h>
38235368Sgnn#include <errno.h>
39235368Sgnn#include <limits.h>
40235368Sgnn#include <pthread.h>
41235368Sgnn#include <stdlib.h>
42235368Sgnn#include <unistd.h>
43235368Sgnn
44235368Sgnnstatic void	*threadfunc(void *);
45235368Sgnn
46235368Sgnnstatic void *
47235368Sgnnthreadfunc(void *arg)
48235368Sgnn{
49235368Sgnn	int pri, val;
50235368Sgnn
51235368Sgnn	val = *(int *)arg;
52235368Sgnn
53235368Sgnn	errno = 0;
54235368Sgnn	pri = getpriority(PRIO_PROCESS, 0);
55235368Sgnn	ATF_REQUIRE(errno == 0);
56235368Sgnn
57235368Sgnn	if (pri != val)
58235368Sgnn		atf_tc_fail("nice(3) value was not propagated to threads");
59235368Sgnn
60235368Sgnn	return NULL;
61235368Sgnn}
62235368Sgnn
63235368SgnnATF_TC(nice_err);
64235368SgnnATF_TC_HEAD(nice_err, tc)
65235368Sgnn{
66235368Sgnn	atf_tc_set_md_var(tc, "descr",
67235368Sgnn	    "Test nice(3) for invalid parameters (PR lib/42587)");
68235368Sgnn	atf_tc_set_md_var(tc, "require.user", "unprivileged");
69235368Sgnn}
70235368Sgnn
71235368SgnnATF_TC_BODY(nice_err, tc)
72235368Sgnn{
73235368Sgnn	int i;
74235368Sgnn
75235368Sgnn	/*
76235368Sgnn	 * The call should fail with EPERM if the
77235368Sgnn	 * supplied parameter is negative and the
78235368Sgnn	 * caller does not have privileges.
79235368Sgnn	 */
80235368Sgnn	for (i = -20; i < 0; i++) {
81235368Sgnn
82235368Sgnn		errno = 0;
83235368Sgnn
84235368Sgnn		ATF_REQUIRE_ERRNO(EPERM, nice(i) == -1);
85235368Sgnn	}
86235368Sgnn}
87235368Sgnn
88235368SgnnATF_TC(nice_priority);
89235368SgnnATF_TC_HEAD(nice_priority, tc)
90235368Sgnn{
91235368Sgnn	atf_tc_set_md_var(tc, "descr", "Test nice(3) vs. getpriority(2)");
92235368Sgnn}
93
94ATF_TC_BODY(nice_priority, tc)
95{
96#ifdef __FreeBSD__
97	int i, pri, pri2, nic;
98#else
99	int i, pri, nic;
100#endif
101	pid_t pid;
102	int sta;
103
104	for (i = 0; i <= 20; i++) {
105
106		nic = nice(i);
107		ATF_REQUIRE(nic != -1);
108
109		errno = 0;
110		pri = getpriority(PRIO_PROCESS, 0);
111		ATF_REQUIRE(errno == 0);
112
113#ifdef __NetBSD__
114		if (nic != pri)
115			atf_tc_fail("nice(3) and getpriority(2) conflict");
116#endif
117
118		/*
119		 * Also verify that the nice(3) values
120		 * are inherited by child processes.
121		 */
122		pid = fork();
123		ATF_REQUIRE(pid >= 0);
124
125		if (pid == 0) {
126
127			errno = 0;
128#ifdef __FreeBSD__
129			pri = getpriority(PRIO_PROCESS, 0);
130#else
131			pri2 = getpriority(PRIO_PROCESS, 0);
132#endif
133			ATF_REQUIRE(errno == 0);
134
135#ifdef __FreeBSD__
136			if (pri != pri2)
137#else
138			if (nic != pri)
139#endif
140				_exit(EXIT_FAILURE);
141
142			_exit(EXIT_SUCCESS);
143		}
144
145		(void)wait(&sta);
146
147		if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS)
148			atf_tc_fail("nice(3) value was not inherited");
149	}
150}
151
152ATF_TC(nice_root);
153ATF_TC_HEAD(nice_root, tc)
154{
155	atf_tc_set_md_var(tc, "descr", "Test that nice(3) works");
156	atf_tc_set_md_var(tc, "require.user", "root");
157}
158
159ATF_TC_BODY(nice_root, tc)
160{
161	int i;
162
163	for (i = -20; i <= 20; i++) {
164
165		ATF_REQUIRE(nice(i) != -1);
166	}
167}
168
169ATF_TC(nice_thread);
170ATF_TC_HEAD(nice_thread, tc)
171{
172	atf_tc_set_md_var(tc, "descr", "Test nice(3) with threads");
173}
174
175ATF_TC_BODY(nice_thread, tc)
176{
177	pthread_t tid[5];
178#ifdef __FreeBSD__
179	int pri, rv, val;
180#else
181	int rv, val;
182#endif
183	size_t i;
184
185	/*
186	 * Test that the scheduling priority is
187	 * propagated to all system scope threads.
188	 */
189	for (i = 0; i < __arraycount(tid); i++) {
190
191		val = nice(i);
192		ATF_REQUIRE(val != -1);
193
194#ifdef __FreeBSD__
195		pri = getpriority(PRIO_PROCESS, 0);
196		rv = pthread_create(&tid[i], NULL, threadfunc, &pri);
197#else
198		rv = pthread_create(&tid[i], NULL, threadfunc, &val);
199#endif
200		ATF_REQUIRE(rv == 0);
201
202		rv = pthread_join(tid[i], NULL);
203		ATF_REQUIRE(rv == 0);
204	}
205}
206
207ATF_TP_ADD_TCS(tp)
208{
209
210	ATF_TP_ADD_TC(tp, nice_err);
211	ATF_TP_ADD_TC(tp, nice_priority);
212	ATF_TP_ADD_TC(tp, nice_root);
213	ATF_TP_ADD_TC(tp, nice_thread);
214
215	return atf_no_error();
216}
217