Changed some log messages.
[idzebra-moved-to-github.git] / index / locksrv.c
1 /*
2  * Copyright (C) 1994-1996, Index Data I/S 
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: locksrv.c,v $
7  * Revision 1.5  1996-05-15 11:58:18  adam
8  * Changed some log messages.
9  *
10  * Revision 1.4  1996/04/10  16:01:27  quinn
11  * Fixed order of path/filename.
12  *
13  * Revision 1.3  1995/12/11  11:43:29  adam
14  * Locking based on fcntl instead of flock.
15  * Setting commitEnable removed. Command line option -n can be used to
16  * prevent commit if commit setting is defined in the configuration file.
17  *
18  * Revision 1.2  1995/12/08  16:22:55  adam
19  * Work on update while servers are running. Three lock files introduced.
20  * The servers reload their registers when necessary, but they don't
21  * reestablish result sets yet.
22  *
23  * Revision 1.1  1995/12/07  17:38:47  adam
24  * Work locking mechanisms for concurrent updates/commit.
25  *
26  */
27 #include <stdio.h>
28 #include <assert.h>
29 #include <unistd.h>
30 #include <sys/stat.h>
31 #include <fcntl.h>
32 #include <string.h>
33 #include <errno.h>
34
35 #include <alexutil.h>
36 #include "zserver.h"
37
38 static int server_lock_cmt = -1;
39 static int server_lock_org = -1;
40
41 int zebraServerLock (int commitPhase)
42 {
43     char pathPrefix[1024];
44     char path[1024];
45     
46     zebraLockPrefix (pathPrefix);
47
48     if (server_lock_cmt == -1)
49     {
50         sprintf (path, "%s%s", pathPrefix, FNAME_COMMIT_LOCK);
51         if ((server_lock_cmt = open (path, O_CREAT|O_RDWR, 0666))
52             == -1)
53         {
54             logf (LOG_FATAL|LOG_ERRNO, "create %s", path);
55             return -1;
56         }
57         assert (server_lock_org == -1);
58
59         sprintf (path, "%s%s", pathPrefix, FNAME_ORG_LOCK);
60         if ((server_lock_org = open (path, O_CREAT|O_RDWR, 0666))
61             == -1)
62         {
63             logf (LOG_FATAL|LOG_ERRNO, "create %s", path);
64             return -1;
65         }
66     }
67     if (commitPhase)
68     {
69         logf (LOG_DEBUG, "Server locks org");
70         zebraLock (server_lock_org, 0);
71     }
72     else
73     {
74         logf (LOG_DEBUG, "Server locks cmt");
75         zebraLock (server_lock_cmt, 0);
76     }
77     return 0;
78 }
79
80 void zebraServerUnlock (int commitPhase)
81 {
82     if (server_lock_org == -1)
83         return;
84     if (commitPhase)
85     {
86         logf (LOG_DEBUG, "Server unlocks org");
87         zebraUnlock (server_lock_org);
88     }
89     else
90     {
91         logf (LOG_DEBUG, "Server unlocks cmt");
92         zebraUnlock (server_lock_cmt);
93     }
94 }
95
96 int zebraServerLockGetState (time_t *timep)
97 {
98     char pathPrefix[1024];
99     char path[1024];
100     char buf[256];
101     int fd;
102     struct stat xstat;
103     
104     zebraLockPrefix (pathPrefix);
105
106     sprintf (path, "%s%s", pathPrefix, FNAME_TOUCH_TIME);
107     if (stat (path, &xstat) == -1)
108         *timep = 1;
109     else
110         *timep = xstat.st_ctime;
111     
112     sprintf (path, "%s%s", pathPrefix, FNAME_MAIN_LOCK);
113     fd = open (path, O_RDONLY);
114     if (fd == -1)
115     {
116         *buf = 0;
117         return 0;
118     }
119     if (read (fd, buf, 2) == 0)
120     {
121         *buf = 0;
122         return 0;
123     }
124     close (fd);
125     return *buf;
126 }