Locking based on fcntl instead of flock.
[idzebra-moved-to-github.git] / index / locksrv.c
1 /*
2  * Copyright (C) 1994-1995, Index Data I/S 
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: locksrv.c,v $
7  * Revision 1.3  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.2  1995/12/08  16:22:55  adam
13  * Work on update while servers are running. Three lock files introduced.
14  * The servers reload their registers when necessary, but they don't
15  * reestablish result sets yet.
16  *
17  * Revision 1.1  1995/12/07  17:38:47  adam
18  * Work locking mechanisms for concurrent updates/commit.
19  *
20  */
21 #include <stdio.h>
22 #include <assert.h>
23 #include <unistd.h>
24 #include <sys/stat.h>
25 #include <fcntl.h>
26 #include <string.h>
27 #include <errno.h>
28
29 #include <alexutil.h>
30 #include "zserver.h"
31
32 static int server_lock_cmt = -1;
33 static int server_lock_org = -1;
34
35 int zebraServerLock (int commitPhase)
36 {
37     char pathPrefix[1024];
38     char path[1024];
39     
40     zebraLockPrefix (pathPrefix);
41
42     if (server_lock_cmt == -1)
43     {
44         sprintf (path, "%s%s", FNAME_COMMIT_LOCK, pathPrefix);
45         if ((server_lock_cmt = open (path, O_CREAT|O_RDWR, 0666))
46             == -1)
47         {
48             logf (LOG_FATAL|LOG_ERRNO, "create %s", path);
49             return -1;
50         }
51         assert (server_lock_org == -1);
52
53         sprintf (path, "%s%s", FNAME_ORG_LOCK, pathPrefix);
54         if ((server_lock_org = open (path, O_CREAT|O_RDWR, 0666))
55             == -1)
56         {
57             logf (LOG_FATAL|LOG_ERRNO, "create %s", path);
58             return -1;
59         }
60     }
61     if (commitPhase)
62     {
63         logf (LOG_LOG, "Server locks org");
64         zebraLock (server_lock_org, 0);
65     }
66     else
67     {
68         logf (LOG_LOG, "Server locks cmt");
69         zebraLock (server_lock_cmt, 0);
70     }
71     return 0;
72 }
73
74 void zebraServerUnlock (int commitPhase)
75 {
76     if (server_lock_org == -1)
77         return;
78     if (commitPhase)
79     {
80         logf (LOG_LOG, "Server unlocks org");
81         zebraUnlock (server_lock_org);
82     }
83     else
84     {
85         logf (LOG_LOG, "Server unlocks cmt");
86         zebraUnlock (server_lock_cmt);
87     }
88 }
89
90 int zebraServerLockGetState (time_t *timep)
91 {
92     char pathPrefix[1024];
93     char path[1024];
94     char buf[256];
95     int fd;
96     struct stat xstat;
97     
98     zebraLockPrefix (pathPrefix);
99
100     sprintf (path, "%s%s", pathPrefix, FNAME_TOUCH_TIME);
101     if (stat (path, &xstat) == -1)
102         *timep = 1;
103     else
104         *timep = xstat.st_ctime;
105     
106     sprintf (path, "%s%s", pathPrefix, FNAME_MAIN_LOCK);
107     fd = open (path, O_RDONLY);
108     if (fd == -1)
109     {
110         *buf = 0;
111         return 0;
112     }
113     if (read (fd, buf, 2) == 0)
114     {
115         *buf = 0;
116         return 0;
117     }
118     close (fd);
119     return *buf;
120 }