plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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 |
Wanlong Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | /* |
| 21 | * NAME |
nstraz | fa31d55 | 2002-05-14 16:50:06 +0000 | [diff] [blame] | 22 | * umask03.c |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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> |
nstraz | fa31d55 | 2002-05-14 16:50:06 +0000 | [diff] [blame] | 33 | * umask03 [-c n] [-i n] [-I x] [-P x] [-t] |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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 | |
plars | 74948ad | 2002-11-14 16:16:14 +0000 | [diff] [blame] | 48 | #include <sys/types.h> |
| 49 | #include <sys/stat.h> |
| 50 | #include <fcntl.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 51 | #include <stdio.h> |
| 52 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 53 | #include <sys/types.h> |
| 54 | #include <sys/stat.h> |
| 55 | |
nstraz | fa31d55 | 2002-05-14 16:50:06 +0000 | [diff] [blame] | 56 | char *TCID = "umask03"; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 57 | int TST_TOTAL = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 58 | |
| 59 | char filname[40]; |
| 60 | |
| 61 | void setup(void); |
| 62 | void cleanup(void); |
| 63 | |
plars | 74948ad | 2002-11-14 16:16:14 +0000 | [diff] [blame] | 64 | int main(int argc, char **argv) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 65 | { |
| 66 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 67 | const char *msg; |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 68 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 69 | struct stat statbuf; |
| 70 | int mskval = 0000; |
plars | b846e7b | 2003-07-08 18:55:39 +0000 | [diff] [blame] | 71 | int failcnt = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 72 | int fildes, i; |
| 73 | unsigned low9mode; |
| 74 | |
Garrett Cooper | a9e49f1 | 2010-12-16 10:54:03 -0800 | [diff] [blame] | 75 | if ((msg = parse_opts(argc, argv, NULL, NULL)) != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 76 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 77 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 78 | setup(); /* global setup */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 79 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 80 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 81 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 82 | /* reset tst_count in case we are looping */ |
| 83 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 84 | |
| 85 | for (umask(mskval = 0077), i = 1; mskval < 01000; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 86 | i++, umask(++mskval)) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 87 | 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_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 98 | tst_resm(TFAIL, |
subrata_modak | 923b23f | 2009-11-02 13:57:16 +0000 | [diff] [blame] | 99 | "got %o expected %o" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 100 | "mask didnot take", |
| 101 | low9mode, |
| 102 | (~mskval & 0777)); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 103 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 104 | } |
| 105 | } |
| 106 | close(fildes); |
| 107 | } |
plars | b846e7b | 2003-07-08 18:55:39 +0000 | [diff] [blame] | 108 | if (!failcnt) |
| 109 | tst_resm(TPASS, "umask correctly returns the " |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 110 | "previous value for all masks"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 111 | } |
| 112 | cleanup(); |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 113 | tst_exit(); |
robbiew | 9a66de1 | 2003-03-27 22:16:14 +0000 | [diff] [blame] | 114 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | /* |
| 118 | * setup |
| 119 | * performs all ONE TIME setup for this test |
| 120 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 121 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 122 | { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 123 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 124 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 125 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 126 | /* 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 Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 144 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 145 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 146 | |
| 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 Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 156 | |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 157 | } |