2 * Copyright (C) 1994-1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.6 1996-10-29 14:08:14 adam
8 * Uses resource lockDir instead of lockPath.
10 * Revision 1.5 1996/03/26 16:01:13 adam
11 * New setting lockPath: directory of various lock files.
13 * Revision 1.4 1995/12/13 08:46:10 adam
14 * Locking uses F_WRLCK and F_RDLCK again!
16 * Revision 1.3 1995/12/12 16:00:57 adam
17 * System call sync(2) used after update/commit.
18 * Locking (based on fcntl) uses F_EXLCK and F_SHLCK instead of F_WRLCK
21 * Revision 1.2 1995/12/11 11:43:29 adam
22 * Locking based on fcntl instead of flock.
23 * Setting commitEnable removed. Command line option -n can be used to
24 * prevent commit if commit setting is defined in the configuration file.
26 * Revision 1.1 1995/12/07 17:38:47 adam
27 * Work locking mechanisms for concurrent updates/commit.
35 #include <sys/types.h>
40 static char *lockDir = NULL;
42 void zebraLockPrefix (char *pathPrefix)
45 lockDir = res_get_def (common_resource, "lockDir", "");
48 strcpy (pathPrefix, lockDir);
49 if (*pathPrefix && pathPrefix[strlen(pathPrefix)-1] != '/')
50 strcat (pathPrefix, "/");
53 static int intLock (int fd, int type, int cmd)
57 area.l_whence = SEEK_SET;
58 area.l_len = area.l_start = 0L;
59 return fcntl (fd, cmd, &area);
62 int zebraLock (int fd, int wr)
65 return intLock (fd, wr ? F_EXLCK : F_SHLCK, F_SETLKW);
67 return intLock (fd, wr ? F_WRLCK : F_RDLCK, F_SETLKW);
71 int zebraLockNB (int fd, int wr)
74 return intLock (fd, wr ? F_EXLCK : F_SHLCK, F_SETLK);
76 return intLock (fd, wr ? F_WRLCK : F_RDLCK, F_SETLK);
80 int zebraUnlock (int fd)
82 return intLock (fd, F_UNLCK, F_SETLKW);