1 /* simple program to test file locking */
11 #define DEBUG(x,body) (printf body)
12 #define HAVE_FCNTL_LOCK 1
15 #define strerror(i) sys_errlist[i]
18 /****************************************************************************
19 simple routines to do connection counting
20 ****************************************************************************/
21 BOOL fcntl_lock(int fd,int op,int offset,int count,int type)
26 unsigned long mask = (1<<31);
28 /* interpret negative counts as large numbers */
32 /* no negative offsets */
35 /* count + offset must be in range */
36 while ((offset < 0 || (offset + count < 0)) && mask)
42 DEBUG(5,("fcntl_lock %d %d %d %d %d\n",fd,op,offset,count,type));
45 lock.l_whence = SEEK_SET;
46 lock.l_start = offset;
52 ret = fcntl(fd,op,&lock);
55 DEBUG(3,("fcntl lock gave errno %d (%s)\n",errno,strerror(errno)));
61 (lock.l_type != F_UNLCK) &&
63 (lock.l_pid != getpid()))
65 DEBUG(3,("fd %d is locked by pid %d\n",fd,lock.l_pid));
69 /* it must be not locked or locked by me */
73 /* a lock set or unset */
76 DEBUG(3,("lock failed at offset %d count %d op %d type %d (%s)\n",
77 offset,count,op,type,strerror(errno)));
79 /* perhaps it doesn't support this sort of locking?? */
82 DEBUG(3,("locking not supported? returning True\n"));
89 /* everything went OK */
96 void lock_test(char *fname)
98 int fd = open(fname,O_RDWR | O_CREAT,0644);
101 write(fd, "hello", 5);
103 fcntl_lock(fd,F_SETLK,1,1,F_WRLCK);
107 fcntl_lock(fd,F_GETLK,0,0,F_WRLCK);
108 fcntl_lock(fd,F_SETLK,0,0,F_UNLCK);
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);
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);
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);
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);
129 main(int argc,char *argv[])