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