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 | * fcntl18.c |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 23 | * |
| 24 | * DESCRIPTION |
| 25 | * Test to check the error conditions in fcntl system call |
| 26 | * |
| 27 | * USAGE |
nstraz | fa31d55 | 2002-05-14 16:50:06 +0000 | [diff] [blame] | 28 | * fcntl18 |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 29 | * |
| 30 | * HISTORY |
| 31 | * 07/2001 Ported by Wayne Boyer |
| 32 | * |
| 33 | * RESTRICTIONS |
| 34 | * NONE |
| 35 | */ |
| 36 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 37 | #include <signal.h> |
| 38 | #include <errno.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 39 | #include <pwd.h> |
robbiew | 5aab8a7 | 2003-03-26 18:05:30 +0000 | [diff] [blame] | 40 | #include <sys/types.h> |
mridge | db63921 | 2005-01-04 21:04:11 +0000 | [diff] [blame] | 41 | #include <sys/wait.h> |
robbiew | 5aab8a7 | 2003-03-26 18:05:30 +0000 | [diff] [blame] | 42 | #include <sys/stat.h> |
| 43 | #include <sys/param.h> |
mridge | db63921 | 2005-01-04 21:04:11 +0000 | [diff] [blame] | 44 | #include <fcntl.h> |
| 45 | #include <unistd.h> |
robbiew | 5aab8a7 | 2003-03-26 18:05:30 +0000 | [diff] [blame] | 46 | #include "test.h" |
| 47 | #include "usctest.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 48 | |
| 49 | #define INVAL_FLAG -1 |
| 50 | #define INVAL_MIN (-2147483647L-1L) |
| 51 | |
| 52 | int fd; |
| 53 | char string[40] = ""; |
| 54 | |
nstraz | fa31d55 | 2002-05-14 16:50:06 +0000 | [diff] [blame] | 55 | char *TCID = "fcntl18"; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 56 | int TST_TOTAL = 1; |
| 57 | extern int Tst_count; |
| 58 | struct passwd *pass; |
| 59 | |
| 60 | void setup(void); |
| 61 | void cleanup(void); |
| 62 | int fail; |
| 63 | |
robbiew | 5aab8a7 | 2003-03-26 18:05:30 +0000 | [diff] [blame] | 64 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 65 | { |
| 66 | int retval; |
| 67 | struct flock fl; |
| 68 | int pid, status; |
| 69 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 70 | char *msg; /* message returned from parse_opts */ |
| 71 | |
| 72 | /* parse standard options */ |
| 73 | if ((msg = parse_opts(ac, av, (option_t *)NULL, NULL)) != (char *)NULL){ |
| 74 | tst_brkm(TBROK, cleanup, "OPTION PARSING ERROR - %s", msg); |
| 75 | } |
| 76 | |
| 77 | setup(); /* global setup */ |
| 78 | |
mridge | db63921 | 2005-01-04 21:04:11 +0000 | [diff] [blame] | 79 | /* //block1: */ |
vapier | 7ec19d9 | 2006-02-27 04:38:56 +0000 | [diff] [blame] | 80 | #ifndef UCLINUX |
| 81 | /* Skip since uClinux does not implement memory protection */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 82 | tst_resm(TINFO, "Enter block 1"); |
| 83 | fail = 0; |
subrata_modak | e4cf63d | 2008-01-21 11:16:15 +0000 | [diff] [blame] | 84 | if ((fd = open("temp.dat", O_CREAT|O_RDWR, 0777)) < 0) { //mode must be specified when O_CREATE is in the flag |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 85 | tst_resm(TFAIL, "file opening error"); |
| 86 | fail = 1; |
| 87 | } |
| 88 | |
| 89 | /* Error condition if address is bad */ |
| 90 | retval = fcntl(fd, F_GETLK, (struct flock *)INVAL_FLAG); |
| 91 | if (errno == EFAULT) { |
| 92 | tst_resm(TPASS, "Test F_GETLK: for errno EFAULT PASSED"); |
| 93 | } else { |
| 94 | tst_resm(TFAIL, "Test F_GETLK: for errno EFAULT FAILED"); |
| 95 | fail = 1; |
| 96 | } |
| 97 | if (fail) { |
| 98 | tst_resm(TINFO, "Block 1 FAILED"); |
| 99 | } else { |
| 100 | tst_resm(TINFO, "Block 1 PASSED"); |
| 101 | } |
| 102 | tst_resm(TINFO, "Exit block 1"); |
vapier | 7ec19d9 | 2006-02-27 04:38:56 +0000 | [diff] [blame] | 103 | #else |
| 104 | tst_resm(TINFO, "Skip block 1 on uClinux"); |
| 105 | #endif |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 106 | |
mridge | db63921 | 2005-01-04 21:04:11 +0000 | [diff] [blame] | 107 | /* //block2: */ |
vapier | 7ec19d9 | 2006-02-27 04:38:56 +0000 | [diff] [blame] | 108 | #ifndef UCLINUX |
| 109 | /* Skip since uClinux does not implement memory protection */ |
robbiew | f46d59e | 2005-08-03 16:53:25 +0000 | [diff] [blame] | 110 | tst_resm(TINFO, "Enter block 2"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 111 | fail = 0; |
| 112 | /* Error condition if address is bad */ |
| 113 | retval = fcntl(fd, F_GETLK64, (struct flock *)INVAL_FLAG); |
| 114 | if (errno == EFAULT) { |
| 115 | tst_resm(TPASS, "Test F_GETLK64: for errno EFAULT PASSED"); |
| 116 | } else { |
| 117 | tst_resm(TFAIL, "Test F_GETLK64: for errno EFAULT FAILED"); |
| 118 | fail = 1; |
| 119 | } |
| 120 | if (fail) { |
| 121 | tst_resm(TINFO, "Block 2 FAILED"); |
| 122 | } else { |
| 123 | tst_resm(TINFO, "Block 2 PASSED"); |
| 124 | } |
| 125 | tst_resm(TINFO, "Exit block 2"); |
vapier | 7ec19d9 | 2006-02-27 04:38:56 +0000 | [diff] [blame] | 126 | #else |
| 127 | tst_resm(TINFO, "Skip block 2 on uClinux"); |
| 128 | #endif |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 129 | |
mridge | db63921 | 2005-01-04 21:04:11 +0000 | [diff] [blame] | 130 | /* //block3: */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 131 | tst_resm(TINFO, "Enter block 3"); |
| 132 | fail = 0; |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 133 | if ((pid = FORK_OR_VFORK()) == 0) { /* child */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 134 | fail = 0; |
| 135 | pass = getpwnam("nobody"); |
| 136 | retval = setreuid(-1, pass->pw_uid); |
| 137 | if (retval < 0) { |
| 138 | tst_resm(TFAIL, "setreuid to user nobody failed, " |
| 139 | "errno: %d", errno); |
| 140 | fail = 1; |
| 141 | } |
| 142 | |
| 143 | /* Error condition: invalid cmd */ |
| 144 | retval = fcntl(fd, INVAL_FLAG, &fl); |
| 145 | if (errno == EINVAL) { |
| 146 | tst_resm(TPASS, "Test for errno EINVAL PASSED"); |
| 147 | } else { |
| 148 | tst_resm(TFAIL, "Test for errno EINVAL FAILED, " |
| 149 | "got: %d", errno); |
| 150 | fail = 1; |
| 151 | } |
| 152 | exit(fail); |
| 153 | } else { /* parent */ |
| 154 | waitpid(pid, &status, 0); |
| 155 | if (WEXITSTATUS(status) != 0) { |
| 156 | tst_resm(TFAIL, "child returned bad exit status"); |
| 157 | fail = 1; |
| 158 | } |
| 159 | if (fail) { |
| 160 | tst_resm(TINFO, "Block 3 FAILED"); |
| 161 | } else { |
| 162 | tst_resm(TINFO, "Block 3 PASSED"); |
| 163 | } |
| 164 | } |
| 165 | tst_resm(TINFO, "Exit block 3"); |
| 166 | |
| 167 | cleanup(); |
subrata_modak | 43337a3 | 2009-02-26 11:43:51 +0000 | [diff] [blame^] | 168 | return 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | /* |
| 172 | * setup() |
| 173 | * performs all ONE TIME setup for this test |
| 174 | */ |
| 175 | void |
| 176 | setup() |
| 177 | { |
| 178 | /* capture signals */ |
| 179 | tst_sig(FORK, DEF_HANDLER, cleanup); |
| 180 | |
| 181 | umask(0); |
| 182 | |
| 183 | /* Pause if that option was specified */ |
| 184 | TEST_PAUSE; |
| 185 | |
| 186 | /* make a temp directory and cd to it */ |
| 187 | tst_tmpdir(); |
| 188 | |
nstraz | fa31d55 | 2002-05-14 16:50:06 +0000 | [diff] [blame] | 189 | sprintf(string, "./fcntl18.%d.1", getpid()); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 190 | unlink(string); |
| 191 | } |
| 192 | |
| 193 | /* |
| 194 | * cleanup() |
| 195 | * performs all the ONE TIME cleanup for this test at completion or |
| 196 | * or premature exit. |
| 197 | */ |
| 198 | void |
| 199 | cleanup() |
| 200 | { |
| 201 | /* |
| 202 | * print timing status if that option was specified. |
| 203 | * print errno log if that option was specified |
| 204 | */ |
mridge | 22c9044 | 2004-08-25 15:26:18 +0000 | [diff] [blame] | 205 | close(fd); |
| 206 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 207 | TEST_CLEANUP; |
| 208 | |
| 209 | /* Remove tmp dir and all files in it */ |
| 210 | tst_rmdir(); |
| 211 | |
| 212 | /* exit with return code appropriate for results */ |
| 213 | unlink("temp.dat"); |
| 214 | tst_exit(); |
| 215 | } |