2 * Copyright (C) 1994-1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.2 1995-12-08 16:22:54 adam
8 * Work on update while servers are running. Three lock files introduced.
9 * The servers reload their registers when necessary, but they don't
10 * reestablish result sets yet.
12 * Revision 1.1 1995/12/07 17:38:47 adam
13 * Work locking mechanisms for concurrent updates/commit.
27 static int lock_fd = -1;
28 static int server_lock_cmt = -1;
29 static int server_lock_org = -1;
31 int zebraIndexWait (int commitPhase)
33 char pathPrefix[1024];
37 zebraLockPrefix (pathPrefix);
39 if (server_lock_cmt == -1)
41 sprintf (path, "%s%s", FNAME_COMMIT_LOCK, pathPrefix);
42 if ((server_lock_cmt = open (path, O_CREAT|O_RDWR|O_SYNC, 0666))
45 logf (LOG_FATAL|LOG_ERRNO, "create %s", path);
50 flock (server_lock_cmt, LOCK_UN);
51 if (server_lock_org == -1)
53 sprintf (path, "%s%s", FNAME_ORG_LOCK, pathPrefix);
54 if ((server_lock_org = open (path, O_CREAT|O_RDWR|O_SYNC, 0666))
57 logf (LOG_FATAL|LOG_ERRNO, "create %s", path);
62 flock (server_lock_org, LOCK_UN);
67 if (flock (fd, LOCK_EX|LOCK_NB) == -1)
69 if (errno != EWOULDBLOCK)
71 logf (LOG_FATAL|LOG_ERRNO, "flock");
75 logf (LOG_LOG, "Waiting for lock cmt");
77 logf (LOG_LOG, "Waiting for lock org");
78 if (flock (fd, LOCK_EX) == -1)
80 logf (LOG_FATAL|LOG_ERRNO, "flock");
89 void zebraIndexLockMsg (const char *str)
92 char pathPrefix[1024];
95 assert (lock_fd != -1);
96 lseek (lock_fd, 0L, SEEK_SET);
98 r = write (lock_fd, str, l);
101 logf (LOG_FATAL|LOG_ERRNO, "write lock file");
104 zebraLockPrefix (pathPrefix);
105 sprintf (path, "%s%s", pathPrefix, FNAME_TOUCH_TIME);
106 fd = creat (path, 0666);
110 void zebraIndexUnlock (void)
113 char pathPrefix[1024];
115 zebraLockPrefix (pathPrefix);
116 sprintf (path, "%s%s", pathPrefix, FNAME_MAIN_LOCK);
120 void zebraIndexLock (int commitNow)
123 char pathPrefix[1024];
129 zebraLockPrefix (pathPrefix);
130 sprintf (path, "%s%s", pathPrefix, FNAME_MAIN_LOCK);
133 lock_fd = open (path, O_CREAT|O_RDWR|O_EXCL|O_SYNC, 0666);
136 lock_fd = open (path, O_RDONLY);
137 assert (lock_fd != -1);
138 if (flock (lock_fd, LOCK_EX|LOCK_NB) == -1)
140 if (errno == EWOULDBLOCK)
142 logf (LOG_LOG, "Waiting for other index process");
143 flock (lock_fd, LOCK_EX);
148 logf (LOG_FATAL|LOG_ERRNO, "flock %s", path);
154 logf (LOG_WARN, "Unlocked %s", path);
155 r = read (lock_fd, buf, 256);
158 logf (LOG_WARN, "Zero length %s", path);
165 logf (LOG_FATAL|LOG_ERRNO, "read %s", path);
170 logf (LOG_WARN, "Previous transaction didn't"
177 else if (*buf == 'd')
179 logf (LOG_WARN, "Commit file wan't deleted after commit");
185 else if (*buf == 'w')
187 logf (LOG_WARN, "Your index may be inconsistent");
190 else if (*buf == 'c')
198 logf (LOG_FATAL, "Previous transaction didn't"
199 " finish commit. Commit now!");
204 logf (LOG_FATAL, "Unknown id 0x%02x in %s", *buf,
213 flock (lock_fd, LOCK_EX);