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 | * fcntl14.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 | * File locking test cases for fcntl. In Linux, S_ENFMT is not implemented |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 26 | * in the kernel. However all standard Unix kernels define S_ENFMT as |
| 27 | * S_ISGID. So this test defines S_ENFMT as S_ISGID. |
| 28 | * |
| 29 | * ALGORITHM |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 30 | * Various test cases are used to lock a file opened without mandatory |
| 31 | * locking, with mandatory locking and mandatory locking with NOBLOCK |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 32 | * |
| 33 | * USAGE |
nstraz | fa31d55 | 2002-05-14 16:50:06 +0000 | [diff] [blame] | 34 | * fcntl14 |
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 | */ |
robbiew | 4cf8096 | 2003-03-25 21:53:41 +0000 | [diff] [blame] | 42 | #define _GNU_SOURCE 1 |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 43 | #include <fcntl.h> |
| 44 | #include <sys/types.h> |
| 45 | #include <sys/stat.h> |
| 46 | #include <signal.h> |
| 47 | #include <errno.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 | #include "test.h" |
Cyril Hrubis | 0f0e348 | 2014-02-27 16:08:04 +0100 | [diff] [blame] | 51 | #include "tst_fs_type.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 52 | |
mridge | db63921 | 2005-01-04 21:04:11 +0000 | [diff] [blame] | 53 | #define SKIP 0x0c00 |
| 54 | #if SKIP == F_RDLCK || SKIP== F_WRLCK |
| 55 | #error invalid value for SKIP, must be distinct from F_RDLCK and F_WRLCK |
| 56 | #endif |
| 57 | #ifndef S_ENFMT |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 58 | #define S_ENFMT S_ISGID |
mridge | db63921 | 2005-01-04 21:04:11 +0000 | [diff] [blame] | 59 | #endif |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 60 | |
| 61 | /* NOBLOCK - immediate success */ |
| 62 | #define NOBLOCK 2 |
| 63 | |
| 64 | /* WILLBLOCK - blocks, then succeeds (parent must unlock records) */ |
| 65 | #define WILLBLOCK 3 |
| 66 | |
| 67 | #define TIME_OUT 60 |
| 68 | |
| 69 | typedef struct { |
| 70 | short a_type; |
| 71 | short a_whence; |
| 72 | long a_start; |
| 73 | long a_len; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 74 | short b_type; /* SKIP means suppress fcntl call */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 75 | short b_whence; |
| 76 | long b_start; |
| 77 | long b_len; |
| 78 | short c_type; |
subrata_modak | b370c3c | 2007-11-25 16:37:35 +0000 | [diff] [blame] | 79 | int c_whence; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 80 | long c_start; |
| 81 | long c_len; |
| 82 | short c_flag; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 83 | } testcase; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 84 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 85 | static testcase testcases[] = { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 86 | /* Test cases: entire boundary */ |
| 87 | /* #1 Parent making a write lock on entire file */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 88 | {F_WRLCK, 0, 0L, 0L, SKIP, 0, 0L, 0L, |
| 89 | /* Child attempting a read lock on entire file */ |
| 90 | F_RDLCK, 0, 0L, 0L, WILLBLOCK}, |
| 91 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 92 | /* #2 Parent making a write lock on entire file */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 93 | {F_WRLCK, 0, 0L, 0L, SKIP, 0, 0L, 0L, |
| 94 | /* Child attempting a write lock on entire file */ |
| 95 | F_WRLCK, 0, 0L, 0L, WILLBLOCK}, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 96 | |
| 97 | /* #3 Parent making a read lock on entire file */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 98 | {F_RDLCK, 0, 0L, 0L, SKIP, 0, 0L, 0L, |
| 99 | /* Child attempting a read lock on entire file */ |
| 100 | F_RDLCK, 0, 0L, 0L, NOBLOCK}, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 101 | |
| 102 | /* #4 Parent making a read lock on entire file */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 103 | {F_RDLCK, 0, 0L, 0L, SKIP, 0, 0L, 0L, |
| 104 | /* Child attempting a write lock on entire file */ |
| 105 | F_WRLCK, 0, 0L, 0L, WILLBLOCK}, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 106 | |
| 107 | /* Test case: start boundary */ |
| 108 | /* #5 Parent making a write lock on entire file */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 109 | {F_WRLCK, 0, 0L, 0L, SKIP, 0, 0L, 0L, |
| 110 | /* |
| 111 | * Child attempting a read lock from beginning of |
| 112 | * file for 5 bytes |
| 113 | */ |
| 114 | F_RDLCK, 0, 0L, 5L, WILLBLOCK}, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 115 | |
| 116 | /* #6 Parent making a write lock on entire file */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 117 | {F_WRLCK, 0, 0L, 0L, SKIP, 0, 0L, 0L, |
| 118 | /* |
| 119 | * Child attempting a write lock from beginning of |
| 120 | * file for 5 bytes |
| 121 | */ |
| 122 | F_WRLCK, 0, 0L, 5L, WILLBLOCK}, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 123 | |
| 124 | /* #7 Parent making a read lock on entire file */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 125 | {F_RDLCK, 0, 0L, 0L, SKIP, 0, 0L, 0L, |
| 126 | /* |
| 127 | * Child attempting a read lock from beginning of |
| 128 | * file for 5 bytes |
| 129 | */ |
| 130 | F_RDLCK, 0, 0L, 5L, NOBLOCK}, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 131 | |
| 132 | /* #8 Parent making a read lock on entire file */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 133 | {F_RDLCK, 0, 0L, 0L, SKIP, 0, 0L, 0L, |
| 134 | /* |
| 135 | * Child attempting a write lock from beginning of |
| 136 | * file for 5 bytes |
| 137 | */ |
| 138 | F_WRLCK, 0, 0L, 5L, WILLBLOCK}, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 139 | |
| 140 | /* Test cases: end boundary */ |
| 141 | /* #9 Parent making a write lock on entire file */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 142 | {F_WRLCK, 0, 0L, 0L, SKIP, 0, 0L, 0L, |
| 143 | /* Child attempting a read lock from byte 7 to end of file */ |
| 144 | F_RDLCK, 0, 7L, 0L, WILLBLOCK}, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 145 | |
| 146 | /* #10 Parent making a write lock on entire file */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 147 | {F_WRLCK, 0, 0L, 0L, SKIP, 0, 0L, 0L, |
| 148 | /* Child attempting a write lock from byte 7 to end of file */ |
| 149 | F_WRLCK, 0, 7L, 0L, WILLBLOCK}, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 150 | |
| 151 | /* #11 Parent making a read lock on entire file */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 152 | {F_RDLCK, 0, 0L, 0L, SKIP, 0, 0L, 0L, |
| 153 | /* Child attempting a read lock from byte 7 to end of file */ |
| 154 | F_RDLCK, 0, 7L, 0L, NOBLOCK}, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 155 | |
| 156 | /* #12 Parent making a read lock on entire file */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 157 | {F_RDLCK, 0, 0L, 0L, SKIP, 0, 0L, 0L, |
| 158 | /* Child attempting a write lock from byte 7 to end of file */ |
| 159 | F_WRLCK, 0, 7L, 0L, WILLBLOCK}, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 160 | |
| 161 | /* Test cases: entire boundary ( less than entire file) */ |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 162 | /* |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 163 | * #13 Parent making a write lock from beginning of |
| 164 | * file for 5 bytes |
| 165 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 166 | {F_WRLCK, 0, 0L, 5L, SKIP, 0, 0L, 0L, |
| 167 | /* |
| 168 | * Child attempting a read lock from beginning of |
| 169 | * file for 5 bytes |
| 170 | */ |
| 171 | F_RDLCK, 0, 0L, 5L, WILLBLOCK}, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 172 | |
| 173 | /* |
| 174 | * #14 Parent making a write lock from beginning of file |
| 175 | * for 5 bytes |
| 176 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 177 | {F_WRLCK, 0, 0L, 5L, SKIP, 0, 0L, 0L, |
| 178 | /* |
| 179 | * Child attempting a write lock from beginning of |
| 180 | * file for 5 bytes |
| 181 | */ |
| 182 | F_WRLCK, 0, 0L, 5L, WILLBLOCK}, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 183 | |
| 184 | /* |
| 185 | * #15 Parent making a read lock from beginning of |
| 186 | * file for 5 bytes |
| 187 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 188 | {F_RDLCK, 0, 0L, 5L, SKIP, 0, 0L, 0L, |
| 189 | /* |
| 190 | * Child attempting a read lock from beginning of |
| 191 | * file for 5 bytes |
| 192 | */ |
| 193 | F_RDLCK, 0, 0L, 5L, NOBLOCK}, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 194 | |
| 195 | /* |
| 196 | * #16 Parent making a read lock from beginning of |
| 197 | * file for 5 bytes |
| 198 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 199 | {F_RDLCK, 0, 0L, 5L, SKIP, 0, 0L, 0L, |
| 200 | /* |
| 201 | * Child attempting a write lock from beginning |
| 202 | * of file for 5 bytes |
| 203 | */ |
| 204 | F_WRLCK, 0, 0L, 5L, WILLBLOCK}, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 205 | |
| 206 | /* Test cases: inside boundary */ |
| 207 | /* |
| 208 | * #17 Parent making a write lock from beginning |
| 209 | * of file for 5 bytes |
| 210 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 211 | {F_WRLCK, 0, 0L, 5L, SKIP, 0, 0L, 0L, |
| 212 | /* Child attempting a read lock from byte 2 to byte 4 */ |
| 213 | F_RDLCK, 0, 1L, 3L, WILLBLOCK}, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 214 | |
| 215 | /* |
| 216 | * #18 Parent making a write lock from beginning of |
| 217 | * file for 5 bytes |
| 218 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 219 | {F_WRLCK, 0, 0L, 5L, SKIP, 0, 0L, 0L, |
| 220 | /* Child attempting a write lock from byte 2 to byte 4 */ |
| 221 | F_WRLCK, 0, 1L, 3L, WILLBLOCK}, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 222 | |
| 223 | /* |
| 224 | * #19 Parent making a read lock from beginning of |
| 225 | * file for 5 bytes |
| 226 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 227 | {F_RDLCK, 0, 0L, 5L, SKIP, 0, 0L, 0L, |
| 228 | /* Child attempting a read lock from byte 2 to byte 4 */ |
| 229 | F_RDLCK, 0, 1L, 3L, NOBLOCK}, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 230 | |
| 231 | /* |
| 232 | * #20 Parent making a read lock from beginning of |
| 233 | * file for 5 bytes |
| 234 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 235 | {F_RDLCK, 0, 0L, 5L, SKIP, 0, 0L, 0L, |
| 236 | /* Child attempting a write lock from byte 2 to byte 4 */ |
| 237 | F_WRLCK, 0, 1L, 3L, WILLBLOCK}, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 238 | |
| 239 | /* Test cases: cross boundary (inside to after) */ |
| 240 | /* |
| 241 | * #21 Parent making a write lock from beginning of |
| 242 | * file for 5 bytes |
| 243 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 244 | {F_WRLCK, 0, 0L, 5L, SKIP, 0, 0L, 0L, |
| 245 | /* Child attempting a read lock from byte 3 to byte 7 */ |
| 246 | F_RDLCK, 0, 2L, 5L, WILLBLOCK}, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 247 | |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 248 | /* |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 249 | * #22 Parent making a write lock from beginning |
| 250 | * of file for 5 bytes |
| 251 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 252 | {F_WRLCK, 0, 0L, 5L, SKIP, 0, 0L, 0L, |
| 253 | /* Child attempting a write lock from byte 3 to byte 7 */ |
| 254 | F_WRLCK, 0, 2L, 5L, WILLBLOCK}, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 255 | |
| 256 | /* |
| 257 | * #23 Parent making a read lock from beginning of |
| 258 | * file for 5 bytes |
| 259 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 260 | {F_RDLCK, 0, 0L, 5L, SKIP, 0, 0L, 0L, |
| 261 | /* Child attempting a read lock from byte 3 to byte 7 */ |
| 262 | F_RDLCK, 0, 2L, 5L, NOBLOCK}, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 263 | |
| 264 | /* |
| 265 | * #24 Parent making a read lock from beginning of |
| 266 | * file for 5 bytes |
| 267 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 268 | {F_RDLCK, 0, 0L, 5L, SKIP, 0, 0L, 0L, |
| 269 | /* Child attempting a write lock from byte 3 to byte 7 */ |
| 270 | F_WRLCK, 0, 2L, 5L, WILLBLOCK}, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 271 | |
| 272 | /* Test cases: outside boundary (after) */ |
| 273 | |
| 274 | /* |
| 275 | * #25 Parent making a write lock from beginning of |
| 276 | * file for 5 bytes |
| 277 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 278 | {F_WRLCK, 0, 0L, 5L, SKIP, 0, 0L, 0L, |
| 279 | /* Child attempting a read lock from byte 7 to end of file */ |
| 280 | F_RDLCK, 0, 6L, 0L, NOBLOCK}, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 281 | |
| 282 | /* |
| 283 | * #26 Parent making a write lock from beginning of |
| 284 | * file for 5 bytes |
| 285 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 286 | {F_WRLCK, 0, 0L, 5L, SKIP, 0, 0L, 0L, |
| 287 | /* Child attempting a write lock from byte 7 to end of file */ |
| 288 | F_WRLCK, 0, 6L, 0L, NOBLOCK}, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 289 | |
| 290 | /* |
| 291 | * #27 Parent making a read lock from beginning of |
| 292 | * file for 5 bytes |
| 293 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 294 | {F_RDLCK, 0, 0L, 5L, SKIP, 0, 0L, 0L, |
| 295 | /* Child attempting a read lock from byte 7 to end of file */ |
| 296 | F_RDLCK, 0, 6L, 0L, NOBLOCK}, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 297 | |
| 298 | /* |
| 299 | * #28 Parent making a read lock from beginning of |
| 300 | * file for 5 bytes |
| 301 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 302 | {F_RDLCK, 0, 0L, 5L, SKIP, 0, 0L, 0L, |
| 303 | /* Child attempting a write lock from byte 7 to end of file */ |
| 304 | F_WRLCK, 0, 6L, 0L, NOBLOCK}, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 305 | |
| 306 | /* Test cases: outside boundary (before) */ |
| 307 | |
| 308 | /* #29 Parent making a write lock from byte 3 to byte 7 */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 309 | {F_WRLCK, 0, 2L, 5L, SKIP, 0, 0L, 0L, |
| 310 | /* Child attempting a read lock from beginning of file to byte 2 */ |
| 311 | F_RDLCK, 0, 0L, 2L, NOBLOCK}, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 312 | |
| 313 | /* #30 Parent making a write lock from byte 3 to byte 7 */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 314 | {F_WRLCK, 0, 2L, 5L, SKIP, 0, 0L, 0L, |
| 315 | /* Child attempting a write lock from beginning of file to byte 2 */ |
| 316 | F_WRLCK, 0, 0L, 2L, NOBLOCK}, |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 317 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 318 | /* #31 Parent making a write lock from byte 3 to byte 7 */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 319 | {F_RDLCK, 0, 2L, 5L, SKIP, 0, 0L, 0L, |
| 320 | /* Child attempting a read lock from beginning of file to byte 2 */ |
| 321 | F_RDLCK, 0, 0L, 2L, NOBLOCK}, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 322 | |
| 323 | /* #32 Parent making a write lock from byte 3 to byte 7 */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 324 | {F_RDLCK, 0, 2L, 5L, SKIP, 0, 0L, 0L, |
| 325 | /* Child attempting a write lock from beginning of file to byte 2 */ |
| 326 | F_WRLCK, 0, 0L, 2L, NOBLOCK}, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 327 | |
| 328 | /* Test cases: cross boundary (before to inside) */ |
| 329 | /* #33 Parent making a write lock from byte 5 to end of file */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 330 | {F_WRLCK, 0, 4L, 0L, SKIP, 0, 0L, 0L, |
| 331 | /* Child attempting a read lock from byte 3 to byte 7 */ |
| 332 | F_RDLCK, 0, 2L, 5L, WILLBLOCK}, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 333 | |
| 334 | /* #34 Parent making a write lock from byte 5 to end of file */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 335 | {F_WRLCK, 0, 4L, 0L, SKIP, 0, 0L, 0L, |
| 336 | /* Child attempting a write lock from byte 3 to byte 7 */ |
| 337 | F_WRLCK, 0, 2L, 5L, WILLBLOCK}, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 338 | |
| 339 | /* #35 Parent making a read lock from byte 5 to end of file */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 340 | {F_RDLCK, 0, 4L, 0L, SKIP, 0, 0L, 0L, |
| 341 | /* Child attempting a read lock from byte 3 to byte 7 */ |
| 342 | F_RDLCK, 0, 2L, 5L, NOBLOCK}, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 343 | |
| 344 | /* #36 Parent making a read lock from byte 5 to end of file */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 345 | {F_RDLCK, 0, 4L, 0L, SKIP, 0, 0L, 0L, |
| 346 | /* Child attempting a write lock from byte 3 to byte 7 */ |
| 347 | F_WRLCK, 0, 2L, 5L, WILLBLOCK}, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 348 | |
| 349 | /* Start of negative L_start and L_len test cases */ |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 350 | /* |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 351 | * #37 Parent making write lock from byte 2 to byte 3 |
| 352 | * with L_start = -3 |
| 353 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 354 | {F_WRLCK, 1, -3L, 2L, SKIP, 0, 0L, 0L, |
| 355 | /* Child attempting write lock on byte 1 */ |
| 356 | F_WRLCK, 0, 1L, 1L, NOBLOCK}, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 357 | |
| 358 | /* |
| 359 | * #38 Parent making write lock from byte 2 to byte 3 |
| 360 | * with L_start = -3 |
| 361 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 362 | {F_WRLCK, 1, -3L, 2L, SKIP, 0, 0L, 0L, |
| 363 | /* Child attempting write lock on byte 4 */ |
| 364 | F_WRLCK, 0, 4L, 1L, NOBLOCK}, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 365 | |
| 366 | /* |
| 367 | * #39 Parent making write lock from byte 2 to byte 3 |
| 368 | * with L_start = -3 |
| 369 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 370 | {F_WRLCK, 1, -3L, 2L, SKIP, 0, 0L, 0L, |
| 371 | /* Child attempting write lock on byte 2 */ |
| 372 | F_WRLCK, 0, 2L, 1L, WILLBLOCK}, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 373 | |
| 374 | /* |
| 375 | * #40 Parent making write lock from byte 2 to byte 3 |
| 376 | * with L_start = -3 |
| 377 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 378 | {F_WRLCK, 1, -3L, 2L, SKIP, 0, 0L, 0L, |
| 379 | /* Child attempting write lock on byte 3 */ |
| 380 | F_WRLCK, 0, 3L, 1L, WILLBLOCK}, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 381 | |
| 382 | /* |
| 383 | * #41 Parent making write lock from byte 2 to byte 6 |
| 384 | * with L_start = -3 |
| 385 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 386 | {F_WRLCK, 1, -3L, 5L, SKIP, 0, 0L, 0L, |
| 387 | /* Child attempting write lock on byte 1 */ |
| 388 | F_WRLCK, 0, 1L, 1L, NOBLOCK}, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 389 | |
| 390 | /* |
| 391 | * #42 Parent making write lock from byte 2 to byte 6 |
| 392 | * with L_start = -3 |
| 393 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 394 | {F_WRLCK, 1, -3L, 5L, SKIP, 0, 0L, 0L, |
| 395 | /* Child attempting write lock on byte 7 */ |
| 396 | F_WRLCK, 0, 1L, 1L, NOBLOCK}, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 397 | |
| 398 | /* |
| 399 | * #43 Parent making write lock from byte 2 to byte 6 |
| 400 | * with L_start = -3 |
| 401 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 402 | {F_WRLCK, 1, -3L, 5L, SKIP, 0, 0L, 0L, |
| 403 | /* Child attempting write lock on byte 2 */ |
| 404 | F_WRLCK, 0, 2L, 1L, WILLBLOCK}, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 405 | |
| 406 | /* |
| 407 | * #44 Parent making write lock from byte 2 to byte 6 |
| 408 | * with L_start = -3 |
| 409 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 410 | {F_WRLCK, 1, -3L, 5L, SKIP, 0, 0L, 0L, |
| 411 | /* Child attempting write lock on byte 5 */ |
| 412 | F_WRLCK, 0, 5L, 1L, WILLBLOCK}, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 413 | |
| 414 | /* |
| 415 | * #45 Parent making write lock from byte 2 to byte 6 |
| 416 | * with L_start = -3 |
| 417 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 418 | {F_WRLCK, 1, -3L, 5L, SKIP, 0, 0L, 0L, |
| 419 | /* Child attempting write lock on byte 6 */ |
| 420 | F_WRLCK, 0, 6L, 1L, WILLBLOCK}, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 421 | |
| 422 | /* |
| 423 | * #46 Parent making write lock from byte 2 to byte 3 with |
| 424 | * L_start = -2 and L_len = -2 |
| 425 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 426 | {F_WRLCK, 1, 2L, -2L, SKIP, 0, 0L, 0L, |
| 427 | /* Child attempting write lock on byte 1 */ |
| 428 | F_WRLCK, 0, 1L, 1L, NOBLOCK}, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 429 | |
| 430 | /* |
| 431 | * #47 Parent making write lock from byte 2 to byte 3 with |
| 432 | * L_start = -2 and L_len = -2 |
| 433 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 434 | {F_WRLCK, 1, -2L, -2L, SKIP, 0, 0L, 0L, |
| 435 | /* Child attempting write lock on byte 4 */ |
| 436 | F_WRLCK, 0, 4L, 1L, NOBLOCK}, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 437 | |
| 438 | /* |
| 439 | * #48 Parent making write lock from byte 2 to byte 3 with |
| 440 | * L_start = -2 and L_len = -2 |
| 441 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 442 | {F_WRLCK, 1, -2L, -2L, SKIP, 0, 0L, 0L, |
| 443 | /* Child attempting write lock on byte 2 */ |
| 444 | F_WRLCK, 0, 2L, 1L, WILLBLOCK}, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 445 | |
| 446 | /* |
| 447 | * #49 Parent making write lock from byte 2 to byte 3 with |
| 448 | * L_start = -2 and L_len = -2 |
| 449 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 450 | {F_WRLCK, 1, -2L, -2L, SKIP, 0, 0L, 0L, |
| 451 | /* Child attempting write lock on byte 3 */ |
| 452 | F_WRLCK, 0, 3L, 1L, WILLBLOCK}, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 453 | |
| 454 | /* |
| 455 | * #50 Parent making write lock from byte 6 to byte 7 with |
| 456 | * L_start = 2 and L_len = -2 |
| 457 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 458 | {F_WRLCK, 1, 2L, -2L, SKIP, 0, 0L, 0L, |
| 459 | /* Child attempting write lock on byte 5 */ |
| 460 | F_WRLCK, 0, 5L, 1L, NOBLOCK}, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 461 | |
| 462 | /* |
| 463 | * #51 Parent making write lock from byte 6 to byte 7 with |
| 464 | * L_start = 2 and L_len = -2 |
| 465 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 466 | {F_WRLCK, 1, 2L, -2L, SKIP, 0, 0L, 0L, |
| 467 | /* Child attempting write lock on byte 8 */ |
| 468 | F_WRLCK, 0, 8L, 1L, NOBLOCK}, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 469 | |
| 470 | /* |
| 471 | * #52 Parent making write lock from byte 6 to byte 7 with |
| 472 | * L_start = 2 and L_len = -2 |
| 473 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 474 | {F_WRLCK, 1, 2L, -2L, SKIP, 0, 0L, 0L, |
| 475 | /* Child attempting write lock on byte 6 */ |
| 476 | F_WRLCK, 0, 6L, 1L, WILLBLOCK}, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 477 | |
| 478 | /* |
| 479 | * #53 Parent making write lock from byte 6 to byte 7 with |
| 480 | * L_start = 2 and L_len = -2 |
| 481 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 482 | {F_WRLCK, 1, 2L, -2L, SKIP, 0, 0L, 0L, |
| 483 | /* Child attempting write lock on byte 7 */ |
| 484 | F_WRLCK, 0, 7L, 1L, WILLBLOCK}, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 485 | |
| 486 | /* |
| 487 | * #54 Parent making write lock from byte 3 to byte 7 with |
| 488 | * L_start = 2 and L_len = -5 |
| 489 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 490 | {F_WRLCK, 1, 2L, -5L, SKIP, 0, 0L, 0L, |
| 491 | /* Child attempting write lock on byte 2 */ |
| 492 | F_WRLCK, 0, 2L, 1L, NOBLOCK}, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 493 | |
| 494 | /* |
| 495 | * #55 Parent making write lock from byte 3 to byte 7 with |
| 496 | * L_start = 2 and L_len = -5 |
| 497 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 498 | {F_WRLCK, 1, 2L, -5L, SKIP, 0, 0L, 0L, |
| 499 | /* Child attempting write lock on byte 8 */ |
| 500 | F_WRLCK, 0, 8L, 1L, NOBLOCK}, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 501 | |
| 502 | /* |
| 503 | * #56 Parent making write lock from byte 3 to byte 7 with |
| 504 | * L_start = 2 and L_len = -5 |
| 505 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 506 | {F_WRLCK, 1, 2L, -5L, SKIP, 0, 0L, 0L, |
| 507 | /* Child attempting write lock on byte 3 */ |
| 508 | F_WRLCK, 0, 3L, 1L, WILLBLOCK}, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 509 | |
| 510 | /* |
| 511 | * #57 Parent making write lock from byte 3 to byte 7 with |
| 512 | * L_start = 2 and L_len = -5 |
| 513 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 514 | {F_WRLCK, 1, 2L, -5L, SKIP, 0, 0L, 0L, |
| 515 | /* Child attempting write lock on byte 5 */ |
| 516 | F_WRLCK, 0, 5L, 1L, WILLBLOCK}, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 517 | |
| 518 | /* |
| 519 | * #58 Parent making write lock from byte 3 to byte 7 with |
| 520 | * L_start = 2 and L_len = -5 |
| 521 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 522 | {F_WRLCK, 1, 2L, -5L, SKIP, 0, 0L, 0L, |
| 523 | /* Child attempting write lock on byte 7 */ |
| 524 | F_WRLCK, 0, 7L, 1L, WILLBLOCK}, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 525 | |
| 526 | /* Test case for block 4 */ |
| 527 | /* #59 Parent making write lock on entire file */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 528 | {F_WRLCK, 0, 0L, 0L, SKIP, 0, 0L, 0L, |
| 529 | /* Child attempting write lock on byte 15 to end of file */ |
| 530 | F_WRLCK, 0, 15L, 0L, WILLBLOCK}, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 531 | }; |
| 532 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 533 | static testcase *thiscase; |
| 534 | static struct flock flock; |
| 535 | static int parent, child, status, fail = 0; |
| 536 | static int got1 = 0; |
| 537 | static int fd; |
| 538 | static int test; |
| 539 | static char tmpname[40]; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 540 | |
| 541 | #define FILEDATA "ten bytes!" |
| 542 | |
Mike Frysinger | e61ddba | 2014-04-09 23:24:32 -0400 | [diff] [blame] | 543 | void catch1(int sig); /* signal catching subroutine */ |
| 544 | void catch_alarm(int sig); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 545 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 546 | char *TCID = "fcntl14"; |
| 547 | int TST_TOTAL = 1; |
Xiong Zhou | 11930d7 | 2014-09-25 03:21:39 -0400 | [diff] [blame] | 548 | int NO_NFS = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 549 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 550 | #ifdef UCLINUX |
| 551 | static char *argv0; /* Set by main(), passed to self_exec() */ |
| 552 | #endif |
robbiew | 4cf8096 | 2003-03-25 21:53:41 +0000 | [diff] [blame] | 553 | |
| 554 | /* |
| 555 | * cleanup() |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 556 | * performs all the ONE TIME cleanup for this test at completion or |
| 557 | * premature exit |
robbiew | 4cf8096 | 2003-03-25 21:53:41 +0000 | [diff] [blame] | 558 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 559 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 560 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 561 | |
robbiew | 4cf8096 | 2003-03-25 21:53:41 +0000 | [diff] [blame] | 562 | tst_rmdir(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 563 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | /* |
| 567 | * setup |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 568 | * performs all ONE TIME setup for this test |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 569 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 570 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 571 | { |
robbiew | 1e4cf0c | 2005-02-24 17:03:42 +0000 | [diff] [blame] | 572 | struct sigaction act; |
| 573 | |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 574 | tst_sig(FORK, DEF_HANDLER, cleanup); |
subrata_modak | b370c3c | 2007-11-25 16:37:35 +0000 | [diff] [blame] | 575 | signal(SIGHUP, SIG_IGN); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 576 | umask(0); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 577 | TEST_PAUSE; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 578 | tst_tmpdir(); /* make temp dir and cd to it */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 579 | parent = getpid(); |
| 580 | |
| 581 | /* setup temporary file name */ |
| 582 | sprintf(tmpname, "fcntl2.%d", parent); |
| 583 | |
| 584 | /* setup signal handler for signal from child */ |
robbiew | 1e4cf0c | 2005-02-24 17:03:42 +0000 | [diff] [blame] | 585 | memset(&act, 0, sizeof(act)); |
| 586 | act.sa_handler = catch1; |
| 587 | sigemptyset(&act.sa_mask); |
| 588 | sigaddset(&act.sa_mask, SIGUSR1); |
| 589 | if ((sigaction(SIGUSR1, &act, NULL)) < 0) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 590 | tst_resm(TFAIL, "SIGUSR1 signal setup failed, errno = %d", |
| 591 | errno); |
| 592 | cleanup(); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 593 | } |
robbiew | 1e4cf0c | 2005-02-24 17:03:42 +0000 | [diff] [blame] | 594 | |
| 595 | memset(&act, 0, sizeof(act)); |
| 596 | act.sa_handler = catch_alarm; |
| 597 | sigemptyset(&act.sa_mask); |
| 598 | sigaddset(&act.sa_mask, SIGALRM); |
| 599 | if ((sigaction(SIGALRM, &act, NULL)) < 0) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 600 | tst_resm(TFAIL, "SIGALRM signal setup failed"); |
| 601 | cleanup(); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 602 | } |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | void wake_parent(void) |
| 606 | { |
| 607 | if ((kill(parent, SIGUSR1)) < 0) { |
| 608 | tst_resm(TFAIL, "Attempt to send signal to parent " "failed"); |
| 609 | tst_resm(TFAIL, "Test case %d, errno = %d", test + 1, errno); |
| 610 | fail = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 611 | } |
| 612 | } |
| 613 | |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 614 | void do_usleep_child(void) |
mridge | db63921 | 2005-01-04 21:04:11 +0000 | [diff] [blame] | 615 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 616 | usleep(100000); /* XXX how long is long enough? */ |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 617 | wake_parent(); |
| 618 | exit(0); |
| 619 | } |
| 620 | |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 621 | void dochild(void) |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 622 | { /* child process */ |
| 623 | int rc; |
| 624 | pid_t pid; |
mridge | db63921 | 2005-01-04 21:04:11 +0000 | [diff] [blame] | 625 | |
robbiew | 4cf8096 | 2003-03-25 21:53:41 +0000 | [diff] [blame] | 626 | /* Initialize the child lock structure */ |
| 627 | flock.l_type = thiscase->c_type; |
| 628 | flock.l_whence = thiscase->c_whence; |
| 629 | flock.l_start = thiscase->c_start; |
| 630 | flock.l_len = thiscase->c_len; |
| 631 | flock.l_pid = 0; |
| 632 | fail = 0; |
| 633 | |
| 634 | /* |
| 635 | * Check to see if child lock will succeed. If it will, FLOCK |
| 636 | * structure will return with l_type changed to F_UNLCK. If it will |
| 637 | * not, the parent pid will be returned in l_pid and the type of |
| 638 | * lock that will block it in l_type. |
| 639 | */ |
mridge | db63921 | 2005-01-04 21:04:11 +0000 | [diff] [blame] | 640 | if ((rc = fcntl(fd, F_GETLK, &flock)) < 0) { |
robbiew | 4cf8096 | 2003-03-25 21:53:41 +0000 | [diff] [blame] | 641 | tst_resm(TFAIL, "Attempt to check lock status failed"); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 642 | tst_resm(TFAIL, "Test case %d, errno = %d", test + 1, errno); |
robbiew | 4cf8096 | 2003-03-25 21:53:41 +0000 | [diff] [blame] | 643 | fail = 1; |
mridge | db63921 | 2005-01-04 21:04:11 +0000 | [diff] [blame] | 644 | } else { |
robbiew | 4cf8096 | 2003-03-25 21:53:41 +0000 | [diff] [blame] | 645 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 646 | if ((thiscase->c_flag) == NOBLOCK) { |
| 647 | if (flock.l_type != F_UNLCK) { |
| 648 | tst_resm(TFAIL, |
| 649 | "Test case %d, GETLK: type = %d, " |
| 650 | "%d was expected", test + 1, |
| 651 | flock.l_type, F_UNLCK); |
| 652 | fail = 1; |
| 653 | } |
robbiew | 4cf8096 | 2003-03-25 21:53:41 +0000 | [diff] [blame] | 654 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 655 | if (flock.l_whence != thiscase->c_whence) { |
| 656 | tst_resm(TFAIL, |
| 657 | "Test case %d, GETLK: whence = %d, " |
| 658 | "should have remained %d", test + 1, |
| 659 | flock.l_whence, thiscase->c_whence); |
| 660 | fail = 1; |
| 661 | } |
robbiew | 4cf8096 | 2003-03-25 21:53:41 +0000 | [diff] [blame] | 662 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 663 | if (flock.l_start != thiscase->c_start) { |
| 664 | tst_resm(TFAIL, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 665 | "Test case %d, GETLK: start = %" PRId64 |
| 666 | ", " "should have remained %" PRId64, |
| 667 | test + 1, (int64_t) flock.l_start, |
| 668 | (int64_t) thiscase->c_start); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 669 | fail = 1; |
| 670 | } |
robbiew | 4cf8096 | 2003-03-25 21:53:41 +0000 | [diff] [blame] | 671 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 672 | if (flock.l_len != thiscase->c_len) { |
| 673 | tst_resm(TFAIL, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 674 | "Test case %d, GETLK: len = %" PRId64 |
| 675 | ", " "should have remained %" PRId64, |
| 676 | test + 1, (int64_t) flock.l_len, |
| 677 | (int64_t) thiscase->c_len); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 678 | fail = 1; |
| 679 | } |
robbiew | 4cf8096 | 2003-03-25 21:53:41 +0000 | [diff] [blame] | 680 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 681 | if (flock.l_pid != 0) { |
| 682 | tst_resm(TFAIL, |
| 683 | "Test case %d, GETLK: pid = %d, " |
| 684 | "should have remained 0", test + 1, |
| 685 | flock.l_pid); |
| 686 | fail = 1; |
| 687 | } |
| 688 | } else { |
| 689 | if (flock.l_pid != parent) { |
| 690 | tst_resm(TFAIL, |
| 691 | "Test case %d, GETLK: pid = %d, " |
| 692 | "should be parent's id of %d", |
| 693 | test + 1, flock.l_pid, parent); |
| 694 | fail = 1; |
| 695 | } |
robbiew | 4cf8096 | 2003-03-25 21:53:41 +0000 | [diff] [blame] | 696 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 697 | if (flock.l_type != thiscase->a_type) { |
| 698 | tst_resm(TFAIL, |
| 699 | "Test case %d, GETLK: type = %d, " |
| 700 | "should be parent's first lock type of %d", |
| 701 | test + 1, flock.l_type, |
| 702 | thiscase->a_type); |
| 703 | fail = 1; |
| 704 | } |
robbiew | 4cf8096 | 2003-03-25 21:53:41 +0000 | [diff] [blame] | 705 | } |
| 706 | } |
| 707 | |
| 708 | /* |
| 709 | * now try to set the lock, nonblocking |
| 710 | * This will succeed for NOBLOCK, |
| 711 | * fail for WILLBLOCK |
| 712 | */ |
| 713 | flock.l_type = thiscase->c_type; |
| 714 | flock.l_whence = thiscase->c_whence; |
| 715 | flock.l_start = thiscase->c_start; |
| 716 | flock.l_len = thiscase->c_len; |
| 717 | flock.l_pid = 0; |
| 718 | |
mridge | db63921 | 2005-01-04 21:04:11 +0000 | [diff] [blame] | 719 | if ((rc = fcntl(fd, F_SETLK, &flock)) < 0) { |
robbiew | 4cf8096 | 2003-03-25 21:53:41 +0000 | [diff] [blame] | 720 | if ((thiscase->c_flag) == NOBLOCK) { |
| 721 | tst_resm(TFAIL, "Attempt to set child NONBLOCKING " |
| 722 | "lock failed"); |
| 723 | tst_resm(TFAIL, "Test case %d, errno = %d", |
| 724 | test + 1, errno); |
| 725 | fail = 1; |
| 726 | } |
| 727 | } |
| 728 | |
| 729 | if ((thiscase->c_flag) == WILLBLOCK) { |
| 730 | /* Check for proper errno condition */ |
mridge | db63921 | 2005-01-04 21:04:11 +0000 | [diff] [blame] | 731 | if (rc != -1 || (errno != EACCES && errno != EAGAIN)) { |
| 732 | tst_resm(TFAIL, |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 733 | "SETLK: rc = %d, errno = %d, -1/EAGAIN or EACCES " |
mridge | db63921 | 2005-01-04 21:04:11 +0000 | [diff] [blame] | 734 | "was expected", rc, errno); |
robbiew | 4cf8096 | 2003-03-25 21:53:41 +0000 | [diff] [blame] | 735 | fail = 1; |
| 736 | } |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 737 | if (rc == 0) { |
| 738 | /* accidentally got the lock */ |
| 739 | /* XXX how to clean up? */ |
| 740 | (void)fcntl(fd, F_UNLCK, &flock); |
| 741 | } |
robbiew | 4cf8096 | 2003-03-25 21:53:41 +0000 | [diff] [blame] | 742 | /* |
| 743 | * Lock should succeed after blocking and parent releases |
mridge | db63921 | 2005-01-04 21:04:11 +0000 | [diff] [blame] | 744 | * lock, tell the parent to release the locks. |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 745 | * Do the lock in this process, send the signal in a child |
| 746 | * process, so that the SETLKW actually uses the blocking |
| 747 | * mechanism in the kernel. |
| 748 | * |
| 749 | * XXX inherent race: we want to wait until the |
| 750 | * F_SETLKW has started, but we don't have a way to |
| 751 | * check that reliably in the child. (We'd |
| 752 | * need some way to have fcntl() atomically unblock a |
| 753 | * signal and wait for the lock.) |
robbiew | 4cf8096 | 2003-03-25 21:53:41 +0000 | [diff] [blame] | 754 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 755 | pid = FORK_OR_VFORK(); |
| 756 | switch (pid) { |
| 757 | case -1: |
| 758 | tst_resm(TFAIL, "Fork failed"); |
| 759 | break; |
| 760 | case 0: /* child */ |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 761 | #ifdef UCLINUX |
Garrett Cooper | 64bcf4a | 2010-12-21 12:00:28 -0800 | [diff] [blame] | 762 | if (self_exec(argv0, "nd", 1, parent) < 0) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 763 | tst_resm(TFAIL, "self_exec failed"); |
| 764 | break; |
| 765 | } |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 766 | #else |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 767 | do_usleep_child(); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 768 | #endif |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 769 | break; |
mridge | db63921 | 2005-01-04 21:04:11 +0000 | [diff] [blame] | 770 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 771 | default: |
| 772 | if ((rc = fcntl(fd, F_SETLKW, &flock)) < 0) { |
| 773 | tst_resm(TFAIL, "Attempt to set child BLOCKING " |
| 774 | "lock failed"); |
| 775 | tst_resm(TFAIL, "Test case %d, errno = %d", |
| 776 | test + 1, errno); |
| 777 | fail = 1; |
| 778 | } |
| 779 | waitpid(pid, &status, 0); |
| 780 | break; |
| 781 | } |
robbiew | 4cf8096 | 2003-03-25 21:53:41 +0000 | [diff] [blame] | 782 | } |
| 783 | if (fail) { |
| 784 | exit(1); |
| 785 | } else { |
| 786 | exit(0); |
| 787 | } |
| 788 | } |
| 789 | |
robbiew | 4cf8096 | 2003-03-25 21:53:41 +0000 | [diff] [blame] | 790 | void run_test(int file_flag, int file_mode, int seek, int start, int end) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 791 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 792 | extern long time(); |
| 793 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 794 | /* reset fail to 0 for each run_test call */ |
| 795 | fail = 0; |
robbiew | 4905842 | 2003-07-25 16:10:11 +0000 | [diff] [blame] | 796 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 797 | /* loop thru all test cases */ |
| 798 | for (test = start; test < end; test++) { |
| 799 | /* open a temp file to lock */ |
| 800 | fd = open(tmpname, file_flag, file_mode); |
| 801 | if (fd < 0) { |
| 802 | tst_brkm(TBROK, cleanup, "open() failed"); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 803 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 804 | |
| 805 | /* write some dummy data to the file */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 806 | if (write(fd, FILEDATA, 10) < 0) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 807 | tst_brkm(TBROK, cleanup, "write() failed"); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 808 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 809 | |
| 810 | /* seek into file if indicated */ |
| 811 | if (seek) { |
| 812 | if (lseek(fd, seek, 0) < 0) { |
| 813 | tst_brkm(TBROK, cleanup, "lseek() failed"); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 814 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 815 | } |
| 816 | |
| 817 | /* Initialize first parent lock structure */ |
| 818 | thiscase = &testcases[test]; |
| 819 | flock.l_type = thiscase->a_type; |
| 820 | flock.l_whence = thiscase->a_whence; |
| 821 | flock.l_start = thiscase->a_start; |
| 822 | flock.l_len = thiscase->a_len; |
| 823 | |
| 824 | /* set the initial parent lock on the file */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 825 | if ((fcntl(fd, F_SETLK, &flock)) < 0) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 826 | tst_resm(TFAIL, "First parent lock failed"); |
| 827 | tst_resm(TFAIL, "Test case %d, errno = %d", |
| 828 | test + 1, errno); |
| 829 | fail = 1; |
| 830 | } |
| 831 | |
| 832 | if ((thiscase->b_type) != SKIP) { |
| 833 | /* Initialize second parent lock structure */ |
| 834 | flock.l_type = thiscase->b_type; |
| 835 | flock.l_whence = thiscase->b_whence; |
| 836 | flock.l_start = thiscase->b_start; |
| 837 | flock.l_len = thiscase->b_len; |
| 838 | |
| 839 | /* set the second parent lock */ |
| 840 | if ((fcntl(fd, F_SETLK, &flock)) < 0) { |
| 841 | tst_resm(TFAIL, "Second parent lock failed"); |
| 842 | tst_resm(TFAIL, "Test case %d, errno = %d", |
| 843 | test + 1, errno); |
| 844 | fail = 1; |
| 845 | } |
| 846 | } |
| 847 | if ((thiscase->c_type) == SKIP) { |
| 848 | /* close the temp file and move to next test case */ |
| 849 | close(fd); |
| 850 | tst_resm(TINFO, "skipping test %d", test + 1); |
| 851 | continue; /* continue to the next case */ |
| 852 | } |
| 853 | /* |
| 854 | * Mask SIG_USR1 before forking child, to avoid race |
| 855 | */ |
| 856 | (void)sighold(SIGUSR1); |
| 857 | |
robbiew | 4905842 | 2003-07-25 16:10:11 +0000 | [diff] [blame] | 858 | /* flush the stdout to avoid garbled output */ |
| 859 | fflush(stdout); |
| 860 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 861 | /* spawn a child process */ |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 862 | if ((child = FORK_OR_VFORK()) == 0) { |
| 863 | #ifdef UCLINUX |
| 864 | if (self_exec(argv0, "nddddddddd", 2, thiscase->c_type, |
| 865 | thiscase->c_whence, thiscase->c_start, |
| 866 | thiscase->c_len, thiscase->c_flag, |
| 867 | thiscase->a_type, fd, test, parent) < 0) { |
| 868 | tst_resm(TFAIL, "self_exec failed"); |
| 869 | cleanup(); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 870 | } |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 871 | #else |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 872 | dochild(); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 873 | #endif |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 874 | } |
| 875 | if (child < 0) { |
| 876 | tst_resm(TFAIL, "Fork failed"); |
| 877 | cleanup(); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 878 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 879 | /* parent process */ |
| 880 | if ((thiscase->c_flag) == WILLBLOCK) { |
| 881 | /* |
| 882 | * Wait for a signal from the child then remove |
| 883 | * blocking lock. Set a 60 second alarm to break the |
| 884 | * pause just in case the child never signals us. |
| 885 | */ |
| 886 | alarm(TIME_OUT); |
| 887 | sigpause(SIGUSR1); |
| 888 | |
| 889 | /* turn off the alarm timer */ |
| 890 | alarm((unsigned)0); |
| 891 | if (got1 != 1) |
| 892 | tst_resm(TINFO, "Pause terminated without " |
| 893 | "signal SIGUSR1 from child"); |
| 894 | got1 = 0; /* reset the flag */ |
| 895 | |
| 896 | /* |
| 897 | * setup lock structure for parent to delete |
| 898 | * blocking lock then wait for child to exit |
| 899 | */ |
| 900 | flock.l_type = F_UNLCK; |
| 901 | flock.l_whence = 0; |
| 902 | flock.l_start = 0L; |
| 903 | flock.l_len = 0L; |
| 904 | if ((fcntl(fd, F_SETLK, &flock)) < 0) { |
| 905 | tst_resm(TFAIL, "Attempt to release parent " |
| 906 | "lock failed"); |
| 907 | tst_resm(TFAIL, "Test case %d, errno = %d", |
| 908 | test + 1, errno); |
| 909 | fail = 1; |
| 910 | } |
| 911 | } |
| 912 | /* |
| 913 | * set a 60 second alarm to break the wait just in case the |
| 914 | * child doesn't terminate on its own accord |
| 915 | */ |
| 916 | alarm(TIME_OUT); |
| 917 | |
| 918 | /* wait for the child to terminate and close the file */ |
mridge | db63921 | 2005-01-04 21:04:11 +0000 | [diff] [blame] | 919 | waitpid(child, &status, 0); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 920 | /* turn off the alarm clock */ |
| 921 | alarm((unsigned)0); |
| 922 | if (status != 0) { |
| 923 | tst_resm(TFAIL, "tchild returned status 0x%x", status); |
| 924 | fail = 1; |
| 925 | } |
| 926 | close(fd); |
| 927 | if (fail) { |
| 928 | tst_resm(TFAIL, "testcase:%d FAILED", test + 1); |
| 929 | } else { |
| 930 | tst_resm(TPASS, "testcase:%d PASSED", test + 1); |
| 931 | } |
| 932 | } |
| 933 | unlink(tmpname); |
| 934 | } |
| 935 | |
Mike Frysinger | e61ddba | 2014-04-09 23:24:32 -0400 | [diff] [blame] | 936 | void catch_alarm(int sig) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 937 | { |
| 938 | /* |
| 939 | * Timer has runout and child has not signaled, need |
| 940 | * to kill off the child as it appears it will not |
| 941 | * on its own accord. Check that it is still around |
| 942 | * as it may have terminated abnormally while parent |
| 943 | * was waiting for SIGUSR1 signal from it. |
| 944 | */ |
| 945 | if (kill(child, 0) == 0) { |
| 946 | kill(child, SIGKILL); |
| 947 | perror("The child didnot terminate on its own accord"); |
| 948 | } |
| 949 | } |
| 950 | |
Mike Frysinger | e61ddba | 2014-04-09 23:24:32 -0400 | [diff] [blame] | 951 | void catch1(int sig) |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 952 | { /* invoked on catching SIGUSR1 */ |
robbiew | 1e4cf0c | 2005-02-24 17:03:42 +0000 | [diff] [blame] | 953 | struct sigaction act; |
| 954 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 955 | /* |
| 956 | * Set flag to let parent know that child is ready to have lock |
| 957 | * removed |
| 958 | */ |
robbiew | 1e4cf0c | 2005-02-24 17:03:42 +0000 | [diff] [blame] | 959 | memset(&act, 0, sizeof(act)); |
| 960 | act.sa_handler = catch1; |
| 961 | sigemptyset(&act.sa_mask); |
| 962 | sigaddset(&act.sa_mask, SIGUSR1); |
| 963 | sigaction(SIGUSR1, &act, NULL); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 964 | got1++; |
| 965 | } |
| 966 | |
Xiong Zhou | 8218050 | 2014-09-25 03:17:57 -0400 | [diff] [blame] | 967 | static void testcheck_end(int check_fail, char *msg) |
| 968 | { |
| 969 | if (check_fail) { |
| 970 | tst_resm(TFAIL, "%s FAILED", msg); |
| 971 | } else { |
| 972 | tst_resm(TPASS, "%s PASSED", msg); |
| 973 | } |
| 974 | } |
| 975 | |
robbiew | 4cf8096 | 2003-03-25 21:53:41 +0000 | [diff] [blame] | 976 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 977 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 978 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 979 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 980 | |
Garrett Cooper | 45e285d | 2010-11-22 12:19:25 -0800 | [diff] [blame] | 981 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) { |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 982 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
robbiew | 4cf8096 | 2003-03-25 21:53:41 +0000 | [diff] [blame] | 983 | } |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 984 | #ifdef UCLINUX |
| 985 | argv0 = av[0]; |
| 986 | |
Garrett Cooper | 64bcf4a | 2010-12-21 12:00:28 -0800 | [diff] [blame] | 987 | maybe_run_child(&do_usleep_child, "nd", 1, &parent); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 988 | thiscase = malloc(sizeof(testcase)); |
| 989 | |
| 990 | maybe_run_child(&dochild, "nddddddddd", 2, &thiscase->c_type, |
| 991 | &thiscase->c_whence, &thiscase->c_start, |
| 992 | &thiscase->c_len, &thiscase->c_flag, &thiscase->a_type, |
| 993 | &fd, &test, &parent); |
| 994 | #endif |
| 995 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 996 | setup(); /* global setup */ |
robbiew | 4cf8096 | 2003-03-25 21:53:41 +0000 | [diff] [blame] | 997 | |
Xiong Zhou | 11930d7 | 2014-09-25 03:21:39 -0400 | [diff] [blame] | 998 | /* Check if test on NFS or not*/ |
| 999 | if (tst_fs_type(cleanup, ".") == TST_NFS_MAGIC) |
| 1000 | NO_NFS = 0; |
subrata_modak | 5387ba0 | 2008-05-05 08:49:34 +0000 | [diff] [blame] | 1001 | |
robbiew | 4cf8096 | 2003-03-25 21:53:41 +0000 | [diff] [blame] | 1002 | /* Check for looping state if -i option is given */ |
| 1003 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 1004 | /* reset tst_count in case we are looping */ |
| 1005 | tst_count = 0; |
robbiew | 4cf8096 | 2003-03-25 21:53:41 +0000 | [diff] [blame] | 1006 | |
mridge | db63921 | 2005-01-04 21:04:11 +0000 | [diff] [blame] | 1007 | /* //block1: */ |
robbiew | 4cf8096 | 2003-03-25 21:53:41 +0000 | [diff] [blame] | 1008 | tst_resm(TINFO, "Enter block 1: without mandatory locking"); |
| 1009 | fail = 0; |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 1010 | /* |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 1011 | * try various file locks on an ordinary file without |
robbiew | 4cf8096 | 2003-03-25 21:53:41 +0000 | [diff] [blame] | 1012 | * mandatory locking |
| 1013 | */ |
| 1014 | (void)run_test(O_CREAT | O_RDWR | O_TRUNC, 0777, 0, 0, 36); |
Xiong Zhou | 8218050 | 2014-09-25 03:17:57 -0400 | [diff] [blame] | 1015 | testcheck_end(fail, "Block 1, test 1"); |
robbiew | 4cf8096 | 2003-03-25 21:53:41 +0000 | [diff] [blame] | 1016 | |
| 1017 | /* Now try with negative values for L_start and L_len */ |
| 1018 | (void)run_test(O_CREAT | O_RDWR | O_TRUNC, 0777, 5, 36, 45); |
Xiong Zhou | 8218050 | 2014-09-25 03:17:57 -0400 | [diff] [blame] | 1019 | testcheck_end(fail, "Block 1, test 2"); |
robbiew | 4cf8096 | 2003-03-25 21:53:41 +0000 | [diff] [blame] | 1020 | |
robbiew | 4cf8096 | 2003-03-25 21:53:41 +0000 | [diff] [blame] | 1021 | tst_resm(TINFO, "Exit block 1"); |
| 1022 | |
mridge | db63921 | 2005-01-04 21:04:11 +0000 | [diff] [blame] | 1023 | /* //block2: */ |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 1024 | /* |
Xiong Zhou | 11930d7 | 2014-09-25 03:21:39 -0400 | [diff] [blame] | 1025 | * Skip block2 if test on NFS, since NFS does not support |
| 1026 | * mandatory locking |
robbiew | 4cf8096 | 2003-03-25 21:53:41 +0000 | [diff] [blame] | 1027 | */ |
Xiong Zhou | 11930d7 | 2014-09-25 03:21:39 -0400 | [diff] [blame] | 1028 | tst_resm(TINFO, "Enter block 2: with mandatory locking"); |
| 1029 | if (NO_NFS) { |
| 1030 | fail = 0; |
| 1031 | /* |
| 1032 | * Try various locks on a file with mandatory |
| 1033 | * record locking this should behave the same |
| 1034 | * as an ordinary file |
| 1035 | */ |
| 1036 | (void)run_test(O_CREAT | O_RDWR | O_TRUNC, |
| 1037 | S_ENFMT | S_IRUSR | S_IWUSR, 0, 0, 36); |
| 1038 | testcheck_end(fail, "Block 2, test 1"); |
robbiew | 4cf8096 | 2003-03-25 21:53:41 +0000 | [diff] [blame] | 1039 | |
Xiong Zhou | 11930d7 | 2014-09-25 03:21:39 -0400 | [diff] [blame] | 1040 | /* Now try negative values for L_start and L_len */ |
| 1041 | (void)run_test(O_CREAT | O_RDWR | O_TRUNC, |
| 1042 | S_ENFMT | S_IRUSR | S_IWUSR, 5, 36, 45); |
| 1043 | testcheck_end(fail, "Block 2, test 2"); |
| 1044 | } else |
| 1045 | tst_resm(TCONF, "Skip block 2 as NFS does not" |
| 1046 | " support mandatory locking"); |
robbiew | 4cf8096 | 2003-03-25 21:53:41 +0000 | [diff] [blame] | 1047 | tst_resm(TINFO, "Exit block 2"); |
| 1048 | |
mridge | db63921 | 2005-01-04 21:04:11 +0000 | [diff] [blame] | 1049 | /* //block3: */ |
robbiew | 4cf8096 | 2003-03-25 21:53:41 +0000 | [diff] [blame] | 1050 | tst_resm(TINFO, "Enter block 3"); |
| 1051 | fail = 0; |
| 1052 | /* |
| 1053 | * Check that proper error status is returned when invalid |
| 1054 | * argument used for WHENCE (negative value) |
| 1055 | */ |
| 1056 | |
| 1057 | /* open a temporary file to lock */ |
| 1058 | fd = open(tmpname, O_CREAT | O_RDWR | O_TRUNC, 0777); |
| 1059 | if (fd < 0) { |
| 1060 | tst_brkm(TBROK, cleanup, "open failed"); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1061 | } |
robbiew | 4cf8096 | 2003-03-25 21:53:41 +0000 | [diff] [blame] | 1062 | |
| 1063 | /* Write some dummy data to the file */ |
| 1064 | if (write(fd, FILEDATA, 10) < 0) { |
| 1065 | tst_brkm(TBROK, cleanup, "write failed"); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1066 | } |
robbiew | 4cf8096 | 2003-03-25 21:53:41 +0000 | [diff] [blame] | 1067 | |
| 1068 | /* Initialize lock structure */ |
| 1069 | flock.l_type = F_WRLCK; |
| 1070 | flock.l_whence = -1; |
| 1071 | flock.l_start = 0L; |
| 1072 | flock.l_len = 0L; |
| 1073 | |
| 1074 | /* Set the lock on the file */ |
| 1075 | if ((fcntl(fd, F_SETLK, &flock)) < 0) { |
| 1076 | if (errno != EINVAL) { |
| 1077 | tst_resm(TFAIL, "Expected %d got %d", |
| 1078 | EINVAL, errno); |
| 1079 | fail = 1; |
| 1080 | } |
| 1081 | } else { |
| 1082 | tst_resm(TFAIL, "Lock succeeded when it should have " |
| 1083 | "failed"); |
| 1084 | fail = 1; |
| 1085 | } |
| 1086 | |
| 1087 | /* Close and remove temp file */ |
| 1088 | close(fd); |
| 1089 | unlink(tmpname); |
| 1090 | |
Xiong Zhou | 8218050 | 2014-09-25 03:17:57 -0400 | [diff] [blame] | 1091 | testcheck_end(fail, "Test with negative whence locking"); |
robbiew | 4cf8096 | 2003-03-25 21:53:41 +0000 | [diff] [blame] | 1092 | tst_resm(TINFO, "Exit block 3"); |
| 1093 | |
mridge | db63921 | 2005-01-04 21:04:11 +0000 | [diff] [blame] | 1094 | /* //block4: */ |
robbiew | 4cf8096 | 2003-03-25 21:53:41 +0000 | [diff] [blame] | 1095 | tst_resm(TINFO, "Enter block 4"); |
| 1096 | fail = 0; |
| 1097 | /* |
| 1098 | * Check that a lock on end of file is still valid when |
| 1099 | * additional data is appended to end of file and a new |
| 1100 | * process attempts to lock new data |
| 1101 | */ |
| 1102 | /* open a temp file to lock */ |
| 1103 | fd = open(tmpname, O_CREAT | O_RDWR | O_TRUNC, 0777); |
| 1104 | if (fd < 0) { |
| 1105 | tst_brkm(TBROK, cleanup, "open failed"); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1106 | } |
robbiew | 4cf8096 | 2003-03-25 21:53:41 +0000 | [diff] [blame] | 1107 | |
| 1108 | /* Write some dummy data to the file */ |
| 1109 | if (write(fd, FILEDATA, 10) < 0) { |
| 1110 | tst_brkm(TBROK, cleanup, "write failed"); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1111 | } |
robbiew | 4cf8096 | 2003-03-25 21:53:41 +0000 | [diff] [blame] | 1112 | |
| 1113 | /* Initialize first parent lock structure */ |
| 1114 | thiscase = &testcases[58]; |
| 1115 | flock.l_type = thiscase->a_type; |
| 1116 | flock.l_whence = thiscase->a_whence; |
| 1117 | flock.l_start = thiscase->a_start; |
| 1118 | flock.l_len = thiscase->a_len; |
| 1119 | |
| 1120 | /* Set the initial parent lock on the file */ |
| 1121 | if ((fcntl(fd, F_SETLK, &flock)) < 0) { |
| 1122 | tst_resm(TFAIL, "First parent lock failed"); |
| 1123 | tst_resm(TFAIL, "Test case %d, errno = %d", 58, errno); |
| 1124 | fail = 1; |
| 1125 | } |
| 1126 | |
| 1127 | /* Write some additional data to end of file */ |
| 1128 | if (write(fd, FILEDATA, 10) < 0) { |
| 1129 | tst_brkm(TBROK, cleanup, "write failed"); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1130 | } |
robbiew | 4cf8096 | 2003-03-25 21:53:41 +0000 | [diff] [blame] | 1131 | |
| 1132 | /* Mask signal to avoid race */ |
| 1133 | if (sighold(SIGUSR1) < 0) { |
| 1134 | tst_brkm(TBROK, cleanup, "sighold failed"); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1135 | } |
robbiew | 4cf8096 | 2003-03-25 21:53:41 +0000 | [diff] [blame] | 1136 | |
| 1137 | /* spawn a child process */ |
subrata_modak | b370c3c | 2007-11-25 16:37:35 +0000 | [diff] [blame] | 1138 | if ((child = FORK_OR_VFORK()) == 0) { |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 1139 | #ifdef UCLINUX |
| 1140 | if (self_exec(argv0, "nddddddddd", 2, thiscase->c_type, |
| 1141 | thiscase->c_whence, thiscase->c_start, |
| 1142 | thiscase->c_len, thiscase->c_flag, |
| 1143 | thiscase->a_type, fd, test, parent) < 0) { |
| 1144 | tst_resm(TFAIL, "self_exec failed"); |
| 1145 | cleanup(); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1146 | } |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 1147 | #else |
robbiew | 4cf8096 | 2003-03-25 21:53:41 +0000 | [diff] [blame] | 1148 | dochild(); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 1149 | #endif |
robbiew | 4cf8096 | 2003-03-25 21:53:41 +0000 | [diff] [blame] | 1150 | } |
| 1151 | if (child < 0) { |
| 1152 | tst_resm(TFAIL, "Fork failed"); |
| 1153 | cleanup(); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 1154 | } |
robbiew | 4cf8096 | 2003-03-25 21:53:41 +0000 | [diff] [blame] | 1155 | |
| 1156 | /* parent process */ |
| 1157 | |
| 1158 | /* |
| 1159 | * Wait for a signal from the child then remove blocking lock. |
| 1160 | * Set a 60 sec alarm to break the pause just in case the |
| 1161 | * child doesn't terminate on its own accord |
| 1162 | */ |
| 1163 | (void)alarm(TIME_OUT); |
| 1164 | |
| 1165 | /* pause for the SIGUSR1 signal from child */ |
| 1166 | (void)sigpause(SIGUSR1); |
| 1167 | |
| 1168 | /* turn off the alarm timer */ |
| 1169 | (void)alarm((unsigned)0); |
| 1170 | if (got1 != 1) { |
| 1171 | tst_resm(TINFO, "Pause terminated without signal " |
| 1172 | "SIGUSR1 from child"); |
| 1173 | } |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 1174 | got1 = 0; /* reset flag */ |
robbiew | 4cf8096 | 2003-03-25 21:53:41 +0000 | [diff] [blame] | 1175 | |
| 1176 | /* |
| 1177 | * Set up lock structure for parent to delete |
| 1178 | * blocking lock then wait for child to exit |
| 1179 | */ |
| 1180 | flock.l_type = F_UNLCK; |
| 1181 | flock.l_whence = 0; |
| 1182 | flock.l_start = 0L; |
| 1183 | flock.l_len = 0L; |
| 1184 | if ((fcntl(fd, F_SETLK, &flock)) < 0) { |
| 1185 | tst_resm(TFAIL, "Attempt to release parent lock " |
| 1186 | "failed"); |
| 1187 | tst_resm(TFAIL, "Test case %d, errno = %d", test + 1, |
| 1188 | errno); |
| 1189 | fail = 1; |
| 1190 | } |
| 1191 | |
| 1192 | /* |
| 1193 | * set a 60 sec alarm to break the wait just in case the |
| 1194 | * child doesn't terminate on its own accord |
| 1195 | */ |
| 1196 | (void)alarm(TIME_OUT); |
| 1197 | |
| 1198 | /* wait for the child to terminate and close the file */ |
| 1199 | waitpid(child, &status, 0); |
| 1200 | if (WEXITSTATUS(status) != 0) { |
| 1201 | fail = 1; |
| 1202 | tst_resm(TFAIL, "child returned bad exit status"); |
| 1203 | } |
| 1204 | |
| 1205 | /* turn off the alarm clock */ |
| 1206 | (void)alarm((unsigned)0); |
| 1207 | if (status != 0) { |
| 1208 | tst_resm(TFAIL, "child returned status 0x%x", status); |
| 1209 | fail = 1; |
| 1210 | } |
| 1211 | close(fd); |
| 1212 | unlink(tmpname); |
| 1213 | |
Xiong Zhou | 8218050 | 2014-09-25 03:17:57 -0400 | [diff] [blame] | 1214 | testcheck_end(fail, "Test of locks on file"); |
robbiew | 4cf8096 | 2003-03-25 21:53:41 +0000 | [diff] [blame] | 1215 | tst_resm(TINFO, "Exit block 4"); |
| 1216 | } |
| 1217 | cleanup(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 1218 | tst_exit(); |
Garrett Cooper | 64bcf4a | 2010-12-21 12:00:28 -0800 | [diff] [blame] | 1219 | } |