Whamcloud - gitweb
corrected an error made in setepall in test-framework.sh, which affects
[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 #include <errno.h>
5 #include <fcntl.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <unistd.h>
10
11 #include <sys/file.h>
12
13 void usage(void)
14 {
15         fprintf(stderr, "usage: ./flocks_test on|off -c|-f|-l /path/to/file\n");
16         exit(EXIT_FAILURE);
17 }
18
19 int main(int argc, char *argv[])
20 {
21         int fd;
22         int mount_with_flock = 0;
23         int error = 0;
24
25         if (argc != 4)
26                 usage();
27         
28         if (!strncmp(argv[1], "on", 3)) {
29                 mount_with_flock = 1;
30         } else if (!strncmp(argv[1], "off", 4)) {
31                 mount_with_flock = 0;
32         } else {
33                 usage();
34         }
35
36         if ((fd = open(argv[3], O_RDWR)) < 0) {
37                 fprintf(stderr, "Couldn't open file: %s\n", argv[2]);
38                 exit(EXIT_FAILURE);
39         }
40
41         if (!strncmp(argv[2], "-c", 3)) {
42                 struct flock fl;
43
44                 fl.l_type = F_RDLCK;
45                 fl.l_whence = SEEK_SET;
46                 fl.l_start = 0;
47                 fl.l_len = 1;
48
49                 error = fcntl(fd, F_SETLK, &fl);
50         } else if (!strncmp(argv[2], "-l", 3)) {
51                 error = lockf(fd, F_LOCK, 1);
52         } else if (!strncmp(argv[2], "-f", 3)) {
53                 error = flock(fd, LOCK_EX);
54         } else {
55                 usage();
56         }
57
58         if (mount_with_flock)
59                 return((error == 0) ? EXIT_SUCCESS : EXIT_FAILURE);
60         else
61                 return((error == 0) ? EXIT_FAILURE : EXIT_SUCCESS);
62 }