2 * Copyright (C) 1994-1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.4 1995-12-12 16:00:57 adam
8 * System call sync(2) used after update/commit.
9 * Locking (based on fcntl) uses F_EXLCK and F_SHLCK instead of F_WRLCK
12 * Revision 1.3 1995/12/11 11:43:29 adam
13 * Locking based on fcntl instead of flock.
14 * Setting commitEnable removed. Command line option -n can be used to
15 * prevent commit if commit setting is defined in the configuration file.
17 * Revision 1.2 1995/12/08 16:22:54 adam
18 * Work on update while servers are running. Three lock files introduced.
19 * The servers reload their registers when necessary, but they don't
20 * reestablish result sets yet.
22 * Revision 1.1 1995/12/07 17:38:47 adam
23 * Work locking mechanisms for concurrent updates/commit.
36 static int lock_fd = -1;
37 static int server_lock_cmt = -1;
38 static int server_lock_org = -1;
40 int zebraIndexWait (int commitPhase)
42 char pathPrefix[1024];
46 zebraLockPrefix (pathPrefix);
48 if (server_lock_cmt == -1)
50 sprintf (path, "%s%s", FNAME_COMMIT_LOCK, pathPrefix);
51 if ((server_lock_cmt = open (path, O_CREAT|O_RDWR|O_SYNC, 0666))
54 logf (LOG_FATAL|LOG_ERRNO, "create %s", path);
59 zebraUnlock (server_lock_cmt);
60 if (server_lock_org == -1)
62 sprintf (path, "%s%s", FNAME_ORG_LOCK, pathPrefix);
63 if ((server_lock_org = open (path, O_CREAT|O_RDWR|O_SYNC, 0666))
66 logf (LOG_FATAL|LOG_ERRNO, "create %s", path);
71 zebraUnlock (server_lock_org);
76 if (zebraLockNB (fd, 1) == -1)
78 if (errno != EWOULDBLOCK)
80 logf (LOG_FATAL|LOG_ERRNO, "flock");
84 logf (LOG_LOG, "Waiting for lock cmt");
86 logf (LOG_LOG, "Waiting for lock org");
87 if (zebraLock (fd, 1) == -1)
89 logf (LOG_FATAL|LOG_ERRNO, "flock");
98 void zebraIndexLockMsg (const char *str)
101 char pathPrefix[1024];
104 assert (lock_fd != -1);
105 lseek (lock_fd, 0L, SEEK_SET);
107 r = write (lock_fd, str, l);
110 logf (LOG_FATAL|LOG_ERRNO, "write lock file");
113 zebraLockPrefix (pathPrefix);
114 sprintf (path, "%s%s", pathPrefix, FNAME_TOUCH_TIME);
115 fd = creat (path, 0666);
119 void zebraIndexUnlock (void)
122 char pathPrefix[1024];
124 zebraLockPrefix (pathPrefix);
125 sprintf (path, "%s%s", pathPrefix, FNAME_MAIN_LOCK);
129 void zebraIndexLock (int commitNow)
132 char pathPrefix[1024];
138 zebraLockPrefix (pathPrefix);
139 sprintf (path, "%s%s", pathPrefix, FNAME_MAIN_LOCK);
142 lock_fd = open (path, O_CREAT|O_RDWR|O_EXCL, 0666);
145 lock_fd = open (path, O_RDONLY);
150 logf (LOG_FATAL|LOG_ERRNO, "open %s", path);
153 if (zebraLockNB (lock_fd, 1) == -1)
155 if (errno == EWOULDBLOCK)
157 logf (LOG_LOG, "Waiting for other index process");
158 zebraLock (lock_fd, 1);
163 logf (LOG_FATAL|LOG_ERRNO, "flock %s", path);
169 logf (LOG_WARN, "Unlocked %s", path);
170 r = read (lock_fd, buf, 256);
173 logf (LOG_WARN, "Zero length %s", path);
180 logf (LOG_FATAL|LOG_ERRNO, "read %s", path);
185 logf (LOG_WARN, "Previous transaction didn't"
192 else if (*buf == 'd')
194 logf (LOG_WARN, "Commit file wan't deleted after commit");
200 else if (*buf == 'w')
202 logf (LOG_WARN, "Your index may be inconsistent");
205 else if (*buf == 'c')
213 logf (LOG_FATAL, "Previous transaction didn't"
214 " finish commit. Commit now!");
219 logf (LOG_FATAL, "Unknown id 0x%02x in %s", *buf,
228 zebraLock (lock_fd, 1);