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