1 /* $Id: flock.c,v 1.21 2007-01-15 15:10:26 adam Exp $
2 Copyright (C) 1995-2007
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 this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
29 #include <sys/types.h>
32 #include <sys/locking.h>
38 #include <idzebra/flock.h>
39 #include <zebra-lock.h>
40 #include <yaz/xmalloc.h>
43 /** have this module (mutex) been initialized? */
44 static int initialized = 0;
46 /** whether fcntl locks are shared for all threads in a process (POSIX) */
47 static int posix_locks = 1;
49 /** mutex for lock_list below */
50 Zebra_mutex lock_list_mutex;
52 /** our list of file locked files */
53 static struct zebra_lock_info *lock_list = 0;
55 /** the internal handle, with a pointer to each lock file info */
56 struct zebra_lock_handle {
58 /** so we can call zebra_lock_rdwr_wunlock or zebra_lock_lock_runlock */
61 struct zebra_lock_info *p;
64 struct zebra_lock_info {
65 /** file descriptor */
67 /** full path (xmalloc'ed) */
69 /** reference counter: number of zebra_lock_handles pointing to us */
72 /** number of file write locks/read locks */
73 int no_file_write_lock;
74 int no_file_read_lock;
75 Zebra_lock_rdwr rdwr_lock;
76 Zebra_mutex file_mutex;
78 /** next in lock list */
79 struct zebra_lock_info *next;
82 static int log_level = 0;
84 char *zebra_mk_fname(const char *dir, const char *name)
86 int dlen = dir ? strlen(dir) : 0;
87 char *fname = xmalloc(dlen + strlen(name) + 3);
92 int last_one = dir[dlen-1];
94 if (!strchr("/\\:", last_one))
95 sprintf(fname, "%s\\%s", dir, name);
97 sprintf(fname, "%s%s", dir, name);
100 sprintf(fname, "%s", name);
104 int last_one = dir[dlen-1];
106 if (!strchr("/", last_one))
107 sprintf(fname, "%s/%s", dir, name);
109 sprintf(fname, "%s%s", dir, name);
112 sprintf(fname, "%s", name);
117 ZebraLockHandle zebra_lock_create(const char *dir, const char *name)
119 char *fname = zebra_mk_fname(dir, name);
120 struct zebra_lock_info *p = 0;
121 ZebraLockHandle h = 0;
125 zebra_mutex_lock(&lock_list_mutex);
126 /* see if we have the same filename in a global list of "lock files" */
130 for (p = lock_list; p ; p = p->next)
131 if (!strcmp(p->fname, fname))
136 { /* didn't match (or we didn't want it to match! */
137 p = (struct zebra_lock_info *) xmalloc(sizeof(*p));
141 p->fd = open(name, O_BINARY|O_RDONLY);
143 p->fd = open(fname, (O_BINARY|O_CREAT|O_RDWR), 0666);
145 p->fd = open(fname, (O_BINARY|O_CREAT|O_RDWR), 0666);
150 yaz_log(YLOG_WARN | YLOG_ERRNO,
151 "zebra_lock_create fail fname=%s", fname);
157 fname = 0; /* fname buffer now owned by p->fname */
160 zebra_lock_rdwr_init(&p->rdwr_lock);
162 zebra_mutex_init(&p->file_mutex);
163 p->no_file_write_lock = 0;
164 p->no_file_read_lock = 0;
172 /* we have lock info so we can make a handle pointing to that */
174 h = (ZebraLockHandle) xmalloc(sizeof(*h));
179 yaz_log(log_level, "zebra_lock_create fd=%d p=%p fname=%s",
180 h->p->fd, h, p->fname);
182 zebra_mutex_unlock(&lock_list_mutex);
183 xfree(fname); /* free it - if it's still there */
188 void zebra_lock_destroy(ZebraLockHandle h)
192 yaz_log(log_level, "zebra_lock_destroy fd=%d p=%p fname=%s",
193 h->p->fd, h, h->p->fname);
194 zebra_mutex_lock(&lock_list_mutex);
195 yaz_log(log_level, "zebra_lock_destroy fd=%d p=%p fname=%s refcount=%d",
196 h->p->fd, h, h->p->fname, h->p->ref_count);
197 assert(h->p->ref_count > 0);
199 if (h->p->ref_count == 0)
201 /* must remove shared info from lock_list */
202 struct zebra_lock_info **hp = &lock_list;
214 yaz_log(log_level, "zebra_lock_destroy fd=%d p=%p fname=%s remove",
215 h->p->fd, h, h->p->fname);
219 zebra_lock_rdwr_destroy(&h->p->rdwr_lock);
220 zebra_mutex_destroy(&h->p->file_mutex);
228 zebra_mutex_unlock(&lock_list_mutex);
232 static int unixLock(int fd, int type, int cmd)
237 area.l_whence = SEEK_SET;
238 area.l_len = area.l_start = 0L;
240 yaz_log(log_level, "fcntl begin type=%d fd=%d", type, fd);
241 r = fcntl(fd, cmd, &area);
243 yaz_log(YLOG_WARN|YLOG_ERRNO, "fcntl FAIL type=%d fd=%d", type, fd);
245 yaz_log(log_level, "fcntl type=%d OK fd=%d", type, fd);
251 int zebra_lock_w(ZebraLockHandle h)
255 yaz_log(log_level, "zebra_lock_w fd=%d p=%p fname=%s begin",
256 h->p->fd, h, h->p->fname);
259 while ((r = _locking(h->p->fd, _LK_LOCK, 1)))
263 zebra_lock_rdwr_wlock(&h->p->rdwr_lock);
265 zebra_mutex_lock(&h->p->file_mutex);
266 if (h->p->no_file_write_lock == 0)
268 h->p->no_file_write_lock++;
271 /* if there is already a read lock.. upgrade to write lock */
272 r = unixLock(h->p->fd, F_WRLCK, F_SETLKW);
278 zebra_mutex_unlock(&h->p->file_mutex);
282 yaz_log(log_level, "zebra_lock_w fd=%d p=%p fname=%s end",
283 h->p->fd, h, h->p->fname);
288 int zebra_lock_r(ZebraLockHandle h)
293 yaz_log(log_level, "zebra_lock_r fd=%d p=%p fname=%s begin",
294 h->p->fd, h, h->p->fname);
296 while ((r = _locking(h->p->fd, _LK_LOCK, 1)))
300 zebra_lock_rdwr_rlock(&h->p->rdwr_lock);
302 zebra_mutex_lock(&h->p->file_mutex);
303 if (h->p->no_file_read_lock == 0 && h->p->no_file_write_lock == 0)
305 h->p->no_file_read_lock++;
308 /* only read lock if no write locks already */
309 r = unixLock(h->p->fd, F_RDLCK, F_SETLKW);
315 zebra_mutex_unlock(&h->p->file_mutex);
319 yaz_log(log_level, "zebra_lock_r fd=%d p=%p fname=%s end",
320 h->p->fd, h, h->p->fname);
324 int zebra_unlock(ZebraLockHandle h)
327 yaz_log(log_level, "zebra_unlock fd=%d p=%p fname=%s begin",
328 h->p->fd, h, h->p->fname);
330 r = _locking(h->p->fd, _LK_UNLCK, 1);
332 zebra_mutex_lock(&h->p->file_mutex);
335 if (h->p->no_file_write_lock > 0)
336 h->p->no_file_write_lock--;
340 if (h->p->no_file_read_lock > 0)
341 h->p->no_file_read_lock--;
343 if (h->p->no_file_read_lock == 0 && h->p->no_file_write_lock == 0)
344 r = unixLock(h->p->fd, F_UNLCK, F_SETLKW);
351 zebra_mutex_unlock(&h->p->file_mutex);
356 zebra_lock_rdwr_wunlock(&h->p->rdwr_lock);
358 zebra_lock_rdwr_runlock(&h->p->rdwr_lock);
361 yaz_log(log_level, "zebra_unlock fd=%d p=%p fname=%s end",
362 h->p->fd, h, h->p->fname);
366 /** \brief see if the fcntl locking is not POSIX
368 * The default posix_locks=1 is assumed.. This function sets posix_locks
369 * to zero if linuxthreads is in use.
371 static int check_for_linuxthreads(void)
374 #ifdef _CS_GNU_LIBPTHREAD_VERSION
376 size_t r = confstr(_CS_GNU_LIBPTHREAD_VERSION, conf_buf, sizeof(conf_buf));
379 yaz_log(YLOG_WARN|YLOG_ERRNO, "confstr failed");
382 if (strncmp(conf_buf, "linuxthreads", 12) == 0)
383 posix_locks = 0; /* Using linuxthreads.. */
385 posix_locks = 0; /* Old GLIBC on Linux. Assume linuxthreads */
391 void zebra_flock_init()
396 log_level = yaz_log_module_level("flock");
397 yaz_log(log_level, "zebra_flock_init");
398 check_for_linuxthreads();
399 zebra_mutex_init(&lock_list_mutex);
400 yaz_log(log_level, "posix_locks: %d", posix_locks);
407 * indent-tabs-mode: nil
409 * vim: shiftwidth=4 tabstop=8 expandtab