2 * Copyright (C) 1994-2002, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
6 * $Id: lockutil.c,v 1.13 2002-02-20 17:30:01 adam Exp $
13 #include <sys/types.h>
16 #include <sys/locking.h>
23 struct zebra_lock_info {
28 ZebraLockHandle zebra_lock_create (const char *name, int excl_flag)
30 ZebraLockHandle h = (ZebraLockHandle) xmalloc (sizeof(*h));
31 h->excl_flag = excl_flag;
35 h->fd = open (name, O_BINARY|O_RDONLY);
37 h->fd = open (name, ((h->excl_flag > 1) ? O_EXCL : 0)|
38 (O_BINARY|O_CREAT|O_RDWR), 0666);
40 h->fd= open (name, ((h->excl_flag > 1) ? O_EXCL : 0)|
41 (O_BINARY|O_CREAT|O_RDWR|O_SYNC), 0666);
45 if (h->excl_flag <= 1)
46 logf (LOG_WARN|LOG_ERRNO, "open %s", name);
53 void zebra_lock_destroy (ZebraLockHandle h)
62 void zebra_lock_prefix (Res res, char *path)
64 char *lock_dir = res_get_def (res, "lockDir", "");
66 strcpy (path, lock_dir);
67 if (*path && path[strlen(path)-1] != '/')
72 static int unixLock (int fd, int type, int cmd)
76 area.l_whence = SEEK_SET;
77 area.l_len = area.l_start = 0L;
78 return fcntl (fd, cmd, &area);
82 int zebra_lock_w (ZebraLockHandle h)
85 return _locking (h->fd, _LK_LOCK, 1);
87 return unixLock (h->fd, F_WRLCK, F_SETLKW);
91 int zebra_lock_r (ZebraLockHandle h)
94 return _locking (h->fd, _LK_LOCK, 1);
96 return unixLock (h->fd, F_RDLCK, F_SETLKW);
100 int zebra_lock (ZebraLockHandle h)
103 return _locking (h->fd, _LK_LOCK, 1);
105 return unixLock (h->fd, h->excl_flag ? F_WRLCK : F_RDLCK, F_SETLKW);
109 int zebra_lock_nb (ZebraLockHandle h)
112 return _locking (h->fd, _LK_NBLCK, 1);
114 return unixLock (h->fd, h->excl_flag ? F_WRLCK : F_RDLCK, F_SETLK);
118 int zebra_unlock (ZebraLockHandle h)
121 return _locking (h->fd, _LK_UNLCK, 1);
123 return unixLock (h->fd, F_UNLCK, F_SETLKW);
127 int zebra_lock_fd (ZebraLockHandle h)