Use HAVE_UNISTD_H when including unistd.h.
[idzebra-moved-to-github.git] / index / lockidx.c
1 /* $Id: lockidx.c,v 1.25 2005-06-14 20:28:54 adam Exp $
2    Copyright (C) 1995-2005
3    Index Data ApS
4
5 This file is part of the Zebra server.
6
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra.  If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.
21 */
22
23
24 #include <stdio.h>
25 #include <assert.h>
26 #ifdef WIN32
27 #include <io.h>
28 #endif
29 #if HAVE_UNISTD_H
30 #include <unistd.h>
31 #endif
32 #include <fcntl.h>
33 #include <string.h>
34 #include <errno.h>
35
36 #include "index.h"
37 #include "zserver.h"
38
39 static ZebraLockHandle server_lock_main = NULL;
40 static ZebraLockHandle server_lock_cmt = NULL;
41 static ZebraLockHandle server_lock_org = NULL;
42
43 int zebraIndexWait (ZebraHandle zh, int commitPhase)
44 {
45     ZebraLockHandle h;
46
47     if (server_lock_cmt)
48         zebra_unlock (server_lock_cmt);
49     else
50     {
51         char path[1024];
52
53         zebra_lock_prefix (zh->service->res, path);
54         strcat (path, FNAME_COMMIT_LOCK);
55         server_lock_cmt = zebra_lock_create (path, 1);
56         if (!server_lock_cmt)
57         {
58             yaz_log (YLOG_WARN|YLOG_ERRNO, "cannot create lock %s", path);
59             return -1;
60         }
61     }
62     if (server_lock_org)
63         zebra_unlock (server_lock_org);
64     else
65     {
66         char path[1024];
67
68         zebra_lock_prefix (zh->service->res, path);
69         strcat (path, FNAME_ORG_LOCK);
70         server_lock_org = zebra_lock_create (path, 1);
71         if (!server_lock_org)
72         {
73             yaz_log (YLOG_WARN|YLOG_ERRNO, "cannot create lock %s", path);
74             return -1;
75         }
76     }
77     if (commitPhase)
78         h = server_lock_cmt;
79     else
80         h = server_lock_org;
81     if (zebra_lock_nb (h))
82     {
83 #ifndef WIN32
84         if (errno != EWOULDBLOCK)
85         {
86             yaz_log (YLOG_FATAL|YLOG_ERRNO, "flock");
87             exit (1);
88         }
89 #endif
90         if (commitPhase)
91             yaz_log (YLOG_LOG, "Waiting for lock cmt");
92         else
93             yaz_log (YLOG_LOG, "Waiting for lock org");
94         if (zebra_lock (h) == -1)
95         {
96             yaz_log (YLOG_FATAL, "flock");
97             exit (1);
98         }
99     }
100     zebra_unlock (h);
101     return 0;
102 }
103
104
105 void zebraIndexLockMsg (ZebraHandle zh, const char *str)
106 {
107     char path[1024];
108     int l, r, fd;
109
110     assert (server_lock_main);
111     fd = zebra_lock_fd (server_lock_main);
112     lseek (fd, 0L, SEEK_SET);
113     l = strlen(str);
114     r = write (fd, str, l);
115     if (r != l)
116     {
117         yaz_log (YLOG_FATAL|YLOG_ERRNO, "write lock file");
118         exit (1);
119     }
120     zebra_lock_prefix (zh->service->res, path);
121     strcat (path, FNAME_TOUCH_TIME);
122     fd = creat (path, 0666);
123     close (fd);
124 }
125
126 void zebraIndexUnlock (ZebraHandle zh)
127 {
128     char path[1024];
129     
130     zebra_lock_prefix (zh->service->res, path);
131     strcat (path, FNAME_MAIN_LOCK);
132 #ifdef WIN32
133     zebra_lock_destroy (server_lock_main);
134     if (unlink (path) && errno != ENOENT)
135         yaz_log (YLOG_WARN|YLOG_ERRNO, "unlink %s failed", path);
136 #else
137     if (unlink (path) && errno != ENOENT)
138         yaz_log (YLOG_WARN|YLOG_ERRNO, "unlink %s failed", path);
139     zebra_lock_destroy (server_lock_main);
140 #endif
141     server_lock_main = 0;
142 }
143
144 int zebraIndexLock (BFiles bfs, ZebraHandle zh, int commitNow,
145                     const char *rval)
146 {
147     char path[1024];
148     char buf[256];
149     int r;
150
151     if (server_lock_main)
152         return 0;
153
154     zebra_lock_prefix (zh->service->res, path);
155     strcat (path, FNAME_MAIN_LOCK);
156     while (1)
157     {
158         server_lock_main = zebra_lock_create (path, 2);
159         if (!server_lock_main)
160         {
161             server_lock_main = zebra_lock_create (path, 1);
162             if (!server_lock_main)
163             {
164                 yaz_log (YLOG_FATAL, "couldn't obtain indexer lock");
165                 exit (1);
166             }
167             if (zebra_lock_nb (server_lock_main) == -1)
168             {
169 #ifdef WIN32
170                 yaz_log (YLOG_LOG, "waiting for other index process");
171                 zebra_lock (server_lock_main);
172                 zebra_unlock (server_lock_main);
173                 zebra_lock_destroy (server_lock_main);
174                 continue;
175 #else
176                 if (errno == EWOULDBLOCK)
177                 {
178                     yaz_log (YLOG_LOG, "waiting for other index process");
179                     zebra_lock (server_lock_main);
180                     zebra_unlock (server_lock_main);
181                     zebra_lock_destroy (server_lock_main);
182                     continue;
183                 }
184                 else
185                 {
186                     yaz_log (YLOG_FATAL|YLOG_ERRNO, "flock %s", path);
187                     exit (1);
188                 }
189 #endif
190             }
191             else
192             {
193                 int fd = zebra_lock_fd (server_lock_main);
194
195                 yaz_log (YLOG_WARN, "unlocked %s", path);
196                 r = read (fd, buf, 256);
197                 if (r == 0)
198                 {
199                     yaz_log (YLOG_WARN, "zero length %s", path);
200                     zebra_lock_destroy (server_lock_main);
201                     unlink (path);
202                     continue;
203                 }
204                 else if (r == -1)
205                 {
206                     yaz_log (YLOG_FATAL|YLOG_ERRNO, "read %s", path);
207                     exit (1);
208                 }
209                 if (*buf == 'r')
210                 {
211                     yaz_log (YLOG_WARN, "previous transaction didn't"
212                           " reach commit");
213                     zebra_lock_destroy (server_lock_main);
214                     bf_commitClean (bfs, rval);
215                     unlink (path);
216                     continue;
217                 }
218                 else if (*buf == 'd')
219                 {
220                     yaz_log (YLOG_WARN, "commit file wan't deleted after commit");
221                     zebra_lock_destroy (server_lock_main);
222                     bf_commitClean (bfs, rval);
223                     unlink (path);
224                     continue;
225                 }                    
226                 else if (*buf == 'w')
227                 {
228                     yaz_log (YLOG_WARN,
229                           "The lock file indicates that your index is");
230                     yaz_log (YLOG_WARN, "inconsistent. Perhaps the indexer");
231                     yaz_log (YLOG_WARN, "terminated abnormally in the previous");
232                     yaz_log (YLOG_WARN, "run. You can try to proceed by");
233                     yaz_log (YLOG_WARN, "deleting the file %s", path);
234                     exit (1);
235                 }
236                 else if (*buf == 'c')
237                 {
238                     if (commitNow)
239                     {
240                         unlink (path);
241                         zebra_lock_destroy (server_lock_main);
242                         continue;
243                     }
244                     yaz_log (YLOG_FATAL, "previous transaction didn't"
245                           " finish commit. Commit now!");
246                     exit (1);
247                 }
248                 else 
249                 {
250                     yaz_log (YLOG_FATAL, "unknown id 0x%02x in %s", *buf,
251                           path);
252                     exit (1);
253                 }
254             }
255         }
256         else
257             break;
258     }
259     zebra_lock (server_lock_main);
260     return 0;
261 }
262