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