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