setpriority05.c revision d4ceb37d3ab506483612ef0ad74c88e5828a9779
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 * 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;
68char nobody_uid[] = "nobody";
69struct passwd *ltpuser;
70
71int exp_enos[] = { EPERM, 0 };
72
73int main(int ac, char **av)
74{
75	int lc;
76	const char *msg;
77	int new_val = 2;
78	int init_val = 1;	/* the init process = id 1 */
79
80	if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) {
81		tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
82	}
83
84	setup();		/* global setup */
85
86	/* The following loop checks looping state if -i option given */
87
88	for (lc = 0; TEST_LOOPING(lc); lc++) {
89		/* reset tst_count in case we are looping */
90		tst_count = 0;
91
92		/*
93		 * Try to access the init process.
94		 * This should give an EPERM error.
95		 */
96
97		/* call the system call with the TEST() macro */
98		TEST(setpriority(PRIO_PROCESS, init_val, new_val));
99
100		if (TEST_RETURN == 0) {
101			tst_resm(TFAIL, "call failed to produce expected error "
102				 "- errno = %d - %s", TEST_ERRNO,
103				 strerror(TEST_ERRNO));
104		}
105
106		TEST_ERROR_LOG(TEST_ERRNO);
107
108		switch (TEST_ERRNO) {
109		case EPERM:
110			tst_resm(TPASS, "expected failure - errno = %d - %s",
111				 TEST_ERRNO, strerror(TEST_ERRNO));
112			break;
113		default:
114			tst_resm(TFAIL, "call failed to produce expected error "
115				 "- errno = %d - %s", TEST_ERRNO,
116				 strerror(TEST_ERRNO));
117		}
118	}
119	cleanup();
120	tst_exit();
121
122}
123
124/*
125 * setup() - performs all the ONE TIME setup for this test.
126 */
127void setup(void)
128{
129	tst_require_root(NULL);
130
131	/* Switch to nobody user for correct error code collection */
132	ltpuser = getpwnam(nobody_uid);
133	if (setuid(ltpuser->pw_uid) == -1) {
134		tst_resm(TINFO, "setuid failed to "
135			 "to set the effective uid to %d", ltpuser->pw_uid);
136		perror("setuid");
137	}
138
139	tst_sig(NOFORK, DEF_HANDLER, cleanup);
140
141	TEST_EXP_ENOS(exp_enos);
142
143	TEST_PAUSE;
144}
145
146/*
147 * cleanup() - performs all the ONE TIME cleanup for this test at completion
148 *	       or premature exit.
149 */
150void cleanup(void)
151{
152	/*
153	 * print timing stats if that option was specified.
154	 * print errno log if that option was specified.
155	 */
156	TEST_CLEANUP;
157
158}
159