tests/fcntl_lock.c

説明を見る。
00001 /* test whether fcntl locking works on this system */
00002 
00003 #if defined(HAVE_UNISTD_H)
00004 #include <unistd.h>
00005 #endif
00006 
00007 #include <stdio.h>
00008 #include <stdlib.h>
00009 #include <sys/types.h>
00010 
00011 #ifdef HAVE_FCNTL_H
00012 #include <fcntl.h>
00013 #endif
00014 
00015 #ifdef HAVE_SYS_FCNTL_H
00016 #include <sys/fcntl.h>
00017 #endif
00018 
00019 #ifdef HAVE_SYS_WAIT_H
00020 #include <sys/wait.h>
00021 #endif
00022 
00023 #include <errno.h>
00024 
00025 static int sys_waitpid(pid_t pid,int *status,int options)
00026 {
00027 #ifdef HAVE_WAITPID
00028   return waitpid(pid,status,options);
00029 #else /* USE_WAITPID */
00030   return wait4(pid, status, options, NULL);
00031 #endif /* USE_WAITPID */
00032 }
00033 
00034 #define DATA "conftest.fcntl"
00035 
00036 #ifndef SEEK_SET
00037 #define SEEK_SET 0
00038 #endif
00039 
00040 /* lock a byte range in a open file */
00041 int main(int argc, char *argv[])
00042 {
00043         struct flock lock;
00044         int fd, ret, status=1;
00045         pid_t pid;
00046         char *testdir = NULL;
00047 
00048         testdir = getenv("TESTDIR");
00049         if (testdir) chdir(testdir);
00050 
00051         alarm(10);
00052 
00053         if (!(pid=fork())) {
00054                 sleep(2);
00055                 fd = open(DATA, O_RDONLY);
00056 
00057                 if (fd == -1) {
00058                         fprintf(stderr,"ERROR: failed to open %s (errno=%d)\n", 
00059                                 DATA, (int)errno);
00060                         exit(1);
00061                 }
00062 
00063                 lock.l_type = F_WRLCK;
00064                 lock.l_whence = SEEK_SET;
00065                 lock.l_start = 0;
00066                 lock.l_len = 4;
00067                 lock.l_pid = getpid();
00068                 
00069                 lock.l_type = F_WRLCK;
00070                 
00071                 /* check if a lock applies */
00072                 ret = fcntl(fd,F_GETLK,&lock);
00073 
00074                 if ((ret == -1) ||
00075                     (lock.l_type == F_UNLCK)) {
00076                         fprintf(stderr,"ERROR: lock test failed (ret=%d errno=%d)\n", ret, (int)errno);
00077                         exit(1);
00078                 } else {
00079                         exit(0);
00080                 }
00081         }
00082 
00083         unlink(DATA);
00084         fd = open(DATA, O_RDWR|O_CREAT|O_EXCL, 0600);
00085 
00086         if (fd == -1) {
00087                 fprintf(stderr,"ERROR: failed to open %s (errno=%d)\n", 
00088                         DATA, (int)errno);
00089                 exit(1);
00090         }
00091 
00092         lock.l_type = F_WRLCK;
00093         lock.l_whence = SEEK_SET;
00094         lock.l_start = 0;
00095         lock.l_len = 4;
00096         lock.l_pid = getpid();
00097 
00098         /* set a 4 byte write lock */
00099         fcntl(fd,F_SETLK,&lock);
00100 
00101         sys_waitpid(pid, &status, 0);
00102 
00103         unlink(DATA);
00104 
00105 #if defined(WIFEXITED) && defined(WEXITSTATUS)
00106     if(WIFEXITED(status)) {
00107         status = WEXITSTATUS(status);
00108     } else {
00109         status = 1;
00110     }
00111 #else /* defined(WIFEXITED) && defined(WEXITSTATUS) */
00112         status = (status == 0) ? 0 : 1;
00113 #endif /* defined(WIFEXITED) && defined(WEXITSTATUS) */
00114 
00115         if (status) {
00116                 fprintf(stderr,"ERROR: lock test failed with status=%d\n", 
00117                         status);
00118         }
00119 
00120         exit(status);
00121 }

Sambaに対してSat Aug 29 21:23:27 2009に生成されました。  doxygen 1.4.7