Use HAVE_UNISTD_H when including unistd.h.
[idzebra-moved-to-github.git] / index / locksrv.c
1 /* $Id: locksrv.c,v 1.20 2005-06-14 20:28:54 adam Exp $
2    Copyright (C) 1995-2005
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 #endif
29 #if HAVE_UNISTD_H
30 #include <unistd.h>
31 #endif
32 #include <sys/stat.h>
33 #include <fcntl.h>
34 #include <string.h>
35 #include <errno.h>
36
37 #include "zserver.h"
38
39 int zebra_server_lock_init (ZebraService zi)
40 {
41     char path_prefix[1024];
42
43     zi->server_lock_cmt = NULL;
44     zi->server_lock_org = NULL;
45
46     zebra_lock_prefix (zi->res, path_prefix);
47     zi->server_path_prefix = (char *) xmalloc (strlen(path_prefix)+1);
48     strcpy (zi->server_path_prefix, path_prefix);
49
50     yaz_log (YLOG_DEBUG, "Locking system initialized");
51     return 0;
52 }
53
54 int zebra_server_lock_destroy (ZebraService zi)
55 {
56     xfree (zi->server_path_prefix);
57     zebra_lock_destroy (zi->server_lock_cmt);
58     zebra_lock_destroy (zi->server_lock_org);
59     yaz_log (YLOG_DEBUG, "Locking system destroyed");
60     return 0;
61 }
62
63 int zebra_server_lock (ZebraService zi, int commitPhase)
64 {
65     if (!zi->server_lock_cmt)
66     {
67         char path[1024];
68
69         strcpy (path, zi->server_path_prefix);
70         strcat (path, FNAME_COMMIT_LOCK);
71         if (!(zi->server_lock_cmt = zebra_lock_create (path, 0)))
72         {
73             yaz_log (YLOG_FATAL|YLOG_ERRNO, "create %s", path);
74             return -1;
75         }
76         assert (zi->server_lock_org == NULL);
77
78         strcpy (path, zi->server_path_prefix);
79         strcat (path, FNAME_ORG_LOCK);
80         if (!(zi->server_lock_org = zebra_lock_create (path, 0)))
81         {
82             yaz_log (YLOG_FATAL|YLOG_ERRNO, "create %s", path);
83             return -1;
84         }
85     }
86     if (commitPhase)
87     {
88         yaz_log (YLOG_DEBUG, "Server locks org");
89         zebra_lock (zi->server_lock_org);
90     }
91     else
92     {
93         yaz_log (YLOG_DEBUG, "Server locks cmt");
94         zebra_lock (zi->server_lock_cmt);
95     }
96     return 0;
97 }
98
99 void zebra_server_unlock (ZebraService zi, int commitPhase)
100 {
101     if (zi->server_lock_org == NULL)
102         return;
103     yaz_log (YLOG_DEBUG, "Server unlocks org");
104     zebra_unlock (zi->server_lock_org);
105     yaz_log (YLOG_DEBUG, "Server unlocks cmt");
106     zebra_unlock (zi->server_lock_cmt);
107 }
108
109 int zebra_server_lock_get_state (ZebraService zi, time_t *timep)
110 {
111     char path[1024];
112     char buf[256];
113     int fd;
114     struct stat xstat;
115     
116     strcpy (path, zi->server_path_prefix);
117     strcat (path, FNAME_TOUCH_TIME);
118     if (stat (path, &xstat) == -1)
119         *timep = 1;
120     else
121         *timep = xstat.st_mtime;
122
123     strcpy (path, zi->server_path_prefix);
124     strcat (path, FNAME_MAIN_LOCK);
125     fd = open (path, O_BINARY|O_RDONLY);
126     if (fd == -1)
127     {
128         *buf = 0;
129         return 0;
130     }
131     if (read (fd, buf, 2) == 0)
132     {
133         *buf = 0;
134         return 0;
135     }
136     close (fd);
137     return *buf;
138 }