blob: 9e2ed24332e81735aab4edc91a24ec9e298dea93 [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
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"
plars865695b2001-08-27 22:15:12 +000053#include <sys/types.h>
54#include <sys/stat.h>
55
nstrazfa31d552002-05-14 16:50:06 +000056char *TCID = "umask03";
plars865695b2001-08-27 22:15:12 +000057int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000058
59char filname[40];
60
61void setup(void);
62void cleanup(void);
63
plars74948ad2002-11-14 16:16:14 +000064int main(int argc, char **argv)
plars865695b2001-08-27 22:15:12 +000065{
66 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020067 const char *msg;
subrata_modakbdbaec52009-02-26 12:14:51 +000068
plars865695b2001-08-27 22:15:12 +000069 struct stat statbuf;
70 int mskval = 0000;
plarsb846e7b2003-07-08 18:55:39 +000071 int failcnt = 0;
plars865695b2001-08-27 22:15:12 +000072 int fildes, i;
73 unsigned low9mode;
74
Garrett Coopera9e49f12010-12-16 10:54:03 -080075 if ((msg = parse_opts(argc, argv, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -080076 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +000077
subrata_modak56207ce2009-03-23 13:35:39 +000078 setup(); /* global setup */
plars865695b2001-08-27 22:15:12 +000079
plars865695b2001-08-27 22:15:12 +000080 for (lc = 0; TEST_LOOPING(lc); lc++) {
81
Caspar Zhangd59a6592013-03-07 14:59:12 +080082 /* reset tst_count in case we are looping */
83 tst_count = 0;
plars865695b2001-08-27 22:15:12 +000084
85 for (umask(mskval = 0077), i = 1; mskval < 01000;
subrata_modak56207ce2009-03-23 13:35:39 +000086 i++, umask(++mskval)) {
plars865695b2001-08-27 22:15:12 +000087 unlink(filname);
88 if ((fildes = creat(filname, 0777)) == -1) {
89 tst_resm(TBROK, "cannot create "
90 "file with mskval 0%o %d",
91 mskval, mskval);
92 } else {
93 if (fstat(fildes, &statbuf) != 0) {
94 tst_resm(TBROK, "cannot fstat file");
95 } else {
96 low9mode = statbuf.st_mode & 0777;
97 if (low9mode != (~mskval & 0777)) {
subrata_modak4bb656a2009-02-26 12:02:09 +000098 tst_resm(TFAIL,
subrata_modak923b23f2009-11-02 13:57:16 +000099 "got %o expected %o"
plars865695b2001-08-27 22:15:12 +0000100 "mask didnot take",
101 low9mode,
102 (~mskval & 0777));
Wanlong Gao354ebb42012-12-07 10:10:04 +0800103 }
plars865695b2001-08-27 22:15:12 +0000104 }
105 }
106 close(fildes);
107 }
plarsb846e7b2003-07-08 18:55:39 +0000108 if (!failcnt)
109 tst_resm(TPASS, "umask correctly returns the "
subrata_modak56207ce2009-03-23 13:35:39 +0000110 "previous value for all masks");
plars865695b2001-08-27 22:15:12 +0000111 }
112 cleanup();
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800113 tst_exit();
robbiew9a66de12003-03-27 22:16:14 +0000114
plars865695b2001-08-27 22:15:12 +0000115}
116
117/*
118 * setup
119 * performs all ONE TIME setup for this test
120 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400121void setup(void)
plars865695b2001-08-27 22:15:12 +0000122{
Garrett Cooper2c282152010-12-16 00:55:50 -0800123
plars865695b2001-08-27 22:15:12 +0000124 tst_sig(NOFORK, DEF_HANDLER, cleanup);
125
plars865695b2001-08-27 22:15:12 +0000126 /* Pause if that option was specified
127 * TEST_PAUSE contains the code to fork the test with the -i option.
128 * You want to make sure you do this before you create your temporary
129 * directory.
130 */
131 TEST_PAUSE;
132
133 /* make temp dir and cd to it */
134 tst_tmpdir();
135
136 sprintf(filname, "umask2.%d", getpid());
137}
138
139/*
140 * cleanup
141 * performs all ONE TIME cleanup for this test at completion or
142 * premature exit
143 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400144void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000145{
plars865695b2001-08-27 22:15:12 +0000146
147 /*
148 * cleanup the temporary files and the temporary directory
149 */
150 unlink(filname);
151 tst_rmdir();
152
153 /*
154 * exit with return code appropriate for results
155 */
Garrett Cooper2c282152010-12-16 00:55:50 -0800156
Chris Dearmanec6edca2012-10-17 19:54:01 -0700157}