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)
117 "unlink(%s) error: %s\n", path, strerror(errno));
121 /** =================================================================
129 "usage: flocks_test 1 {on|off} {-c|-f|-l} /path/to/file\n");
132 int t1(int argc, char *argv[])
135 int mount_with_flock = 0;
144 if (!strncmp(argv[2], "on", 3)) {
145 mount_with_flock = 1;
146 } else if (!strncmp(argv[2], "off", 4)) {
147 mount_with_flock = 0;
153 fd = open(argv[4], O_RDWR);
155 fprintf(stderr, "Couldn't open file '%s': %s\n", argv[4],
160 if (!strncmp(argv[3], "-c", 3)) {
164 fl.l_whence = SEEK_SET;
168 error = fcntl(fd, F_SETLK, &fl);
169 } else if (!strncmp(argv[3], "-l", 3)) {
170 error = lockf(fd, F_LOCK, 1);
171 } else if (!strncmp(argv[3], "-f", 3)) {
172 error = flock(fd, LOCK_EX);
179 if (mount_with_flock)
180 rc = ((error == 0) ? EXIT_SUCCESS : EXIT_FAILURE);
182 rc = ((error == 0) ? EXIT_FAILURE : EXIT_SUCCESS);
190 /** ===============================================================
193 * 2 threads flock ops interweave
201 void *t2_thread1(void *arg)
203 struct thread_info *ti = arg;
204 struct flock *lock = ti->lock;
207 printf("thread 1: set write lock (blocking): rc = %d\n", ti->rc);
208 lock->l_type = F_WRLCK;
209 t_fcntl(fd, F_SETLKW, lock);
210 printf("thread 1: set write lock done: rc = %d\n", ti->rc);
211 (void)t_fcntl(fd, F_GETLK, lock); /* ignore this, operation will fail */
212 printf("thread 1: unlock: rc = %d\n", ti->rc);
213 lock->l_type = F_UNLCK;
214 ti->rc += t_fcntl(fd, F_SETLK, lock);
215 printf("thread 1: unlock done: rc = %d\n", ti->rc);
218 fprintf(stdout, "thread1 exiting with rc = %d\n", ti->rc);
222 void *t2_thread2(void *arg)
224 struct thread_info *ti = arg;
225 struct flock *lock = ti->lock;
229 printf("thread 2: unlock: rc = %d\n", ti->rc);
230 lock->l_type = F_UNLCK;
231 ti->rc += t_fcntl(fd, F_SETLK, lock);
232 printf("thread 2: unlock done: rc = %d\n", ti->rc);
233 printf("thread 2: set write lock (non-blocking): rc = %d\n", ti->rc);
234 lock->l_type = F_WRLCK;
235 ti->rc += t_fcntl(fd, F_SETLK, lock);
236 printf("thread 2: set write lock done: rc = %d\n", ti->rc);
237 (void)t_fcntl(fd, F_GETLK, lock); /* ignore this, operation will fail */
240 fprintf(stdout, "thread2 exiting with rc = %d\n", ti->rc);
244 int t2(int argc, char *argv[])
246 struct flock lock = {
248 .l_whence = SEEK_SET,
250 char file[MAX_PATH_LENGTH] = "";
253 struct thread_info ti;
255 snprintf(file, MAX_PATH_LENGTH, "%s/test_t2_file", argv[2]);
257 fd = open(file, O_RDWR|O_CREAT, (mode_t)0666);
259 fprintf(stderr, "error open file '%s': %s\n", file,
264 t_fcntl(fd, F_SETFL, O_APPEND);
265 rc = t_fcntl(fd, F_GETFL);
266 if ((rc < 0) || (rc & O_APPEND) == 0) {
267 fprintf(stderr, "error get flag: ret %x\n", rc);
275 rc = pthread_create(&th1, NULL, t2_thread1, &ti);
277 fprintf(stderr, "error create thread 1\n");
281 rc = pthread_create(&th2, NULL, t2_thread2, &ti);
283 fprintf(stderr, "error create thread 2\n");
287 pthread_join(th1, NULL);
288 pthread_join(th2, NULL);
298 /** =================================================================
301 * Bug 24040: Two conflicting flocks from same process different fds should fail
302 * two conflicting flocks from different processes but same fs
305 int t3(int argc, char *argv[])
309 int rc = EXIT_SUCCESS;
312 fprintf(stderr, "usage: flocks_test 3 filename\n");
316 fd = open(argv[2], O_RDWR);
318 fprintf(stderr, "Couldn't open file '%s': %s\n", argv[2],
322 if (flock(fd, LOCK_EX | LOCK_NB) < 0) {
323 perror("first flock failed");
327 fd2 = open(argv[2], O_RDWR);
329 fprintf(stderr, "Couldn't open file '%s': %s\n", argv[2],
334 if (flock(fd2, LOCK_EX | LOCK_NB) >= 0) {
335 fprintf(stderr, "Second flock succeeded - FAIL\n");
351 fd2 = open(argv[2], O_RDWR);
353 fprintf(stderr, "Couldn't open file '%s': %s\n",
354 argv[1], strerror(errno));
358 if (flock(fd2, LOCK_EX | LOCK_NB) >= 0) {
359 fprintf(stderr, "Second flock succeeded - FAIL\n");
363 if (flock(fd, LOCK_UN) == -1) {
364 fprintf(stderr, "Child unlock on parent fd failed\n");
368 if (flock(fd2, LOCK_EX | LOCK_NB) == -1) {
369 fprintf(stderr, "Relock after parent unlock failed!\n");
378 waitpid(pid, &rc, 0);
384 int t4(int argc, char *argv[])
386 struct flock lock = {
388 .l_whence = SEEK_SET,
396 int rc = EXIT_SUCCESS;
399 fprintf(stderr, "usage: flocks_test 4 file1 file2\n");
403 fd = open(argv[2], O_RDWR);
405 fprintf(stderr, "Couldn't open file: %s\n", argv[2]);
408 fd2 = open(argv[3], O_RDWR);
410 fprintf(stderr, "Couldn't open file: %s\n", argv[3]);
422 if (child_pid == 0) {
423 printf("%d: get lock1\n", getpid());
425 if (t_fcntl(fd, F_SETLKW, &lock) < 0) {
426 fprintf(stderr, "%d: cannot get lock1: %s\n",
427 getpid(), strerror(errno));
431 printf("%d: done\n", getpid());
433 printf("%d: get lock2\n", getpid());
435 if (t_fcntl(fd2, F_SETLKW, &lock) < 0) {
436 fprintf(stderr, "%d: cannot get lock2: %s\n",
437 getpid(), strerror(errno));
439 if (errno == EDEADLK)
446 printf("%d: done\n", getpid());
448 printf("%d: exit rc=%d\n", getpid(), rc);
451 printf("%d: get lock2\n", getpid());
453 if (t_fcntl(fd2, F_SETLKW, &lock) < 0) {
454 fprintf(stderr, "%d: cannot get lock2: %s\n",
455 getpid(), strerror(errno));
459 printf("%d: done\n", getpid());
461 printf("%d: get lock1\n", getpid());
463 if (t_fcntl(fd, F_SETLKW, &lock) < 0) {
464 fprintf(stderr, "%d: cannot get lock1: %s\n",
465 getpid(), strerror(errno));
467 if (errno != EDEADLK) {
472 printf("%d: done\n", getpid());
478 fprintf(stderr, "%d: error closing file1: %s\n",
479 getpid(), strerror(errno));
483 if (close(fd2) < 0) {
484 fprintf(stderr, "%d: error closing file2: %s\n",
485 getpid(), strerror(errno));
489 if (waitpid(child_pid, &child_status, 0) < 0) {
490 fprintf(stderr, "%d: cannot get termination status of %d: %s\n",
491 getpid(), child_pid, strerror(errno));
493 } else if (!WIFEXITED(child_status)) {
494 fprintf(stderr, "%d: child %d terminated with status %d\n",
495 getpid(), child_pid, child_status);
498 rc = WEXITSTATUS(child_status);
502 printf("%d: exit rc=%d\n", getpid(), rc);
507 "usage: flocks_test 5 {set|get|unlock} [read|write] [sleep N] file1\n" \
508 " set: F_SETLKW F_WRLCK\n" \
509 " get: F_GETLK F_WRLCK (conflict)\n" \
510 " unlock: F_SETLKW F_UNLCK\n" \
511 " read|write: lock mode, write by default\n" \
512 " sleep N: sleep for N secs after fcntl\n" \
513 " file1: fcntl is called for this file\n"
515 int t5(int argc, char *argv[])
517 struct flock lock = {
519 .l_whence = SEEK_SET,
522 int setlk = 0, getlk = 0, unlk = 0, secs = 0;
527 if (argc < 4 || argc > 7) {
528 fprintf(stderr, T5_USAGE);
532 if (!strncmp(argv[2], "set", 4))
534 else if (!strncmp(argv[2], "get", 4))
536 else if (!strncmp(argv[2], "unlock", 7))
539 fprintf(stderr, "Wrong 2nd argument: %s\n", argv[2]);
545 if (!strncmp(argv[pos], "read", 5)) {
546 lock.l_type = F_RDLCK;
548 } else if (!strncmp(argv[pos], "write", 6)) {
549 lock.l_type = F_WRLCK;
553 if (!strncmp(argv[pos], "sleep", 6)) {
554 secs = atoi(argv[pos + 1]);
555 if (secs < 0 || secs > 10) {
556 fprintf(stderr, "Sleep argument is wrong: %s\n",
563 fd = open(argv[pos], O_RDWR);
565 fprintf(stderr, "Couldn't open file: %s\n", argv[pos]);
569 fprintf(stderr, "\nFLOCKS_TEST 5: %s %s flock\n",
570 setlk ? "SET" : getlk ? "GET" : "UNLOCK",
571 lock.l_type == F_WRLCK ? "write" : "read");
574 rc = t_fcntl(fd, F_SETLKW, &lock);
576 rc = t_fcntl(fd, F_GETLK, &lock);
578 lock.l_type = F_UNLCK;
579 rc = t_fcntl(fd, F_SETLKW, &lock);
586 return rc < 0 ? -rc : 0;
590 /** ==============================================================
596 "usage: flocks_test test# [corresponding arguments]\n");
599 int main(int argc, char *argv[])
601 int rc = EXIT_SUCCESS;
608 switch (atoi(argv[1])) {
625 fprintf(stderr, "unknown test number '%s'\n", argv[1]);
630 fprintf(stderr, "exiting with rc = %d\n", rc);