blob: bf0bc26ff7b26a818f65594cec34c55f70fe4ec4 [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/*
nstrazfa31d552002-05-14 16:50:06 +000021 * Test Name: fchmod07
plars865695b2001-08-27 22:15:12 +000022 *
23 * Test Description:
24 * Verify that, fchmod(2) succeeds when used to change the mode permissions
subrata_modak4bb656a2009-02-26 12:02:09 +000025 * of a file specified by file descriptor.
plars865695b2001-08-27 22:15:12 +000026 *
27 * Expected Result:
subrata_modak4bb656a2009-02-26 12:02:09 +000028 * fchmod(2) should return 0 and the mode permissions set on file should match
plars865695b2001-08-27 22:15:12 +000029 * the specified mode.
subrata_modakbdbaec52009-02-26 12:14:51 +000030 *
plars865695b2001-08-27 22:15:12 +000031 * Algorithm:
32 * Setup:
33 * Setup signal handling.
34 * Create temporary directory.
35 * Pause for SIGUSR1 if option specified.
36 *
37 * Test:
38 * Loop if the proper options are given.
39 * Execute system call
40 * Check return code, if system call failed (return=-1)
41 * Log the errno and Issue a FAIL message.
42 * Otherwise,
subrata_modakbdbaec52009-02-26 12:14:51 +000043 * Verify the Functionality of system call
plars865695b2001-08-27 22:15:12 +000044 * if successful,
45 * Issue Functionality-Pass message.
46 * Otherwise,
47 * Issue Functionality-Fail message.
48 * Cleanup:
49 * Print errno log and/or timing stats if options given
50 * Delete the temporary directory created.
51 *
52 * Usage: <for command-line>
nstrazfa31d552002-05-14 16:50:06 +000053 * fchmod07 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
plars865695b2001-08-27 22:15:12 +000054 * where, -c n : Run n copies concurrently.
55 * -f : Turn off functionality Testing.
56 * -i n : Execute test n times.
57 * -I x : Execute test for x seconds.
58 * -P x : Pause for x seconds between iterations.
59 * -t : Turn on syscall timing.
60 *
61 * HISTORY
62 * 07/2001 Ported by Wayne Boyer
63 *
64 * RESTRICTIONS:
65 * None.
66 */
67
68#include <stdio.h>
69#include <sys/types.h>
70#include <sys/stat.h>
71#include <sys/fcntl.h>
72#include <errno.h>
73#include <string.h>
74#include <signal.h>
75
76#include "test.h"
plars865695b2001-08-27 22:15:12 +000077
78#define FILE_MODE S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
79#define TESTFILE "testfile"
80
81int fd; /* file descriptor for testfile */
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020082char *TCID = "fchmod07";
subrata_modak56207ce2009-03-23 13:35:39 +000083int TST_TOTAL = 8; /* Total number of test conditions */
plars865695b2001-08-27 22:15:12 +000084
subrata_modak56207ce2009-03-23 13:35:39 +000085int Modes[] = { 0, 07, 070, 0700, 0777, 02777, 04777, 06777 };
plars865695b2001-08-27 22:15:12 +000086
87void setup(); /* setup function for the test */
88void cleanup(); /* cleanup function for the test */
89
subrata_modak56207ce2009-03-23 13:35:39 +000090int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000091{
92 struct stat stat_buf; /* stat(2) struct contents */
Cyril Hrubis89af32a2012-10-24 16:39:11 +020093 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020094 const char *msg;
plars865695b2001-08-27 22:15:12 +000095 int ind; /* counter variable for chmod(2) tests */
96 int mode; /* file mode permission */
97
98 TST_TOTAL = sizeof(Modes) / sizeof(int);
99
Garrett Cooper45e285d2010-11-22 12:19:25 -0800100 msg = parse_opts(ac, av, NULL, NULL);
101 if (msg != NULL) {
plars865695b2001-08-27 22:15:12 +0000102 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
Garrett Cooper2c282152010-12-16 00:55:50 -0800103
plars865695b2001-08-27 22:15:12 +0000104 }
105
plars865695b2001-08-27 22:15:12 +0000106 setup();
107
plars865695b2001-08-27 22:15:12 +0000108 for (lc = 0; TEST_LOOPING(lc); lc++) {
Garrett Cooper2c282152010-12-16 00:55:50 -0800109
Caspar Zhangd59a6592013-03-07 14:59:12 +0800110 tst_count = 0;
plars865695b2001-08-27 22:15:12 +0000111
112 for (ind = 0; ind < TST_TOTAL; ind++) {
113 mode = Modes[ind];
114
subrata_modak4bb656a2009-02-26 12:02:09 +0000115 /*
plars865695b2001-08-27 22:15:12 +0000116 * Call fchmod(2) with different mode permission
117 * bits to set it for "testfile".
subrata_modak56207ce2009-03-23 13:35:39 +0000118 */
plars865695b2001-08-27 22:15:12 +0000119 TEST(fchmod(fd, mode));
120
plars865695b2001-08-27 22:15:12 +0000121 if (TEST_RETURN == -1) {
122 tst_resm(TFAIL, "fchmod(%d, %#o) Failed, "
123 "errno=%d : %s", fd, mode, TEST_ERRNO,
124 strerror(TEST_ERRNO));
125 continue;
126 }
127 /*
Cyril Hrubise38b9612014-06-02 17:20:57 +0200128 * Get the testfile information using
129 * fstat(2).
plars865695b2001-08-27 22:15:12 +0000130 */
Cyril Hrubise38b9612014-06-02 17:20:57 +0200131 if (fstat(fd, &stat_buf) < 0) {
132 tst_brkm(TFAIL, cleanup,
133 "fstat(2) of "
134 "%s failed, errno:%d",
135 TESTFILE, TEST_ERRNO);
136 }
137 stat_buf.st_mode &= ~S_IFREG;
plars865695b2001-08-27 22:15:12 +0000138
Cyril Hrubise38b9612014-06-02 17:20:57 +0200139 /*
140 * Check for expected mode permissions
141 * on testfile.
142 */
143 if (stat_buf.st_mode == mode) {
144 tst_resm(TPASS,
145 "Functionality of "
146 "fchmod(%d, %#o) successful",
147 fd, mode);
plars865695b2001-08-27 22:15:12 +0000148 } else {
Cyril Hrubise38b9612014-06-02 17:20:57 +0200149 tst_resm(TFAIL, "%s: Incorrect modes "
150 "0%03o, Expected 0%03o",
151 TESTFILE, stat_buf.st_mode,
152 mode);
plars865695b2001-08-27 22:15:12 +0000153 }
154 }
Garrett Cooper2c282152010-12-16 00:55:50 -0800155 }
plars865695b2001-08-27 22:15:12 +0000156
plars865695b2001-08-27 22:15:12 +0000157 cleanup();
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800158 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800159}
plars865695b2001-08-27 22:15:12 +0000160
161/*
162 * void
163 * setup() - performs all ONE TIME setup for this test.
164 * Create a temporary directory and change directory to it.
165 * Create a test file under temporary directory.
166 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400167void setup(void)
plars865695b2001-08-27 22:15:12 +0000168{
Garrett Cooper2c282152010-12-16 00:55:50 -0800169
plars865695b2001-08-27 22:15:12 +0000170 tst_sig(NOFORK, DEF_HANDLER, cleanup);
171
plars865695b2001-08-27 22:15:12 +0000172 TEST_PAUSE;
173
plars865695b2001-08-27 22:15:12 +0000174 tst_tmpdir();
175
subrata_modak56207ce2009-03-23 13:35:39 +0000176 if ((fd = open(TESTFILE, O_RDWR | O_CREAT, FILE_MODE)) == -1) {
plars865695b2001-08-27 22:15:12 +0000177 tst_brkm(TBROK, cleanup,
178 "open(%s, O_RDWR|O_CREAT, %o) Failed, errno=%d : %s",
179 TESTFILE, FILE_MODE, errno, strerror(errno));
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 * Close the testfile created in the setup.
188 * Remove the test directory and testfile created in the setup.
189 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400190void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000191{
plars865695b2001-08-27 22:15:12 +0000192
193 /* Close the TESTFILE opened in the setup() */
194 if (close(fd) == -1) {
195 tst_brkm(TBROK, NULL,
196 "close(%s) Failed, errno=%d : %s",
197 TESTFILE, errno, strerror(errno));
198 }
199
plars865695b2001-08-27 22:15:12 +0000200 tst_rmdir();
201
Chris Dearmanec6edca2012-10-17 19:54:01 -0700202}