Imported upstream version 0.59.3
[hcoop/debian/courier-authlib.git] / liblock / locktest.c
1 /*
2 ** Copyright 1998 - 1999 Double Precision, Inc. See COPYING for
3 ** distribution information.
4 */
5
6 /* $Id */
7
8 #include "liblock.h"
9 #if USE_FCNTL
10 #include "lockfcntl.c"
11 #endif
12 #if USE_FLOCK
13 #include "lockflock.c"
14 #endif
15 #if USE_LOCKF
16 #include "locklockf.c"
17 #endif
18 #include <signal.h>
19 #include <stdlib.h>
20
21 int main()
22 {
23 int fd[2];
24 pid_t p;
25 int s;
26 int f;
27
28 signal(SIGCHLD, SIG_DFL);
29 if (pipe(fd))
30 {
31 perror("pipe");
32 return (1);
33 }
34
35 if ((p=fork()) == (pid_t)-1)
36 {
37 perror("fork");
38 return (1);
39 }
40
41 if (p == 0)
42 {
43 char c;
44
45 close(fd[1]);
46 read(fd[0], &c, 1);
47 close(fd[0]);
48
49 if ((f=open("conftest.lock", O_RDWR|O_CREAT, 0644)) < 0)
50 {
51 perror("open");
52 exit(1);
53 }
54 alarm(5);
55
56 if (ll_lockfd(f, ll_writelock, 0, 0))
57 {
58 close(f);
59 exit(0);
60 }
61 close(f);
62 exit(1);
63 }
64
65 if ((f=open("conftest.lock", O_RDWR|O_CREAT, 0644)) < 0)
66 {
67 perror("open");
68 exit(1);
69 }
70
71 if (ll_lockfd(f, ll_writelock, 0, 0))
72 {
73 perror("lock");
74 close(f);
75 exit(1);
76 }
77 close(fd[1]);
78 close(fd[0]);
79 while (wait(&s) != p)
80 ;
81 if (s == 0)
82 exit(0);
83 exit(1);
84 }