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 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
47 #define MAX_PATH_LENGTH 4096
51 int t_fcntl(int fd, int cmd, ...)
64 fprintf(stderr, "fcntl GETFL failed: %s\n",
70 arg = va_arg(ap, long);
72 rc = fcntl(fd, cmd, arg);
74 fprintf(stderr, "fcntl SETFL %ld failed: %s\n",
75 arg, strerror(errno));
82 lock = va_arg(ap, struct flock *);
84 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);
96 fprintf(stderr, "fcntl F_DUPFD %d failed: %s\n",
97 (int)arg, strerror(errno));
103 fprintf(stderr, "fcntl cmd %d not supported\n", cmd);
109 int t_unlink(const char *path)
115 fprintf(stderr, "unlink(%s) error: %s\n", path, strerror(errno));
119 /** =================================================================
126 fprintf(stderr, "usage: ./flocks_test 1 on|off -c|-f|-l /path/to/file\n");
129 int t1(int argc, char *argv[])
132 int mount_with_flock = 0;
140 if (!strncmp(argv[2], "on", 3)) {
141 mount_with_flock = 1;
142 } else if (!strncmp(argv[2], "off", 4)) {
143 mount_with_flock = 0;
149 if ((fd = open(argv[4], O_RDWR)) < 0) {
150 fprintf(stderr, "Couldn't open file: %s\n", argv[3]);
154 if (!strncmp(argv[3], "-c", 3)) {
158 fl.l_whence = SEEK_SET;
162 error = fcntl(fd, F_SETLK, &fl);
163 } else if (!strncmp(argv[3], "-l", 3)) {
164 error = lockf(fd, F_LOCK, 1);
165 } else if (!strncmp(argv[3], "-f", 3)) {
166 error = flock(fd, LOCK_EX);
172 if (mount_with_flock)
173 return((error == 0) ? EXIT_SUCCESS : EXIT_FAILURE);
175 return((error == 0) ? EXIT_FAILURE : EXIT_SUCCESS);
178 /** ===============================================================
181 * 2 threads flock ops interweave
188 void* t2_thread1(void *arg)
190 struct flock *lock = ((th_data *)arg)->lock;
191 int fd = ((th_data *)arg)->fd;
193 printf("thread 1: set write lock (blocking)\n");
194 lock->l_type = F_WRLCK;
195 t_fcntl(fd, F_SETLKW, lock);
196 printf("thread 1: set write lock done\n");
197 t_fcntl(fd, F_GETLK, lock);
198 printf("thread 1: unlock\n");
199 lock->l_type = F_UNLCK;
200 t_fcntl(fd, F_SETLK, lock);
201 printf("thread 1: unlock done\n");
205 void* t2_thread2(void *arg)
207 struct flock *lock = ((th_data *)arg)->lock;
208 int fd = ((th_data *)arg)->fd;
211 printf("thread 2: unlock\n");
212 lock->l_type = F_UNLCK;
213 t_fcntl(fd, F_SETLK, lock);
214 printf("thread 2: unlock done\n");
215 printf("thread 2: set write lock (non-blocking)\n");
216 lock->l_type = F_WRLCK;
217 t_fcntl(fd, F_SETLK, lock);
218 printf("thread 2: set write lock done\n");
219 t_fcntl(fd, F_GETLK, lock);
223 int t2(int argc, char* argv[])
225 struct flock lock = {
227 .l_whence = SEEK_SET,
229 char file[MAX_PATH_LENGTH] = "";
234 snprintf(file, MAX_PATH_LENGTH, "%s/test_t2_file", argv[2]);
236 fd = open(file, O_RDWR|O_CREAT, (mode_t)0666);
238 fprintf(stderr, "error open file: %s\n", file);
242 t_fcntl(fd, F_SETFL, O_APPEND);
243 rc = t_fcntl(fd, F_GETFL);
244 if ((rc & O_APPEND) == 0) {
245 fprintf(stderr, "error get flag: ret %x\n", rc);
251 rc = pthread_create(&th1, NULL, t2_thread1, &ta);
253 fprintf(stderr, "error create thread 1\n");
257 rc = pthread_create(&th2, NULL, t2_thread2, &ta);
259 fprintf(stderr, "error create thread 2\n");
263 (void)pthread_join(th1, NULL);
264 (void)pthread_join(th2, NULL);
271 /** ==============================================================
276 fprintf(stderr, "usage: ./flocks_test test# [corresponding arguments]\n");
279 int main(int argc, char* argv[])
282 int rc = EXIT_SUCCESS;
288 test_no = atoi(argv[1]);
298 fprintf(stderr, "unknow test number %s\n", argv[1]);