1 /* $NetBSD: t_nice.c,v 1.4 2011/04/10 10:59:13 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.4 2011/04/10 10:59:13 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 <stdlib.h> 41 #include <unistd.h> 42 43 ATF_TC(nice_err); 44 ATF_TC_HEAD(nice_err, tc) 45 { 46 atf_tc_set_md_var(tc, "descr", "Test nice(3) for invalid parameters"); 47 atf_tc_set_md_var(tc, "require.user", "unprivileged"); 48 } 49 50 ATF_TC_BODY(nice_err, tc) 51 { 52 int i; 53 54 /* 55 * The call should fail with EPERM if the 56 * supplied parameter is negative and the 57 * caller does not have privileges. Note 58 * that the errno is thus "wrong" in NetBSD. 59 */ 60 atf_tc_expect_fail("PR lib/42587"); 61 62 for (i = -20; i < 0; i++) { 63 64 errno = 0; 65 66 ATF_REQUIRE(nice(i) == -1); 67 68 if (errno != EPERM) 69 atf_tc_fail("wrong errno"); 70 } 71 } 72 73 ATF_TC(nice_priority); 74 ATF_TC_HEAD(nice_priority, tc) 75 { 76 atf_tc_set_md_var(tc, "descr", "Test nice(3) vs. getpriority(2)"); 77 } 78 79 ATF_TC_BODY(nice_priority, tc) 80 { 81 int i, pri, nic; 82 pid_t pid; 83 int sta; 84 85 for (i = 0; i <= 20; i++) { 86 87 nic = nice(i); 88 ATF_REQUIRE(nic != -1); 89 90 errno = 0; 91 pri = getpriority(PRIO_PROCESS, 0); 92 ATF_REQUIRE(errno == 0); 93 94 if (nic != pri) 95 atf_tc_fail("nice(3) and getpriority(2) conflict"); 96 97 /* 98 * Also verify that the nice(3) values 99 * are inherited by child processes. 100 */ 101 pid = fork(); 102 ATF_REQUIRE(pid >= 0); 103 104 if (pid == 0) { 105 106 errno = 0; 107 pri = getpriority(PRIO_PROCESS, 0); 108 ATF_REQUIRE(errno == 0); 109 110 if (nic != pri) 111 _exit(EXIT_FAILURE); 112 113 _exit(EXIT_SUCCESS); 114 } 115 116 (void)wait(&sta); 117 118 if (WIFEXITED(sta) == 0 || WEXITSTATUS(sta) != EXIT_SUCCESS) 119 atf_tc_fail("nice(3) value was not inherited"); 120 } 121 } 122 123 ATF_TC(nice_root); 124 ATF_TC_HEAD(nice_root, tc) 125 { 126 atf_tc_set_md_var(tc, "descr", "Test that nice(3) works"); 127 atf_tc_set_md_var(tc, "require.user", "root"); 128 } 129 130 ATF_TC_BODY(nice_root, tc) 131 { 132 int i; 133 134 for (i = -20; i <= 20; i++) { 135 136 ATF_REQUIRE(nice(i) != -1); 137 } 138 } 139 140 ATF_TP_ADD_TCS(tp) 141 { 142 143 ATF_TP_ADD_TC(tp, nice_err); 144 ATF_TP_ADD_TC(tp, nice_priority); 145 ATF_TP_ADD_TC(tp, nice_root); 146 147 return atf_no_error(); 148 } 149