2 * Copyright (C) 1994-1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.6 1996-03-26 16:01:13 adam
8 * New setting lockPath: directory of various lock files.
10 * Revision 1.5 1995/12/13 08:46:09 adam
11 * Locking uses F_WRLCK and F_RDLCK again!
13 * Revision 1.4 1995/12/12 16:00:57 adam
14 * System call sync(2) used after update/commit.
15 * Locking (based on fcntl) uses F_EXLCK and F_SHLCK instead of F_WRLCK
18 * Revision 1.3 1995/12/11 11:43:29 adam
19 * Locking based on fcntl instead of flock.
20 * Setting commitEnable removed. Command line option -n can be used to
21 * prevent commit if commit setting is defined in the configuration file.
23 * Revision 1.2 1995/12/08 16:22:54 adam
24 * Work on update while servers are running. Three lock files introduced.
25 * The servers reload their registers when necessary, but they don't
26 * reestablish result sets yet.
28 * Revision 1.1 1995/12/07 17:38:47 adam
29 * Work locking mechanisms for concurrent updates/commit.
42 static int lock_fd = -1;
43 static int server_lock_cmt = -1;
44 static int server_lock_org = -1;
46 int zebraIndexWait (int commitPhase)
48 char pathPrefix[1024];
52 zebraLockPrefix (pathPrefix);
54 if (server_lock_cmt == -1)
56 sprintf (path, "%s%s", pathPrefix, FNAME_COMMIT_LOCK);
57 if ((server_lock_cmt = open (path, O_CREAT|O_RDWR|O_SYNC, 0666))
60 logf (LOG_FATAL|LOG_ERRNO, "create %s", path);
65 zebraUnlock (server_lock_cmt);
66 if (server_lock_org == -1)
68 sprintf (path, "%s%s", pathPrefix, FNAME_ORG_LOCK);
69 if ((server_lock_org = open (path, O_CREAT|O_RDWR|O_SYNC, 0666))
72 logf (LOG_FATAL|LOG_ERRNO, "create %s", path);
77 zebraUnlock (server_lock_org);
82 if (zebraLockNB (fd, 1) == -1)
84 if (errno != EWOULDBLOCK)
86 logf (LOG_FATAL|LOG_ERRNO, "flock");
90 logf (LOG_LOG, "Waiting for lock cmt");
92 logf (LOG_LOG, "Waiting for lock org");
93 if (zebraLock (fd, 1) == -1)
95 logf (LOG_FATAL|LOG_ERRNO, "flock");
104 void zebraIndexLockMsg (const char *str)
107 char pathPrefix[1024];
110 assert (lock_fd != -1);
111 lseek (lock_fd, 0L, SEEK_SET);
113 r = write (lock_fd, str, l);
116 logf (LOG_FATAL|LOG_ERRNO, "write lock file");
119 zebraLockPrefix (pathPrefix);
120 sprintf (path, "%s%s", pathPrefix, FNAME_TOUCH_TIME);
121 fd = creat (path, 0666);
125 void zebraIndexUnlock (void)
128 char pathPrefix[1024];
130 zebraLockPrefix (pathPrefix);
131 sprintf (path, "%s%s", pathPrefix, FNAME_MAIN_LOCK);
135 void zebraIndexLock (int commitNow)
138 char pathPrefix[1024];
144 zebraLockPrefix (pathPrefix);
145 sprintf (path, "%s%s", pathPrefix, FNAME_MAIN_LOCK);
148 lock_fd = open (path, O_CREAT|O_RDWR|O_EXCL, 0666);
151 lock_fd = open (path, O_RDWR);
156 logf (LOG_FATAL|LOG_ERRNO, "open %s", path);
159 logf (LOG_LOG, "zebraLockNB");
160 if (zebraLockNB (lock_fd, 1) == -1)
162 if (errno == EWOULDBLOCK)
164 logf (LOG_LOG, "Waiting for other index process");
165 zebraLock (lock_fd, 1);
166 zebraUnlock (lock_fd);
172 logf (LOG_FATAL|LOG_ERRNO, "flock %s", path);
178 logf (LOG_WARN, "Unlocked %s", path);
179 r = read (lock_fd, buf, 256);
182 logf (LOG_WARN, "Zero length %s", path);
189 logf (LOG_FATAL|LOG_ERRNO, "read %s", path);
194 logf (LOG_WARN, "Previous transaction didn't"
201 else if (*buf == 'd')
203 logf (LOG_WARN, "Commit file wan't deleted after commit");
209 else if (*buf == 'w')
211 logf (LOG_WARN, "Your index may be inconsistent");
214 else if (*buf == 'c')
222 logf (LOG_FATAL, "Previous transaction didn't"
223 " finish commit. Commit now!");
228 logf (LOG_FATAL, "Unknown id 0x%02x in %s", *buf,
237 zebraLock (lock_fd, 1);