Work on update while servers are running. Three lock files introduced.
[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.2  1995-12-08 16:22:55  adam
8  * Work on update while servers are running. Three lock files introduced.
9  * The servers reload their registers when necessary, but they don't
10  * reestablish result sets yet.
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 <unistd.h>
19 #include <sys/file.h>
20 #include <sys/stat.h>
21 #include <fcntl.h>
22 #include <string.h>
23 #include <errno.h>
24
25 #include <alexutil.h>
26 #include "zserver.h"
27
28 static int server_lock_cmt = -1;
29 static int server_lock_org = -1;
30
31 int zebraServerLock (int commitPhase)
32 {
33     char pathPrefix[1024];
34     char path[1024];
35     
36     zebraLockPrefix (pathPrefix);
37
38     if (server_lock_cmt == -1)
39     {
40         sprintf (path, "%s%s", FNAME_COMMIT_LOCK, pathPrefix);
41         if ((server_lock_cmt = open (path, O_CREAT|O_RDWR, 0666))
42             == -1)
43         {
44             logf (LOG_FATAL|LOG_ERRNO, "create %s", path);
45             return -1;
46         }
47         assert (server_lock_org == -1);
48
49         sprintf (path, "%s%s", FNAME_ORG_LOCK, pathPrefix);
50         if ((server_lock_org = open (path, O_CREAT|O_RDWR, 0666))
51             == -1)
52         {
53             logf (LOG_FATAL|LOG_ERRNO, "create %s", path);
54             return -1;
55         }
56     }
57     if (commitPhase)
58     {
59         logf (LOG_LOG, "Server locks org");
60         flock (server_lock_org, LOCK_SH);
61     }
62     else
63     {
64         logf (LOG_LOG, "Server locks cmt");
65         flock (server_lock_cmt, LOCK_SH);
66     }
67     return 0;
68 }
69
70 void zebraServerUnlock (int commitPhase)
71 {
72     if (server_lock_org == -1)
73         return;
74     if (commitPhase)
75     {
76         logf (LOG_LOG, "Server unlocks org");
77         flock (server_lock_org, LOCK_UN);
78     }
79     else
80     {
81         logf (LOG_LOG, "Server unlocks cmt");
82         flock (server_lock_cmt, LOCK_UN);
83     }
84 }
85
86 int zebraServerLockGetState (time_t *timep)
87 {
88     char pathPrefix[1024];
89     char path[1024];
90     char buf[256];
91     int fd;
92     struct stat xstat;
93     
94     zebraLockPrefix (pathPrefix);
95
96     sprintf (path, "%s%s", pathPrefix, FNAME_TOUCH_TIME);
97     if (stat (path, &xstat) == -1)
98         *timep = 1;
99     else
100         *timep = xstat.st_ctime;
101     
102     sprintf (path, "%s%s", pathPrefix, FNAME_MAIN_LOCK);
103     fd = open (path, O_RDONLY);
104     if (fd == -1)
105     {
106         *buf = 0;
107         return 0;
108     }
109     if (read (fd, buf, 2) == 0)
110     {
111         *buf = 0;
112         return 0;
113     }
114     close (fd);
115     return *buf;
116 }