1 /* $Id: flock.c,v 1.16 2006-07-03 13:40:58 adam Exp $
2 Copyright (C) 1995-2006
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>
38 #include <idzebra/flock.h>
39 #include <zebra-lock.h>
40 #include <yaz/xmalloc.h>
43 /** whether this module should debug */
46 /** have this module (mutex) been initialized? */
47 static int initialized = 0;
49 /** whether fcntl locks are shared for all threads in a process (POSIX) */
50 static int posix_locks = 1;
52 /** mutex for lock_list below */
53 Zebra_mutex lock_list_mutex;
55 /** our list of file locked files */
56 static struct zebra_lock_info *lock_list = 0;
58 /** the internal handle, with a pointer to each lock file info */
59 struct zebra_lock_handle {
61 /** so we can call zebra_lock_rdwr_wunlock or zebra_lock_lock_runlock */
64 struct zebra_lock_info *p;
67 struct zebra_lock_info {
68 /** file descriptor */
70 /** full path (xmalloc'ed) */
72 /** reference counter: number of zebra_lock_handles pointing to us */
75 /** number of file write locks/read locks */
76 int no_file_write_lock;
77 int no_file_read_lock;
78 Zebra_lock_rdwr rdwr_lock;
79 Zebra_mutex file_mutex;
81 /** next in lock list */
82 struct zebra_lock_info *next;
85 static int log_level = 0;
87 char *zebra_mk_fname(const char *dir, const char *name)
89 int dlen = dir ? strlen(dir) : 0;
90 char *fname = xmalloc(dlen + strlen(name) + 3);
95 int last_one = dir[dlen-1];
97 if (!strchr("/\\:", last_one))
98 sprintf(fname, "%s\\%s", dir, name);
100 sprintf(fname, "%s%s", dir, name);
103 sprintf(fname, "%s", name);
107 int last_one = dir[dlen-1];
109 if (!strchr("/", last_one))
110 sprintf(fname, "%s/%s", dir, name);
112 sprintf(fname, "%s%s", dir, name);
115 sprintf(fname, "%s", name);
120 ZebraLockHandle zebra_lock_create(const char *dir, const char *name)
122 char *fname = zebra_mk_fname(dir, name);
123 struct zebra_lock_info *p = 0;
124 ZebraLockHandle h = 0;
128 zebra_mutex_lock(&lock_list_mutex);
129 /* see if we have the same filename in a global list of "lock files" */
133 for (p = lock_list; p ; p = p->next)
134 if (!strcmp(p->fname, fname))
139 { /* didn't match (or we didn't want it to match! */
140 p = (struct zebra_lock_info *) xmalloc(sizeof(*p));
144 p->fd = open(name, O_BINARY|O_RDONLY);
146 p->fd = open(fname, (O_BINARY|O_CREAT|O_RDWR), 0666);
148 p->fd = open(fname, (O_BINARY|O_CREAT|O_RDWR), 0666);
153 yaz_log(YLOG_WARN | YLOG_ERRNO,
154 "zebra_lock_create fail fname=%s", fname);
160 fname = 0; /* fname buffer now owned by p->fname */
163 zebra_lock_rdwr_init(&p->rdwr_lock);
165 zebra_mutex_init(&p->file_mutex);
166 p->no_file_write_lock = 0;
167 p->no_file_read_lock = 0;
175 /* we have lock info so we can make a handle pointing to that */
177 h = (ZebraLockHandle) xmalloc(sizeof(*h));
182 yaz_log(log_level, "zebra_lock_create fd=%d p=%p fname=%s",
183 h->p->fd, h, p->fname);
185 zebra_mutex_unlock(&lock_list_mutex);
186 xfree(fname); /* free it - if it's still there */
191 void zebra_lock_destroy(ZebraLockHandle h)
195 yaz_log(log_level, "zebra_lock_destroy fd=%d p=%p fname=%s",
196 h->p->fd, h, h->p->fname);
197 zebra_mutex_lock(&lock_list_mutex);
198 yaz_log(log_level, "zebra_lock_destroy fd=%d p=%p fname=%s refcount=%d",
199 h->p->fd, h, h->p->fname, h->p->ref_count);
200 assert(h->p->ref_count > 0);
202 if (h->p->ref_count == 0)
204 /* must remove shared info from lock_list */
205 struct zebra_lock_info **hp = &lock_list;
217 yaz_log(log_level, "zebra_lock_destroy fd=%d p=%p fname=%s remove",
218 h->p->fd, h, h->p->fname);
222 zebra_lock_rdwr_destroy(&h->p->rdwr_lock);
223 zebra_mutex_destroy(&h->p->file_mutex);
231 zebra_mutex_unlock(&lock_list_mutex);
235 static int unixLock(int fd, int type, int cmd)
240 area.l_whence = SEEK_SET;
241 area.l_len = area.l_start = 0L;
243 yaz_log(log_level, "fcntl begin type=%d fd=%d", type, fd);
244 r = fcntl(fd, cmd, &area);
246 yaz_log(YLOG_WARN|YLOG_ERRNO, "fcntl FAIL type=%d fd=%d", type, fd);
248 yaz_log(log_level, "fcntl type=%d OK fd=%d", type, fd);
254 int zebra_lock_w(ZebraLockHandle h)
258 yaz_log(log_level, "zebra_lock_w fd=%d p=%p fname=%s begin",
259 h->p->fd, h, h->p->fname);
262 while ((r = _locking(h->p->fd, _LK_LOCK, 1)))
266 zebra_lock_rdwr_wlock(&h->p->rdwr_lock);
268 zebra_mutex_lock(&h->p->file_mutex);
269 if (h->p->no_file_write_lock == 0)
271 h->p->no_file_write_lock++;
274 /* if there is already a read lock.. upgrade to write lock */
275 r = unixLock(h->p->fd, F_WRLCK, F_SETLKW);
281 zebra_mutex_unlock(&h->p->file_mutex);
285 yaz_log(log_level, "zebra_lock_w fd=%d p=%p fname=%s end",
286 h->p->fd, h, h->p->fname);
291 int zebra_lock_r(ZebraLockHandle h)
296 yaz_log(log_level, "zebra_lock_r fd=%d p=%p fname=%s begin",
297 h->p->fd, h, h->p->fname);
299 while ((r = _locking(h->p->fd, _LK_LOCK, 1)))
303 zebra_lock_rdwr_rlock(&h->p->rdwr_lock);
305 zebra_mutex_lock(&h->p->file_mutex);
306 if (h->p->no_file_read_lock == 0 && h->p->no_file_write_lock == 0)
308 h->p->no_file_read_lock++;
311 /* only read lock if no write locks already */
312 r = unixLock(h->p->fd, F_RDLCK, F_SETLKW);
318 zebra_mutex_unlock(&h->p->file_mutex);
322 yaz_log(log_level, "zebra_lock_r fd=%d p=%p fname=%s end",
323 h->p->fd, h, h->p->fname);
327 int zebra_unlock(ZebraLockHandle h)
331 yaz_log(log_level, "zebra_unlock fd=%d p=%p fname=%s begin",
332 h->p->fd, h, h->p->fname);
334 r = _locking(h->p->fd, _LK_UNLCK, 1);
336 zebra_mutex_lock(&h->p->file_mutex);
339 if (h->p->no_file_write_lock > 0)
340 h->p->no_file_write_lock--;
344 if (h->p->no_file_read_lock > 0)
345 h->p->no_file_read_lock--;
347 if (h->p->no_file_read_lock == 0 && h->p->no_file_write_lock == 0)
350 r = unixLock(h->p->fd, F_UNLCK, F_SETLKW);
355 zebra_mutex_unlock(&h->p->file_mutex);
360 zebra_lock_rdwr_wunlock(&h->p->rdwr_lock);
362 zebra_lock_rdwr_runlock(&h->p->rdwr_lock);
365 yaz_log(log_level, "zebra_unlock fd=%d p=%p fname=%s end",
366 h->p->fd, h, h->p->fname);
370 /** \brief see if the fcntl locking is not POSIX
372 * The default posix_locks=1 is assumed.. This function sets posix_locks
373 * to zero if linuxthreads is in use.
375 static int check_for_linuxthreads()
378 #ifdef _CS_GNU_LIBPTHREAD_VERSION
380 size_t r = confstr(_CS_GNU_LIBPTHREAD_VERSION, conf_buf, sizeof(conf_buf));
383 yaz_log(YLOG_WARN|YLOG_ERRNO, "confstr failed");
386 if (strncmp(conf_buf, "linuxthreads", 12) == 0)
387 posix_locks = 0; /* Using linuxthreads.. */
389 posix_locks = 0; /* Old GLIBC on Linux. Assume linuxthreads */
395 void zebra_flock_init()
400 log_level = yaz_log_module_level("flock");
402 log_level = YLOG_LOG|YLOG_FLUSH;
404 check_for_linuxthreads();
405 zebra_mutex_init(&lock_list_mutex);
406 yaz_log(log_level, "posix_locks: %d", posix_locks);
408 yaz_log(log_level, "zebra_flock_init");
414 * indent-tabs-mode: nil
416 * vim: shiftwidth=4 tabstop=8 expandtab