setpriority05.c revision bdbaec51a423e715c2b03ed9e497e9a1fba6103e
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20/*
21 * NAME
22 *	setpriority05.c
23 *
24 * DESCRIPTION
25 *	setpriority05 - test for an expected failure by trying to change
26 *			a process with an ID that is different from the
27 *			test process
28 *
29 * ALGORITHM
30 *	loop if that option was specified
31 *	issue the system call with init's process id (1)
32 *	check the errno value
33 *	  issue a PASS message if we get EPERM - errno 1
34 *	otherwise, the tests fails
35 *	  issue a FAIL message
36 *	  break any remaining tests
37 *	  call cleanup
38 *
39 * USAGE:  <for command-line>
40 *  setpriority05 [-c n] [-e] [-i n] [-I x] [-p x] [-t]
41 *     where,  -c n : Run n copies concurrently.
42 *             -e   : Turn on errno logging.
43 *	       -i n : Execute test n times.
44 *	       -I x : Execute test for x seconds.
45 *	       -P x : Pause for x seconds between iterations.
46 *	       -t   : Turn on syscall timing.
47 *
48 * HISTORY
49 *	03/2001 - Written by Wayne Boyer
50 *
51 * RESTRICTIONS
52 *	none
53 */
54
55#include "test.h"
56#include "usctest.h"
57
58#include <errno.h>
59#include <sys/time.h>
60#include <sys/resource.h>
61#include <pwd.h>
62
63void cleanup(void);
64void setup(void);
65
66char *TCID= "setpriority05";
67int TST_TOTAL = 1;
68extern int Tst_count;
69char nobody_uid[] = "nobody";
70struct passwd *ltpuser;
71
72int exp_enos[] = {EPERM, 0};
73
74int main(int ac, char **av)
75{
76	int lc;				/* loop counter */
77	char *msg;			/* message returned from parse_opts */
78	int new_val = 2;
79	int init_val = 1;		/* the init process = id 1 */
80
81	/* parse standard options */
82	if ((msg = parse_opts(ac, av, (option_t *)NULL, NULL)) != (char *)NULL){
83		tst_brkm(TBROK, tst_exit, "OPTION PARSING ERROR - %s", msg);
84	}
85
86	setup();			/* global setup */
87
88	/* The following loop checks looping state if -i option given */
89
90	for (lc = 0; TEST_LOOPING(lc); lc++) {
91		/* reset Tst_count in case we are looping */
92		Tst_count = 0;
93
94		/*
95		 * Try to access the init process.
96		 * This should give an EPERM error.
97		 */
98
99		/* call the system call with the TEST() macro */
100		TEST(setpriority(PRIO_PROCESS, init_val, new_val));
101
102		if (TEST_RETURN == 0) {
103	                tst_resm(TFAIL, "call failed to produce expected error "
104				 "- errno = %d - %s", TEST_ERRNO,
105				 strerror(TEST_ERRNO));
106		}
107
108		TEST_ERROR_LOG(TEST_ERRNO);
109
110		switch (TEST_ERRNO) {
111		case EPERM:
112			tst_resm(TPASS, "expected failure - errno = %d - %s",
113				 TEST_ERRNO, strerror(TEST_ERRNO));
114			break;
115		default:
116			tst_resm(TFAIL, "call failed to produce expected error "
117				 "- errno = %d - %s", TEST_ERRNO,
118				 strerror(TEST_ERRNO));
119		}
120	}
121	cleanup();
122
123	/*NOTREACHED*/
124
125  return 0;
126
127}
128
129/*
130 * setup() - performs all the ONE TIME setup for this test.
131 */
132void
133setup(void)
134{
135	/* Switch to nobody user for correct error code collection */
136        if (geteuid() != 0) {
137                tst_brkm(TBROK, tst_exit, "Test must be run as root");
138        }
139         ltpuser = getpwnam(nobody_uid);
140         if (setuid(ltpuser->pw_uid) == -1) {
141                tst_resm(TINFO, "setuid failed to "
142                         "to set the effective uid to %d",
143                         ltpuser->pw_uid);
144                perror("setuid");
145         }
146
147
148	/* capture signals */
149	tst_sig(NOFORK, DEF_HANDLER, cleanup);
150
151	/* Set up the expected error numbers */
152	TEST_EXP_ENOS(exp_enos);
153
154	/* Pause if that option was specified */
155	TEST_PAUSE;
156}
157
158/*
159 * cleanup() - performs all the ONE TIME cleanup for this test at completion
160 * 	       or premature exit.
161 */
162void
163cleanup(void)
164{
165	/*
166	 * print timing stats if that option was specified.
167	 * print errno log if that option was specified.
168	 */
169	TEST_CLEANUP;
170
171	/* exit with return code appropriate for results */
172	tst_exit();
173}
174