2 * Copyright (C) 1994-1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.13 1997-09-29 09:08:36 adam
8 * Revised locking system to be thread safe for the server.
10 * Revision 1.12 1997/09/25 14:54:43 adam
11 * WIN32 files lock support.
13 * Revision 1.11 1997/09/17 12:19:15 adam
14 * Zebra version corresponds to YAZ version 1.4.
15 * Changed Zebra server so that it doesn't depend on global common_resource.
17 * Revision 1.10 1997/09/09 13:38:07 adam
18 * Partial port to WIN95/NT.
20 * Revision 1.9 1997/09/04 13:58:04 adam
21 * Added O_BINARY for open calls.
23 * Revision 1.8 1997/02/12 20:39:46 adam
24 * Implemented options -f <n> that limits the log to the first <n>
26 * Changed some log messages also.
28 * Revision 1.7 1996/10/29 14:08:13 adam
29 * Uses resource lockDir instead of lockPath.
31 * Revision 1.6 1996/03/26 16:01:13 adam
32 * New setting lockPath: directory of various lock files.
34 * Revision 1.5 1995/12/13 08:46:09 adam
35 * Locking uses F_WRLCK and F_RDLCK again!
37 * Revision 1.4 1995/12/12 16:00:57 adam
38 * System call sync(2) used after update/commit.
39 * Locking (based on fcntl) uses F_EXLCK and F_SHLCK instead of F_WRLCK
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.
47 * Revision 1.2 1995/12/08 16:22:54 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.
52 * Revision 1.1 1995/12/07 17:38:47 adam
53 * Work locking mechanisms for concurrent updates/commit.
69 static ZebraLockHandle server_lock_main = NULL;
70 static ZebraLockHandle server_lock_cmt = NULL;
71 static ZebraLockHandle server_lock_org = NULL;
73 int zebraIndexWait (int commitPhase)
78 zebra_unlock (server_lock_cmt);
83 zebra_lock_prefix (common_resource, path);
84 strcat (path, FNAME_COMMIT_LOCK);
85 server_lock_cmt = zebra_lock_create (path, 1);
88 logf (LOG_WARN|LOG_ERRNO, "cannot create lock %s", path);
93 zebra_unlock (server_lock_org);
98 zebra_lock_prefix (common_resource, path);
99 strcat (path, FNAME_ORG_LOCK);
100 server_lock_org = zebra_lock_create (path, 1);
101 if (!server_lock_org)
103 logf (LOG_WARN|LOG_ERRNO, "cannot create lock %s", path);
111 if (zebra_lock_nb (h))
114 if (errno != EWOULDBLOCK)
116 logf (LOG_FATAL|LOG_ERRNO, "flock");
121 logf (LOG_LOG, "Waiting for lock cmt");
123 logf (LOG_LOG, "Waiting for lock org");
124 if (zebra_lock (h) == -1)
126 logf (LOG_FATAL, "flock");
135 void zebraIndexLockMsg (const char *str)
140 assert (server_lock_main);
141 fd = zebra_lock_fd (server_lock_main);
142 lseek (fd, 0L, SEEK_SET);
144 r = write (fd, str, l);
147 logf (LOG_FATAL|LOG_ERRNO, "write lock file");
150 zebra_lock_prefix (common_resource, path);
151 strcat (path, FNAME_TOUCH_TIME);
152 fd = creat (path, 0666);
156 void zebraIndexUnlock (void)
160 zebra_lock_prefix (common_resource, path);
161 strcat (path, FNAME_MAIN_LOCK);
165 void zebraIndexLock (BFiles bfs, int commitNow, const char *rval)
171 if (server_lock_main)
174 zebra_lock_prefix (common_resource, path);
175 strcat (path, FNAME_MAIN_LOCK);
178 server_lock_main = zebra_lock_create (path, 2);
179 if (!server_lock_main)
181 server_lock_main = zebra_lock_create (path, 1);
182 if (!server_lock_main)
186 logf (LOG_FATAL|LOG_ERRNO, "open %s", path);
189 if (zebra_lock_nb (server_lock_main) == -1)
192 logf (LOG_LOG, "waiting for other index process");
193 zebra_lock (server_lock_main);
194 zebra_unlock (server_lock_main);
195 zebra_lock_destroy (server_lock_main);
198 if (errno == EWOULDBLOCK)
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);
208 logf (LOG_FATAL|LOG_ERRNO, "flock %s", path);
215 int fd = zebra_lock_fd (server_lock_main);
217 logf (LOG_WARN, "unlocked %s", path);
218 r = read (fd, buf, 256);
221 logf (LOG_WARN, "zero length %s", path);
222 zebra_lock_destroy (server_lock_main);
228 logf (LOG_FATAL|LOG_ERRNO, "read %s", path);
233 logf (LOG_WARN, "previous transaction didn't"
235 zebra_lock_destroy (server_lock_main);
236 bf_commitClean (bfs, rval);
240 else if (*buf == 'd')
242 logf (LOG_WARN, "commit file wan't deleted after commit");
243 zebra_lock_destroy (server_lock_main);
244 bf_commitClean (bfs, rval);
248 else if (*buf == 'w')
250 logf (LOG_WARN, "your index may be inconsistent");
253 else if (*buf == 'c')
258 zebra_lock_destroy (server_lock_main);
261 logf (LOG_FATAL, "previous transaction didn't"
262 " finish commit. Commit now!");
267 logf (LOG_FATAL, "unknown id 0x%02x in %s", *buf,
276 zebra_lock (server_lock_main);