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