2 * Copyright (C) 1994-1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.7 1996-10-29 14:08:13 adam
8 * Uses resource lockDir instead of lockPath.
10 * Revision 1.6 1996/03/26 16:01:13 adam
11 * New setting lockPath: directory of various lock files.
13 * Revision 1.5 1995/12/13 08:46:09 adam
14 * Locking uses F_WRLCK and F_RDLCK again!
16 * Revision 1.4 1995/12/12 16:00:57 adam
17 * System call sync(2) used after update/commit.
18 * Locking (based on fcntl) uses F_EXLCK and F_SHLCK instead of F_WRLCK
21 * Revision 1.3 1995/12/11 11:43:29 adam
22 * Locking based on fcntl instead of flock.
23 * Setting commitEnable removed. Command line option -n can be used to
24 * prevent commit if commit setting is defined in the configuration file.
26 * Revision 1.2 1995/12/08 16:22:54 adam
27 * Work on update while servers are running. Three lock files introduced.
28 * The servers reload their registers when necessary, but they don't
29 * reestablish result sets yet.
31 * Revision 1.1 1995/12/07 17:38:47 adam
32 * Work locking mechanisms for concurrent updates/commit.
44 static int lock_fd = -1;
45 static int server_lock_cmt = -1;
46 static int server_lock_org = -1;
48 int zebraIndexWait (int commitPhase)
50 char pathPrefix[1024];
54 zebraLockPrefix (pathPrefix);
56 if (server_lock_cmt == -1)
58 sprintf (path, "%s%s", pathPrefix, FNAME_COMMIT_LOCK);
59 if ((server_lock_cmt = open (path, O_CREAT|O_RDWR|O_SYNC, 0666))
62 logf (LOG_FATAL|LOG_ERRNO, "create %s", path);
67 zebraUnlock (server_lock_cmt);
68 if (server_lock_org == -1)
70 sprintf (path, "%s%s", pathPrefix, FNAME_ORG_LOCK);
71 if ((server_lock_org = open (path, O_CREAT|O_RDWR|O_SYNC, 0666))
74 logf (LOG_FATAL|LOG_ERRNO, "create %s", path);
79 zebraUnlock (server_lock_org);
84 if (zebraLockNB (fd, 1) == -1)
86 if (errno != EWOULDBLOCK)
88 logf (LOG_FATAL|LOG_ERRNO, "flock");
92 logf (LOG_LOG, "Waiting for lock cmt");
94 logf (LOG_LOG, "Waiting for lock org");
95 if (zebraLock (fd, 1) == -1)
97 logf (LOG_FATAL|LOG_ERRNO, "flock");
106 void zebraIndexLockMsg (const char *str)
109 char pathPrefix[1024];
112 assert (lock_fd != -1);
113 lseek (lock_fd, 0L, SEEK_SET);
115 r = write (lock_fd, str, l);
118 logf (LOG_FATAL|LOG_ERRNO, "write lock file");
121 zebraLockPrefix (pathPrefix);
122 sprintf (path, "%s%s", pathPrefix, FNAME_TOUCH_TIME);
123 fd = creat (path, 0666);
127 void zebraIndexUnlock (void)
130 char pathPrefix[1024];
132 zebraLockPrefix (pathPrefix);
133 sprintf (path, "%s%s", pathPrefix, FNAME_MAIN_LOCK);
137 void zebraIndexLock (int commitNow)
140 char pathPrefix[1024];
146 zebraLockPrefix (pathPrefix);
147 sprintf (path, "%s%s", pathPrefix, FNAME_MAIN_LOCK);
150 lock_fd = open (path, O_CREAT|O_RDWR|O_EXCL, 0666);
153 lock_fd = open (path, O_RDWR);
158 logf (LOG_FATAL|LOG_ERRNO, "open %s", path);
161 logf (LOG_LOG, "zebraLockNB");
162 if (zebraLockNB (lock_fd, 1) == -1)
164 if (errno == EWOULDBLOCK)
166 logf (LOG_LOG, "Waiting for other index process");
167 zebraLock (lock_fd, 1);
168 zebraUnlock (lock_fd);
174 logf (LOG_FATAL|LOG_ERRNO, "flock %s", path);
180 logf (LOG_WARN, "Unlocked %s", path);
181 r = read (lock_fd, buf, 256);
184 logf (LOG_WARN, "Zero length %s", path);
191 logf (LOG_FATAL|LOG_ERRNO, "read %s", path);
196 logf (LOG_WARN, "Previous transaction didn't"
203 else if (*buf == 'd')
205 logf (LOG_WARN, "Commit file wan't deleted after commit");
211 else if (*buf == 'w')
213 logf (LOG_WARN, "Your index may be inconsistent");
216 else if (*buf == 'c')
224 logf (LOG_FATAL, "Previous transaction didn't"
225 " finish commit. Commit now!");
230 logf (LOG_FATAL, "Unknown id 0x%02x in %s", *buf,
239 zebraLock (lock_fd, 1);