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