00001 /* -*- Mode: C; c-basic-offset:4 ; -*- */ 00002 /* 00003 * (C) 2004 by Argonne National Laboratory. 00004 * See COPYRIGHT in top-level directory. 00005 */ 00006 #include <stdio.h> 00007 #include <fcntl.h> 00008 #include <errno.h> 00009 #include <unistd.h> 00010 00011 /* 00012 * This program tests to see if fcntl returns success when asked to 00013 * establish a file lock. This test is intended for use on file systems 00014 * such as NFS that may not implement file locks. ROMIO makes use 00015 * of file locks to implement certain operations, and may not work 00016 * properly if file locks are not available. 00017 * 00018 * This is a simple test and has at least two limitations: 00019 * 00020 * 1. Some implementations of NFS are known to return success for 00021 * setting a file lock when in fact no lock has been set. This 00022 * test will not detect such erroneous implementations of NFS 00023 * 00024 * 2. Some implementations will hang (enter and wait indefinitately) 00025 * within the fcntl call. This program will also hang in that case. 00026 * Under normal conditions, this program should only take a few seconds to 00027 * run. 00028 * 00029 * The program prints a message showing the success or failure of 00030 * setting the file lock and sets the return status to 0 on success and 00031 * non-zero on failure. If there is a failure, the system routine 00032 * perror is also called to explain the reason. 00033 */ 00034 00035 /* style: allow:printf:2 sig:0 */ 00036 00037 int main( int argc, char *argv[] ) 00038 { 00039 struct flock lock; 00040 int fd, err; 00041 char *filename; 00042 00043 /* Set the filename. Either arg[1] or conftest.dat */ 00044 if (argc > 1 && argv[1]) { 00045 filename = argv[1]; 00046 } 00047 else { 00048 filename = "conftest.dat"; 00049 } 00050 00051 00052 lock.l_type = F_WRLCK; 00053 lock.l_start = 0; 00054 lock.l_whence = SEEK_SET; 00055 lock.l_len = 100; 00056 00057 fd = open(filename, O_RDWR | O_CREAT, 0644); 00058 00059 err = fcntl(fd, F_SETLKW, &lock); 00060 00061 if (err) { 00062 printf( "Failed to set a file lock on %s\n", filename ); 00063 perror( "Reason " ); 00064 } 00065 else { 00066 printf( "fcntl claims success in setting a file lock on %s\n", filename ); 00067 } 00068 /* printf("err = %d, errno = %d\n", err, errno); */ 00069 close(fd); 00070 unlink( filename ); 00071 return err; 00072 }