Home | History | Annotate | Download | only in stime
      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: stime02
     22  *
     23  * Test Description:
     24  *   Verify that the system call stime() fails to set the system's idea
     25  *   of data and time if invoked by "non-root" user.
     26  *
     27  * Expected Result:
     28  *  stime() should fail with return value -1 and set 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  *   		Issue sys call fails with expected return value and errno.
     41  *   	Otherwise,
     42  *		Issue sys call fails with unexpected errno.
     43  *   Otherwise,
     44  *	Issue sys call returns unexpected value.
     45  *
     46  *  Cleanup:
     47  *   Print errno log and/or timing stats if options given
     48  *
     49  * Usage:  <for command-line>
     50  *  stime02 [-c n] [-e] [-i n] [-I x] [-p x] [-t]
     51  *	where,  -c n : Run n copies concurrently.
     52  *		-e   : Turn on errno logging.
     53  *		-i n : Execute test n times.
     54  *		-I x : Execute test for x seconds.
     55  *		-P x : Pause for x seconds between iterations.
     56  *		-t   : Turn on syscall timing.
     57  *
     58  * History
     59  *	07/2001 John George
     60  *		-Ported
     61  *
     62  * Restrictions:
     63  */
     64 
     65 #include <stdio.h>
     66 #include <unistd.h>
     67 #include <sys/types.h>
     68 #include <errno.h>
     69 #include <fcntl.h>
     70 #include <time.h>
     71 #include <string.h>
     72 #include <sys/stat.h>
     73 #include <signal.h>
     74 #include <pwd.h>
     75 
     76 #include "test.h"
     77 
     78 #define INCR_TIME	10	/* increment in the system's current time */
     79 
     80 char *TCID = "stime02";
     81 int TST_TOTAL = 1;
     82 
     83 time_t curr_time;		/* system's current time in seconds */
     84 time_t new_time;		/* system's new time */
     85 time_t tloc;			/* argument var. for time() */
     86 char nobody_uid[] = "nobody";
     87 struct passwd *ltpuser;
     88 
     89 void setup();			/* Main setup function of test */
     90 void cleanup();			/* cleanup function for the test */
     91 
     92 int main(int ac, char **av)
     93 {
     94 	int lc;
     95 
     96 	tst_parse_opts(ac, av, NULL, NULL);
     97 
     98 	setup();
     99 
    100 	for (lc = 0; TEST_LOOPING(lc); lc++) {
    101 
    102 		tst_count = 0;
    103 
    104 		/*
    105 		 * Invoke stime(2) to set the system's time
    106 		 * to the specified new_time as non-root user.
    107 		 */
    108 		TEST(stime(&new_time));
    109 
    110 		if (TEST_RETURN == -1) {
    111 			if (TEST_ERRNO == EPERM) {
    112 				tst_resm(TPASS, "stime(2) fails, Caller not "
    113 					 "root, errno:%d", TEST_ERRNO);
    114 			} else {
    115 				tst_resm(TFAIL, "stime(2) fails, Caller not "
    116 					 "root, errno:%d, expected errno:%d",
    117 					 TEST_ERRNO, EPERM);
    118 			}
    119 		} else {
    120 			tst_resm(TFAIL, "stime(2) returned %ld, expected -1, "
    121 				 "errno:%d", TEST_RETURN, EPERM);
    122 		}
    123 		tst_count++;	/* incr TEST_LOOP counter */
    124 	}
    125 
    126 	cleanup();
    127 	tst_exit();
    128 
    129 }
    130 
    131 /*
    132  * void
    133  * setup() - performs all ONE TIME setup for this test.
    134  *  Get the current time and system's new time.
    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 	/* Get the current time */
    153 	if ((curr_time = time(&tloc)) < 0) {
    154 		tst_brkm(TBROK, cleanup,
    155 			 "time() failed to get current time, errno=%d", errno);
    156 	}
    157 
    158 	/* Get the system's new time */
    159 	new_time = curr_time + INCR_TIME;
    160 }
    161 
    162 /*
    163  * void
    164  * cleanup() - performs all ONE TIME cleanup for this test at
    165  *             completion or premature exit.
    166  */
    167 void cleanup(void)
    168 {
    169 
    170 }
    171