blob: 2b93a44fbadcabafa1b502df5238a45f1431a477 [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: fchmod03
22 *
23 * Test Description:
24 * Verify that, fchmod(2) will succeed to change the mode of a file
25 * and set the sticky bit on it if invoked by non-root (uid != 0)
26 * process with the following constraints,
27 * - the process is the owner of the file.
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 file.
subrata_modakbdbaec52009-02-26 12:14:51 +000030 *
plars865695b2001-08-27 22:15:12 +000031 * Expected Result:
32 * fchmod() should return value 0 on success and succeeds to change
33 * the mode of specified file, sets sticky bit on it.
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 * fchmod03 [-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>
robbiewcf3e9cb2001-08-31 16:25:47 +000080#include <pwd.h>
plars865695b2001-08-27 22:15:12 +000081
82#include "test.h"
plars865695b2001-08-27 22:15:12 +000083
Garrett Cooper15697992010-12-18 06:31:28 -080084#define FILE_MODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)
85#define PERMS 01777
plars865695b2001-08-27 22:15:12 +000086#define TESTFILE "testfile"
87
88int fd; /* file descriptor for test file */
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020089char *TCID = "fchmod03";
90int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000091
robbiewcf3e9cb2001-08-31 16:25:47 +000092char nobody_uid[] = "nobody";
93struct passwd *ltpuser;
94
plars865695b2001-08-27 22:15:12 +000095void setup(); /* Main setup function for the test */
96void cleanup(); /* Main cleanup function for the test */
97
subrata_modak56207ce2009-03-23 13:35:39 +000098int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000099{
subrata_modak56207ce2009-03-23 13:35:39 +0000100 struct stat stat_buf; /* stat struct. */
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200101 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200102 const char *msg;
plars865695b2001-08-27 22:15:12 +0000103 mode_t file_mode; /* mode permissions set on testfile */
104
Garrett Cooper15697992010-12-18 06:31:28 -0800105 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
plars865695b2001-08-27 22:15:12 +0000106 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
Garrett Cooper2c282152010-12-16 00:55:50 -0800107
plars865695b2001-08-27 22:15:12 +0000108 setup();
109
plars865695b2001-08-27 22:15:12 +0000110 for (lc = 0; TEST_LOOPING(lc); lc++) {
Garrett Cooper2c282152010-12-16 00:55:50 -0800111
Caspar Zhangd59a6592013-03-07 14:59:12 +0800112 tst_count = 0;
plars865695b2001-08-27 22:15:12 +0000113
plars865695b2001-08-27 22:15:12 +0000114 TEST(fchmod(fd, PERMS));
subrata_modakbdbaec52009-02-26 12:14:51 +0000115
plars865695b2001-08-27 22:15:12 +0000116 if (TEST_RETURN == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800117 tst_resm(TFAIL | TTERRNO, "fchmod failed");
plars865695b2001-08-27 22:15:12 +0000118 continue;
119 }
120 /*
Cyril Hrubise38b9612014-06-02 17:20:57 +0200121 * Get the file information using
122 * fstat(2).
plars865695b2001-08-27 22:15:12 +0000123 */
Cyril Hrubise38b9612014-06-02 17:20:57 +0200124 if (fstat(fd, &stat_buf) == -1)
125 tst_brkm(TFAIL | TERRNO, cleanup,
126 "fstat failed");
127 file_mode = stat_buf.st_mode;
plars865695b2001-08-27 22:15:12 +0000128
Cyril Hrubise38b9612014-06-02 17:20:57 +0200129 /* Verify STICKY BIT set on testfile */
130 if ((file_mode & PERMS) != PERMS)
131 tst_resm(TFAIL, "%s: Incorrect modes 0%3o, "
132 "Expected 0777", TESTFILE, file_mode);
133 else
134 tst_resm(TPASS, "Functionality of fchmod(%d, "
135 "%#o) successful", fd, PERMS);
Garrett Cooper2c282152010-12-16 00:55:50 -0800136 }
plars865695b2001-08-27 22:15:12 +0000137
plars865695b2001-08-27 22:15:12 +0000138 cleanup();
Garrett Cooper15697992010-12-18 06:31:28 -0800139 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800140}
plars865695b2001-08-27 22:15:12 +0000141
Mike Frysingerc57fba52014-04-09 18:56:30 -0400142void setup(void)
plars865695b2001-08-27 22:15:12 +0000143{
Garrett Cooper2c282152010-12-16 00:55:50 -0800144
plars865695b2001-08-27 22:15:12 +0000145 tst_sig(NOFORK, DEF_HANDLER, cleanup);
146
Garrett Cooper15697992010-12-18 06:31:28 -0800147 tst_require_root(NULL);
148
subrata_modak56207ce2009-03-23 13:35:39 +0000149 ltpuser = getpwnam(nobody_uid);
Garrett Cooper15697992010-12-18 06:31:28 -0800150 if (ltpuser == NULL)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800151 tst_brkm(TBROK | TERRNO, NULL, "getpwnam failed");
Garrett Cooper15697992010-12-18 06:31:28 -0800152 if (seteuid(ltpuser->pw_uid) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800153 tst_brkm(TBROK | TERRNO, NULL, "seteuid failed");
plars865695b2001-08-27 22:15:12 +0000154
plars865695b2001-08-27 22:15:12 +0000155 TEST_PAUSE;
156
plars865695b2001-08-27 22:15:12 +0000157 tst_tmpdir();
158
159 /*
160 * Create a test file under temporary directory with specified
161 * mode permissios and set the ownership of the test file to the
162 * uid/gid of guest user.
163 */
Garrett Cooper15697992010-12-18 06:31:28 -0800164 if ((fd = open(TESTFILE, O_RDWR | O_CREAT, FILE_MODE)) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800165 tst_brkm(TBROK | TERRNO, cleanup, "open failed");
Garrett Cooper2c282152010-12-16 00:55:50 -0800166}
plars865695b2001-08-27 22:15:12 +0000167
Mike Frysingerc57fba52014-04-09 18:56:30 -0400168void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000169{
Garrett Cooper15697992010-12-18 06:31:28 -0800170 if (close(fd) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800171 tst_resm(TWARN | TERRNO, "close failed");
plars865695b2001-08-27 22:15:12 +0000172
plars865695b2001-08-27 22:15:12 +0000173 tst_rmdir();
174
Chris Dearmanec6edca2012-10-17 19:54:01 -0700175}