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