2 * Copyright (C) 1994-1998, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.15 1998-02-17 10:31:33 adam
8 * Fixed bug in zebraIndexUnlock. On NT, the lock files wasn't removed.
10 * Revision 1.14 1998/01/12 15:04:08 adam
11 * The test option (-s) only uses read-lock (and not write lock).
13 * Revision 1.13 1997/09/29 09:08:36 adam
14 * Revised locking system to be thread safe for the server.
16 * Revision 1.12 1997/09/25 14:54:43 adam
17 * WIN32 files lock support.
19 * Revision 1.11 1997/09/17 12:19:15 adam
20 * Zebra version corresponds to YAZ version 1.4.
21 * Changed Zebra server so that it doesn't depend on global common_resource.
23 * Revision 1.10 1997/09/09 13:38:07 adam
24 * Partial port to WIN95/NT.
26 * Revision 1.9 1997/09/04 13:58:04 adam
27 * Added O_BINARY for open calls.
29 * Revision 1.8 1997/02/12 20:39:46 adam
30 * Implemented options -f <n> that limits the log to the first <n>
32 * Changed some log messages also.
34 * Revision 1.7 1996/10/29 14:08:13 adam
35 * Uses resource lockDir instead of lockPath.
37 * Revision 1.6 1996/03/26 16:01:13 adam
38 * New setting lockPath: directory of various lock files.
40 * Revision 1.5 1995/12/13 08:46:09 adam
41 * Locking uses F_WRLCK and F_RDLCK again!
43 * Revision 1.4 1995/12/12 16:00:57 adam
44 * System call sync(2) used after update/commit.
45 * Locking (based on fcntl) uses F_EXLCK and F_SHLCK instead of F_WRLCK
48 * Revision 1.3 1995/12/11 11:43:29 adam
49 * Locking based on fcntl instead of flock.
50 * Setting commitEnable removed. Command line option -n can be used to
51 * prevent commit if commit setting is defined in the configuration file.
53 * Revision 1.2 1995/12/08 16:22:54 adam
54 * Work on update while servers are running. Three lock files introduced.
55 * The servers reload their registers when necessary, but they don't
56 * reestablish result sets yet.
58 * Revision 1.1 1995/12/07 17:38:47 adam
59 * Work locking mechanisms for concurrent updates/commit.
75 static ZebraLockHandle server_lock_main = NULL;
76 static ZebraLockHandle server_lock_cmt = NULL;
77 static ZebraLockHandle server_lock_org = NULL;
79 int zebraIndexWait (int commitPhase)
84 zebra_unlock (server_lock_cmt);
89 zebra_lock_prefix (common_resource, path);
90 strcat (path, FNAME_COMMIT_LOCK);
91 server_lock_cmt = zebra_lock_create (path, 1);
94 logf (LOG_WARN|LOG_ERRNO, "cannot create lock %s", path);
99 zebra_unlock (server_lock_org);
104 zebra_lock_prefix (common_resource, path);
105 strcat (path, FNAME_ORG_LOCK);
106 server_lock_org = zebra_lock_create (path, 1);
107 if (!server_lock_org)
109 logf (LOG_WARN|LOG_ERRNO, "cannot create lock %s", path);
117 if (zebra_lock_nb (h))
120 if (errno != EWOULDBLOCK)
122 logf (LOG_FATAL|LOG_ERRNO, "flock");
127 logf (LOG_LOG, "Waiting for lock cmt");
129 logf (LOG_LOG, "Waiting for lock org");
130 if (zebra_lock (h) == -1)
132 logf (LOG_FATAL, "flock");
141 void zebraIndexLockMsg (const char *str)
146 assert (server_lock_main);
147 fd = zebra_lock_fd (server_lock_main);
148 lseek (fd, 0L, SEEK_SET);
150 r = write (fd, str, l);
153 logf (LOG_FATAL|LOG_ERRNO, "write lock file");
156 zebra_lock_prefix (common_resource, path);
157 strcat (path, FNAME_TOUCH_TIME);
158 fd = creat (path, 0666);
162 void zebraIndexUnlock (void)
166 zebra_lock_destroy (server_lock_main);
167 zebra_lock_prefix (common_resource, path);
168 strcat (path, FNAME_MAIN_LOCK);
170 logf (LOG_WARN|LOG_ERRNO, "unlink %s", path);
173 void zebraIndexLock (BFiles bfs, int commitNow, const char *rval)
179 if (server_lock_main)
182 zebra_lock_prefix (common_resource, path);
183 strcat (path, FNAME_MAIN_LOCK);
186 server_lock_main = zebra_lock_create (path, 2);
187 if (!server_lock_main)
189 server_lock_main = zebra_lock_create (path, 1);
190 if (!server_lock_main)
194 logf (LOG_FATAL|LOG_ERRNO, "open %s", path);
197 if (zebra_lock_nb (server_lock_main) == -1)
200 logf (LOG_LOG, "waiting for other index process");
201 zebra_lock (server_lock_main);
202 zebra_unlock (server_lock_main);
203 zebra_lock_destroy (server_lock_main);
206 if (errno == EWOULDBLOCK)
208 logf (LOG_LOG, "waiting for other index process");
209 zebra_lock (server_lock_main);
210 zebra_unlock (server_lock_main);
211 zebra_lock_destroy (server_lock_main);
216 logf (LOG_FATAL|LOG_ERRNO, "flock %s", path);
223 int fd = zebra_lock_fd (server_lock_main);
225 logf (LOG_WARN, "unlocked %s", path);
226 r = read (fd, buf, 256);
229 logf (LOG_WARN, "zero length %s", path);
230 zebra_lock_destroy (server_lock_main);
236 logf (LOG_FATAL|LOG_ERRNO, "read %s", path);
241 logf (LOG_WARN, "previous transaction didn't"
243 zebra_lock_destroy (server_lock_main);
244 bf_commitClean (bfs, rval);
248 else if (*buf == 'd')
250 logf (LOG_WARN, "commit file wan't deleted after commit");
251 zebra_lock_destroy (server_lock_main);
252 bf_commitClean (bfs, rval);
256 else if (*buf == 'w')
259 "The lock file indicates that your index is");
260 logf (LOG_WARN, "inconsistent. Perhaps the indexer");
261 logf (LOG_WARN, "terminated abnormally in the previous");
262 logf (LOG_WARN, "run. You can try to proceed by");
263 logf (LOG_WARN, "deleting the file %s", path);
266 else if (*buf == 'c')
271 zebra_lock_destroy (server_lock_main);
274 logf (LOG_FATAL, "previous transaction didn't"
275 " finish commit. Commit now!");
280 logf (LOG_FATAL, "unknown id 0x%02x in %s", *buf,
289 zebra_lock (server_lock_main);