chmod04.c revision 45e285d46ab47b0ff76c88acb5ba97b0bd5f753d
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 * Test Name: chmod04
22 *
23 * Test Description:
24 *  Verify that, chmod(2) will succeed to change the mode of a directory
25 *  and set the sticky bit on it if invoked by non-root (uid != 0) process
26 *  with the following constraints,
27 *	- the process is the owner of the directory.
28 *	- the effective group ID or one of the supplementary group ID's of the
29 *	  process is equal to the group ID of the directory.
30 *
31 * Expected Result:
32 *  chmod() should return value 0 on success and succeeds to set sticky bit
33 *  on the specified directory.
34 *
35 * Algorithm:
36 *  Setup:
37 *   Setup signal handling.
38 *   Create temporary directory.
39 *   Pause for SIGUSR1 if option specified.
40 *
41 *  Test:
42 *   Loop if the proper options are given.
43 *   Execute system call
44 *   Check return code, if system call failed (return=-1)
45 *   	Log the errno and Issue a FAIL message.
46 *   Otherwise,
47 *   	Verify the Functionality of system call
48 *      if successful,
49 *      	Issue Functionality-Pass message.
50 *      Otherwise,
51 *		Issue Functionality-Fail message.
52 *  Cleanup:
53 *   Print errno log and/or timing stats if options given
54 *   Delete the temporary directory created.
55 *
56 * Usage:  <for command-line>
57 *  chmod04 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
58 *     where,  -c n : Run n copies concurrently.
59 *             -f   : Turn off functionality Testing.
60 *	       -i n : Execute test n times.
61 *	       -I x : Execute test for x seconds.
62 *	       -P x : Pause for x seconds between iterations.
63 *	       -t   : Turn on syscall timing.
64 *
65 * HISTORY
66 *	07/2001 Ported by Wayne Boyer
67 *
68 * RESTRICTIONS:
69 *  This test should be run by 'non-super-user' only.
70 *
71 */
72
73#include <stdio.h>
74#include <sys/types.h>
75#include <sys/stat.h>
76#include <sys/fcntl.h>
77#include <errno.h>
78#include <string.h>
79#include <signal.h>
80#include <pwd.h>
81
82#include "test.h"
83#include "usctest.h"
84
85#define DIR_MODE 	S_IRWXU | S_IRWXG | S_IRWXO
86#define PERMS		01777	/*
87				 * Mode permissions of test directory with
88				 * sticky bit set.
89				 */
90#define TESTDIR		"testdir_4"
91
92char *TCID = "chmod04";		/* Test program identifier.    */
93int TST_TOTAL = 1;		/* Total number of test cases. */
94extern int Tst_count;		/* Test Case counter for tst_* routines */
95char nobody_uid[] = "nobody";
96struct passwd *ltpuser;
97
98void setup();			/* Setup function for the test */
99void cleanup();			/* Cleanup function for the test */
100
101int main(int ac, char **av)
102{
103	struct stat stat_buf;	/* stat struct. */
104	int lc;			/* loop counter */
105	char *msg;		/* message returned from parse_opts */
106	mode_t dir_mode;	/* mode permissions set on testdirectory */
107
108	/* Parse standard options given to run the test. */
109	msg = parse_opts(ac, av, NULL, NULL);
110	if (msg != NULL) {
111		tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
112		tst_exit();
113	}
114
115	/* Perform global setup for test */
116	setup();
117
118	/* Check looping state if -i option given */
119	for (lc = 0; TEST_LOOPING(lc); lc++) {
120		/* Reset Tst_count in case we are looping. */
121		Tst_count = 0;
122
123		/*
124		 * Call chmod(2) with mode argument to
125		 * set sticky bit on TESTDIR
126		 */
127		TEST(chmod(TESTDIR, PERMS));
128
129		/* check return code of chmod(2) */
130		if (TEST_RETURN == -1) {
131			tst_resm(TFAIL|TTERRNO, "chmod(%s, %#o) failed",
132				 TESTDIR, PERMS);
133			continue;
134		}
135
136		/*
137		 * Perform functional verification if test
138		 * executed without (-f) option.
139		 */
140		if (STD_FUNCTIONAL_TEST) {
141			/*
142			 * Get the file information using
143			 * stat(2).
144			 */
145			if (stat(TESTDIR, &stat_buf) < 0) {
146				tst_brkm(TFAIL, cleanup,
147					 "stat(2) of %s failed, errno:%d",
148					 TESTDIR, TEST_ERRNO);
149			}
150			dir_mode = stat_buf.st_mode;
151
152			/* Verify STICKY BIT SET on directory */
153			if ((dir_mode & PERMS) == PERMS) {
154				tst_resm(TPASS, "Functionality of "
155					 "chmod(%s, %#o) successful",
156					 TESTDIR, PERMS);
157			} else {
158				tst_resm(TFAIL, "%s: Incorrect modes 0%03o, "
159					 "Expected 0%03o",
160					 TESTDIR, dir_mode, PERMS);
161			}
162		} else {
163			tst_resm(TPASS, "call succeeded");
164		}
165	}			/* End for TEST_LOOPING */
166
167	/* Call cleanup() to undo setup done for the test. */
168	cleanup();
169
170	return 0;
171 /*NOTREACHED*/}		/* End main */
172
173/*
174 * void
175 * setup() - performs all ONE TIME setup for this test.
176 *  Create a temporary directory and cd to it.
177 *  Create another test directory under temporary directory.
178 */
179void setup()
180{
181	/* capture signals */
182	tst_sig(NOFORK, DEF_HANDLER, cleanup);
183
184	/* Switch to nobody user for correct error code collection */
185	if (geteuid() != 0) {
186		tst_brkm(TBROK, tst_exit, "Test must be run as root");
187	}
188	ltpuser = getpwnam(nobody_uid);
189	if (setuid(ltpuser->pw_uid) == -1)
190		tst_resm(TINFO|TERRNO, "setuid(%u) failed", ltpuser->pw_uid);
191
192	/* Pause if that option was specified */
193	TEST_PAUSE;
194
195	/* make a temp directory and cd to it */
196	tst_tmpdir();
197
198	/*
199	 * Create a test directory under temporary directory with specified
200	 * mode permissios.
201	 */
202	if (mkdir(TESTDIR, DIR_MODE) < 0) {
203		tst_brkm(TBROK, cleanup, "mkdir(2) of %s failed", TESTDIR);
204	}
205}				/* End setup() */
206
207/*
208 * void
209 * cleanup() - performs all ONE TIME cleanup for this test at
210 *		completion or premature exit.
211 *  Remove the test directory and temporary directory created in setup().
212 */
213void cleanup()
214{
215	/*
216	 * print timing stats if that option was specified.
217	 */
218	TEST_CLEANUP;
219
220	/* Remove tmp dir and all files in it */
221	tst_rmdir();
222
223	/* exit with return code appropriate for results */
224	tst_exit();
225}				/* End cleanup() */
226