2 * Copyright (C) 1994-1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.5 1995-12-13 08:46:09 adam
8 * Locking uses F_WRLCK and F_RDLCK again!
10 * Revision 1.4 1995/12/12 16:00:57 adam
11 * System call sync(2) used after update/commit.
12 * Locking (based on fcntl) uses F_EXLCK and F_SHLCK instead of F_WRLCK
15 * Revision 1.3 1995/12/11 11:43:29 adam
16 * Locking based on fcntl instead of flock.
17 * Setting commitEnable removed. Command line option -n can be used to
18 * prevent commit if commit setting is defined in the configuration file.
20 * Revision 1.2 1995/12/08 16:22:54 adam
21 * Work on update while servers are running. Three lock files introduced.
22 * The servers reload their registers when necessary, but they don't
23 * reestablish result sets yet.
25 * Revision 1.1 1995/12/07 17:38:47 adam
26 * Work locking mechanisms for concurrent updates/commit.
39 static int lock_fd = -1;
40 static int server_lock_cmt = -1;
41 static int server_lock_org = -1;
43 int zebraIndexWait (int commitPhase)
45 char pathPrefix[1024];
49 zebraLockPrefix (pathPrefix);
51 if (server_lock_cmt == -1)
53 sprintf (path, "%s%s", FNAME_COMMIT_LOCK, pathPrefix);
54 if ((server_lock_cmt = open (path, O_CREAT|O_RDWR|O_SYNC, 0666))
57 logf (LOG_FATAL|LOG_ERRNO, "create %s", path);
62 zebraUnlock (server_lock_cmt);
63 if (server_lock_org == -1)
65 sprintf (path, "%s%s", FNAME_ORG_LOCK, pathPrefix);
66 if ((server_lock_org = open (path, O_CREAT|O_RDWR|O_SYNC, 0666))
69 logf (LOG_FATAL|LOG_ERRNO, "create %s", path);
74 zebraUnlock (server_lock_org);
79 if (zebraLockNB (fd, 1) == -1)
81 if (errno != EWOULDBLOCK)
83 logf (LOG_FATAL|LOG_ERRNO, "flock");
87 logf (LOG_LOG, "Waiting for lock cmt");
89 logf (LOG_LOG, "Waiting for lock org");
90 if (zebraLock (fd, 1) == -1)
92 logf (LOG_FATAL|LOG_ERRNO, "flock");
101 void zebraIndexLockMsg (const char *str)
104 char pathPrefix[1024];
107 assert (lock_fd != -1);
108 lseek (lock_fd, 0L, SEEK_SET);
110 r = write (lock_fd, str, l);
113 logf (LOG_FATAL|LOG_ERRNO, "write lock file");
116 zebraLockPrefix (pathPrefix);
117 sprintf (path, "%s%s", pathPrefix, FNAME_TOUCH_TIME);
118 fd = creat (path, 0666);
122 void zebraIndexUnlock (void)
125 char pathPrefix[1024];
127 zebraLockPrefix (pathPrefix);
128 sprintf (path, "%s%s", pathPrefix, FNAME_MAIN_LOCK);
132 void zebraIndexLock (int commitNow)
135 char pathPrefix[1024];
141 zebraLockPrefix (pathPrefix);
142 sprintf (path, "%s%s", pathPrefix, FNAME_MAIN_LOCK);
145 lock_fd = open (path, O_CREAT|O_RDWR|O_EXCL, 0666);
148 lock_fd = open (path, O_RDWR);
153 logf (LOG_FATAL|LOG_ERRNO, "open %s", path);
156 logf (LOG_LOG, "zebraLockNB");
157 if (zebraLockNB (lock_fd, 1) == -1)
159 if (errno == EWOULDBLOCK)
161 logf (LOG_LOG, "Waiting for other index process");
162 zebraLock (lock_fd, 1);
163 zebraUnlock (lock_fd);
169 logf (LOG_FATAL|LOG_ERRNO, "flock %s", path);
175 logf (LOG_WARN, "Unlocked %s", path);
176 r = read (lock_fd, buf, 256);
179 logf (LOG_WARN, "Zero length %s", path);
186 logf (LOG_FATAL|LOG_ERRNO, "read %s", path);
191 logf (LOG_WARN, "Previous transaction didn't"
198 else if (*buf == 'd')
200 logf (LOG_WARN, "Commit file wan't deleted after commit");
206 else if (*buf == 'w')
208 logf (LOG_WARN, "Your index may be inconsistent");
211 else if (*buf == 'c')
219 logf (LOG_FATAL, "Previous transaction didn't"
220 " finish commit. Commit now!");
225 logf (LOG_FATAL, "Unknown id 0x%02x in %s", *buf,
234 zebraLock (lock_fd, 1);