関数 | |
static int | sys_waitpid (pid_t pid, int *status, int options) |
int | main (int argc, char *argv[]) |
static int sys_waitpid | ( | pid_t | pid, | |
int * | status, | |||
int | options | |||
) | [static] |
int main | ( | int | argc, | |
char * | argv[] | |||
) |
fcntl_lock.c の 41 行で定義されています。
参照先 errno・fd・fprintf()・status・sys_waitpid().
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 }