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 |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 22 | * fcntl18.c |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 23 | * |
| 24 | * DESCRIPTION |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 25 | * Test to check the error conditions in fcntl system call |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 26 | * |
| 27 | * USAGE |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +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 |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 34 | * NONE |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 47 | |
| 48 | #define INVAL_FLAG -1 |
| 49 | #define INVAL_MIN (-2147483647L-1L) |
| 50 | |
| 51 | int fd; |
| 52 | char string[40] = ""; |
| 53 | |
nstraz | fa31d55 | 2002-05-14 16:50:06 +0000 | [diff] [blame] | 54 | char *TCID = "fcntl18"; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 55 | int TST_TOTAL = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 56 | struct passwd *pass; |
| 57 | |
| 58 | void setup(void); |
| 59 | void cleanup(void); |
| 60 | int fail; |
| 61 | |
robbiew | 5aab8a7 | 2003-03-26 18:05:30 +0000 | [diff] [blame] | 62 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 63 | { |
| 64 | int retval; |
| 65 | struct flock fl; |
| 66 | int pid, status; |
| 67 | |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 68 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 69 | |
Garrett Cooper | 45e285d | 2010-11-22 12:19:25 -0800 | [diff] [blame] | 70 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) { |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 71 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 72 | } |
| 73 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 74 | setup(); /* global setup */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 75 | |
mridge | db63921 | 2005-01-04 21:04:11 +0000 | [diff] [blame] | 76 | /* //block1: */ |
vapier | 7ec19d9 | 2006-02-27 04:38:56 +0000 | [diff] [blame] | 77 | #ifndef UCLINUX |
| 78 | /* Skip since uClinux does not implement memory protection */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 79 | tst_resm(TINFO, "Enter block 1"); |
| 80 | fail = 0; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 81 | 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] | 82 | tst_resm(TFAIL, "file opening error"); |
| 83 | fail = 1; |
| 84 | } |
| 85 | |
| 86 | /* Error condition if address is bad */ |
| 87 | retval = fcntl(fd, F_GETLK, (struct flock *)INVAL_FLAG); |
| 88 | if (errno == EFAULT) { |
| 89 | tst_resm(TPASS, "Test F_GETLK: for errno EFAULT PASSED"); |
| 90 | } else { |
| 91 | tst_resm(TFAIL, "Test F_GETLK: for errno EFAULT FAILED"); |
| 92 | fail = 1; |
| 93 | } |
| 94 | if (fail) { |
| 95 | tst_resm(TINFO, "Block 1 FAILED"); |
| 96 | } else { |
| 97 | tst_resm(TINFO, "Block 1 PASSED"); |
| 98 | } |
| 99 | tst_resm(TINFO, "Exit block 1"); |
vapier | 7ec19d9 | 2006-02-27 04:38:56 +0000 | [diff] [blame] | 100 | #else |
| 101 | tst_resm(TINFO, "Skip block 1 on uClinux"); |
| 102 | #endif |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 103 | |
mridge | db63921 | 2005-01-04 21:04:11 +0000 | [diff] [blame] | 104 | /* //block2: */ |
vapier | 7ec19d9 | 2006-02-27 04:38:56 +0000 | [diff] [blame] | 105 | #ifndef UCLINUX |
| 106 | /* Skip since uClinux does not implement memory protection */ |
robbiew | f46d59e | 2005-08-03 16:53:25 +0000 | [diff] [blame] | 107 | tst_resm(TINFO, "Enter block 2"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 108 | fail = 0; |
| 109 | /* Error condition if address is bad */ |
| 110 | retval = fcntl(fd, F_GETLK64, (struct flock *)INVAL_FLAG); |
| 111 | if (errno == EFAULT) { |
| 112 | tst_resm(TPASS, "Test F_GETLK64: for errno EFAULT PASSED"); |
| 113 | } else { |
| 114 | tst_resm(TFAIL, "Test F_GETLK64: for errno EFAULT FAILED"); |
| 115 | fail = 1; |
| 116 | } |
| 117 | if (fail) { |
| 118 | tst_resm(TINFO, "Block 2 FAILED"); |
| 119 | } else { |
| 120 | tst_resm(TINFO, "Block 2 PASSED"); |
| 121 | } |
| 122 | tst_resm(TINFO, "Exit block 2"); |
vapier | 7ec19d9 | 2006-02-27 04:38:56 +0000 | [diff] [blame] | 123 | #else |
| 124 | tst_resm(TINFO, "Skip block 2 on uClinux"); |
| 125 | #endif |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 126 | |
mridge | db63921 | 2005-01-04 21:04:11 +0000 | [diff] [blame] | 127 | /* //block3: */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 128 | tst_resm(TINFO, "Enter block 3"); |
| 129 | fail = 0; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 130 | if ((pid = FORK_OR_VFORK()) == 0) { /* child */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 131 | fail = 0; |
| 132 | pass = getpwnam("nobody"); |
| 133 | retval = setreuid(-1, pass->pw_uid); |
| 134 | if (retval < 0) { |
| 135 | tst_resm(TFAIL, "setreuid to user nobody failed, " |
| 136 | "errno: %d", errno); |
| 137 | fail = 1; |
| 138 | } |
| 139 | |
| 140 | /* Error condition: invalid cmd */ |
| 141 | retval = fcntl(fd, INVAL_FLAG, &fl); |
| 142 | if (errno == EINVAL) { |
| 143 | tst_resm(TPASS, "Test for errno EINVAL PASSED"); |
| 144 | } else { |
| 145 | tst_resm(TFAIL, "Test for errno EINVAL FAILED, " |
| 146 | "got: %d", errno); |
| 147 | fail = 1; |
| 148 | } |
| 149 | exit(fail); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 150 | } else { /* parent */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 151 | waitpid(pid, &status, 0); |
| 152 | if (WEXITSTATUS(status) != 0) { |
| 153 | tst_resm(TFAIL, "child returned bad exit status"); |
| 154 | fail = 1; |
| 155 | } |
| 156 | if (fail) { |
| 157 | tst_resm(TINFO, "Block 3 FAILED"); |
| 158 | } else { |
| 159 | tst_resm(TINFO, "Block 3 PASSED"); |
| 160 | } |
| 161 | } |
| 162 | tst_resm(TINFO, "Exit block 3"); |
| 163 | |
| 164 | cleanup(); |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 165 | tst_exit(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 166 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | /* |
| 170 | * setup() |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 171 | * performs all ONE TIME setup for this test |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 172 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 173 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 174 | { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 175 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 176 | tst_sig(FORK, DEF_HANDLER, cleanup); |
| 177 | |
| 178 | umask(0); |
| 179 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 180 | TEST_PAUSE; |
| 181 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 182 | tst_tmpdir(); |
| 183 | |
nstraz | fa31d55 | 2002-05-14 16:50:06 +0000 | [diff] [blame] | 184 | sprintf(string, "./fcntl18.%d.1", getpid()); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 185 | unlink(string); |
| 186 | } |
| 187 | |
| 188 | /* |
| 189 | * cleanup() |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 190 | * performs all the ONE TIME cleanup for this test at completion or |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 191 | * or premature exit. |
| 192 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 193 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 194 | { |
| 195 | /* |
| 196 | * print timing status if that option was specified. |
| 197 | * print errno log if that option was specified |
| 198 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 199 | close(fd); |
mridge | 22c9044 | 2004-08-25 15:26:18 +0000 | [diff] [blame] | 200 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 201 | tst_rmdir(); |
| 202 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 203 | unlink("temp.dat"); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 204 | |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 205 | } |