Locking based on fcntl instead of flock.
[idzebra-moved-to-github.git] / index / lockutil.c
1 /*
2  * Copyright (C) 1994-1995, Index Data I/S 
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: lockutil.c,v $
7  * Revision 1.2  1995-12-11 11:43:29  adam
8  * Locking based on fcntl instead of flock.
9  * Setting commitEnable removed. Command line option -n can be used to
10  * prevent commit if commit setting is defined in the configuration file.
11  *
12  * Revision 1.1  1995/12/07  17:38:47  adam
13  * Work locking mechanisms for concurrent updates/commit.
14  *
15  */
16 #include <stdio.h>
17 #include <assert.h>
18 #include <string.h>
19 #include <errno.h>
20 #include <fcntl.h>
21 #include <sys/types.h>
22 #include <unistd.h>
23
24 #include <alexutil.h>
25 #include "index.h"
26
27 static char *lockDir = NULL;
28
29 void zebraLockPrefix (char *pathPrefix)
30 {
31     if (!lockDir)
32         lockDir = res_get_def (common_resource, "lockDir", "");
33     assert (lockDir);
34     
35     strcpy (pathPrefix, lockDir);
36     if (*pathPrefix && pathPrefix[strlen(pathPrefix)-1] != '/')
37         strcat (pathPrefix, "/");
38 }
39
40 static int intLock (int fd, int type, int cmd)
41 {
42     struct flock area;
43     area.l_type = type;
44     area.l_whence = SEEK_SET;
45     area.l_len = area.l_start = 0L;
46     return fcntl (fd, cmd, &area);
47 }
48
49 int zebraLock (int fd, int wr)
50 {
51     return intLock (fd, wr ? F_WRLCK : F_RDLCK, F_SETLKW);
52 }
53
54 int zebraLockNB (int fd, int wr)
55 {
56     return intLock (fd, wr ? F_WRLCK : F_RDLCK, F_SETLK);
57 }
58
59 int zebraUnlock (int fd)
60 {
61     return intLock (fd, F_UNLCK, F_SETLKW);
62 }