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