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