blob: 3cd0c71cf3c49e1a8596036706de321a97b91a40 [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
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
Wanlong Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
plars865695b2001-08-27 22:15:12 +000018 */
19
subrata_modak4bb656a2009-02-26 12:02:09 +000020/*
plars865695b2001-08-27 22:15:12 +000021 * 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.
subrata_modakbdbaec52009-02-26 12:14:51 +000030 *
plars865695b2001-08-27 22:15:12 +000031 * 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,
subrata_modakbdbaec52009-02-26 12:14:51 +000047 * Verify the Functionality of system call
plars865695b2001-08-27 22:15:12 +000048 * 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>
robbiewa7142d92001-08-28 18:21:45 +000080#include <pwd.h>
plars865695b2001-08-27 22:15:12 +000081
82#include "test.h"
plars865695b2001-08-27 22:15:12 +000083
84#define DIR_MODE S_IRWXU | S_IRWXG | S_IRWXO
85#define PERMS 01777 /*
subrata_modak4bb656a2009-02-26 12:02:09 +000086 * Mode permissions of test directory with
plars865695b2001-08-27 22:15:12 +000087 * sticky bit set.
88 */
89#define TESTDIR "testdir_4"
90
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020091char *TCID = "chmod04";
92int TST_TOTAL = 1;
robbiewa7142d92001-08-28 18:21:45 +000093char nobody_uid[] = "nobody";
94struct passwd *ltpuser;
plars865695b2001-08-27 22:15:12 +000095
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020096void setup();
97void cleanup();
plars865695b2001-08-27 22:15:12 +000098
subrata_modak56207ce2009-03-23 13:35:39 +000099int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +0000100{
101 struct stat stat_buf; /* stat struct. */
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200102 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200103 const char *msg;
plars865695b2001-08-27 22:15:12 +0000104 mode_t dir_mode; /* mode permissions set on testdirectory */
subrata_modak56207ce2009-03-23 13:35:39 +0000105
Garrett Cooperc5f40232010-12-17 12:26:46 -0800106 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
plars865695b2001-08-27 22:15:12 +0000107 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
Garrett Cooper2c282152010-12-16 00:55:50 -0800108
plars865695b2001-08-27 22:15:12 +0000109 setup();
110
plars865695b2001-08-27 22:15:12 +0000111 for (lc = 0; TEST_LOOPING(lc); lc++) {
Garrett Cooper2c282152010-12-16 00:55:50 -0800112
Caspar Zhangd59a6592013-03-07 14:59:12 +0800113 tst_count = 0;
plars865695b2001-08-27 22:15:12 +0000114
subrata_modak4bb656a2009-02-26 12:02:09 +0000115 /*
subrata_modak56207ce2009-03-23 13:35:39 +0000116 * Call chmod(2) with mode argument to
plars865695b2001-08-27 22:15:12 +0000117 * set sticky bit on TESTDIR
subrata_modak56207ce2009-03-23 13:35:39 +0000118 */
plars865695b2001-08-27 22:15:12 +0000119 TEST(chmod(TESTDIR, PERMS));
subrata_modakbdbaec52009-02-26 12:14:51 +0000120
plars865695b2001-08-27 22:15:12 +0000121 if (TEST_RETURN == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800122 tst_resm(TFAIL | TTERRNO, "chmod(%s, %#o) failed",
vapier45996092009-08-28 11:49:07 +0000123 TESTDIR, PERMS);
plars865695b2001-08-27 22:15:12 +0000124 continue;
125 }
126
127 /*
Cyril Hrubise38b9612014-06-02 17:20:57 +0200128 * Get the file information using
129 * stat(2).
plars865695b2001-08-27 22:15:12 +0000130 */
Cyril Hrubise38b9612014-06-02 17:20:57 +0200131 if (stat(TESTDIR, &stat_buf) < 0) {
132 tst_brkm(TFAIL, cleanup,
133 "stat(2) of %s failed, errno:%d",
134 TESTDIR, TEST_ERRNO);
135 }
136 dir_mode = stat_buf.st_mode;
plars865695b2001-08-27 22:15:12 +0000137
Cyril Hrubise38b9612014-06-02 17:20:57 +0200138 /* Verify STICKY BIT SET on directory */
139 if ((dir_mode & PERMS) == PERMS) {
140 tst_resm(TPASS, "Functionality of "
141 "chmod(%s, %#o) successful",
142 TESTDIR, PERMS);
143 } else {
144 tst_resm(TFAIL, "%s: Incorrect modes 0%03o, "
145 "Expected 0%03o",
146 TESTDIR, dir_mode, PERMS);
147 }
Garrett Cooper2c282152010-12-16 00:55:50 -0800148 }
plars865695b2001-08-27 22:15:12 +0000149
plars865695b2001-08-27 22:15:12 +0000150 cleanup();
Garrett Cooperc5f40232010-12-17 12:26:46 -0800151 tst_exit();
152}
plars865695b2001-08-27 22:15:12 +0000153
154/*
155 * void
156 * setup() - performs all ONE TIME setup for this test.
157 * Create a temporary directory and cd to it.
158 * Create another test directory under temporary directory.
159 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400160void setup(void)
plars865695b2001-08-27 22:15:12 +0000161{
Garrett Cooper2c282152010-12-16 00:55:50 -0800162
plars865695b2001-08-27 22:15:12 +0000163 tst_sig(NOFORK, DEF_HANDLER, cleanup);
164
Garrett Cooperc5f40232010-12-17 12:26:46 -0800165 tst_require_root(NULL);
subrata_modak56207ce2009-03-23 13:35:39 +0000166 ltpuser = getpwnam(nobody_uid);
vapier45996092009-08-28 11:49:07 +0000167 if (setuid(ltpuser->pw_uid) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800168 tst_resm(TINFO | TERRNO, "setuid(%u) failed", ltpuser->pw_uid);
robbiewa7142d92001-08-28 18:21:45 +0000169
subrata_modak56207ce2009-03-23 13:35:39 +0000170 TEST_PAUSE;
plars865695b2001-08-27 22:15:12 +0000171
plars865695b2001-08-27 22:15:12 +0000172 tst_tmpdir();
173
174 /*
175 * Create a test directory under temporary directory with specified
176 * mode permissios.
177 */
178 if (mkdir(TESTDIR, DIR_MODE) < 0) {
179 tst_brkm(TBROK, cleanup, "mkdir(2) of %s failed", TESTDIR);
180 }
Garrett Cooper2c282152010-12-16 00:55:50 -0800181}
plars865695b2001-08-27 22:15:12 +0000182
183/*
184 * void
185 * cleanup() - performs all ONE TIME cleanup for this test at
186 * completion or premature exit.
187 * Remove the test directory and temporary directory created in setup().
188 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400189void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000190{
plars865695b2001-08-27 22:15:12 +0000191
plars865695b2001-08-27 22:15:12 +0000192 tst_rmdir();
193
Chris Dearmanec6edca2012-10-17 19:54:01 -0700194}