4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2014, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
44 #define MAX_PATH_LENGTH 4096
48 int t_fcntl(int fd, int cmd, ...)
62 fprintf(stderr, "fcntl GETFL failed: %s\n",
68 arg = va_arg(ap, long);
70 rc = fcntl(fd, cmd, arg);
73 fprintf(stderr, "fcntl SETFL %ld failed: %s\n",
74 arg, strerror(errno));
81 lock = va_arg(ap, struct flock *);
83 rc = fcntl(fd, cmd, lock);
86 fprintf(stderr, "fcntl cmd %d failed: %s\n",
87 cmd, strerror(errno));
92 arg = va_arg(ap, long);
94 rc = fcntl(fd, cmd, arg);
97 fprintf(stderr, "fcntl F_DUPFD %d failed: %s\n",
98 (int)arg, strerror(errno));
104 fprintf(stderr, "fcntl cmd %d not supported\n", cmd);
110 int t_unlink(const char *path)
116 fprintf(stderr, "unlink(%s) error: %s\n", path, strerror(errno));
120 /** =================================================================
127 fprintf(stderr, "usage: ./flocks_test 1 on|off -c|-f|-l /path/to/file\n");
130 int t1(int argc, char *argv[])
133 int mount_with_flock = 0;
142 if (!strncmp(argv[2], "on", 3)) {
143 mount_with_flock = 1;
144 } else if (!strncmp(argv[2], "off", 4)) {
145 mount_with_flock = 0;
151 if ((fd = open(argv[4], O_RDWR)) < 0) {
152 fprintf(stderr, "Couldn't open file: %s\n", argv[4]);
156 if (!strncmp(argv[3], "-c", 3)) {
160 fl.l_whence = SEEK_SET;
164 error = fcntl(fd, F_SETLK, &fl);
165 } else if (!strncmp(argv[3], "-l", 3)) {
166 error = lockf(fd, F_LOCK, 1);
167 } else if (!strncmp(argv[3], "-f", 3)) {
168 error = flock(fd, LOCK_EX);
175 if (mount_with_flock)
176 rc = ((error == 0) ? EXIT_SUCCESS : EXIT_FAILURE);
178 rc = ((error == 0) ? EXIT_FAILURE : EXIT_SUCCESS);
186 /** ===============================================================
189 * 2 threads flock ops interweave
196 void* t2_thread1(void *arg)
198 struct flock *lock = ((th_data *)arg)->lock;
199 int fd = ((th_data *)arg)->fd;
201 printf("thread 1: set write lock (blocking)\n");
202 lock->l_type = F_WRLCK;
203 t_fcntl(fd, F_SETLKW, lock);
204 printf("thread 1: set write lock done\n");
205 t_fcntl(fd, F_GETLK, lock);
206 printf("thread 1: unlock\n");
207 lock->l_type = F_UNLCK;
208 t_fcntl(fd, F_SETLK, lock);
209 printf("thread 1: unlock done\n");
213 void* t2_thread2(void *arg)
215 struct flock *lock = ((th_data *)arg)->lock;
216 int fd = ((th_data *)arg)->fd;
219 printf("thread 2: unlock\n");
220 lock->l_type = F_UNLCK;
221 t_fcntl(fd, F_SETLK, lock);
222 printf("thread 2: unlock done\n");
223 printf("thread 2: set write lock (non-blocking)\n");
224 lock->l_type = F_WRLCK;
225 t_fcntl(fd, F_SETLK, lock);
226 printf("thread 2: set write lock done\n");
227 t_fcntl(fd, F_GETLK, lock);
231 int t2(int argc, char* argv[])
233 struct flock lock = {
235 .l_whence = SEEK_SET,
237 char file[MAX_PATH_LENGTH] = "";
242 snprintf(file, MAX_PATH_LENGTH, "%s/test_t2_file", argv[2]);
244 fd = open(file, O_RDWR|O_CREAT, (mode_t)0666);
246 fprintf(stderr, "error open file: %s\n", file);
250 t_fcntl(fd, F_SETFL, O_APPEND);
251 rc = t_fcntl(fd, F_GETFL);
252 if ((rc < 0) || (rc & O_APPEND) == 0) {
253 fprintf(stderr, "error get flag: ret %x\n", rc);
260 rc = pthread_create(&th1, NULL, t2_thread1, &ta);
262 fprintf(stderr, "error create thread 1\n");
266 rc = pthread_create(&th2, NULL, t2_thread2, &ta);
268 fprintf(stderr, "error create thread 2\n");
272 (void)pthread_join(th1, NULL);
273 (void)pthread_join(th2, NULL);
280 /** =================================================================
283 * Bug 24040: Two conflicting flocks from same process different fds should fail
284 * two conflicting flocks from different processes but same fs
287 int t3(int argc, char *argv[])
291 int rc = EXIT_SUCCESS;
294 fprintf(stderr, "Usage: ./flocks_test 3 filename\n");
298 if ((fd = open(argv[2], O_RDWR)) < 0) {
299 fprintf(stderr, "Couldn't open file: %s\n", argv[2]);
302 if (flock(fd, LOCK_EX | LOCK_NB) < 0) {
303 perror("first flock failed");
307 if ((fd2 = open(argv[2], O_RDWR)) < 0) {
308 fprintf(stderr, "Couldn't open file: %s\n", argv[2]);
312 if (flock(fd2, LOCK_EX | LOCK_NB) >= 0) {
313 fprintf(stderr, "Second flock succeeded - FAIL\n");
329 if ((fd2 = open(argv[2], O_RDWR)) < 0) {
330 fprintf(stderr, "Couldn't open file: %s\n", argv[1]);
334 if (flock(fd2, LOCK_EX | LOCK_NB) >= 0) {
335 fprintf(stderr, "Second flock succeeded - FAIL\n");
339 if (flock(fd, LOCK_UN) == -1) {
340 fprintf(stderr, "Child unlock on parent fd failed\n");
344 if (flock(fd2, LOCK_EX | LOCK_NB) == -1) {
345 fprintf(stderr, "Relock after parent unlock failed!\n");
354 waitpid(pid, &rc, 0);
360 int t4(int argc, char *argv[])
362 struct flock lock = {
364 .l_whence = SEEK_SET,
372 int rc = EXIT_SUCCESS;
375 fprintf(stderr, "Usage: ./flocks_test 4 file1 file2\n");
379 if ((fd = open(argv[2], O_RDWR)) < 0) {
380 fprintf(stderr, "Couldn't open file: %s\n", argv[2]);
383 if ((fd2 = open(argv[3], O_RDWR)) < 0) {
384 fprintf(stderr, "Couldn't open file: %s\n", argv[3]);
396 if (child_pid == 0) {
397 printf("%d: get lock1\n", getpid());
399 if (t_fcntl(fd, F_SETLKW, &lock) < 0) {
400 fprintf(stderr, "%d: cannot get lock1: %s\n",
401 getpid(), strerror(errno));
405 printf("%d: done\n", getpid());
407 printf("%d: get lock2\n", getpid());
409 if (t_fcntl(fd2, F_SETLKW, &lock) < 0) {
410 fprintf(stderr, "%d: cannot get lock2: %s\n",
411 getpid(), strerror(errno));
413 if (errno == EDEADLK)
420 printf("%d: done\n", getpid());
422 printf("%d: exit rc=%d\n", getpid(), rc);
425 printf("%d: get lock2\n", getpid());
427 if (t_fcntl(fd2, F_SETLKW, &lock) < 0) {
428 fprintf(stderr, "%d: cannot get lock2: %s\n",
429 getpid(), strerror(errno));
433 printf("%d: done\n", getpid());
435 printf("%d: get lock1\n", getpid());
437 if (t_fcntl(fd, F_SETLKW, &lock) < 0) {
438 fprintf(stderr, "%d: cannot get lock1: %s\n",
439 getpid(), strerror(errno));
441 if (errno != EDEADLK) {
446 printf("%d: done\n", getpid());
452 fprintf(stderr, "%d: error closing file1: %s\n",
453 getpid(), strerror(errno));
457 if (close(fd2) < 0) {
458 fprintf(stderr, "%d: error closing file2: %s\n",
459 getpid(), strerror(errno));
463 if (waitpid(child_pid, &child_status, 0) < 0) {
464 fprintf(stderr, "%d: cannot get termination status of %d: %s\n",
465 getpid(), child_pid, strerror(errno));
467 } else if (!WIFEXITED(child_status)) {
468 fprintf(stderr, "%d: child %d terminated with status %d\n",
469 getpid(), child_pid, child_status);
472 rc = WEXITSTATUS(child_status);
476 printf("%d: exit rc=%d\n", getpid(), rc);
481 "Usage: ./flocks_test 5 set|get|unlock [read|write] [sleep N] file1\n"\
482 " set: F_SETLKW F_WRLCK\n" \
483 " get: F_GETLK F_WRLCK (conflict)\n" \
484 " unlock: F_SETLKW F_UNLCK\n" \
485 " read|write: lock mode, write by default\n" \
486 " sleep N: sleep for N secs after fcntl\n" \
487 " file1: fcntl is called for this file\n"
489 int t5(int argc, char *argv[])
491 struct flock lock = {
493 .l_whence = SEEK_SET,
496 int setlk = 0, getlk = 0, unlk = 0, secs = 0;
501 if (argc < 4 || argc > 7) {
502 fprintf(stderr, T5_USAGE);
506 if (!strncmp(argv[2], "set", 4))
508 else if (!strncmp(argv[2], "get", 4))
510 else if (!strncmp(argv[2], "unlock", 7))
513 fprintf(stderr, "Wrong 2nd argument: %s\n", argv[2]);
519 if (!strncmp(argv[pos], "read", 5)) {
520 lock.l_type = F_RDLCK;
522 } else if (!strncmp(argv[pos], "write", 6)) {
523 lock.l_type = F_WRLCK;
527 if (!strncmp(argv[pos], "sleep", 6)) {
528 secs = atoi(argv[pos + 1]);
529 if (secs < 0 || secs > 10) {
530 fprintf(stderr, "Sleep argument is wrong: %s\n",
537 fd = open(argv[pos], O_RDWR);
539 fprintf(stderr, "Couldn't open file: %s\n", argv[pos]);
543 fprintf(stderr, "\nFLOCKS_TEST 5: %s %s flock\n",
544 setlk ? "SET" : getlk ? "GET" : "UNLOCK",
545 lock.l_type == F_WRLCK ? "write" : "read");
548 rc = t_fcntl(fd, F_SETLKW, &lock);
550 rc = t_fcntl(fd, F_GETLK, &lock);
552 lock.l_type = F_UNLCK;
553 rc = t_fcntl(fd, F_SETLKW, &lock);
560 return rc < 0 ? -rc : 0;
564 /** ==============================================================
569 fprintf(stderr, "usage: ./flocks_test test# [corresponding arguments]\n");
572 int main(int argc, char* argv[])
575 int rc = EXIT_SUCCESS;
581 test_no = atoi(argv[1]);
600 fprintf(stderr, "unknow test number %s\n", argv[1]);