1 /* $Id: lockutil.c,v 1.21 2005-06-14 20:28:54 adam Exp $
2 Copyright (C) 1995-2005
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>
40 struct zebra_lock_info {
45 char *zebra_mk_fname (const char *dir, const char *name)
47 int dlen = dir ? strlen(dir) : 0;
48 char *fname = xmalloc (dlen + strlen(name) + 3);
53 int last_one = dir[dlen-1];
55 if (!strchr ("/\\:", last_one))
56 sprintf (fname, "%s\\%s", dir, name);
58 sprintf (fname, "%s%s", dir, name);
61 sprintf (fname, "%s", name);
65 int last_one = dir[dlen-1];
67 if (!strchr ("/", last_one))
68 sprintf (fname, "%s/%s", dir, name);
70 sprintf (fname, "%s%s", dir, name);
73 sprintf (fname, "%s", name);
78 ZebraLockHandle zebra_lock_create (const char *dir,
79 const char *name, int excl_flag)
81 char *fname = zebra_mk_fname(dir, name);
82 ZebraLockHandle h = (ZebraLockHandle) xmalloc (sizeof(*h));
84 h->excl_flag = excl_flag;
90 h->fd = open (name, O_BINARY|O_RDONLY);
92 h->fd = open (fname, ((h->excl_flag > 1) ? O_EXCL : 0)|
93 (O_BINARY|O_CREAT|O_RDWR), 0666);
95 h->fd= open (fname, ((h->excl_flag > 1) ? O_EXCL : 0)|
96 (O_BINARY|O_CREAT|O_RDWR), 0666);
100 if (h->excl_flag <= 1)
101 yaz_log (YLOG_WARN|YLOG_ERRNO, "open %s", fname);
109 void zebra_lock_destroy (ZebraLockHandle h)
118 void zebra_lock_prefix (Res res, char *path)
120 const char *lock_dir = res_get_def (res, "lockDir", "");
122 strcpy (path, lock_dir);
123 if (*path && path[strlen(path)-1] != '/')
128 static int unixLock (int fd, int type, int cmd)
132 area.l_whence = SEEK_SET;
133 area.l_len = area.l_start = 0L;
134 return fcntl (fd, cmd, &area);
138 int zebra_lock_w (ZebraLockHandle h)
141 return _locking (h->fd, _LK_LOCK, 1);
143 return unixLock (h->fd, F_WRLCK, F_SETLKW);
147 int zebra_lock_r (ZebraLockHandle h)
150 return _locking (h->fd, _LK_LOCK, 1);
152 return unixLock (h->fd, F_RDLCK, F_SETLKW);
156 int zebra_lock (ZebraLockHandle h)
159 return _locking (h->fd, _LK_LOCK, 1);
161 return unixLock (h->fd, h->excl_flag ? F_WRLCK : F_RDLCK, F_SETLKW);
165 int zebra_lock_nb (ZebraLockHandle h)
168 return _locking (h->fd, _LK_NBLCK, 1);
170 return unixLock (h->fd, h->excl_flag ? F_WRLCK : F_RDLCK, F_SETLK);
174 int zebra_unlock (ZebraLockHandle h)
177 return _locking (h->fd, _LK_UNLCK, 1);
179 return unixLock (h->fd, F_UNLCK, F_SETLKW);
183 int zebra_lock_fd (ZebraLockHandle h)