2 * Copyright (C) 1994-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.12.2.1 2002-07-23 12:40:33 adam
8 * Use busy wait for file locking on NT
10 * Revision 1.12 1999/05/26 07:49:13 adam
13 * Revision 1.11 1999/02/02 14:50:59 adam
14 * Updated WIN32 code specific sections. Changed header.
16 * Revision 1.10 1997/09/29 09:08:36 adam
17 * Revised locking system to be thread safe for the server.
19 * Revision 1.9 1997/09/25 14:54:43 adam
20 * WIN32 files lock support.
22 * Revision 1.8 1997/09/17 12:19:15 adam
23 * Zebra version corresponds to YAZ version 1.4.
24 * Changed Zebra server so that it doesn't depend on global common_resource.
26 * Revision 1.7 1997/09/09 13:38:08 adam
27 * Partial port to WIN95/NT.
29 * Revision 1.6 1996/10/29 14:08:14 adam
30 * Uses resource lockDir instead of lockPath.
32 * Revision 1.5 1996/03/26 16:01:13 adam
33 * New setting lockPath: directory of various lock files.
35 * Revision 1.4 1995/12/13 08:46:10 adam
36 * Locking uses F_WRLCK and F_RDLCK again!
38 * Revision 1.3 1995/12/12 16:00:57 adam
39 * System call sync(2) used after update/commit.
40 * Locking (based on fcntl) uses F_EXLCK and F_SHLCK instead of F_WRLCK
43 * Revision 1.2 1995/12/11 11:43:29 adam
44 * Locking based on fcntl instead of flock.
45 * Setting commitEnable removed. Command line option -n can be used to
46 * prevent commit if commit setting is defined in the configuration file.
48 * Revision 1.1 1995/12/07 17:38:47 adam
49 * Work locking mechanisms for concurrent updates/commit.
57 #include <sys/types.h>
60 #include <sys/locking.h>
67 struct zebra_lock_info {
72 ZebraLockHandle zebra_lock_create (const char *name, int excl_flag)
74 ZebraLockHandle h = (ZebraLockHandle) xmalloc (sizeof(*h));
75 h->excl_flag = excl_flag;
77 yaz_log (LOG_DEBUG, "zebra_lock_create %s %d begin", name, excl_flag);
80 h->fd = open (name, O_BINARY|O_RDONLY);
82 h->fd = open (name, ((h->excl_flag > 1) ? O_EXCL : 0)|
83 (O_BINARY|O_CREAT|O_RDWR), 0666);
85 h->fd= open (name, ((h->excl_flag > 1) ? O_EXCL : 0)|
86 (O_BINARY|O_CREAT|O_RDWR|O_SYNC), 0666);
90 if (h->excl_flag <= 1)
91 logf (LOG_WARN|LOG_ERRNO, "open %s", name);
95 yaz_log (LOG_DEBUG, "zebra_lock_create %s %d end", name, excl_flag);
99 void zebra_lock_destroy (ZebraLockHandle h)
108 void zebra_lock_prefix (Res res, char *path)
110 char *lock_dir = res_get_def (res, "lockDir", "");
112 strcpy (path, lock_dir);
113 if (*path && path[strlen(path)-1] != '/')
118 static int unixLock (int fd, int type, int cmd)
122 area.l_whence = SEEK_SET;
123 area.l_len = area.l_start = 0L;
124 return fcntl (fd, cmd, &area);
128 int zebra_lock (ZebraLockHandle h)
132 yaz_log(LOG_DEBUG, "lock begin");
133 while (_locking (h->fd, _LK_NBLCK, 1) != 0)
141 yaz_log(LOG_DEBUG, "lock end m=%d", m);
144 return unixLock (h->fd, h->excl_flag ? F_WRLCK : F_RDLCK, F_SETLKW);
148 int zebra_lock_nb (ZebraLockHandle h)
151 yaz_log(LOG_DEBUG, "lock_nb begin");
152 return _locking (h->fd, _LK_NBLCK, 1);
153 yaz_log(LOG_DEBUG, "lock_nb end");
155 return unixLock (h->fd, h->excl_flag ? F_WRLCK : F_RDLCK, F_SETLK);
159 int zebra_unlock (ZebraLockHandle h)
162 return _locking (h->fd, _LK_UNLCK, 1);
164 return unixLock (h->fd, F_UNLCK, F_SETLKW);
168 int zebra_lock_fd (ZebraLockHandle h)