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