2 * Copyright (C) 1994-1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.8 1997-02-12 20:39:46 adam
8 * Implemented options -f <n> that limits the log to the first <n>
10 * Changed some log messages also.
12 * Revision 1.7 1996/10/29 14:08:13 adam
13 * Uses resource lockDir instead of lockPath.
15 * Revision 1.6 1996/03/26 16:01:13 adam
16 * New setting lockPath: directory of various lock files.
18 * Revision 1.5 1995/12/13 08:46:09 adam
19 * Locking uses F_WRLCK and F_RDLCK again!
21 * Revision 1.4 1995/12/12 16:00:57 adam
22 * System call sync(2) used after update/commit.
23 * Locking (based on fcntl) uses F_EXLCK and F_SHLCK instead of F_WRLCK
26 * Revision 1.3 1995/12/11 11:43:29 adam
27 * Locking based on fcntl instead of flock.
28 * Setting commitEnable removed. Command line option -n can be used to
29 * prevent commit if commit setting is defined in the configuration file.
31 * Revision 1.2 1995/12/08 16:22:54 adam
32 * Work on update while servers are running. Three lock files introduced.
33 * The servers reload their registers when necessary, but they don't
34 * reestablish result sets yet.
36 * Revision 1.1 1995/12/07 17:38:47 adam
37 * Work locking mechanisms for concurrent updates/commit.
49 static int lock_fd = -1;
50 static int server_lock_cmt = -1;
51 static int server_lock_org = -1;
53 int zebraIndexWait (int commitPhase)
55 char pathPrefix[1024];
59 zebraLockPrefix (pathPrefix);
61 if (server_lock_cmt == -1)
63 sprintf (path, "%s%s", pathPrefix, FNAME_COMMIT_LOCK);
64 if ((server_lock_cmt = open (path, O_CREAT|O_RDWR|O_SYNC, 0666))
67 logf (LOG_FATAL|LOG_ERRNO, "create %s", path);
72 zebraUnlock (server_lock_cmt);
73 if (server_lock_org == -1)
75 sprintf (path, "%s%s", pathPrefix, FNAME_ORG_LOCK);
76 if ((server_lock_org = open (path, O_CREAT|O_RDWR|O_SYNC, 0666))
79 logf (LOG_FATAL|LOG_ERRNO, "create %s", path);
84 zebraUnlock (server_lock_org);
89 if (zebraLockNB (fd, 1) == -1)
91 if (errno != EWOULDBLOCK)
93 logf (LOG_FATAL|LOG_ERRNO, "flock");
97 logf (LOG_LOG, "Waiting for lock cmt");
99 logf (LOG_LOG, "Waiting for lock org");
100 if (zebraLock (fd, 1) == -1)
102 logf (LOG_FATAL|LOG_ERRNO, "flock");
111 void zebraIndexLockMsg (const char *str)
114 char pathPrefix[1024];
117 assert (lock_fd != -1);
118 lseek (lock_fd, 0L, SEEK_SET);
120 r = write (lock_fd, str, l);
123 logf (LOG_FATAL|LOG_ERRNO, "write lock file");
126 zebraLockPrefix (pathPrefix);
127 sprintf (path, "%s%s", pathPrefix, FNAME_TOUCH_TIME);
128 fd = creat (path, 0666);
132 void zebraIndexUnlock (void)
135 char pathPrefix[1024];
137 zebraLockPrefix (pathPrefix);
138 sprintf (path, "%s%s", pathPrefix, FNAME_MAIN_LOCK);
142 void zebraIndexLock (int commitNow)
145 char pathPrefix[1024];
151 zebraLockPrefix (pathPrefix);
152 sprintf (path, "%s%s", pathPrefix, FNAME_MAIN_LOCK);
155 lock_fd = open (path, O_CREAT|O_RDWR|O_EXCL, 0666);
158 lock_fd = open (path, O_RDWR);
163 logf (LOG_FATAL|LOG_ERRNO, "open %s", path);
166 if (zebraLockNB (lock_fd, 1) == -1)
168 if (errno == EWOULDBLOCK)
170 logf (LOG_LOG, "waiting for other index process");
171 zebraLock (lock_fd, 1);
172 zebraUnlock (lock_fd);
178 logf (LOG_FATAL|LOG_ERRNO, "flock %s", path);
184 logf (LOG_WARN, "unlocked %s", path);
185 r = read (lock_fd, buf, 256);
188 logf (LOG_WARN, "zero length %s", path);
195 logf (LOG_FATAL|LOG_ERRNO, "read %s", path);
200 logf (LOG_WARN, "previous transaction didn't"
207 else if (*buf == 'd')
209 logf (LOG_WARN, "commit file wan't deleted after commit");
215 else if (*buf == 'w')
217 logf (LOG_WARN, "your index may be inconsistent");
220 else if (*buf == 'c')
228 logf (LOG_FATAL, "previous transaction didn't"
229 " finish commit. Commit now!");
234 logf (LOG_FATAL, "unknown id 0x%02x in %s", *buf,
243 zebraLock (lock_fd, 1);