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