source: branches/samba-3.2.x/source/tests/fcntl_lock.c

Last change on this file was 133, checked in by Paul Smedley, 17 years ago

Update trunk to 3.2.0pre3

File size: 2.3 KB
Line 
1/* test whether fcntl locking works on this system */
2
3#ifdef __OS2__
4#define HAVE_UNISTD_H 1
5#define HAVE_FCNTL_H 1
6#define HAVE_SYS_WAIT_H 1
7#define HAVE_WAITPID 1
8#endif
9
10#if defined(HAVE_UNISTD_H)
11#include <unistd.h>
12#endif
13
14#include <stdio.h>
15#include <stdlib.h>
16#include <sys/types.h>
17
18#ifdef HAVE_FCNTL_H
19#include <fcntl.h>
20#endif
21
22#ifdef HAVE_SYS_FCNTL_H
23#include <sys/fcntl.h>
24#endif
25
26#ifdef HAVE_SYS_WAIT_H
27#include <sys/wait.h>
28#endif
29
30#include <errno.h>
31
32static int sys_waitpid(pid_t pid,int *status,int options)
33{
34#ifdef HAVE_WAITPID
35 return waitpid(pid,status,options);
36#else /* USE_WAITPID */
37 return wait4(pid, status, options, NULL);
38#endif /* USE_WAITPID */
39}
40
41#define DATA "conftest.fcntl"
42
43#ifndef SEEK_SET
44#define SEEK_SET 0
45#endif
46
47/* lock a byte range in a open file */
48int main(int argc, char *argv[])
49{
50 struct flock lock;
51 int fd, ret, status=1;
52 pid_t pid;
53 char *testdir = NULL;
54
55 testdir = getenv("TESTDIR");
56 if (testdir) chdir(testdir);
57
58 alarm(10);
59
60 if (!(pid=fork())) {
61 sleep(2);
62 fd = open(DATA, O_RDONLY);
63
64 if (fd == -1) {
65 fprintf(stderr,"ERROR: failed to open %s (errno=%d)\n",
66 DATA, (int)errno);
67 exit(1);
68 }
69
70 lock.l_type = F_WRLCK;
71 lock.l_whence = SEEK_SET;
72 lock.l_start = 0;
73 lock.l_len = 4;
74 lock.l_pid = getpid();
75
76 lock.l_type = F_WRLCK;
77
78 /* check if a lock applies */
79 ret = fcntl(fd,F_GETLK,&lock);
80
81 if ((ret == -1) ||
82 (lock.l_type == F_UNLCK)) {
83 fprintf(stderr,"ERROR: lock test failed (ret=%d errno=%d)\n", ret, (int)errno);
84#ifdef __OS2__
85 exit(0);
86#else
87 exit(1);
88#endif
89 } else {
90 exit(0);
91 }
92 }
93
94 unlink(DATA);
95 fd = open(DATA, O_RDWR|O_CREAT|O_EXCL, 0600);
96
97 if (fd == -1) {
98 fprintf(stderr,"ERROR: failed to open %s (errno=%d)\n",
99 DATA, (int)errno);
100 exit(1);
101 }
102
103 lock.l_type = F_WRLCK;
104 lock.l_whence = SEEK_SET;
105 lock.l_start = 0;
106 lock.l_len = 4;
107 lock.l_pid = getpid();
108
109 /* set a 4 byte write lock */
110 fcntl(fd,F_SETLK,&lock);
111
112 sys_waitpid(pid, &status, 0);
113
114 unlink(DATA);
115
116#if defined(WIFEXITED) && defined(WEXITSTATUS)
117 if(WIFEXITED(status)) {
118 status = WEXITSTATUS(status);
119 } else {
120 status = 1;
121 }
122#else /* defined(WIFEXITED) && defined(WEXITSTATUS) */
123 status = (status == 0) ? 0 : 1;
124#endif /* defined(WIFEXITED) && defined(WEXITSTATUS) */
125
126 if (status) {
127 fprintf(stderr,"ERROR: lock test failed with status=%d\n",
128 status);
129 }
130
131 exit(status);
132}
Note: See TracBrowser for help on using the repository browser.