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 | * fcntl20.c |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 23 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 24 | * DESCRIPTION |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 25 | * Check locking of regions of a file |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 26 | * |
| 27 | * ALGORITHM |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 28 | * Test unlocking sections around a read lock |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 29 | * |
| 30 | * USAGE |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 31 | * fcntl20 |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 32 | * |
| 33 | * HISTORY |
| 34 | * 07/2001 Ported by Wayne Boyer |
| 35 | * |
| 36 | * RESTRICTIONS |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 37 | * None |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 38 | */ |
| 39 | |
| 40 | #include <fcntl.h> |
| 41 | #include <errno.h> |
| 42 | #include <signal.h> |
robbiew | 5aab8a7 | 2003-03-26 18:05:30 +0000 | [diff] [blame] | 43 | #include <sys/stat.h> |
| 44 | #include <sys/types.h> |
mridge | db63921 | 2005-01-04 21:04:11 +0000 | [diff] [blame] | 45 | #include <sys/wait.h> |
subrata_modak | 923b23f | 2009-11-02 13:57:16 +0000 | [diff] [blame] | 46 | #include <inttypes.h> |
robbiew | 5aab8a7 | 2003-03-26 18:05:30 +0000 | [diff] [blame] | 47 | #include "test.h" |
Garrett Cooper | 0a643cb | 2010-12-21 11:21:19 -0800 | [diff] [blame] | 48 | #include "safe_macros.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 49 | |
| 50 | #define STRINGSIZE 27 |
| 51 | #define STRING "abcdefghijklmnopqrstuvwxyz\n" |
| 52 | #define STOP 0xFFF0 |
| 53 | |
| 54 | int parent_pipe[2]; |
| 55 | int child_pipe[2]; |
| 56 | int fd; |
plars | a1e518d | 2002-09-09 18:23:52 +0000 | [diff] [blame] | 57 | pid_t parent_pid, child_pid; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 58 | |
| 59 | void parent_put(); |
| 60 | void parent_get(); |
| 61 | void child_put(); |
| 62 | void child_get(); |
| 63 | void stop_child(); |
plars | a1e518d | 2002-09-09 18:23:52 +0000 | [diff] [blame] | 64 | void compare_lock(struct flock *, short, short, int, int, pid_t); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 65 | void unlock_file(); |
| 66 | void do_test(struct flock *, short, short, int, int); |
| 67 | void catch_child(); |
| 68 | char *str_type(); |
| 69 | int do_lock(int, short, short, int, int); |
| 70 | |
nstraz | fa31d55 | 2002-05-14 16:50:06 +0000 | [diff] [blame] | 71 | char *TCID = "fcntl20"; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 72 | int TST_TOTAL = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 73 | |
| 74 | void setup(void); |
| 75 | void cleanup(void); |
| 76 | |
| 77 | int fail = 0; |
| 78 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 79 | /* |
| 80 | * setup |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 81 | * performs all ONE TIME setup for this test |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 82 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 83 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 84 | { |
| 85 | char *buf = STRING; |
robbiew | dad2271 | 2003-04-16 19:14:05 +0000 | [diff] [blame] | 86 | char template[PATH_MAX]; |
robbiew | 1e4cf0c | 2005-02-24 17:03:42 +0000 | [diff] [blame] | 87 | struct sigaction act; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 88 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 89 | tst_sig(FORK, DEF_HANDLER, cleanup); |
| 90 | |
| 91 | umask(0); |
| 92 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 93 | TEST_PAUSE; |
| 94 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 95 | parent_pid = getpid(); |
robbiew | 54f7a71 | 2003-04-16 19:20:30 +0000 | [diff] [blame] | 96 | |
Garrett Cooper | 0a643cb | 2010-12-21 11:21:19 -0800 | [diff] [blame] | 97 | SAFE_PIPE(NULL, parent_pipe); |
| 98 | SAFE_PIPE(NULL, child_pipe); |
| 99 | |
robbiew | 54f7a71 | 2003-04-16 19:20:30 +0000 | [diff] [blame] | 100 | tst_tmpdir(); |
Garrett Cooper | 0a643cb | 2010-12-21 11:21:19 -0800 | [diff] [blame] | 101 | |
robbiew | dad2271 | 2003-04-16 19:14:05 +0000 | [diff] [blame] | 102 | snprintf(template, PATH_MAX, "fcntl20XXXXXX"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 103 | |
Garrett Cooper | 0a643cb | 2010-12-21 11:21:19 -0800 | [diff] [blame] | 104 | if ((fd = mkstemp(template)) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 105 | tst_resm(TFAIL | TERRNO, "mkstemp failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 106 | |
Garrett Cooper | 0a643cb | 2010-12-21 11:21:19 -0800 | [diff] [blame] | 107 | SAFE_WRITE(cleanup, 0, fd, buf, STRINGSIZE); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 108 | |
robbiew | 1e4cf0c | 2005-02-24 17:03:42 +0000 | [diff] [blame] | 109 | memset(&act, 0, sizeof(act)); |
| 110 | act.sa_handler = catch_child; |
| 111 | sigemptyset(&act.sa_mask); |
| 112 | sigaddset(&act.sa_mask, SIGCLD); |
Garrett Cooper | 0a643cb | 2010-12-21 11:21:19 -0800 | [diff] [blame] | 113 | if (sigaction(SIGCLD, &act, NULL) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 114 | tst_brkm(TFAIL | TERRNO, cleanup, "SIGCLD signal setup failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 115 | } |
| 116 | |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 117 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 118 | { |
Garrett Cooper | 0a643cb | 2010-12-21 11:21:19 -0800 | [diff] [blame] | 119 | SAFE_CLOSE(NULL, fd); |
| 120 | |
robbiew | 54f7a71 | 2003-04-16 19:20:30 +0000 | [diff] [blame] | 121 | tst_rmdir(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 122 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 125 | void do_child(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 126 | { |
| 127 | struct flock fl; |
| 128 | |
| 129 | close(parent_pipe[1]); |
| 130 | close(child_pipe[0]); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 131 | while (1) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 132 | child_get(&fl); |
| 133 | if (fcntl(fd, F_GETLK, &fl) < 0) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 134 | tst_resm(TFAIL | TERRNO, "fcntl on file failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 135 | fail = 1; |
| 136 | } |
| 137 | child_put(&fl); |
| 138 | } |
| 139 | } |
| 140 | |
robbiew | 5aab8a7 | 2003-03-26 18:05:30 +0000 | [diff] [blame] | 141 | int do_lock(int cmd, short type, short whence, int start, int len) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 142 | { |
| 143 | struct flock fl; |
| 144 | |
| 145 | fl.l_type = type; |
| 146 | fl.l_whence = whence; |
| 147 | fl.l_start = start; |
| 148 | fl.l_len = len; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 149 | return (fcntl(fd, cmd, &fl)); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 150 | } |
| 151 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 152 | void do_test(struct flock *fl, short type, short whence, int start, int len) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 153 | { |
| 154 | fl->l_type = type; |
| 155 | fl->l_whence = whence; |
| 156 | fl->l_start = start; |
| 157 | fl->l_len = len; |
| 158 | fl->l_pid = (short)0; |
| 159 | |
| 160 | parent_put(fl); |
| 161 | parent_get(fl); |
| 162 | } |
| 163 | |
| 164 | void |
| 165 | compare_lock(struct flock *fl, short type, short whence, int start, int len, |
plars | a1e518d | 2002-09-09 18:23:52 +0000 | [diff] [blame] | 166 | pid_t pid) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 167 | { |
| 168 | if (fl->l_type != type) { |
| 169 | tst_resm(TFAIL, "lock type is wrong should be %s is %s", |
| 170 | str_type(type), str_type(fl->l_type)); |
| 171 | fail = 1; |
| 172 | } |
| 173 | |
| 174 | if (fl->l_whence != whence) { |
| 175 | tst_resm(TFAIL, "lock whence is wrong should be %d is %d", |
| 176 | whence, fl->l_whence); |
| 177 | fail = 1; |
| 178 | } |
| 179 | |
| 180 | if (fl->l_start != start) { |
| 181 | tst_resm(TFAIL, "region starts in wrong place, should be" |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 182 | "%d is %" PRId64, start, (int64_t) fl->l_start); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 183 | fail = 1; |
| 184 | } |
| 185 | |
| 186 | if (fl->l_len != len) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 187 | tst_resm(TFAIL, |
| 188 | "region length is wrong, should be %d is %" PRId64, |
| 189 | len, (int64_t) fl->l_len); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 190 | fail = 1; |
| 191 | } |
| 192 | |
| 193 | if (fl->l_pid != pid) { |
| 194 | tst_resm(TFAIL, "locking pid is wrong, should be %d is %d", |
| 195 | pid, fl->l_pid); |
| 196 | fail = 1; |
| 197 | } |
| 198 | } |
| 199 | |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 200 | void unlock_file(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 201 | { |
| 202 | struct flock fl; |
| 203 | |
| 204 | if (do_lock(F_SETLK, (short)F_UNLCK, (short)0, 0, 0) < 0) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 205 | tst_resm(TFAIL, "fcntl on file failed, errno =%d", errno); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 206 | fail = 1; |
| 207 | } |
| 208 | do_test(&fl, F_WRLCK, 0, 0, 0); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 209 | compare_lock(&fl, (short)F_UNLCK, (short)0, 0, 0, (pid_t) 0); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 210 | } |
| 211 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 212 | char *str_type(int type) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 213 | { |
| 214 | static char buf[20]; |
| 215 | |
| 216 | switch (type) { |
| 217 | case 1: |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 218 | return ("F_RDLCK"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 219 | case 2: |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 220 | return ("F_WRLCK"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 221 | case 3: |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 222 | return ("F_UNLCK"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 223 | default: |
| 224 | sprintf(buf, "BAD VALUE: %d", type); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 225 | return (buf); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 226 | } |
| 227 | } |
| 228 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 229 | void parent_put(struct flock *l) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 230 | { |
| 231 | if (write(parent_pipe[1], l, sizeof(*l)) != sizeof(*l)) { |
| 232 | tst_resm(TFAIL, "couldn't send message to child"); |
| 233 | fail = 1; |
| 234 | } |
| 235 | } |
| 236 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 237 | void parent_get(struct flock *l) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 238 | { |
| 239 | if (read(child_pipe[0], l, sizeof(*l)) != sizeof(*l)) { |
| 240 | tst_resm(TFAIL, "couldn't get message from child"); |
| 241 | fail = 1; |
| 242 | } |
| 243 | } |
| 244 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 245 | void child_put(struct flock *l) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 246 | { |
| 247 | if (write(child_pipe[1], l, sizeof(*l)) != sizeof(*l)) { |
| 248 | tst_resm(TFAIL, "couldn't send message to parent"); |
| 249 | fail = 1; |
| 250 | } |
| 251 | } |
| 252 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 253 | void child_get(struct flock *l) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 254 | { |
| 255 | if (read(parent_pipe[0], l, sizeof(*l)) != sizeof(*l)) { |
| 256 | tst_resm(TFAIL, "couldn't get message from parent"); |
| 257 | cleanup(); |
| 258 | } else if (l->l_type == (short)STOP) { |
| 259 | exit(0); |
| 260 | } |
| 261 | } |
| 262 | |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 263 | void stop_child(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 264 | { |
| 265 | struct flock fl; |
| 266 | |
Mike Frysinger | e61ddba | 2014-04-09 23:24:32 -0400 | [diff] [blame] | 267 | signal(SIGCLD, SIG_DFL); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 268 | fl.l_type = STOP; |
| 269 | parent_put(&fl); |
| 270 | wait(0); |
| 271 | } |
| 272 | |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 273 | void catch_child(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 274 | { |
| 275 | tst_resm(TFAIL, "Unexpected death of child process"); |
| 276 | cleanup(); |
| 277 | } |
robbiew | 5aab8a7 | 2003-03-26 18:05:30 +0000 | [diff] [blame] | 278 | |
| 279 | int main(int ac, char **av) |
| 280 | { |
| 281 | struct flock tl; |
| 282 | |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 283 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 284 | const char *msg; |
robbiew | 5aab8a7 | 2003-03-26 18:05:30 +0000 | [diff] [blame] | 285 | |
Garrett Cooper | 45e285d | 2010-11-22 12:19:25 -0800 | [diff] [blame] | 286 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) { |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 287 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
robbiew | 5aab8a7 | 2003-03-26 18:05:30 +0000 | [diff] [blame] | 288 | } |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 289 | #ifdef UCLINUX |
| 290 | maybe_run_child(&do_child, "ddddd", &parent_pipe[0], &parent_pipe[1], |
| 291 | &child_pipe[0], &child_pipe[1], &fd); |
| 292 | #endif |
| 293 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 294 | setup(); /* global setup */ |
robbiew | 5aab8a7 | 2003-03-26 18:05:30 +0000 | [diff] [blame] | 295 | |
| 296 | /* Check for looping state if -i option is given */ |
| 297 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 298 | /* reset tst_count in case we are looping */ |
| 299 | tst_count = 0; |
robbiew | 5aab8a7 | 2003-03-26 18:05:30 +0000 | [diff] [blame] | 300 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 301 | if ((child_pid = FORK_OR_VFORK()) == 0) { /* child */ |
| 302 | #ifdef UCLINUX |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 303 | if (self_exec |
| 304 | (av[0], "ddddd", parent_pipe[0], parent_pipe[1], |
| 305 | child_pipe[0], child_pipe[1], fd) < 0) { |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 306 | tst_resm(TFAIL, "self_exec failed"); |
| 307 | cleanup(); |
| 308 | } |
| 309 | #else |
robbiew | 5aab8a7 | 2003-03-26 18:05:30 +0000 | [diff] [blame] | 310 | do_child(); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 311 | #endif |
robbiew | 5aab8a7 | 2003-03-26 18:05:30 +0000 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | if (child_pid < 0) { |
| 315 | tst_resm(TFAIL, "Fork failed"); |
| 316 | cleanup(); |
| 317 | } |
| 318 | |
| 319 | (void)close(parent_pipe[0]); |
| 320 | (void)close(child_pipe[1]); |
| 321 | |
mridge | db63921 | 2005-01-04 21:04:11 +0000 | [diff] [blame] | 322 | /* //block1: */ |
robbiew | 5aab8a7 | 2003-03-26 18:05:30 +0000 | [diff] [blame] | 323 | tst_resm(TINFO, "Enter block 1"); |
| 324 | /* |
| 325 | * Add a read lock to the middle of the file and unlock a |
| 326 | * section just before the lock |
| 327 | */ |
| 328 | if (do_lock(F_SETLK, (short)F_RDLCK, (short)0, 10, 5) < 0) { |
robbiew | dad2271 | 2003-04-16 19:14:05 +0000 | [diff] [blame] | 329 | tst_resm(TFAIL, "fcntl on file failed, errno =%d", |
| 330 | errno); |
robbiew | 5aab8a7 | 2003-03-26 18:05:30 +0000 | [diff] [blame] | 331 | fail = 1; |
| 332 | } |
| 333 | |
| 334 | if (do_lock(F_SETLK, (short)F_UNLCK, (short)0, 5, 5) < 0) { |
robbiew | dad2271 | 2003-04-16 19:14:05 +0000 | [diff] [blame] | 335 | tst_resm(TFAIL, "fcntl on file failed, errno =%d", |
| 336 | errno); |
robbiew | 5aab8a7 | 2003-03-26 18:05:30 +0000 | [diff] [blame] | 337 | fail = 1; |
| 338 | } |
| 339 | |
| 340 | /* |
| 341 | * Test read lock |
| 342 | */ |
| 343 | do_test(&tl, (short)F_WRLCK, (short)0, 0, 0); |
| 344 | compare_lock(&tl, (short)F_RDLCK, (short)0, 10, 5, parent_pid); |
| 345 | |
| 346 | /* |
| 347 | * Test that the rest of the file is unlocked |
| 348 | */ |
| 349 | do_test(&tl, (short)F_WRLCK, (short)0, 15, 0); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 350 | compare_lock(&tl, (short)F_UNLCK, (short)0, 15, 0, (pid_t) 0); |
robbiew | 5aab8a7 | 2003-03-26 18:05:30 +0000 | [diff] [blame] | 351 | |
| 352 | /* |
| 353 | * remove all the locks set above |
| 354 | */ |
| 355 | unlock_file(); |
| 356 | |
| 357 | if (fail) { |
| 358 | tst_resm(TINFO, "Test block 1: FAILED"); |
| 359 | } else { |
| 360 | tst_resm(TINFO, "Test block 1: PASSED"); |
| 361 | } |
| 362 | tst_resm(TINFO, "Exit block 1"); |
| 363 | |
mridge | db63921 | 2005-01-04 21:04:11 +0000 | [diff] [blame] | 364 | /* //block2: */ |
robbiew | 5aab8a7 | 2003-03-26 18:05:30 +0000 | [diff] [blame] | 365 | tst_resm(TINFO, "Enter block 2"); |
| 366 | fail = 0; |
| 367 | /* |
| 368 | * Set a read lock in the middle and do an unlock that |
| 369 | * ends at the first byte of the read lock. |
| 370 | */ |
| 371 | if (do_lock(F_SETLK, (short)F_RDLCK, (short)0, 10, 5) < 0) { |
robbiew | dad2271 | 2003-04-16 19:14:05 +0000 | [diff] [blame] | 372 | tst_resm(TFAIL, "fcntl on file failed, errno =%d", |
| 373 | errno); |
robbiew | 5aab8a7 | 2003-03-26 18:05:30 +0000 | [diff] [blame] | 374 | fail = 1; |
| 375 | } |
| 376 | |
| 377 | if (do_lock(F_SETLK, (short)F_UNLCK, (short)0, 5, 6) < 0) { |
robbiew | dad2271 | 2003-04-16 19:14:05 +0000 | [diff] [blame] | 378 | tst_resm(TFAIL, "fcntl on file failed, errno =%d", |
| 379 | errno); |
robbiew | 5aab8a7 | 2003-03-26 18:05:30 +0000 | [diff] [blame] | 380 | fail = 1; |
| 381 | } |
| 382 | |
| 383 | /* |
| 384 | * Test read lock |
| 385 | */ |
| 386 | do_test(&tl, (short)F_WRLCK, (short)0, 0, 0); |
| 387 | compare_lock(&tl, (short)F_RDLCK, (short)0, 11, 4, parent_pid); |
| 388 | |
| 389 | /* |
| 390 | * Test to make sure the rest of the file is unlocked |
| 391 | */ |
| 392 | do_test(&tl, (short)F_WRLCK, (short)0, 15, 0); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 393 | compare_lock(&tl, (short)F_UNLCK, (short)0, 15, 0, (pid_t) 0); |
robbiew | 5aab8a7 | 2003-03-26 18:05:30 +0000 | [diff] [blame] | 394 | |
| 395 | /* |
| 396 | * remove all the locks set above |
| 397 | */ |
| 398 | unlock_file(); |
| 399 | |
Garrett Cooper | 0a643cb | 2010-12-21 11:21:19 -0800 | [diff] [blame] | 400 | if (fail) |
robbiew | 5aab8a7 | 2003-03-26 18:05:30 +0000 | [diff] [blame] | 401 | tst_resm(TINFO, "Test block 2: FAILED"); |
Garrett Cooper | 0a643cb | 2010-12-21 11:21:19 -0800 | [diff] [blame] | 402 | else |
robbiew | 5aab8a7 | 2003-03-26 18:05:30 +0000 | [diff] [blame] | 403 | tst_resm(TINFO, "Test block 2: PASSED"); |
robbiew | 5aab8a7 | 2003-03-26 18:05:30 +0000 | [diff] [blame] | 404 | tst_resm(TINFO, "Exit block 2"); |
| 405 | |
mridge | db63921 | 2005-01-04 21:04:11 +0000 | [diff] [blame] | 406 | /* //block3: */ |
robbiew | 5aab8a7 | 2003-03-26 18:05:30 +0000 | [diff] [blame] | 407 | tst_resm(TINFO, "Enter block 3"); |
| 408 | fail = 0; |
| 409 | |
| 410 | /* |
| 411 | * Set a read lock on the middle of the file and do an |
| 412 | * unlock that overlaps the front of the read |
| 413 | */ |
| 414 | if (do_lock(F_SETLK, (short)F_RDLCK, (short)0, 10, 5) < 0) { |
robbiew | dad2271 | 2003-04-16 19:14:05 +0000 | [diff] [blame] | 415 | tst_resm(TFAIL, "fcntl on file failed, errno =%d", |
| 416 | errno); |
robbiew | 5aab8a7 | 2003-03-26 18:05:30 +0000 | [diff] [blame] | 417 | fail = 1; |
| 418 | } |
| 419 | |
| 420 | if (do_lock(F_SETLK, (short)F_UNLCK, (short)0, 5, 8) < 0) { |
robbiew | dad2271 | 2003-04-16 19:14:05 +0000 | [diff] [blame] | 421 | tst_resm(TFAIL, "fcntl on file failed, errno =%d", |
| 422 | errno); |
robbiew | 5aab8a7 | 2003-03-26 18:05:30 +0000 | [diff] [blame] | 423 | fail = 1; |
| 424 | } |
| 425 | |
| 426 | /* |
| 427 | * Test the read lock |
| 428 | */ |
| 429 | do_test(&tl, (short)F_WRLCK, (short)0, 0, 0); |
| 430 | compare_lock(&tl, (short)F_RDLCK, (short)0, 13, 2, parent_pid); |
| 431 | |
| 432 | /* |
| 433 | * Test to make sure the rest of the file is unlocked |
| 434 | */ |
| 435 | do_test(&tl, (short)F_WRLCK, (short)0, 15, 0); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 436 | compare_lock(&tl, (short)F_UNLCK, (short)0, 15, 0, (pid_t) 0); |
robbiew | 5aab8a7 | 2003-03-26 18:05:30 +0000 | [diff] [blame] | 437 | |
| 438 | /* |
| 439 | * remove all the locks set above |
| 440 | */ |
| 441 | unlock_file(); |
| 442 | |
Garrett Cooper | 0a643cb | 2010-12-21 11:21:19 -0800 | [diff] [blame] | 443 | if (fail) |
robbiew | 5aab8a7 | 2003-03-26 18:05:30 +0000 | [diff] [blame] | 444 | tst_resm(TINFO, "Test block 3: FAILED"); |
Garrett Cooper | 0a643cb | 2010-12-21 11:21:19 -0800 | [diff] [blame] | 445 | else |
robbiew | 5aab8a7 | 2003-03-26 18:05:30 +0000 | [diff] [blame] | 446 | tst_resm(TINFO, "Test block 3: PASSED"); |
robbiew | 5aab8a7 | 2003-03-26 18:05:30 +0000 | [diff] [blame] | 447 | tst_resm(TINFO, "Exit block 3"); |
| 448 | |
mridge | db63921 | 2005-01-04 21:04:11 +0000 | [diff] [blame] | 449 | /* //block4: */ |
robbiew | 5aab8a7 | 2003-03-26 18:05:30 +0000 | [diff] [blame] | 450 | tst_resm(TINFO, "Enter blcok 4"); |
| 451 | fail = 0; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 452 | |
robbiew | 5aab8a7 | 2003-03-26 18:05:30 +0000 | [diff] [blame] | 453 | /* |
| 454 | * Set a read lock in the middle of a file and unlock a |
| 455 | * section in the middle of it |
| 456 | */ |
| 457 | if (do_lock(F_SETLK, (short)F_RDLCK, (short)0, 10, 10) < 0) { |
robbiew | dad2271 | 2003-04-16 19:14:05 +0000 | [diff] [blame] | 458 | tst_resm(TFAIL, "fcntl on file failed, errno =%d", |
| 459 | errno); |
robbiew | 5aab8a7 | 2003-03-26 18:05:30 +0000 | [diff] [blame] | 460 | fail = 1; |
| 461 | } |
| 462 | |
| 463 | if (do_lock(F_SETLK, (short)F_UNLCK, (short)0, 13, 5) < 0) { |
robbiew | dad2271 | 2003-04-16 19:14:05 +0000 | [diff] [blame] | 464 | tst_resm(TFAIL, "fcntl on file failed, errno =%d", |
| 465 | errno); |
robbiew | 5aab8a7 | 2003-03-26 18:05:30 +0000 | [diff] [blame] | 466 | fail = 1; |
| 467 | } |
| 468 | |
| 469 | /* |
| 470 | * Test the first read lock |
| 471 | */ |
| 472 | do_test(&tl, (short)F_WRLCK, (short)0, 0, 0); |
| 473 | compare_lock(&tl, (short)F_RDLCK, (short)0, 10, 3, parent_pid); |
| 474 | |
| 475 | /* |
| 476 | * Test the second read lock |
| 477 | */ |
| 478 | do_test(&tl, (short)F_WRLCK, (short)0, 13, 0); |
| 479 | compare_lock(&tl, (short)F_RDLCK, (short)0, 18, 2, parent_pid); |
| 480 | |
| 481 | /* |
| 482 | * Test to make sure the rest of the file is unlocked |
| 483 | */ |
| 484 | do_test(&tl, (short)F_WRLCK, (short)0, 20, 0); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 485 | compare_lock(&tl, (short)F_UNLCK, (short)0, 20, 0, (pid_t) 0); |
robbiew | 5aab8a7 | 2003-03-26 18:05:30 +0000 | [diff] [blame] | 486 | |
| 487 | /* |
| 488 | * remove all the locks set above |
| 489 | */ |
| 490 | unlock_file(); |
| 491 | |
Garrett Cooper | 0a643cb | 2010-12-21 11:21:19 -0800 | [diff] [blame] | 492 | if (fail) |
robbiew | 5aab8a7 | 2003-03-26 18:05:30 +0000 | [diff] [blame] | 493 | tst_resm(TINFO, "Test block 4: FAILED"); |
Garrett Cooper | 0a643cb | 2010-12-21 11:21:19 -0800 | [diff] [blame] | 494 | else |
robbiew | 5aab8a7 | 2003-03-26 18:05:30 +0000 | [diff] [blame] | 495 | tst_resm(TINFO, "Test block 4: PASSED"); |
robbiew | 5aab8a7 | 2003-03-26 18:05:30 +0000 | [diff] [blame] | 496 | tst_resm(TINFO, "Exit block 4"); |
| 497 | |
mridge | db63921 | 2005-01-04 21:04:11 +0000 | [diff] [blame] | 498 | /* //block5: */ |
robbiew | 5aab8a7 | 2003-03-26 18:05:30 +0000 | [diff] [blame] | 499 | tst_resm(TINFO, "Enter block 5"); |
| 500 | fail = 0; |
| 501 | |
| 502 | /* |
| 503 | * Set a read lock in the middle of the file and do a |
| 504 | * unlock that overlaps the end |
| 505 | */ |
| 506 | if (do_lock(F_SETLK, (short)F_RDLCK, (short)0, 10, 5) < 0) { |
robbiew | dad2271 | 2003-04-16 19:14:05 +0000 | [diff] [blame] | 507 | tst_resm(TFAIL, "fcntl on file failed, errno =%d", |
| 508 | errno); |
robbiew | 5aab8a7 | 2003-03-26 18:05:30 +0000 | [diff] [blame] | 509 | fail = 1; |
| 510 | } |
| 511 | |
| 512 | if (do_lock(F_SETLK, (short)F_UNLCK, (short)0, 13, 5) < 0) { |
robbiew | dad2271 | 2003-04-16 19:14:05 +0000 | [diff] [blame] | 513 | tst_resm(TFAIL, "fcntl on file failed, errno =%d", |
| 514 | errno); |
robbiew | 5aab8a7 | 2003-03-26 18:05:30 +0000 | [diff] [blame] | 515 | fail = 1; |
| 516 | } |
| 517 | |
| 518 | /* |
| 519 | * Test the read lock |
| 520 | */ |
| 521 | do_test(&tl, (short)F_WRLCK, (short)0, 0, 0); |
| 522 | compare_lock(&tl, (short)F_RDLCK, (short)0, 10, 3, parent_pid); |
| 523 | |
| 524 | /* |
| 525 | * Test to make sure the rest of the file is unlocked |
| 526 | */ |
| 527 | do_test(&tl, (short)F_WRLCK, (short)0, 13, 0); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 528 | compare_lock(&tl, (short)F_UNLCK, (short)0, 13, 0, (pid_t) 0); |
robbiew | 5aab8a7 | 2003-03-26 18:05:30 +0000 | [diff] [blame] | 529 | |
| 530 | /* |
| 531 | * remove all the locks set above |
| 532 | */ |
| 533 | unlock_file(); |
| 534 | |
Garrett Cooper | 0a643cb | 2010-12-21 11:21:19 -0800 | [diff] [blame] | 535 | if (fail) |
robbiew | 5aab8a7 | 2003-03-26 18:05:30 +0000 | [diff] [blame] | 536 | tst_resm(TINFO, "Test block 5: FAILED"); |
Garrett Cooper | 0a643cb | 2010-12-21 11:21:19 -0800 | [diff] [blame] | 537 | else |
robbiew | 5aab8a7 | 2003-03-26 18:05:30 +0000 | [diff] [blame] | 538 | tst_resm(TINFO, "Test block 5: PASSED"); |
robbiew | 5aab8a7 | 2003-03-26 18:05:30 +0000 | [diff] [blame] | 539 | tst_resm(TINFO, "Exit block 5"); |
| 540 | |
mridge | db63921 | 2005-01-04 21:04:11 +0000 | [diff] [blame] | 541 | /* //block6: */ |
robbiew | 5aab8a7 | 2003-03-26 18:05:30 +0000 | [diff] [blame] | 542 | tst_resm(TINFO, "Enter block 6"); |
| 543 | fail = 0; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 544 | |
robbiew | 5aab8a7 | 2003-03-26 18:05:30 +0000 | [diff] [blame] | 545 | /* |
| 546 | * Set read lock in the middle of the file and do an unlock |
| 547 | * starting at the last byte of the read lock |
| 548 | */ |
| 549 | if (do_lock(F_SETLK, (short)F_RDLCK, (short)0, 10, 5) < 0) { |
robbiew | dad2271 | 2003-04-16 19:14:05 +0000 | [diff] [blame] | 550 | tst_resm(TFAIL, "fcntl on file failed, errno =%d", |
| 551 | errno); |
robbiew | 5aab8a7 | 2003-03-26 18:05:30 +0000 | [diff] [blame] | 552 | fail = 1; |
| 553 | } |
| 554 | |
| 555 | if (do_lock(F_SETLK, (short)F_UNLCK, (short)0, 14, 5) < 0) { |
robbiew | dad2271 | 2003-04-16 19:14:05 +0000 | [diff] [blame] | 556 | tst_resm(TFAIL, "fcntl on file failed, errno =%d", |
| 557 | errno); |
robbiew | 5aab8a7 | 2003-03-26 18:05:30 +0000 | [diff] [blame] | 558 | fail = 1; |
| 559 | } |
| 560 | |
| 561 | /* |
| 562 | * Test read lock |
| 563 | */ |
| 564 | do_test(&tl, (short)F_WRLCK, (short)0, 10, 0); |
| 565 | compare_lock(&tl, (short)F_RDLCK, (short)0, 10, 4, parent_pid); |
| 566 | |
| 567 | /* |
| 568 | * Test to make sure the end of the file is unlocked |
| 569 | */ |
| 570 | do_test(&tl, (short)F_WRLCK, (short)0, 14, 0); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 571 | compare_lock(&tl, (short)F_UNLCK, (short)0, 14, 0, (pid_t) 0); |
robbiew | 5aab8a7 | 2003-03-26 18:05:30 +0000 | [diff] [blame] | 572 | |
| 573 | /* |
| 574 | * remove all the locks set above |
| 575 | */ |
| 576 | unlock_file(); |
| 577 | |
Garrett Cooper | 0a643cb | 2010-12-21 11:21:19 -0800 | [diff] [blame] | 578 | if (fail) |
robbiew | 5aab8a7 | 2003-03-26 18:05:30 +0000 | [diff] [blame] | 579 | tst_resm(TINFO, "Test block 6: FAILED"); |
Garrett Cooper | 0a643cb | 2010-12-21 11:21:19 -0800 | [diff] [blame] | 580 | else |
robbiew | 5aab8a7 | 2003-03-26 18:05:30 +0000 | [diff] [blame] | 581 | tst_resm(TINFO, "Test block 6: PASSED"); |
robbiew | 5aab8a7 | 2003-03-26 18:05:30 +0000 | [diff] [blame] | 582 | tst_resm(TINFO, "Exit block 6"); |
| 583 | |
mridge | db63921 | 2005-01-04 21:04:11 +0000 | [diff] [blame] | 584 | /* //block7: */ |
robbiew | 5aab8a7 | 2003-03-26 18:05:30 +0000 | [diff] [blame] | 585 | tst_resm(TINFO, "Enter block 7"); |
| 586 | fail = 0; |
| 587 | |
| 588 | /* |
| 589 | * Set a read lock at the middle of the file and do an |
| 590 | * unlock that starts at the byte past the end of the read |
| 591 | * lock |
| 592 | */ |
| 593 | if (do_lock(F_SETLK, (short)F_RDLCK, (short)0, 10, 5) < 0) { |
robbiew | dad2271 | 2003-04-16 19:14:05 +0000 | [diff] [blame] | 594 | tst_resm(TFAIL, "fcntl on file failed, errno =%d", |
| 595 | errno); |
robbiew | 5aab8a7 | 2003-03-26 18:05:30 +0000 | [diff] [blame] | 596 | fail = 1; |
| 597 | } |
| 598 | |
| 599 | if (do_lock(F_SETLK, (short)F_UNLCK, (short)0, 16, 0) < 0) { |
robbiew | dad2271 | 2003-04-16 19:14:05 +0000 | [diff] [blame] | 600 | tst_resm(TFAIL, "fcntl on file failed, errno =%d", |
| 601 | errno); |
robbiew | 5aab8a7 | 2003-03-26 18:05:30 +0000 | [diff] [blame] | 602 | fail = 1; |
| 603 | } |
| 604 | |
| 605 | /* |
| 606 | * Test the read lock |
| 607 | */ |
| 608 | do_test(&tl, (short)F_WRLCK, (short)0, 0, 0); |
| 609 | compare_lock(&tl, (short)F_RDLCK, (short)0, 10, 5, parent_pid); |
| 610 | |
| 611 | /* |
| 612 | * Test to make sure the rest of the file is unlocked |
| 613 | */ |
| 614 | do_test(&tl, (short)F_WRLCK, (short)0, 16, 0); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 615 | compare_lock(&tl, (short)F_UNLCK, (short)0, 16, 0, (pid_t) 0); |
robbiew | 5aab8a7 | 2003-03-26 18:05:30 +0000 | [diff] [blame] | 616 | |
| 617 | /* |
| 618 | * remove all the locks set above |
| 619 | */ |
| 620 | unlock_file(); |
| 621 | |
Garrett Cooper | 0a643cb | 2010-12-21 11:21:19 -0800 | [diff] [blame] | 622 | if (fail) |
robbiew | 5aab8a7 | 2003-03-26 18:05:30 +0000 | [diff] [blame] | 623 | tst_resm(TINFO, "Test block 7: FAILED"); |
Garrett Cooper | 0a643cb | 2010-12-21 11:21:19 -0800 | [diff] [blame] | 624 | else |
robbiew | 5aab8a7 | 2003-03-26 18:05:30 +0000 | [diff] [blame] | 625 | tst_resm(TINFO, "Test block 7: PASSED"); |
robbiew | 5aab8a7 | 2003-03-26 18:05:30 +0000 | [diff] [blame] | 626 | |
| 627 | tst_resm(TINFO, "Exit block 7"); |
| 628 | |
| 629 | stop_child(); |
robbiew | 5aab8a7 | 2003-03-26 18:05:30 +0000 | [diff] [blame] | 630 | } |
| 631 | cleanup(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 632 | tst_exit(); |
Garrett Cooper | 0a643cb | 2010-12-21 11:21:19 -0800 | [diff] [blame] | 633 | } |