1 /* $Id: lockutil.c,v 1.19 2004-11-19 10:26:59 heikki Exp $
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002
5 This file is part of the Zebra server.
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra. If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
29 #include <sys/types.h>
32 #include <sys/locking.h>
39 struct zebra_lock_info {
44 char *zebra_mk_fname (const char *dir, const char *name)
46 int dlen = dir ? strlen(dir) : 0;
47 char *fname = xmalloc (dlen + strlen(name) + 3);
52 int last_one = dir[dlen-1];
54 if (!strchr ("/\\:", last_one))
55 sprintf (fname, "%s\\%s", dir, name);
57 sprintf (fname, "%s%s", dir, name);
60 sprintf (fname, "%s", name);
64 int last_one = dir[dlen-1];
66 if (!strchr ("/", last_one))
67 sprintf (fname, "%s/%s", dir, name);
69 sprintf (fname, "%s%s", dir, name);
72 sprintf (fname, "%s", name);
77 ZebraLockHandle zebra_lock_create (const char *dir,
78 const char *name, int excl_flag)
80 char *fname = zebra_mk_fname(dir, name);
81 ZebraLockHandle h = (ZebraLockHandle) xmalloc (sizeof(*h));
83 h->excl_flag = excl_flag;
89 h->fd = open (name, O_BINARY|O_RDONLY);
91 h->fd = open (fname, ((h->excl_flag > 1) ? O_EXCL : 0)|
92 (O_BINARY|O_CREAT|O_RDWR), 0666);
94 h->fd= open (fname, ((h->excl_flag > 1) ? O_EXCL : 0)|
95 (O_BINARY|O_CREAT|O_RDWR), 0666);
99 if (h->excl_flag <= 1)
100 yaz_log (YLOG_WARN|YLOG_ERRNO, "open %s", fname);
108 void zebra_lock_destroy (ZebraLockHandle h)
117 void zebra_lock_prefix (Res res, char *path)
119 const char *lock_dir = res_get_def (res, "lockDir", "");
121 strcpy (path, lock_dir);
122 if (*path && path[strlen(path)-1] != '/')
127 static int unixLock (int fd, int type, int cmd)
131 area.l_whence = SEEK_SET;
132 area.l_len = area.l_start = 0L;
133 return fcntl (fd, cmd, &area);
137 int zebra_lock_w (ZebraLockHandle h)
140 return _locking (h->fd, _LK_LOCK, 1);
142 return unixLock (h->fd, F_WRLCK, F_SETLKW);
146 int zebra_lock_r (ZebraLockHandle h)
149 return _locking (h->fd, _LK_LOCK, 1);
151 return unixLock (h->fd, F_RDLCK, F_SETLKW);
155 int zebra_lock (ZebraLockHandle h)
158 return _locking (h->fd, _LK_LOCK, 1);
160 return unixLock (h->fd, h->excl_flag ? F_WRLCK : F_RDLCK, F_SETLKW);
164 int zebra_lock_nb (ZebraLockHandle h)
167 return _locking (h->fd, _LK_NBLCK, 1);
169 return unixLock (h->fd, h->excl_flag ? F_WRLCK : F_RDLCK, F_SETLK);
173 int zebra_unlock (ZebraLockHandle h)
176 return _locking (h->fd, _LK_UNLCK, 1);
178 return unixLock (h->fd, F_UNLCK, F_SETLKW);
182 int zebra_lock_fd (ZebraLockHandle h)