2 * Copyright (C) 1994-1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.3 1995-12-11 11:43:29 adam
8 * Locking based on fcntl instead of flock.
9 * Setting commitEnable removed. Command line option -n can be used to
10 * prevent commit if commit setting is defined in the configuration file.
12 * Revision 1.2 1995/12/08 16:22:54 adam
13 * Work on update while servers are running. Three lock files introduced.
14 * The servers reload their registers when necessary, but they don't
15 * reestablish result sets yet.
17 * Revision 1.1 1995/12/07 17:38:47 adam
18 * Work locking mechanisms for concurrent updates/commit.
31 static int lock_fd = -1;
32 static int server_lock_cmt = -1;
33 static int server_lock_org = -1;
35 int zebraIndexWait (int commitPhase)
37 char pathPrefix[1024];
41 zebraLockPrefix (pathPrefix);
43 if (server_lock_cmt == -1)
45 sprintf (path, "%s%s", FNAME_COMMIT_LOCK, pathPrefix);
46 if ((server_lock_cmt = open (path, O_CREAT|O_RDWR|O_SYNC, 0666))
49 logf (LOG_FATAL|LOG_ERRNO, "create %s", path);
54 zebraUnlock (server_lock_cmt);
55 if (server_lock_org == -1)
57 sprintf (path, "%s%s", FNAME_ORG_LOCK, pathPrefix);
58 if ((server_lock_org = open (path, O_CREAT|O_RDWR|O_SYNC, 0666))
61 logf (LOG_FATAL|LOG_ERRNO, "create %s", path);
66 zebraUnlock (server_lock_org);
71 if (zebraLockNB (fd, 1) == -1)
73 if (errno != EWOULDBLOCK)
75 logf (LOG_FATAL|LOG_ERRNO, "flock");
79 logf (LOG_LOG, "Waiting for lock cmt");
81 logf (LOG_LOG, "Waiting for lock org");
82 if (zebraLock (fd, 1) == -1)
84 logf (LOG_FATAL|LOG_ERRNO, "flock");
93 void zebraIndexLockMsg (const char *str)
96 char pathPrefix[1024];
99 assert (lock_fd != -1);
100 lseek (lock_fd, 0L, SEEK_SET);
102 r = write (lock_fd, str, l);
105 logf (LOG_FATAL|LOG_ERRNO, "write lock file");
108 zebraLockPrefix (pathPrefix);
109 sprintf (path, "%s%s", pathPrefix, FNAME_TOUCH_TIME);
110 fd = creat (path, 0666);
114 void zebraIndexUnlock (void)
117 char pathPrefix[1024];
119 zebraLockPrefix (pathPrefix);
120 sprintf (path, "%s%s", pathPrefix, FNAME_MAIN_LOCK);
124 void zebraIndexLock (int commitNow)
127 char pathPrefix[1024];
133 zebraLockPrefix (pathPrefix);
134 sprintf (path, "%s%s", pathPrefix, FNAME_MAIN_LOCK);
137 lock_fd = open (path, O_CREAT|O_RDWR|O_EXCL|O_SYNC, 0666);
140 lock_fd = open (path, O_RDONLY);
141 assert (lock_fd != -1);
142 if (zebraLockNB (lock_fd, 1) == -1)
144 if (errno == EWOULDBLOCK)
146 logf (LOG_LOG, "Waiting for other index process");
147 zebraLock (lock_fd, 1);
152 logf (LOG_FATAL|LOG_ERRNO, "flock %s", path);
158 logf (LOG_WARN, "Unlocked %s", path);
159 r = read (lock_fd, buf, 256);
162 logf (LOG_WARN, "Zero length %s", path);
169 logf (LOG_FATAL|LOG_ERRNO, "read %s", path);
174 logf (LOG_WARN, "Previous transaction didn't"
181 else if (*buf == 'd')
183 logf (LOG_WARN, "Commit file wan't deleted after commit");
189 else if (*buf == 'w')
191 logf (LOG_WARN, "Your index may be inconsistent");
194 else if (*buf == 'c')
202 logf (LOG_FATAL, "Previous transaction didn't"
203 " finish commit. Commit now!");
208 logf (LOG_FATAL, "Unknown id 0x%02x in %s", *buf,
217 zebraLock (lock_fd, 1);