blob: 680976c87323ab29b9247a5ad8cd2bea6c86fcba [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
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20/*
21 * NAME
nstrazfa31d552002-05-14 16:50:06 +000022 * umask03.c
plars865695b2001-08-27 22:15:12 +000023 *
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>
nstrazfa31d552002-05-14 16:50:06 +000033 * umask03 [-c n] [-i n] [-I x] [-P x] [-t]
plars865695b2001-08-27 22:15:12 +000034 * 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
plars74948ad2002-11-14 16:16:14 +000048#include <sys/types.h>
49#include <sys/stat.h>
50#include <fcntl.h>
plars865695b2001-08-27 22:15:12 +000051#include <stdio.h>
52#include "test.h"
53#include "usctest.h"
54#include <sys/types.h>
55#include <sys/stat.h>
56
nstrazfa31d552002-05-14 16:50:06 +000057char *TCID = "umask03";
plars865695b2001-08-27 22:15:12 +000058int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000059
60char filname[40];
61
62void setup(void);
63void cleanup(void);
64
plars74948ad2002-11-14 16:16:14 +000065int main(int argc, char **argv)
plars865695b2001-08-27 22:15:12 +000066{
67 int lc;
68 char *msg;
subrata_modakbdbaec52009-02-26 12:14:51 +000069
plars865695b2001-08-27 22:15:12 +000070 struct stat statbuf;
71 int mskval = 0000;
plarsb846e7b2003-07-08 18:55:39 +000072 int failcnt = 0;
plars865695b2001-08-27 22:15:12 +000073 int fildes, i;
74 unsigned low9mode;
75
76 /* parse standard options */
Garrett Coopera9e49f12010-12-16 10:54:03 -080077 if ((msg = parse_opts(argc, argv, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -080078 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +000079
subrata_modak56207ce2009-03-23 13:35:39 +000080 setup(); /* global setup */
plars865695b2001-08-27 22:15:12 +000081
plars865695b2001-08-27 22:15:12 +000082 for (lc = 0; TEST_LOOPING(lc); lc++) {
83
84 /* reset Tst_count in case we are looping */
85 Tst_count = 0;
86
87 for (umask(mskval = 0077), i = 1; mskval < 01000;
subrata_modak56207ce2009-03-23 13:35:39 +000088 i++, umask(++mskval)) {
plars865695b2001-08-27 22:15:12 +000089 unlink(filname);
90 if ((fildes = creat(filname, 0777)) == -1) {
91 tst_resm(TBROK, "cannot create "
92 "file with mskval 0%o %d",
93 mskval, mskval);
94 } else {
95 if (fstat(fildes, &statbuf) != 0) {
96 tst_resm(TBROK, "cannot fstat file");
97 } else {
98 low9mode = statbuf.st_mode & 0777;
99 if (low9mode != (~mskval & 0777)) {
subrata_modak4bb656a2009-02-26 12:02:09 +0000100 tst_resm(TFAIL,
subrata_modak923b23f2009-11-02 13:57:16 +0000101 "got %o expected %o"
plars865695b2001-08-27 22:15:12 +0000102 "mask didnot take",
103 low9mode,
104 (~mskval & 0777));
Garrett Cooper53740502010-12-16 00:04:01 -0800105 }
plars865695b2001-08-27 22:15:12 +0000106 }
107 }
108 close(fildes);
109 }
plarsb846e7b2003-07-08 18:55:39 +0000110 if (!failcnt)
111 tst_resm(TPASS, "umask correctly returns the "
subrata_modak56207ce2009-03-23 13:35:39 +0000112 "previous value for all masks");
plars865695b2001-08-27 22:15:12 +0000113 }
114 cleanup();
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800115 tst_exit();
robbiew9a66de12003-03-27 22:16:14 +0000116
plars865695b2001-08-27 22:15:12 +0000117}
118
119/*
120 * setup
121 * performs all ONE TIME setup for this test
122 */
subrata_modak56207ce2009-03-23 13:35:39 +0000123void setup()
plars865695b2001-08-27 22:15:12 +0000124{
Garrett Cooper2c282152010-12-16 00:55:50 -0800125
plars865695b2001-08-27 22:15:12 +0000126 tst_sig(NOFORK, DEF_HANDLER, cleanup);
127
plars865695b2001-08-27 22:15:12 +0000128 /* Pause if that option was specified
129 * TEST_PAUSE contains the code to fork the test with the -i option.
130 * You want to make sure you do this before you create your temporary
131 * directory.
132 */
133 TEST_PAUSE;
134
135 /* make temp dir and cd to it */
136 tst_tmpdir();
137
138 sprintf(filname, "umask2.%d", getpid());
139}
140
141/*
142 * cleanup
143 * performs all ONE TIME cleanup for this test at completion or
144 * premature exit
145 */
subrata_modak56207ce2009-03-23 13:35:39 +0000146void cleanup()
plars865695b2001-08-27 22:15:12 +0000147{
148 /*
149 * print timing stats if that option was specified
150 * print errno log if that option was specified
151 */
152 TEST_CLEANUP;
153
154 /*
155 * cleanup the temporary files and the temporary directory
156 */
157 unlink(filname);
158 tst_rmdir();
159
160 /*
161 * exit with return code appropriate for results
162 */
Garrett Cooper2c282152010-12-16 00:55:50 -0800163
Chris Dearmanec6edca2012-10-17 19:54:01 -0700164}