Whamcloud - gitweb
Mass conversion of all copyright messages to Oracle.
[fs/lustre-release.git] / lustre / tests / flocks_test.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
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.
11  *
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).
17  *
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
21  *
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
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  */
36
37 #include <errno.h>
38 #include <fcntl.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <unistd.h>
43 #include <pthread.h>
44 #include <sys/file.h>
45 #include <stdarg.h>
46
47 #define MAX_PATH_LENGTH 4096
48 /**
49  * helper functions
50  */
51 int t_fcntl(int fd, int cmd, ...)
52 {
53         va_list ap;
54         long arg;
55         struct flock *lock;
56         int rc = -1;
57
58         va_start(ap, cmd);
59         switch (cmd) {
60         case F_GETFL:
61                 va_end(ap);
62                 rc = fcntl(fd, cmd);
63                 if (rc == -1) {
64                         fprintf(stderr, "fcntl GETFL failed: %s\n",
65                                 strerror(errno));
66                         return(1);
67                 }
68                 break;
69         case F_SETFL:
70                 arg = va_arg(ap, long);
71                 va_end(ap);
72                 rc = fcntl(fd, cmd, arg);
73                 if (rc == -1) {
74                         fprintf(stderr, "fcntl SETFL %ld failed: %s\n",
75                                 arg, strerror(errno));
76                         return(1);
77                 }
78                 break;
79         case F_GETLK:
80         case F_SETLK:
81         case F_SETLKW:
82                 lock = va_arg(ap, struct flock *);
83                 va_end(ap);
84                 rc = fcntl(fd, cmd, lock);
85                 if (rc == -1) {
86                         fprintf(stderr, "fcntl cmd %d failed: %s\n",
87                                 cmd, strerror(errno));
88                         return(1);
89                 }
90                 break;
91         case F_DUPFD:
92                 arg = va_arg(ap, long);
93                 va_end(ap);
94                 rc = fcntl(fd, cmd, arg);
95                 if (rc == -1) {
96                         fprintf(stderr, "fcntl F_DUPFD %d failed: %s\n",
97                                 (int)arg, strerror(errno));
98                         return(1);
99                 }
100                 break;
101         default:
102                 va_end(ap);
103                 fprintf(stderr, "fcntl cmd %d not supported\n", cmd);
104                 return(1);
105         }
106         return rc;
107 }
108
109 int t_unlink(const char *path)
110 {
111         int rc;
112
113         rc = unlink(path);
114         if (rc)
115                 fprintf(stderr, "unlink(%s) error: %s\n", path, strerror(errno));
116         return rc;
117 }
118
119 /** =================================================================
120  * test number 1
121  * 
122  * normal flock test
123  */
124 void t1_usage(void)
125 {
126         fprintf(stderr, "usage: ./flocks_test 1 on|off -c|-f|-l /path/to/file\n");
127 }
128
129 int t1(int argc, char *argv[])
130 {
131         int fd;
132         int mount_with_flock = 0;
133         int error = 0;
134
135         if (argc != 5) {
136                 t1_usage();
137                 return EXIT_FAILURE;
138         }
139
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;
144         } else {
145                 t1_usage();
146                 return EXIT_FAILURE;
147         }
148
149         if ((fd = open(argv[4], O_RDWR)) < 0) {
150                 fprintf(stderr, "Couldn't open file: %s\n", argv[3]);
151                 return EXIT_FAILURE;
152         }
153
154         if (!strncmp(argv[3], "-c", 3)) {
155                 struct flock fl;
156
157                 fl.l_type = F_RDLCK;
158                 fl.l_whence = SEEK_SET;
159                 fl.l_start = 0;
160                 fl.l_len = 1;
161
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);
167         } else {
168                 t1_usage();
169                 return EXIT_FAILURE;
170         }
171
172         if (mount_with_flock)
173                 return((error == 0) ? EXIT_SUCCESS : EXIT_FAILURE);
174         else
175                 return((error == 0) ? EXIT_FAILURE : EXIT_SUCCESS);
176 }
177
178 /** ===============================================================
179  * test number 2
180  * 
181  * 2 threads flock ops interweave
182  */
183 typedef struct {
184         struct flock* lock;
185         int fd;
186 } th_data;
187
188 void* t2_thread1(void *arg)
189 {
190         struct flock *lock = ((th_data *)arg)->lock;
191         int fd             = ((th_data *)arg)->fd;
192
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");
202         return 0;
203 }
204
205 void* t2_thread2(void *arg)
206 {
207         struct flock *lock = ((th_data *)arg)->lock;
208         int fd             = ((th_data *)arg)->fd;
209
210         sleep(2);
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);
220         return 0;
221 }
222
223 int t2(int argc, char* argv[])
224 {
225         struct flock lock = {
226                 .l_type = F_RDLCK,
227                 .l_whence = SEEK_SET,
228         };
229         char file[MAX_PATH_LENGTH] = "";
230         int  fd, rc;
231         pthread_t th1, th2;
232         th_data   ta;
233
234         snprintf(file, MAX_PATH_LENGTH, "%s/test_t2_file", argv[2]);
235
236         fd = open(file, O_RDWR|O_CREAT, (mode_t)0666);
237         if (fd < 0) {
238                 fprintf(stderr, "error open file: %s\n", file);
239                 return EXIT_FAILURE;
240         }
241
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);
246                 return EXIT_FAILURE;
247         }
248
249         ta.lock = &lock;
250         ta.fd   = fd;
251         rc = pthread_create(&th1, NULL, t2_thread1, &ta);
252         if (rc) {
253                 fprintf(stderr, "error create thread 1\n");
254                 rc = EXIT_FAILURE;
255                 goto out;
256         }
257         rc = pthread_create(&th2, NULL, t2_thread2, &ta);
258         if (rc) {
259                 fprintf(stderr, "error create thread 2\n");
260                 rc = EXIT_FAILURE;
261                 goto out;
262         }
263         (void)pthread_join(th1, NULL);
264         (void)pthread_join(th2, NULL);
265 out:
266         t_unlink(file);
267         close(fd);
268         return rc;
269 }
270
271 /** ==============================================================
272  * program entry
273  */
274 void usage(void)
275 {
276         fprintf(stderr, "usage: ./flocks_test test# [corresponding arguments]\n");
277 }
278
279 int main(int argc, char* argv[])
280 {
281         int test_no;
282         int rc = EXIT_SUCCESS;
283
284         if (argc < 1) {
285                 usage();
286                 exit(EXIT_FAILURE);
287         }
288         test_no = atoi(argv[1]);
289
290         switch(test_no) {
291         case 1:
292                 rc = t1(argc, argv);
293                 break;
294         case 2:
295                 rc = t2(argc, argv);
296                 break;
297         default:
298                 fprintf(stderr, "unknow test number %s\n", argv[1]);
299                 break;
300         }
301         return rc;
302 }