2 * Copyright (C) 1994-1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.9 1997-09-04 13:58:04 adam
8 * Added O_BINARY for open calls.
10 * Revision 1.8 1997/02/12 20:39:46 adam
11 * Implemented options -f <n> that limits the log to the first <n>
13 * Changed some log messages also.
15 * Revision 1.7 1996/10/29 14:08:13 adam
16 * Uses resource lockDir instead of lockPath.
18 * Revision 1.6 1996/03/26 16:01:13 adam
19 * New setting lockPath: directory of various lock files.
21 * Revision 1.5 1995/12/13 08:46:09 adam
22 * Locking uses F_WRLCK and F_RDLCK again!
24 * Revision 1.4 1995/12/12 16:00:57 adam
25 * System call sync(2) used after update/commit.
26 * Locking (based on fcntl) uses F_EXLCK and F_SHLCK instead of F_WRLCK
29 * Revision 1.3 1995/12/11 11:43:29 adam
30 * Locking based on fcntl instead of flock.
31 * Setting commitEnable removed. Command line option -n can be used to
32 * prevent commit if commit setting is defined in the configuration file.
34 * Revision 1.2 1995/12/08 16:22:54 adam
35 * Work on update while servers are running. Three lock files introduced.
36 * The servers reload their registers when necessary, but they don't
37 * reestablish result sets yet.
39 * Revision 1.1 1995/12/07 17:38:47 adam
40 * Work locking mechanisms for concurrent updates/commit.
52 static int lock_fd = -1;
53 static int server_lock_cmt = -1;
54 static int server_lock_org = -1;
56 int zebraIndexWait (int commitPhase)
58 char pathPrefix[1024];
62 zebraLockPrefix (pathPrefix);
64 if (server_lock_cmt == -1)
66 sprintf (path, "%s%s", pathPrefix, FNAME_COMMIT_LOCK);
67 if ((server_lock_cmt = open (path, O_BINARY|O_CREAT|O_RDWR|O_SYNC,
71 logf (LOG_FATAL|LOG_ERRNO, "create %s", path);
76 zebraUnlock (server_lock_cmt);
77 if (server_lock_org == -1)
79 sprintf (path, "%s%s", pathPrefix, FNAME_ORG_LOCK);
80 if ((server_lock_org = open (path, O_BINARY|O_CREAT|O_RDWR|O_SYNC,
84 logf (LOG_FATAL|LOG_ERRNO, "create %s", path);
89 zebraUnlock (server_lock_org);
94 if (zebraLockNB (fd, 1) == -1)
96 if (errno != EWOULDBLOCK)
98 logf (LOG_FATAL|LOG_ERRNO, "flock");
102 logf (LOG_LOG, "Waiting for lock cmt");
104 logf (LOG_LOG, "Waiting for lock org");
105 if (zebraLock (fd, 1) == -1)
107 logf (LOG_FATAL|LOG_ERRNO, "flock");
116 void zebraIndexLockMsg (const char *str)
119 char pathPrefix[1024];
122 assert (lock_fd != -1);
123 lseek (lock_fd, 0L, SEEK_SET);
125 r = write (lock_fd, str, l);
128 logf (LOG_FATAL|LOG_ERRNO, "write lock file");
131 zebraLockPrefix (pathPrefix);
132 sprintf (path, "%s%s", pathPrefix, FNAME_TOUCH_TIME);
133 fd = creat (path, 0666);
137 void zebraIndexUnlock (void)
140 char pathPrefix[1024];
142 zebraLockPrefix (pathPrefix);
143 sprintf (path, "%s%s", pathPrefix, FNAME_MAIN_LOCK);
147 void zebraIndexLock (int commitNow)
150 char pathPrefix[1024];
156 zebraLockPrefix (pathPrefix);
157 sprintf (path, "%s%s", pathPrefix, FNAME_MAIN_LOCK);
160 lock_fd = open (path, O_BINARY|O_CREAT|O_RDWR|O_EXCL, 0666);
163 lock_fd = open (path, O_BINARY|O_RDWR);
168 logf (LOG_FATAL|LOG_ERRNO, "open %s", path);
171 if (zebraLockNB (lock_fd, 1) == -1)
173 if (errno == EWOULDBLOCK)
175 logf (LOG_LOG, "waiting for other index process");
176 zebraLock (lock_fd, 1);
177 zebraUnlock (lock_fd);
183 logf (LOG_FATAL|LOG_ERRNO, "flock %s", path);
189 logf (LOG_WARN, "unlocked %s", path);
190 r = read (lock_fd, buf, 256);
193 logf (LOG_WARN, "zero length %s", path);
200 logf (LOG_FATAL|LOG_ERRNO, "read %s", path);
205 logf (LOG_WARN, "previous transaction didn't"
212 else if (*buf == 'd')
214 logf (LOG_WARN, "commit file wan't deleted after commit");
220 else if (*buf == 'w')
222 logf (LOG_WARN, "your index may be inconsistent");
225 else if (*buf == 'c')
233 logf (LOG_FATAL, "previous transaction didn't"
234 " finish commit. Commit now!");
239 logf (LOG_FATAL, "unknown id 0x%02x in %s", *buf,
248 zebraLock (lock_fd, 1);