1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
32 * Copyright (c) 2011, Whamcloud, Inc.
35 * This file is part of Lustre, http://www.lustre.org/
36 * Lustre is a trademark of Sun Microsystems, Inc.
50 #define MAX_PATH_LENGTH 4096
54 int t_fcntl(int fd, int cmd, ...)
67 fprintf(stderr, "fcntl GETFL failed: %s\n",
73 arg = va_arg(ap, long);
75 rc = fcntl(fd, cmd, arg);
77 fprintf(stderr, "fcntl SETFL %ld failed: %s\n",
78 arg, strerror(errno));
85 lock = va_arg(ap, struct flock *);
87 rc = fcntl(fd, cmd, lock);
89 fprintf(stderr, "fcntl cmd %d failed: %s\n",
90 cmd, strerror(errno));
95 arg = va_arg(ap, long);
97 rc = fcntl(fd, cmd, arg);
99 fprintf(stderr, "fcntl F_DUPFD %d failed: %s\n",
100 (int)arg, strerror(errno));
106 fprintf(stderr, "fcntl cmd %d not supported\n", cmd);
112 int t_unlink(const char *path)
118 fprintf(stderr, "unlink(%s) error: %s\n", path, strerror(errno));
122 /** =================================================================
129 fprintf(stderr, "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;
143 if (!strncmp(argv[2], "on", 3)) {
144 mount_with_flock = 1;
145 } else if (!strncmp(argv[2], "off", 4)) {
146 mount_with_flock = 0;
152 if ((fd = open(argv[4], O_RDWR)) < 0) {
153 fprintf(stderr, "Couldn't open file: %s\n", argv[3]);
157 if (!strncmp(argv[3], "-c", 3)) {
161 fl.l_whence = SEEK_SET;
165 error = fcntl(fd, F_SETLK, &fl);
166 } else if (!strncmp(argv[3], "-l", 3)) {
167 error = lockf(fd, F_LOCK, 1);
168 } else if (!strncmp(argv[3], "-f", 3)) {
169 error = flock(fd, LOCK_EX);
175 if (mount_with_flock)
176 return((error == 0) ? EXIT_SUCCESS : EXIT_FAILURE);
178 return((error == 0) ? EXIT_FAILURE : EXIT_SUCCESS);
181 /** ===============================================================
184 * 2 threads flock ops interweave
191 void* t2_thread1(void *arg)
193 struct flock *lock = ((th_data *)arg)->lock;
194 int fd = ((th_data *)arg)->fd;
196 printf("thread 1: set write lock (blocking)\n");
197 lock->l_type = F_WRLCK;
198 t_fcntl(fd, F_SETLKW, lock);
199 printf("thread 1: set write lock done\n");
200 t_fcntl(fd, F_GETLK, lock);
201 printf("thread 1: unlock\n");
202 lock->l_type = F_UNLCK;
203 t_fcntl(fd, F_SETLK, lock);
204 printf("thread 1: unlock done\n");
208 void* t2_thread2(void *arg)
210 struct flock *lock = ((th_data *)arg)->lock;
211 int fd = ((th_data *)arg)->fd;
214 printf("thread 2: unlock\n");
215 lock->l_type = F_UNLCK;
216 t_fcntl(fd, F_SETLK, lock);
217 printf("thread 2: unlock done\n");
218 printf("thread 2: set write lock (non-blocking)\n");
219 lock->l_type = F_WRLCK;
220 t_fcntl(fd, F_SETLK, lock);
221 printf("thread 2: set write lock done\n");
222 t_fcntl(fd, F_GETLK, lock);
226 int t2(int argc, char* argv[])
228 struct flock lock = {
230 .l_whence = SEEK_SET,
232 char file[MAX_PATH_LENGTH] = "";
237 snprintf(file, MAX_PATH_LENGTH, "%s/test_t2_file", argv[2]);
239 fd = open(file, O_RDWR|O_CREAT, (mode_t)0666);
241 fprintf(stderr, "error open file: %s\n", file);
245 t_fcntl(fd, F_SETFL, O_APPEND);
246 rc = t_fcntl(fd, F_GETFL);
247 if ((rc & O_APPEND) == 0) {
248 fprintf(stderr, "error get flag: ret %x\n", rc);
254 rc = pthread_create(&th1, NULL, t2_thread1, &ta);
256 fprintf(stderr, "error create thread 1\n");
260 rc = pthread_create(&th2, NULL, t2_thread2, &ta);
262 fprintf(stderr, "error create thread 2\n");
266 (void)pthread_join(th1, NULL);
267 (void)pthread_join(th2, NULL);
274 /** =================================================================
277 * Bug 24040: Two conflicting flocks from same process different fds should fail
278 * two conflicting flocks from different processes but same fs
281 int t3(int argc, char *argv[])
285 int rc = EXIT_SUCCESS;
288 fprintf(stderr, "Usage: ./flocks_test 3 filename\n");
292 if ((fd = open(argv[2], O_RDWR)) < 0) {
293 fprintf(stderr, "Couldn't open file: %s\n", argv[1]);
296 if (flock(fd, LOCK_EX | LOCK_NB) < 0) {
297 perror("first flock failed");
301 if ((fd2 = open(argv[2], O_RDWR)) < 0) {
302 fprintf(stderr, "Couldn't open file: %s\n", argv[1]);
306 if (flock(fd2, LOCK_EX | LOCK_NB) >= 0) {
307 fprintf(stderr, "Second flock succeeded - FAIL\n");
323 if ((fd2 = open(argv[2], O_RDWR)) < 0) {
324 fprintf(stderr, "Couldn't open file: %s\n", argv[1]);
328 if (flock(fd2, LOCK_EX | LOCK_NB) >= 0) {
329 fprintf(stderr, "Second flock succeeded - FAIL\n");
333 if (flock(fd, LOCK_UN) == -1) {
334 fprintf(stderr, "Child unlock on parent fd failed\n");
338 if (flock(fd2, LOCK_EX | LOCK_NB) == -1) {
339 fprintf(stderr, "Relock after parent unlock failed!\n");
348 waitpid(pid, &rc, 0);
355 /** ==============================================================
360 fprintf(stderr, "usage: ./flocks_test test# [corresponding arguments]\n");
363 int main(int argc, char* argv[])
366 int rc = EXIT_SUCCESS;
372 test_no = atoi(argv[1]);
385 fprintf(stderr, "unknow test number %s\n", argv[1]);