Work locking mechanisms for concurrent updates/commit.
[idzebra-moved-to-github.git] / index / lockidx.c
1 /*
2  * Copyright (C) 1994-1995, Index Data I/S 
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: lockidx.c,v $
7  * Revision 1.1  1995-12-07 17:38:47  adam
8  * Work locking mechanisms for concurrent updates/commit.
9  *
10  */
11 #include <stdio.h>
12 #include <assert.h>
13 #include <unistd.h>
14 #include <sys/file.h>
15 #include <fcntl.h>
16 #include <string.h>
17 #include <errno.h>
18
19 #include <alexutil.h>
20 #include "index.h"
21
22 static int lock_fd = -1;
23
24 void zebraIndexLockMsg (const char *str)
25 {
26     int l, r;
27
28     assert (lock_fd != -1);
29     lseek (lock_fd, 0L, SEEK_SET);
30     l = strlen(str);
31     r = write (lock_fd, str, l);
32     if (r != l)
33     {
34         logf (LOG_FATAL|LOG_ERRNO, "write lock file");
35         exit (1);
36     }
37 }
38
39 void zebraIndexUnlock (int rw)
40 {
41     char path[1024];
42     char pathPrefix[1024];
43
44     if (lock_fd == -1)
45         return;
46
47     zebraLockPrefix (pathPrefix);
48     sprintf (path, "%s%s", pathPrefix, FNAME_MAIN_LOCK);
49     unlink (path);
50 }
51
52 void zebraIndexLock (int rw)
53 {
54     char path[1024];
55     char pathPrefix[1024];
56     char buf[256];
57     int r;
58
59     zebraLockPrefix (pathPrefix);
60     sprintf (path, "%s%s", pathPrefix, FNAME_MAIN_LOCK);
61     if (rw)
62     {
63         while (1)
64         {
65             lock_fd = open (path, O_CREAT|O_RDWR|O_EXCL|O_SYNC, 0666);
66             if (lock_fd == -1)
67             {
68                 lock_fd = open (path, O_RDONLY);
69                 assert (lock_fd != -1);
70                 if (flock (lock_fd, LOCK_EX|LOCK_NB) == -1)
71                 {
72                     if (errno == EWOULDBLOCK)
73                     {
74                         logf (LOG_LOG, "Waiting for other index process");
75                         flock (lock_fd, LOCK_EX);
76                         continue;
77                     }
78                     else
79                     {
80                         logf (LOG_FATAL|LOG_ERRNO, "flock %s", path);
81                         exit (1);
82                     }
83                 }
84                 else
85                 {
86                     logf (LOG_WARN, "Unlocked %s", path);
87                     r = read (lock_fd, buf, 256);
88                     if (r == 0)
89                     {
90                         logf (LOG_WARN, "Zero length %s", path);
91                         unlink (path);
92                         close (lock_fd);
93                         continue;
94                     }
95                     else if (r == -1)
96                     {
97                         logf (LOG_FATAL|LOG_ERRNO, "read  %s", path);
98                         exit (1);
99                     }
100                     if (*buf == 'r')
101                     {
102                         logf (LOG_WARN, "Previous transaction didn't"
103                               " reach commit");
104                         unlink (path);
105                         close (lock_fd);
106                         continue;
107                     }
108                     else if (*buf == 'w')
109                     {
110                         logf (LOG_WARN, "Your index may be inconsistent");
111                         exit (1);
112                     }
113                     else if (*buf == 'c')
114                     {
115                         logf (LOG_FATAL, "Previous transaction didn't"
116                               " finish commit. Restore now!");
117                         exit (1);
118                     }
119                     else 
120                     {
121                         logf (LOG_FATAL, "Unknown id 0x%02x in %s", *buf,
122                               path);
123                         exit (1);
124                     }
125                 }
126             }
127             else
128                 break;
129         }
130         flock (lock_fd, LOCK_EX);
131     }
132 }
133
134 void zebraIndexLockCommit (void)
135 {
136
137 }
138