• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  * Test Name: fchmod07
22  *
23  * Test Description:
24  *  Verify that, fchmod(2) succeeds when used to change the mode permissions
25  *  of a file specified by file descriptor.
26  *
27  * Expected Result:
28  *  fchmod(2) should return 0 and the mode permissions set on file should match
29  *  the specified mode.
30  *
31  * 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,
43  *   	Verify the Functionality of system call
44  *      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>
53  *  fchmod07 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
54  *     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"
77 
78 #define FILE_MODE	S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
79 #define TESTFILE	"testfile"
80 
81 int fd;				/* file descriptor for testfile */
82 char *TCID = "fchmod07";
83 int TST_TOTAL = 8;		/* Total number of test conditions */
84 
85 int Modes[] = { 0, 07, 070, 0700, 0777, 02777, 04777, 06777 };
86 
87 void setup();			/* setup function for the test */
88 void cleanup();			/* cleanup function for the test */
89 
main(int ac,char ** av)90 int main(int ac, char **av)
91 {
92 	struct stat stat_buf;	/* stat(2) struct contents */
93 	int lc;
94 	int ind;		/* counter variable for chmod(2) tests */
95 	int mode;		/* file mode permission */
96 
97 	TST_TOTAL = sizeof(Modes) / sizeof(int);
98 
99 	tst_parse_opts(ac, av, NULL, NULL);
100 
101 	setup();
102 
103 	for (lc = 0; TEST_LOOPING(lc); lc++) {
104 
105 		tst_count = 0;
106 
107 		for (ind = 0; ind < TST_TOTAL; ind++) {
108 			mode = Modes[ind];
109 
110 			/*
111 			 * Call fchmod(2) with different mode permission
112 			 * bits to set it for "testfile".
113 			 */
114 			TEST(fchmod(fd, mode));
115 
116 			if (TEST_RETURN == -1) {
117 				tst_resm(TFAIL, "fchmod(%d, %#o) Failed, "
118 					 "errno=%d : %s", fd, mode, TEST_ERRNO,
119 					 strerror(TEST_ERRNO));
120 				continue;
121 			}
122 			/*
123 			 * Get the testfile information using
124 			 * fstat(2).
125 			 */
126 			if (fstat(fd, &stat_buf) < 0) {
127 				tst_brkm(TFAIL, cleanup,
128 					 "fstat(2) of "
129 					 "%s failed, errno:%d",
130 					 TESTFILE, TEST_ERRNO);
131 			}
132 			stat_buf.st_mode &= ~S_IFREG;
133 
134 			/*
135 			 * Check for expected mode permissions
136 			 * on testfile.
137 			 */
138 			if (stat_buf.st_mode == mode) {
139 				tst_resm(TPASS,
140 					 "Functionality of "
141 					 "fchmod(%d, %#o) successful",
142 					 fd, mode);
143 			} else {
144 				tst_resm(TFAIL, "%s: Incorrect modes "
145 					 "0%03o, Expected 0%03o",
146 					 TESTFILE, stat_buf.st_mode,
147 					 mode);
148 			}
149 		}
150 	}
151 
152 	cleanup();
153 	tst_exit();
154 }
155 
156 /*
157  * void
158  * setup() - performs all ONE TIME setup for this test.
159  *  Create a temporary directory and change directory to it.
160  *  Create a test file under temporary directory.
161  */
setup(void)162 void setup(void)
163 {
164 
165 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
166 
167 	TEST_PAUSE;
168 
169 	tst_tmpdir();
170 
171 	if ((fd = open(TESTFILE, O_RDWR | O_CREAT, FILE_MODE)) == -1) {
172 		tst_brkm(TBROK, cleanup,
173 			 "open(%s, O_RDWR|O_CREAT, %o) Failed, errno=%d : %s",
174 			 TESTFILE, FILE_MODE, errno, strerror(errno));
175 	}
176 }
177 
178 /*
179  * void
180  * cleanup() - performs all ONE TIME cleanup for this test at
181  *	       completion or premature exit.
182  *  Close the testfile created in the setup.
183  *  Remove the test directory and testfile created in the setup.
184  */
cleanup(void)185 void cleanup(void)
186 {
187 
188 	/* Close the TESTFILE opened in the setup() */
189 	if (close(fd) == -1) {
190 		tst_brkm(TBROK, NULL,
191 			 "close(%s) Failed, errno=%d : %s",
192 			 TESTFILE, errno, strerror(errno));
193 	}
194 
195 	tst_rmdir();
196 
197 }
198