s3:winbindd: make use of samba_tevent_context_init()
[Samba/gebeck_regimport.git] / tests / fcntl_lock.c
blob98d028572ea884b64f6636190a742fc1098ad502
1 /* test whether fcntl locking works on this system */
3 #if defined(HAVE_UNISTD_H)
4 #include <unistd.h>
5 #endif
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <sys/types.h>
11 #ifdef HAVE_FCNTL_H
12 #include <fcntl.h>
13 #endif
15 #ifdef HAVE_SYS_FCNTL_H
16 #include <sys/fcntl.h>
17 #endif
19 #ifdef HAVE_SYS_WAIT_H
20 #include <sys/wait.h>
21 #endif
23 #include <errno.h>
25 static int sys_waitpid(pid_t pid,int *status,int options)
27 #ifdef HAVE_WAITPID
28 return waitpid(pid,status,options);
29 #else /* USE_WAITPID */
30 return wait4(pid, status, options, NULL);
31 #endif /* USE_WAITPID */
34 #define DATA "conftest.fcntl"
36 #ifndef SEEK_SET
37 #define SEEK_SET 0
38 #endif
40 /* lock a byte range in a open file */
41 int main(int argc, char *argv[])
43 struct flock lock;
44 int fd, ret, status=1;
45 pid_t pid;
46 char *testdir = NULL;
48 testdir = getenv("TESTDIR");
49 if (testdir) chdir(testdir);
51 alarm(10);
53 if (!(pid=fork())) {
54 sleep(2);
55 fd = open(DATA, O_RDONLY);
57 if (fd == -1) {
58 fprintf(stderr,"ERROR: failed to open %s (errno=%d)\n",
59 DATA, (int)errno);
60 exit(1);
63 lock.l_type = F_WRLCK;
64 lock.l_whence = SEEK_SET;
65 lock.l_start = 0x100000000LL;
66 lock.l_len = 4;
67 lock.l_pid = getpid();
69 lock.l_type = F_WRLCK;
71 /* check if a lock applies */
72 ret = fcntl(fd,F_GETLK,&lock);
74 if ((ret == -1) ||
75 (lock.l_type == F_UNLCK)) {
76 fprintf(stderr,"ERROR: lock test failed (ret=%d errno=%d)\n", ret, (int)errno);
77 exit(1);
78 } else {
79 exit(0);
83 unlink(DATA);
84 fd = open(DATA, O_RDWR|O_CREAT|O_EXCL, 0600);
86 if (fd == -1) {
87 fprintf(stderr,"ERROR: failed to open %s (errno=%d)\n",
88 DATA, (int)errno);
89 exit(1);
92 lock.l_type = F_WRLCK;
93 lock.l_whence = SEEK_SET;
94 lock.l_start = 0;
95 lock.l_len = 0x100000004LL;
96 lock.l_pid = getpid();
98 /* set a 100000004 byte write lock, should conflict with the above */
99 ret = fcntl(fd,F_SETLK,&lock);
101 sys_waitpid(pid, &status, 0);
103 unlink(DATA);
105 if (ret != 0) {
106 fprintf(stderr,"ERROR: failed to lock %s (errno=%d)\n",
107 DATA, (int)errno);
108 exit(1);
111 if (lock.l_len < 0x100000004LL) {
112 fprintf(stderr,"ERROR: settign lock overflowed\n");
113 exit(1);
116 #if defined(WIFEXITED) && defined(WEXITSTATUS)
117 if(WIFEXITED(status)) {
118 status = WEXITSTATUS(status);
119 } else {
120 status = 1;
122 #else /* defined(WIFEXITED) && defined(WEXITSTATUS) */
123 status = (status == 0) ? 0 : 1;
124 #endif /* defined(WIFEXITED) && defined(WEXITSTATUS) */
126 if (status) {
127 fprintf(stderr,"ERROR: lock test failed with status=%d\n",
128 status);
131 exit(status);