1 /* $Id: locksrv.c,v 1.20 2005-06-14 20:28:54 adam Exp $
2 Copyright (C) 1995-2005
5 This file is part of the Zebra server.
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
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
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
39 int zebra_server_lock_init (ZebraService zi)
41 char path_prefix[1024];
43 zi->server_lock_cmt = NULL;
44 zi->server_lock_org = NULL;
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);
50 yaz_log (YLOG_DEBUG, "Locking system initialized");
54 int zebra_server_lock_destroy (ZebraService zi)
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");
63 int zebra_server_lock (ZebraService zi, int commitPhase)
65 if (!zi->server_lock_cmt)
69 strcpy (path, zi->server_path_prefix);
70 strcat (path, FNAME_COMMIT_LOCK);
71 if (!(zi->server_lock_cmt = zebra_lock_create (path, 0)))
73 yaz_log (YLOG_FATAL|YLOG_ERRNO, "create %s", path);
76 assert (zi->server_lock_org == NULL);
78 strcpy (path, zi->server_path_prefix);
79 strcat (path, FNAME_ORG_LOCK);
80 if (!(zi->server_lock_org = zebra_lock_create (path, 0)))
82 yaz_log (YLOG_FATAL|YLOG_ERRNO, "create %s", path);
88 yaz_log (YLOG_DEBUG, "Server locks org");
89 zebra_lock (zi->server_lock_org);
93 yaz_log (YLOG_DEBUG, "Server locks cmt");
94 zebra_lock (zi->server_lock_cmt);
99 void zebra_server_unlock (ZebraService zi, int commitPhase)
101 if (zi->server_lock_org == NULL)
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);
109 int zebra_server_lock_get_state (ZebraService zi, time_t *timep)
116 strcpy (path, zi->server_path_prefix);
117 strcat (path, FNAME_TOUCH_TIME);
118 if (stat (path, &xstat) == -1)
121 *timep = xstat.st_mtime;
123 strcpy (path, zi->server_path_prefix);
124 strcat (path, FNAME_MAIN_LOCK);
125 fd = open (path, O_BINARY|O_RDONLY);
131 if (read (fd, buf, 2) == 0)