support little-endian bitmaps
[tridge/junkcode.git] / locktst.c
1 /* simple program to test file locking */
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <sys/fcntl.h>
5 #include <errno.h>
6
7 #define True (1==1)
8 #define False (!True)
9
10 #define BOOL int
11 #define DEBUG(x,body) (printf body)
12 #define HAVE_FCNTL_LOCK 1
13
14 #ifndef strerror
15 #define strerror(i) sys_errlist[i]
16 #endif
17
18 /****************************************************************************
19 simple routines to do connection counting
20 ****************************************************************************/
21 BOOL fcntl_lock(int fd,int op,int offset,int count,int type)
22 {
23 #if HAVE_FCNTL_LOCK
24   struct flock lock;
25   int ret;
26   unsigned long mask = (1<<31);
27
28   /* interpret negative counts as large numbers */
29   if (count < 0)
30     count &= ~mask;
31
32   /* no negative offsets */
33   offset &= ~mask;
34
35   /* count + offset must be in range */
36   while ((offset < 0 || (offset + count < 0)) && mask)
37     {
38       offset &= ~mask;
39       mask = mask >> 1;
40     }
41
42   DEBUG(5,("fcntl_lock %d %d %d %d %d\n",fd,op,offset,count,type));
43
44   lock.l_type = type;
45   lock.l_whence = SEEK_SET;
46   lock.l_start = offset;
47   lock.l_len = count;
48   lock.l_pid = 0;
49
50   errno = 0;
51
52   ret = fcntl(fd,op,&lock);
53
54   if (errno != 0)
55     DEBUG(3,("fcntl lock gave errno %d (%s)\n",errno,strerror(errno)));
56
57   /* a lock query */
58   if (op == F_GETLK)
59     {
60       if ((ret != -1) &&
61           (lock.l_type != F_UNLCK) && 
62           (lock.l_pid != 0) && 
63           (lock.l_pid != getpid()))
64         {
65           DEBUG(3,("fd %d is locked by pid %d\n",fd,lock.l_pid));
66           return(True);
67         }
68
69       /* it must be not locked or locked by me */
70       return(False);
71     }
72
73   /* a lock set or unset */
74   if (ret == -1)
75     {
76       DEBUG(3,("lock failed at offset %d count %d op %d type %d (%s)\n",
77                offset,count,op,type,strerror(errno)));
78
79       /* perhaps it doesn't support this sort of locking?? */
80       if (errno == EINVAL)
81         {
82           DEBUG(3,("locking not supported? returning True\n"));
83           return(True);
84         }
85
86       return(False);
87     }
88
89   /* everything went OK */
90   return(True);
91 #else
92   return(False);
93 #endif
94 }
95
96 void lock_test(char *fname)
97 {
98   int fd = open(fname,O_RDWR | O_CREAT,0644);
99   if (fd < 0) return;
100
101   write(fd, "hello", 5);
102
103   fcntl_lock(fd,F_SETLK,1,1,F_WRLCK);
104
105   sleep(60);
106
107   fcntl_lock(fd,F_GETLK,0,0,F_WRLCK);
108   fcntl_lock(fd,F_SETLK,0,0,F_UNLCK);
109
110   fcntl_lock(fd,F_SETLK,0,20,F_WRLCK);
111   fcntl_lock(fd,F_GETLK,0,20,F_WRLCK);
112   fcntl_lock(fd,F_SETLK,0,20,F_UNLCK);
113
114   fcntl_lock(fd,F_SETLK,0x7FFFFF80,20,F_WRLCK);
115   fcntl_lock(fd,F_GETLK,0x7FFFFF80,20,F_WRLCK);
116   fcntl_lock(fd,F_SETLK,0x7FFFFF80,20,F_UNLCK);
117
118   fcntl_lock(fd,F_SETLK,0xFFFFFF80,20,F_WRLCK);
119   fcntl_lock(fd,F_GETLK,0xFFFFFF80,20,F_WRLCK);
120   fcntl_lock(fd,F_SETLK,0xFFFFFF80,20,F_UNLCK);
121
122   fcntl_lock(fd,F_SETLK,0xFFFFFFFF,20,F_WRLCK);
123   fcntl_lock(fd,F_GETLK,0xFFFFFFFF,20,F_WRLCK);
124   fcntl_lock(fd,F_SETLK,0xFFFFFFFF,20,F_UNLCK);
125
126 }
127
128
129 main(int argc,char *argv[])
130 {
131         lock_test(argv[1]);
132 }