2b17ba0fdff8b81ead9506e6453897bb298b2af4
[idzebra-moved-to-github.git] / index / locksrv.c
1 /*
2  * Copyright (C) 1994-1997, Index Data I/S 
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: locksrv.c,v $
7  * Revision 1.10  1997-09-29 09:08:36  adam
8  * Revised locking system to be thread safe for the server.
9  *
10  * Revision 1.9  1997/09/25 14:54:43  adam
11  * WIN32 files lock support.
12  *
13  * Revision 1.8  1997/09/17 12:19:15  adam
14  * Zebra version corresponds to YAZ version 1.4.
15  * Changed Zebra server so that it doesn't depend on global common_resource.
16  *
17  * Revision 1.7  1997/09/04 13:58:04  adam
18  * Added O_BINARY for open calls.
19  *
20  * Revision 1.6  1996/10/29 14:06:52  adam
21  * Include zebrautl.h instead of alexutil.h.
22  *
23  * Revision 1.5  1996/05/15 11:58:18  adam
24  * Changed some log messages.
25  *
26  * Revision 1.4  1996/04/10  16:01:27  quinn
27  * Fixed order of path/filename.
28  *
29  * Revision 1.3  1995/12/11  11:43:29  adam
30  * Locking based on fcntl instead of flock.
31  * Setting commitEnable removed. Command line option -n can be used to
32  * prevent commit if commit setting is defined in the configuration file.
33  *
34  * Revision 1.2  1995/12/08  16:22:55  adam
35  * Work on update while servers are running. Three lock files introduced.
36  * The servers reload their registers when necessary, but they don't
37  * reestablish result sets yet.
38  *
39  * Revision 1.1  1995/12/07  17:38:47  adam
40  * Work locking mechanisms for concurrent updates/commit.
41  *
42  */
43 #include <stdio.h>
44 #include <assert.h>
45 #ifdef WINDOWS
46 #include <io.h>
47 #else
48 #include <unistd.h>
49 #endif
50 #include <sys/stat.h>
51 #include <fcntl.h>
52 #include <string.h>
53 #include <errno.h>
54
55 #include "zserver.h"
56
57 int zebra_server_lock_init (ZServerInfo *zi)
58 {
59     char path_prefix[1024];
60
61     assert (zi->res);
62     zi->server_lock_cmt = NULL;
63     zi->server_lock_org = NULL;
64
65     zebra_lock_prefix (zi->res, path_prefix);
66     zi->server_path_prefix = xmalloc (strlen(path_prefix)+1);
67     strcpy (zi->server_path_prefix, path_prefix);
68
69     logf (LOG_DEBUG, "Locking system initialized");
70     return 0;
71 }
72
73 int zebra_server_lock_destroy (ZServerInfo *zi)
74 {
75     xfree (zi->server_path_prefix);
76     zebra_lock_destroy (zi->server_lock_cmt);
77     zebra_lock_destroy (zi->server_lock_org);
78     logf (LOG_DEBUG, "Locking system destroyed");
79     return 0;
80 }
81
82 int zebra_server_lock (ZServerInfo *zi, int commitPhase)
83 {
84     if (!zi->server_lock_cmt)
85     {
86         char path[1024];
87
88         strcpy (path, zi->server_path_prefix);
89         strcat (path, FNAME_COMMIT_LOCK);
90         if (!(zi->server_lock_cmt = zebra_lock_create (path, 0)))
91         {
92             logf (LOG_FATAL|LOG_ERRNO, "create %s", path);
93             return -1;
94         }
95         assert (zi->server_lock_org == NULL);
96
97         strcpy (path, zi->server_path_prefix);
98         strcat (path, FNAME_ORG_LOCK);
99         if (!(zi->server_lock_org = zebra_lock_create (path, 0)))
100         {
101             logf (LOG_FATAL|LOG_ERRNO, "create %s", path);
102             return -1;
103         }
104     }
105     if (commitPhase)
106     {
107         logf (LOG_DEBUG, "Server locks org");
108         zebra_lock (zi->server_lock_org);
109     }
110     else
111     {
112         logf (LOG_DEBUG, "Server locks cmt");
113         zebra_lock (zi->server_lock_cmt);
114     }
115     return 0;
116 }
117
118 void zebra_server_unlock (ZServerInfo *zi, int commitPhase)
119 {
120     if (zi->server_lock_org == NULL)
121         return;
122     if (commitPhase)
123     {
124         logf (LOG_DEBUG, "Server unlocks org");
125         zebra_unlock (zi->server_lock_org);
126     }
127     else
128     {
129         logf (LOG_DEBUG, "Server unlocks cmt");
130         zebra_unlock (zi->server_lock_cmt);
131     }
132 }
133
134 int zebra_server_lock_get_state (ZServerInfo *zi, time_t *timep)
135 {
136     char path[1024];
137     char buf[256];
138     int fd;
139     struct stat xstat;
140     
141     strcpy (path, zi->server_path_prefix);
142     strcat (path, FNAME_TOUCH_TIME);
143     if (stat (path, &xstat) == -1)
144         *timep = 1;
145     else
146         *timep = xstat.st_ctime;
147
148     strcpy (path, zi->server_path_prefix);
149     strcat (path, FNAME_MAIN_LOCK);
150     fd = open (path, O_BINARY|O_RDONLY);
151     if (fd == -1)
152     {
153         *buf = 0;
154         return 0;
155     }
156     if (read (fd, buf, 2) == 0)
157     {
158         *buf = 0;
159         return 0;
160     }
161     close (fd);
162     return *buf;
163 }