1 /* $NetBSD: t_nice.c,v 1.8 2012/03/18 07:00:51 jruoho Exp $ */
4 * Copyright (c) 2011 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: t_nice.c,v 1.8 2012/03/18 07:00:51 jruoho Exp $");
34 #include <sys/resource.h>
44 static void *threadfunc(void *);
54 pri
= getpriority(PRIO_PROCESS
, 0);
55 ATF_REQUIRE(errno
== 0);
58 atf_tc_fail("nice(3) value was not propagated to threads");
64 ATF_TC_HEAD(nice_err
, tc
)
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");
71 ATF_TC_BODY(nice_err
, tc
)
76 * The call should fail with EPERM if the
77 * supplied parameter is negative and the
78 * caller does not have privileges.
80 for (i
= -20; i
< 0; i
++) {
84 ATF_REQUIRE_ERRNO(EPERM
, nice(i
) == -1);
88 ATF_TC(nice_priority
);
89 ATF_TC_HEAD(nice_priority
, tc
)
91 atf_tc_set_md_var(tc
, "descr", "Test nice(3) vs. getpriority(2)");
94 ATF_TC_BODY(nice_priority
, tc
)
100 for (i
= 0; i
<= 20; i
++) {
103 ATF_REQUIRE(nic
!= -1);
106 pri
= getpriority(PRIO_PROCESS
, 0);
107 ATF_REQUIRE(errno
== 0);
110 atf_tc_fail("nice(3) and getpriority(2) conflict");
113 * Also verify that the nice(3) values
114 * are inherited by child processes.
117 ATF_REQUIRE(pid
>= 0);
122 pri
= getpriority(PRIO_PROCESS
, 0);
123 ATF_REQUIRE(errno
== 0);
133 if (WIFEXITED(sta
) == 0 || WEXITSTATUS(sta
) != EXIT_SUCCESS
)
134 atf_tc_fail("nice(3) value was not inherited");
139 ATF_TC_HEAD(nice_root
, tc
)
141 atf_tc_set_md_var(tc
, "descr", "Test that nice(3) works");
142 atf_tc_set_md_var(tc
, "require.user", "root");
145 ATF_TC_BODY(nice_root
, tc
)
149 for (i
= -20; i
<= 20; i
++) {
151 ATF_REQUIRE(nice(i
) != -1);
156 ATF_TC_HEAD(nice_thread
, tc
)
158 atf_tc_set_md_var(tc
, "descr", "Test nice(3) with threads");
161 ATF_TC_BODY(nice_thread
, tc
)
168 * Test that the scheduling priority is
169 * propagated to all system scope threads.
171 for (i
= 0; i
< __arraycount(tid
); i
++) {
174 ATF_REQUIRE(val
!= -1);
176 rv
= pthread_create(&tid
[i
], NULL
, threadfunc
, &val
);
177 ATF_REQUIRE(rv
== 0);
179 rv
= pthread_join(tid
[i
], NULL
);
180 ATF_REQUIRE(rv
== 0);
187 ATF_TP_ADD_TC(tp
, nice_err
);
188 ATF_TP_ADD_TC(tp
, nice_priority
);
189 ATF_TP_ADD_TC(tp
, nice_root
);
190 ATF_TP_ADD_TC(tp
, nice_thread
);
192 return atf_no_error();