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 |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 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" |
| 53 | #include "usctest.h" |
| 54 | #include <sys/types.h> |
| 55 | #include <sys/stat.h> |
| 56 | |
nstraz | fa31d55 | 2002-05-14 16:50:06 +0000 | [diff] [blame] | 57 | char *TCID = "umask03"; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 58 | int TST_TOTAL = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 59 | |
| 60 | char filname[40]; |
| 61 | |
| 62 | void setup(void); |
| 63 | void cleanup(void); |
| 64 | |
plars | 74948ad | 2002-11-14 16:16:14 +0000 | [diff] [blame] | 65 | int main(int argc, char **argv) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 66 | { |
| 67 | int lc; |
| 68 | char *msg; |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 69 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 70 | struct stat statbuf; |
| 71 | int mskval = 0000; |
plars | b846e7b | 2003-07-08 18:55:39 +0000 | [diff] [blame] | 72 | int failcnt = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 73 | int fildes, i; |
| 74 | unsigned low9mode; |
| 75 | |
| 76 | /* parse standard options */ |
Garrett Cooper | a9e49f1 | 2010-12-16 10:54:03 -0800 | [diff] [blame] | 77 | if ((msg = parse_opts(argc, argv, NULL, NULL)) != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 78 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 79 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 80 | setup(); /* global setup */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 81 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 82 | 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_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 88 | i++, umask(++mskval)) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 89 | 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_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 100 | tst_resm(TFAIL, |
subrata_modak | 923b23f | 2009-11-02 13:57:16 +0000 | [diff] [blame] | 101 | "got %o expected %o" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 102 | "mask didnot take", |
| 103 | low9mode, |
| 104 | (~mskval & 0777)); |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 105 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 106 | } |
| 107 | } |
| 108 | close(fildes); |
| 109 | } |
plars | b846e7b | 2003-07-08 18:55:39 +0000 | [diff] [blame] | 110 | if (!failcnt) |
| 111 | tst_resm(TPASS, "umask correctly returns the " |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 112 | "previous value for all masks"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 113 | } |
| 114 | cleanup(); |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 115 | tst_exit(); |
robbiew | 9a66de1 | 2003-03-27 22:16:14 +0000 | [diff] [blame] | 116 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | /* |
| 120 | * setup |
| 121 | * performs all ONE TIME setup for this test |
| 122 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 123 | void setup() |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 124 | { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 125 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 126 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 127 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 128 | /* 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_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 146 | void cleanup() |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 147 | { |
| 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 Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 163 | |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame^] | 164 | } |