2 * Copyright (C) 1994-1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.11 1997-09-17 12:19:15 adam
8 * Zebra version corresponds to YAZ version 1.4.
9 * Changed Zebra server so that it doesn't depend on global common_resource.
11 * Revision 1.10 1997/09/09 13:38:07 adam
12 * Partial port to WIN95/NT.
14 * Revision 1.9 1997/09/04 13:58:04 adam
15 * Added O_BINARY for open calls.
17 * Revision 1.8 1997/02/12 20:39:46 adam
18 * Implemented options -f <n> that limits the log to the first <n>
20 * Changed some log messages also.
22 * Revision 1.7 1996/10/29 14:08:13 adam
23 * Uses resource lockDir instead of lockPath.
25 * Revision 1.6 1996/03/26 16:01:13 adam
26 * New setting lockPath: directory of various lock files.
28 * Revision 1.5 1995/12/13 08:46:09 adam
29 * Locking uses F_WRLCK and F_RDLCK again!
31 * Revision 1.4 1995/12/12 16:00:57 adam
32 * System call sync(2) used after update/commit.
33 * Locking (based on fcntl) uses F_EXLCK and F_SHLCK instead of F_WRLCK
36 * Revision 1.3 1995/12/11 11:43:29 adam
37 * Locking based on fcntl instead of flock.
38 * Setting commitEnable removed. Command line option -n can be used to
39 * prevent commit if commit setting is defined in the configuration file.
41 * Revision 1.2 1995/12/08 16:22:54 adam
42 * Work on update while servers are running. Three lock files introduced.
43 * The servers reload their registers when necessary, but they don't
44 * reestablish result sets yet.
46 * Revision 1.1 1995/12/07 17:38:47 adam
47 * Work locking mechanisms for concurrent updates/commit.
63 static int lock_fd = -1;
64 static int server_lock_cmt = -1;
65 static int server_lock_org = -1;
67 int zebraIndexWait (int commitPhase)
69 char pathPrefix[1024];
73 zebraLockPrefix (common_resource, pathPrefix);
75 if (server_lock_cmt == -1)
77 sprintf (path, "%s%s", pathPrefix, FNAME_COMMIT_LOCK);
78 if ((server_lock_cmt = open (path, O_BINARY|O_CREAT|O_RDWR|O_SYNC,
82 logf (LOG_FATAL|LOG_ERRNO, "create %s", path);
87 zebraUnlock (server_lock_cmt);
88 if (server_lock_org == -1)
90 sprintf (path, "%s%s", pathPrefix, FNAME_ORG_LOCK);
91 if ((server_lock_org = open (path, O_BINARY|O_CREAT|O_RDWR|O_SYNC,
95 logf (LOG_FATAL|LOG_ERRNO, "create %s", path);
100 zebraUnlock (server_lock_org);
102 fd = server_lock_cmt;
104 fd = server_lock_org;
105 if (zebraLockNB (fd, 1) == -1)
107 if (errno != EWOULDBLOCK)
109 logf (LOG_FATAL|LOG_ERRNO, "flock");
113 logf (LOG_LOG, "Waiting for lock cmt");
115 logf (LOG_LOG, "Waiting for lock org");
116 if (zebraLock (fd, 1) == -1)
118 logf (LOG_FATAL|LOG_ERRNO, "flock");
127 void zebraIndexLockMsg (const char *str)
130 char pathPrefix[1024];
133 assert (lock_fd != -1);
134 lseek (lock_fd, 0L, SEEK_SET);
136 r = write (lock_fd, str, l);
139 logf (LOG_FATAL|LOG_ERRNO, "write lock file");
142 zebraLockPrefix (common_resource, pathPrefix);
143 sprintf (path, "%s%s", pathPrefix, FNAME_TOUCH_TIME);
144 fd = creat (path, 0666);
148 void zebraIndexUnlock (void)
151 char pathPrefix[1024];
153 zebraLockPrefix (common_resource, pathPrefix);
154 sprintf (path, "%s%s", pathPrefix, FNAME_MAIN_LOCK);
158 void zebraIndexLock (BFiles bfs, int commitNow, const char *rval)
161 char pathPrefix[1024];
167 zebraLockPrefix (common_resource, pathPrefix);
168 sprintf (path, "%s%s", pathPrefix, FNAME_MAIN_LOCK);
171 lock_fd = open (path, O_BINARY|O_CREAT|O_RDWR|O_EXCL, 0666);
174 lock_fd = open (path, O_BINARY|O_RDWR);
179 logf (LOG_FATAL|LOG_ERRNO, "open %s", path);
182 if (zebraLockNB (lock_fd, 1) == -1)
184 if (errno == EWOULDBLOCK)
186 logf (LOG_LOG, "waiting for other index process");
187 zebraLock (lock_fd, 1);
188 zebraUnlock (lock_fd);
194 logf (LOG_FATAL|LOG_ERRNO, "flock %s", path);
200 logf (LOG_WARN, "unlocked %s", path);
201 r = read (lock_fd, buf, 256);
204 logf (LOG_WARN, "zero length %s", path);
211 logf (LOG_FATAL|LOG_ERRNO, "read %s", path);
216 logf (LOG_WARN, "previous transaction didn't"
219 bf_commitClean (bfs, rval);
223 else if (*buf == 'd')
225 logf (LOG_WARN, "commit file wan't deleted after commit");
227 bf_commitClean (bfs, rval);
231 else if (*buf == 'w')
233 logf (LOG_WARN, "your index may be inconsistent");
236 else if (*buf == 'c')
244 logf (LOG_FATAL, "previous transaction didn't"
245 " finish commit. Commit now!");
250 logf (LOG_FATAL, "unknown id 0x%02x in %s", *buf,
259 zebraLock (lock_fd, 1);