2 * Copyright (C) 1994-1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.10 1997-09-09 13:38:07 adam
8 * Partial port to WIN95/NT.
10 * Revision 1.9 1997/09/04 13:58:04 adam
11 * Added O_BINARY for open calls.
13 * Revision 1.8 1997/02/12 20:39:46 adam
14 * Implemented options -f <n> that limits the log to the first <n>
16 * Changed some log messages also.
18 * Revision 1.7 1996/10/29 14:08:13 adam
19 * Uses resource lockDir instead of lockPath.
21 * Revision 1.6 1996/03/26 16:01:13 adam
22 * New setting lockPath: directory of various lock files.
24 * Revision 1.5 1995/12/13 08:46:09 adam
25 * Locking uses F_WRLCK and F_RDLCK again!
27 * Revision 1.4 1995/12/12 16:00:57 adam
28 * System call sync(2) used after update/commit.
29 * Locking (based on fcntl) uses F_EXLCK and F_SHLCK instead of F_WRLCK
32 * Revision 1.3 1995/12/11 11:43:29 adam
33 * Locking based on fcntl instead of flock.
34 * Setting commitEnable removed. Command line option -n can be used to
35 * prevent commit if commit setting is defined in the configuration file.
37 * Revision 1.2 1995/12/08 16:22:54 adam
38 * Work on update while servers are running. Three lock files introduced.
39 * The servers reload their registers when necessary, but they don't
40 * reestablish result sets yet.
42 * Revision 1.1 1995/12/07 17:38:47 adam
43 * Work locking mechanisms for concurrent updates/commit.
59 static int lock_fd = -1;
60 static int server_lock_cmt = -1;
61 static int server_lock_org = -1;
63 int zebraIndexWait (int commitPhase)
65 char pathPrefix[1024];
69 zebraLockPrefix (pathPrefix);
71 if (server_lock_cmt == -1)
73 sprintf (path, "%s%s", pathPrefix, FNAME_COMMIT_LOCK);
74 if ((server_lock_cmt = open (path, O_BINARY|O_CREAT|O_RDWR|O_SYNC,
78 logf (LOG_FATAL|LOG_ERRNO, "create %s", path);
83 zebraUnlock (server_lock_cmt);
84 if (server_lock_org == -1)
86 sprintf (path, "%s%s", pathPrefix, FNAME_ORG_LOCK);
87 if ((server_lock_org = open (path, O_BINARY|O_CREAT|O_RDWR|O_SYNC,
91 logf (LOG_FATAL|LOG_ERRNO, "create %s", path);
96 zebraUnlock (server_lock_org);
100 fd = server_lock_org;
101 if (zebraLockNB (fd, 1) == -1)
103 if (errno != EWOULDBLOCK)
105 logf (LOG_FATAL|LOG_ERRNO, "flock");
109 logf (LOG_LOG, "Waiting for lock cmt");
111 logf (LOG_LOG, "Waiting for lock org");
112 if (zebraLock (fd, 1) == -1)
114 logf (LOG_FATAL|LOG_ERRNO, "flock");
123 void zebraIndexLockMsg (const char *str)
126 char pathPrefix[1024];
129 assert (lock_fd != -1);
130 lseek (lock_fd, 0L, SEEK_SET);
132 r = write (lock_fd, str, l);
135 logf (LOG_FATAL|LOG_ERRNO, "write lock file");
138 zebraLockPrefix (pathPrefix);
139 sprintf (path, "%s%s", pathPrefix, FNAME_TOUCH_TIME);
140 fd = creat (path, 0666);
144 void zebraIndexUnlock (void)
147 char pathPrefix[1024];
149 zebraLockPrefix (pathPrefix);
150 sprintf (path, "%s%s", pathPrefix, FNAME_MAIN_LOCK);
154 void zebraIndexLock (int commitNow)
157 char pathPrefix[1024];
163 zebraLockPrefix (pathPrefix);
164 sprintf (path, "%s%s", pathPrefix, FNAME_MAIN_LOCK);
167 lock_fd = open (path, O_BINARY|O_CREAT|O_RDWR|O_EXCL, 0666);
170 lock_fd = open (path, O_BINARY|O_RDWR);
175 logf (LOG_FATAL|LOG_ERRNO, "open %s", path);
178 if (zebraLockNB (lock_fd, 1) == -1)
180 if (errno == EWOULDBLOCK)
182 logf (LOG_LOG, "waiting for other index process");
183 zebraLock (lock_fd, 1);
184 zebraUnlock (lock_fd);
190 logf (LOG_FATAL|LOG_ERRNO, "flock %s", path);
196 logf (LOG_WARN, "unlocked %s", path);
197 r = read (lock_fd, buf, 256);
200 logf (LOG_WARN, "zero length %s", path);
207 logf (LOG_FATAL|LOG_ERRNO, "read %s", path);
212 logf (LOG_WARN, "previous transaction didn't"
219 else if (*buf == 'd')
221 logf (LOG_WARN, "commit file wan't deleted after commit");
227 else if (*buf == 'w')
229 logf (LOG_WARN, "your index may be inconsistent");
232 else if (*buf == 'c')
240 logf (LOG_FATAL, "previous transaction didn't"
241 " finish commit. Commit now!");
246 logf (LOG_FATAL, "unknown id 0x%02x in %s", *buf,
255 zebraLock (lock_fd, 1);