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

Last change on this file was 206, checked in by Herwig Bauernfeind, 16 years ago

Import Samba 3.3 branch at 3.0.0 level (psmedley's port)

File size: 2.5 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#endif
87 exit(1);
88 } else {
89 exit(0);
90 }
91 }
92
93 unlink(DATA);
94 fd = open(DATA, O_RDWR|O_CREAT|O_EXCL, 0600);
95
96 if (fd == -1) {
97 fprintf(stderr,"ERROR: failed to open %s (errno=%d)\n",
98 DATA, (int)errno);
99 exit(1);
100 }
101
102 lock.l_type = F_WRLCK;
103 lock.l_whence = SEEK_SET;
104 lock.l_start = 0;
105 lock.l_len = 4;
106 lock.l_pid = getpid();
107
108 /* set a 4 byte write lock */
109 fcntl(fd,F_SETLK,&lock);
110
111 sys_waitpid(pid, &status, 0);
112
113 unlink(DATA);
114
115#if defined(WIFEXITED) && defined(WEXITSTATUS)
116 if(WIFEXITED(status)) {
117 status = WEXITSTATUS(status);
118 } else {
119 status = 1;
120 }
121#else /* defined(WIFEXITED) && defined(WEXITSTATUS) */
122 status = (status == 0) ? 0 : 1;
123#endif /* defined(WIFEXITED) && defined(WEXITSTATUS) */
124
125 if (status) {
126 fprintf(stderr,"ERROR: lock test failed with status=%d\n",
127 status);
128 }
129
130 exit(status);
131}
Note: See TracBrowser for help on using the repository browser.