2 * Copyright (C) 1994-1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.12 1997-09-25 14:54:43 adam
8 * WIN32 files lock support.
10 * Revision 1.11 1997/09/17 12:19:15 adam
11 * Zebra version corresponds to YAZ version 1.4.
12 * Changed Zebra server so that it doesn't depend on global common_resource.
14 * Revision 1.10 1997/09/09 13:38:07 adam
15 * Partial port to WIN95/NT.
17 * Revision 1.9 1997/09/04 13:58:04 adam
18 * Added O_BINARY for open calls.
20 * Revision 1.8 1997/02/12 20:39:46 adam
21 * Implemented options -f <n> that limits the log to the first <n>
23 * Changed some log messages also.
25 * Revision 1.7 1996/10/29 14:08:13 adam
26 * Uses resource lockDir instead of lockPath.
28 * Revision 1.6 1996/03/26 16:01:13 adam
29 * New setting lockPath: directory of various lock files.
31 * Revision 1.5 1995/12/13 08:46:09 adam
32 * Locking uses F_WRLCK and F_RDLCK again!
34 * Revision 1.4 1995/12/12 16:00:57 adam
35 * System call sync(2) used after update/commit.
36 * Locking (based on fcntl) uses F_EXLCK and F_SHLCK instead of F_WRLCK
39 * Revision 1.3 1995/12/11 11:43:29 adam
40 * Locking based on fcntl instead of flock.
41 * Setting commitEnable removed. Command line option -n can be used to
42 * prevent commit if commit setting is defined in the configuration file.
44 * Revision 1.2 1995/12/08 16:22:54 adam
45 * Work on update while servers are running. Three lock files introduced.
46 * The servers reload their registers when necessary, but they don't
47 * reestablish result sets yet.
49 * Revision 1.1 1995/12/07 17:38:47 adam
50 * Work locking mechanisms for concurrent updates/commit.
66 static ZebraLockHandle server_lock_main = NULL;
67 static ZebraLockHandle server_lock_cmt = NULL;
68 static ZebraLockHandle server_lock_org = NULL;
70 int zebraIndexWait (int commitPhase)
72 char pathPrefix[1024];
76 zebraLockPrefix (common_resource, pathPrefix);
79 zebra_unlock (server_lock_cmt);
82 sprintf (path, "%s%s", pathPrefix, FNAME_COMMIT_LOCK);
83 server_lock_cmt = zebra_lock_create (path, 1);
86 logf (LOG_WARN|LOG_ERRNO, "cannot create lock %s", path);
91 zebra_unlock (server_lock_org);
94 sprintf (path, "%s%s", pathPrefix, FNAME_ORG_LOCK);
95 server_lock_org = zebra_lock_create (path, 1);
98 logf (LOG_WARN|LOG_ERRNO, "cannot create lock %s", path);
106 if (zebra_lock_nb (h))
109 if (errno != EWOULDBLOCK)
111 logf (LOG_FATAL|LOG_ERRNO, "flock");
116 logf (LOG_LOG, "Waiting for lock cmt");
118 logf (LOG_LOG, "Waiting for lock org");
119 if (zebra_lock (h) == -1)
121 logf (LOG_FATAL, "flock");
130 void zebraIndexLockMsg (const char *str)
133 char pathPrefix[1024];
136 assert (server_lock_main);
137 fd = zebra_lock_fd (server_lock_main);
138 lseek (fd, 0L, SEEK_SET);
140 r = write (fd, str, l);
143 logf (LOG_FATAL|LOG_ERRNO, "write lock file");
146 zebraLockPrefix (common_resource, pathPrefix);
147 sprintf (path, "%s%s", pathPrefix, FNAME_TOUCH_TIME);
148 fd = creat (path, 0666);
152 void zebraIndexUnlock (void)
155 char pathPrefix[1024];
157 zebraLockPrefix (common_resource, pathPrefix);
158 sprintf (path, "%s%s", pathPrefix, FNAME_MAIN_LOCK);
162 void zebraIndexLock (BFiles bfs, int commitNow, const char *rval)
165 char pathPrefix[1024];
169 if (server_lock_main)
171 zebraLockPrefix (common_resource, pathPrefix);
172 sprintf (path, "%s%s", pathPrefix, FNAME_MAIN_LOCK);
175 server_lock_main = zebra_lock_create (path, 2);
176 if (!server_lock_main)
178 server_lock_main = zebra_lock_create (path, 1);
179 if (!server_lock_main)
183 logf (LOG_FATAL|LOG_ERRNO, "open %s", path);
186 if (zebra_lock_nb (server_lock_main) == -1)
189 logf (LOG_LOG, "waiting for other index process");
190 zebra_lock (server_lock_main);
191 zebra_unlock (server_lock_main);
192 zebra_lock_destroy (server_lock_main);
195 if (errno == EWOULDBLOCK)
197 logf (LOG_LOG, "waiting for other index process");
198 zebra_lock (server_lock_main);
199 zebra_unlock (server_lock_main);
200 zebra_lock_destroy (server_lock_main);
205 logf (LOG_FATAL|LOG_ERRNO, "flock %s", path);
212 int fd = zebra_lock_fd (server_lock_main);
214 logf (LOG_WARN, "unlocked %s", path);
215 r = read (fd, buf, 256);
218 logf (LOG_WARN, "zero length %s", path);
219 zebra_lock_destroy (server_lock_main);
225 logf (LOG_FATAL|LOG_ERRNO, "read %s", path);
230 logf (LOG_WARN, "previous transaction didn't"
232 zebra_lock_destroy (server_lock_main);
233 bf_commitClean (bfs, rval);
237 else if (*buf == 'd')
239 logf (LOG_WARN, "commit file wan't deleted after commit");
240 zebra_lock_destroy (server_lock_main);
241 bf_commitClean (bfs, rval);
245 else if (*buf == 'w')
247 logf (LOG_WARN, "your index may be inconsistent");
250 else if (*buf == 'c')
255 zebra_lock_destroy (server_lock_main);
258 logf (LOG_FATAL, "previous transaction didn't"
259 " finish commit. Commit now!");
264 logf (LOG_FATAL, "unknown id 0x%02x in %s", *buf,
273 zebra_lock (server_lock_main);