1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
2 /*
3 * (C) 2004 by Argonne National Laboratory.
4 * See COPYRIGHT in top-level directory.
5 */
6 #include <stdio.h>
7 #include <fcntl.h>
8 #include <errno.h>
9 #include <unistd.h>
10
11 /*
12 * This program tests to see if fcntl returns success when asked to
13 * establish a file lock. This test is intended for use on file systems
14 * such as NFS that may not implement file locks. ROMIO makes use
15 * of file locks to implement certain operations, and may not work
16 * properly if file locks are not available.
17 *
18 * This is a simple test and has at least two limitations:
19 *
20 * 1. Some implementations of NFS are known to return success for
21 * setting a file lock when in fact no lock has been set. This
22 * test will not detect such erroneous implementations of NFS
23 *
24 * 2. Some implementations will hang (enter and wait indefinitately)
25 * within the fcntl call. This program will also hang in that case.
26 * Under normal conditions, this program should only take a few seconds to
27 * run.
28 *
29 * The program prints a message showing the success or failure of
30 * setting the file lock and sets the return status to 0 on success and
31 * non-zero on failure. If there is a failure, the system routine
32 * perror is also called to explain the reason.
33 */
34
35 /* style: allow:printf:2 sig:0 */
36
37 int main( int argc, char *argv[] )
38 {
39 struct flock lock;
40 int fd, err;
41 char *filename;
42
43 /* Set the filename. Either arg[1] or conftest.dat */
44 if (argc > 1 && argv[1]) {
45 filename = argv[1];
46 }
47 else {
48 filename = "conftest.dat";
49 }
50
51
52 lock.l_type = F_WRLCK;
53 lock.l_start = 0;
54 lock.l_whence = SEEK_SET;
55 lock.l_len = 100;
56
57 fd = open(filename, O_RDWR | O_CREAT, 0644);
58
59 err = fcntl(fd, F_SETLKW, &lock);
60
61 if (err) {
62 printf( "Failed to set a file lock on %s\n", filename );
63 perror( "Reason " );
64 }
65 else {
66 printf( "fcntl claims success in setting a file lock on %s\n", filename );
67 }
68 /* printf("err = %d, errno = %d\n", err, errno); */
69 close(fd);
70 unlink( filename );
71 return err;
72 }