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  * NAME
22  *	umask03.c
23  *
24  * DESCRIPTION
25  *	Check that umask changes the mask, and that the previous
26  *	value of the mask is returned correctly for each value.
27  *
28  * ALGORITHM
29  *	For each mask value (9 bits) set mask, and check that the return
30  *	corresponds to the previous value set.
31  *
32  * USAGE:  <for command-line>
33  *		umask03 [-c n] [-i n] [-I x] [-P x] [-t]
34  *		where,  -c n : Run n copies concurrently.
35  *			-i n : Execute test n times.
36  *			-I x : Execute test for x seconds.
37  *			-P x : Pause for x seconds between iterations.
38  *			-t   : Turn on syscall timing.
39  *
40  * History
41  *	07/2001 John George
42  *		-Ported
43  *
44  * Restrictions
45  *	None
46  */
47 
48 #include <sys/types.h>
49 #include <sys/stat.h>
50 #include <fcntl.h>
51 #include <stdio.h>
52 #include "test.h"
53 #include <sys/types.h>
54 #include <sys/stat.h>
55 
56 char *TCID = "umask03";
57 int TST_TOTAL = 1;
58 
59 char filname[40];
60 
61 void setup(void);
62 void cleanup(void);
63 
main(int argc,char ** argv)64 int main(int argc, char **argv)
65 {
66 	int lc;
67 
68 	struct stat statbuf;
69 	int mskval = 0000;
70 	int failcnt = 0;
71 	int fildes, i;
72 	unsigned low9mode;
73 
74 	tst_parse_opts(argc, argv, NULL, NULL);
75 
76 	setup();		/* global setup */
77 
78 	for (lc = 0; TEST_LOOPING(lc); lc++) {
79 
80 		/* reset tst_count in case we are looping */
81 		tst_count = 0;
82 
83 		for (umask(mskval = 0077), i = 1; mskval < 01000;
84 		     i++, umask(++mskval)) {
85 			unlink(filname);
86 			if ((fildes = creat(filname, 0777)) == -1) {
87 				tst_resm(TBROK, "cannot create "
88 					 "file with mskval 0%o %d",
89 					 mskval, mskval);
90 			} else {
91 				if (fstat(fildes, &statbuf) != 0) {
92 					tst_resm(TBROK, "cannot fstat file");
93 				} else {
94 					low9mode = statbuf.st_mode & 0777;
95 					if (low9mode != (~mskval & 0777)) {
96 						tst_resm(TFAIL,
97 							 "got %o expected %o"
98 							 "mask didnot take",
99 							 low9mode,
100 							 (~mskval & 0777));
101 					}
102 				}
103 			}
104 			close(fildes);
105 		}
106 		if (!failcnt)
107 			tst_resm(TPASS, "umask correctly returns the "
108 				 "previous value for all masks");
109 	}
110 	cleanup();
111 	tst_exit();
112 
113 }
114 
115 /*
116  * setup
117  *	performs all ONE TIME setup for this test
118  */
setup(void)119 void setup(void)
120 {
121 
122 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
123 
124 	/* Pause if that option was specified
125 	 * TEST_PAUSE contains the code to fork the test with the -i option.
126 	 * You want to make sure you do this before you create your temporary
127 	 * directory.
128 	 */
129 	TEST_PAUSE;
130 
131 	/* make temp dir and cd to it */
132 	tst_tmpdir();
133 
134 	sprintf(filname, "umask2.%d", getpid());
135 }
136 
137 /*
138  * cleanup
139  *	performs all ONE TIME cleanup for this test at completion or
140  *	premature exit
141  */
cleanup(void)142 void cleanup(void)
143 {
144 
145 	/*
146 	 * cleanup the temporary files and the temporary directory
147 	 */
148 	unlink(filname);
149 	tst_rmdir();
150 
151 	/*
152 	 * exit with return code appropriate for results
153 	 */
154 
155 }
156