Using the new ylog.h everywhere, and fixing what that breaks!
[idzebra-moved-to-github.git] / index / locksrv.c
1 /* $Id: locksrv.c,v 1.18 2004-11-19 10:26:58 heikki Exp $
2    Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002
3    Index Data Aps
4
5 This file is part of the Zebra server.
6
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra.  If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.
21 */
22
23
24 #include <stdio.h>
25 #include <assert.h>
26 #ifdef WIN32
27 #include <io.h>
28 #else
29 #include <unistd.h>
30 #endif
31 #include <sys/stat.h>
32 #include <fcntl.h>
33 #include <string.h>
34 #include <errno.h>
35
36 #include "zserver.h"
37
38 int zebra_server_lock_init (ZebraService zi)
39 {
40     char path_prefix[1024];
41
42     zi->server_lock_cmt = NULL;
43     zi->server_lock_org = NULL;
44
45     zebra_lock_prefix (zi->res, path_prefix);
46     zi->server_path_prefix = (char *) xmalloc (strlen(path_prefix)+1);
47     strcpy (zi->server_path_prefix, path_prefix);
48
49     yaz_log (YLOG_DEBUG, "Locking system initialized");
50     return 0;
51 }
52
53 int zebra_server_lock_destroy (ZebraService zi)
54 {
55     xfree (zi->server_path_prefix);
56     zebra_lock_destroy (zi->server_lock_cmt);
57     zebra_lock_destroy (zi->server_lock_org);
58     yaz_log (YLOG_DEBUG, "Locking system destroyed");
59     return 0;
60 }
61
62 int zebra_server_lock (ZebraService zi, int commitPhase)
63 {
64     if (!zi->server_lock_cmt)
65     {
66         char path[1024];
67
68         strcpy (path, zi->server_path_prefix);
69         strcat (path, FNAME_COMMIT_LOCK);
70         if (!(zi->server_lock_cmt = zebra_lock_create (path, 0)))
71         {
72             yaz_log (YLOG_FATAL|YLOG_ERRNO, "create %s", path);
73             return -1;
74         }
75         assert (zi->server_lock_org == NULL);
76
77         strcpy (path, zi->server_path_prefix);
78         strcat (path, FNAME_ORG_LOCK);
79         if (!(zi->server_lock_org = zebra_lock_create (path, 0)))
80         {
81             yaz_log (YLOG_FATAL|YLOG_ERRNO, "create %s", path);
82             return -1;
83         }
84     }
85     if (commitPhase)
86     {
87         yaz_log (YLOG_DEBUG, "Server locks org");
88         zebra_lock (zi->server_lock_org);
89     }
90     else
91     {
92         yaz_log (YLOG_DEBUG, "Server locks cmt");
93         zebra_lock (zi->server_lock_cmt);
94     }
95     return 0;
96 }
97
98 void zebra_server_unlock (ZebraService zi, int commitPhase)
99 {
100     if (zi->server_lock_org == NULL)
101         return;
102     yaz_log (YLOG_DEBUG, "Server unlocks org");
103     zebra_unlock (zi->server_lock_org);
104     yaz_log (YLOG_DEBUG, "Server unlocks cmt");
105     zebra_unlock (zi->server_lock_cmt);
106 }
107
108 int zebra_server_lock_get_state (ZebraService zi, time_t *timep)
109 {
110     char path[1024];
111     char buf[256];
112     int fd;
113     struct stat xstat;
114     
115     strcpy (path, zi->server_path_prefix);
116     strcat (path, FNAME_TOUCH_TIME);
117     if (stat (path, &xstat) == -1)
118         *timep = 1;
119     else
120         *timep = xstat.st_mtime;
121
122     strcpy (path, zi->server_path_prefix);
123     strcat (path, FNAME_MAIN_LOCK);
124     fd = open (path, O_BINARY|O_RDONLY);
125     if (fd == -1)
126     {
127         *buf = 0;
128         return 0;
129     }
130     if (read (fd, buf, 2) == 0)
131     {
132         *buf = 0;
133         return 0;
134     }
135     close (fd);
136     return *buf;
137 }