Home | History | Annotate | Download | only in sched_setparam
      1 /*
      2  * Copyright (c) Wipro Technologies Ltd, 2002.  All Rights Reserved.
      3  *
      4  * This program is free software; you can redistribute it and/or modify it
      5  * under the terms of version 2 of the GNU General Public License as
      6  * published by the Free Software Foundation.
      7  *
      8  * This program is distributed in the hope that it would be useful, but
      9  * WITHOUT ANY WARRANTY; without even the implied warranty of
     10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
     11  *
     12  * You should have received a copy of the GNU General Public License along
     13  * with this program; if not, write the Free Software Foundation, Inc.,
     14  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
     15  *
     16  */
     17 /**********************************************************
     18  *
     19  *    TEST IDENTIFIER	: sched_setparam05
     20  *
     21  *    EXECUTED BY	: root/superuser
     22  *
     23  *    TEST TITLE	: verify that sched_setparam() fails if the user does
     24  *			  not have proper privilages
     25  *
     26  *    TEST CASE TOTAL	: 1
     27  *
     28  *    AUTHOR		: Saji Kumar.V.R <saji.kumar (at) wipro.com>
     29  *
     30  *    SIGNALS
     31  * 	Uses SIGUSR1 to pause before test if option set.
     32  * 	(See the parse_opts(3) man page).
     33  *
     34  *    DESCRIPTION
     35  *	Verify that sched_setparam() fails if the user does
     36  *	not have proper privilages
     37  *
     38  * 	Setup:
     39  * 	  Setup signal handling.
     40  *	  Pause for SIGUSR1 if option specified.
     41  *
     42  * 	Test:
     43  *	 Loop if the proper options are given.
     44  *	 Fork a child
     45  *
     46  *	 CHILD:
     47  *	  Changes euid to "nobody" user.
     48  *	  Try to Change scheduling priority for parent
     49  *	  If call failed with errno = EPERM,
     50  *		Test passed
     51  *	  else
     52  *		Test failed
     53  *
     54  *	 PARENT:
     55  *		wait for child to finish
     56  *
     57  * 	Cleanup:
     58  * 	  Print errno log and/or timing stats if options given
     59  *
     60  * USAGE:  <for command-line>
     61  *  sched_setparam05 [-c n] [-e] [-i n] [-I x] [-P x] [-t] [-h] [-f] [-p]
     62  *			where,  -c n : Run n copies concurrently.
     63  *				-e   : Turn on errno logging.
     64  *				-h   : Show help screen
     65  *				-f   : Turn off functional testing
     66  *				-i n : Execute test n times.
     67  *				-I x : Execute test for x seconds.
     68  *				-p   : Pause for SIGUSR1 before starting
     69  *				-P x : Pause for x seconds between iterations.
     70  *				-t   : Turn on syscall timing.
     71  *
     72  ****************************************************************/
     73 
     74 #include <errno.h>
     75 #include <sched.h>
     76 #include <pwd.h>
     77 #include <sys/wait.h>
     78 #include "test.h"
     79 
     80 static void setup();
     81 static void cleanup();
     82 
     83 char *TCID = "sched_setparam05";
     84 int TST_TOTAL = 1;
     85 
     86 static struct sched_param param = { 0 };
     87 
     88 static char nobody_uid[] = "nobody";
     89 struct passwd *ltpuser;
     90 
     91 int main(int ac, char **av)
     92 {
     93 
     94 	int lc;
     95 	int status;
     96 	pid_t child_pid;
     97 
     98 	tst_parse_opts(ac, av, NULL, NULL);
     99 
    100 	setup();
    101 
    102 	for (lc = 0; TEST_LOOPING(lc); lc++) {
    103 
    104 		tst_count = 0;
    105 
    106 		switch (child_pid = FORK_OR_VFORK()) {
    107 
    108 		case -1:
    109 			/* fork() failed */
    110 			tst_resm(TFAIL, "fork() failed");
    111 			continue;
    112 
    113 		case 0:
    114 			/* Child */
    115 
    116 			/* Switch to nobody user */
    117 			if ((ltpuser = getpwnam(nobody_uid)) == NULL) {
    118 				tst_brkm(TBROK, NULL, "\"nobody\" user"
    119 					 "not present");
    120 			}
    121 			if (seteuid(ltpuser->pw_uid) == -1) {
    122 				tst_resm(TWARN, "seteuid failed to "
    123 					 "to set the effective uid to %d",
    124 					 ltpuser->pw_uid);
    125 				exit(1);
    126 			}
    127 
    128 			/*
    129 			 * Call sched_setparam(2) with pid = getppid()
    130 			 */
    131 			TEST(sched_setparam(getppid(), &param));
    132 
    133 			if ((TEST_RETURN == -1) && (TEST_ERRNO == EPERM)) {
    134 				exit(0);
    135 			}
    136 
    137 			tst_resm(TWARN | TTERRNO,
    138 				 "Test failed, sched_setparam()"
    139 				 " returned : %ld", TEST_RETURN);
    140 			exit(1);
    141 
    142 		default:
    143 			/* Parent */
    144 			if ((waitpid(child_pid, &status, 0)) < 0) {
    145 				tst_resm(TFAIL, "wait() failed");
    146 				continue;
    147 			}
    148 			if ((WIFEXITED(status)) && (WEXITSTATUS(status) == 0)) {
    149 				tst_resm(TPASS, "Test passed, Got EPERM");
    150 			} else {
    151 				tst_resm(TFAIL, "Test Failed");
    152 			}
    153 		}
    154 	}
    155 
    156 	cleanup();
    157 	tst_exit();
    158 }
    159 
    160 /* setup() - performs all ONE TIME setup for this test */
    161 void setup(void)
    162 {
    163 
    164 	tst_require_root();
    165 
    166 	tst_sig(FORK, DEF_HANDLER, cleanup);
    167 
    168 	TEST_PAUSE;
    169 
    170 }
    171 
    172 /*
    173  *cleanup() -  performs all ONE TIME cleanup for this test at
    174  *		completion or premature exit.
    175  */
    176 void cleanup(void)
    177 {
    178 }
    179