Home | History | Annotate | Download | only in nice
      1 /*
      2  *
      3  *   Copyright (c) International Business Machines  Corp., 2001
      4  *
      5  *   This program is free software;  you can redistribute it and/or modify
      6  *   it under the terms of the GNU General Public License as published by
      7  *   the Free Software Foundation; either version 2 of the License, or
      8  *   (at your option) any later version.
      9  *
     10  *   This program is distributed in the hope that it will be useful,
     11  *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
     12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
     13  *   the GNU General Public License for more details.
     14  *
     15  *   You should have received a copy of the GNU General Public License
     16  *   along with this program;  if not, write to the Free Software
     17  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
     18  */
     19 
     20 /*
     21  * Test Name: nice04
     22  *
     23  * Test Description:
     24  *  Verify that, nice(2) fails when, a non-root user attempts to increase
     25  *  the priority of a process by specifying a negative increment value.
     26  *
     27  * Expected Result:
     28  *  nice() returns -1 and sets errno to EPERM.
     29  *
     30  * Algorithm:
     31  *  Setup:
     32  *   Setup signal handling.
     33  *   Pause for SIGUSR1 if option specified.
     34  *
     35  *  Test:
     36  *   Loop if the proper options are given.
     37  *   Execute system call
     38  *   Check return code, if system call failed (return=-1)
     39  *   	if errno set == expected errno
     40  *		PASS
     41  *   		Issue sys call fails with expected return value and errno.
     42  *   	Otherwise,
     43  *		FAIL
     44  *		Issue sys call fails with unexpected errno.
     45  *   Otherwise,
     46  *	Issue sys call returns unexpected value.
     47  *
     48  *  Cleanup:
     49  *   Print errno log and/or timing stats if options given
     50  *
     51  * Usage:  <for command-line>
     52  *  nice04 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
     53  *     where,  -c n : Run n copies concurrently.
     54  *             -e   : Turn on errno logging.
     55  *	       -i n : Execute test n times.
     56  *	       -I x : Execute test for x seconds.
     57  *	       -P x : Pause for x seconds between iterations.
     58  *	       -t   : Turn on syscall timing.
     59  *
     60  * HISTORY
     61  *	07/2001 Ported by Wayne Boyer
     62  *
     63  * RESTRICTIONS:
     64  *  This test should be executed by 'non-super-user' only.
     65  */
     66 #include <pwd.h>
     67 #include <unistd.h>
     68 #include <errno.h>
     69 
     70 #include "test.h"
     71 
     72 char *TCID = "nice04";
     73 int TST_TOTAL = 1;
     74 
     75 char nobody_uid[] = "nobody";
     76 struct passwd *ltpuser;
     77 
     78 struct test_case_t {		/* test case struct. to hold ref. test cond's */
     79 	int nice_val;
     80 	char *desc;
     81 	int exp_errno;
     82 } Test_cases[] = {
     83 	{
     84 	-5, "Non-root cannot specify higher priority", EPERM}
     85 };
     86 
     87 void setup();			/* Main setup function of test */
     88 void cleanup();			/* cleanup function for the test */
     89 
     90 int main(int ac, char **av)
     91 {
     92 	int lc;
     93 	int i;
     94 	int incr_val;		/* nice value for the process */
     95 	char *test_desc;	/* test specific error message */
     96 
     97 	tst_parse_opts(ac, av, NULL, NULL);
     98 
     99 	setup();
    100 
    101 	for (lc = 0; TEST_LOOPING(lc); lc++) {
    102 
    103 		tst_count = 0;
    104 
    105 		for (i = 0; i < TST_TOTAL; i++) {
    106 			incr_val = Test_cases[i].nice_val;
    107 			test_desc = Test_cases[i].desc;
    108 
    109 			/*
    110 			 * Call nice(2) with different 'incr' parameter
    111 			 * values and verify that it fails as expected.
    112 			 */
    113 			TEST(nice(incr_val));
    114 
    115 			/* check return code from nice(2) */
    116 			if (TEST_RETURN == -1) {
    117 				tst_resm(TPASS, "nice(2) returned %ld for %s",
    118 					 TEST_RETURN, test_desc);
    119 			} else {
    120 				tst_resm(TFAIL | TTERRNO,
    121 					 "nice() returned %ld for %s",
    122 					 TEST_RETURN, test_desc);
    123 			}
    124 		}
    125 	}
    126 
    127 	cleanup();
    128 	tst_exit();
    129 
    130 }
    131 
    132 /*
    133  * setup() - performs all ONE TIME setup for this test.
    134  *  Make sure the test process uid is non-root only.
    135  */
    136 void setup(void)
    137 {
    138 	tst_require_root();
    139 
    140 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
    141 
    142 	/* Switch to nobody user for correct error code collection */
    143 	ltpuser = getpwnam(nobody_uid);
    144 	if (setuid(ltpuser->pw_uid) == -1) {
    145 		tst_resm(TINFO, "setuid failed to "
    146 			 "to set the effective uid to %d", ltpuser->pw_uid);
    147 		perror("setuid");
    148 	}
    149 
    150 	TEST_PAUSE;
    151 }
    152 
    153 /*
    154  * cleanup() - performs all ONE TIME cleanup for this test at
    155  *             completion or premature exit.
    156  */
    157 void cleanup(void)
    158 {
    159 
    160 }
    161