1 /* $Id: locksrv.c,v 1.17 2002-08-02 19:26:55 adam Exp $
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002
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
38 int zebra_server_lock_init (ZebraService zi)
40 char path_prefix[1024];
42 zi->server_lock_cmt = NULL;
43 zi->server_lock_org = NULL;
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);
49 logf (LOG_DEBUG, "Locking system initialized");
53 int zebra_server_lock_destroy (ZebraService zi)
55 xfree (zi->server_path_prefix);
56 zebra_lock_destroy (zi->server_lock_cmt);
57 zebra_lock_destroy (zi->server_lock_org);
58 logf (LOG_DEBUG, "Locking system destroyed");
62 int zebra_server_lock (ZebraService zi, int commitPhase)
64 if (!zi->server_lock_cmt)
68 strcpy (path, zi->server_path_prefix);
69 strcat (path, FNAME_COMMIT_LOCK);
70 if (!(zi->server_lock_cmt = zebra_lock_create (path, 0)))
72 logf (LOG_FATAL|LOG_ERRNO, "create %s", path);
75 assert (zi->server_lock_org == NULL);
77 strcpy (path, zi->server_path_prefix);
78 strcat (path, FNAME_ORG_LOCK);
79 if (!(zi->server_lock_org = zebra_lock_create (path, 0)))
81 logf (LOG_FATAL|LOG_ERRNO, "create %s", path);
87 logf (LOG_DEBUG, "Server locks org");
88 zebra_lock (zi->server_lock_org);
92 logf (LOG_DEBUG, "Server locks cmt");
93 zebra_lock (zi->server_lock_cmt);
98 void zebra_server_unlock (ZebraService zi, int commitPhase)
100 if (zi->server_lock_org == NULL)
102 logf (LOG_DEBUG, "Server unlocks org");
103 zebra_unlock (zi->server_lock_org);
104 logf (LOG_DEBUG, "Server unlocks cmt");
105 zebra_unlock (zi->server_lock_cmt);
108 int zebra_server_lock_get_state (ZebraService zi, time_t *timep)
115 strcpy (path, zi->server_path_prefix);
116 strcat (path, FNAME_TOUCH_TIME);
117 if (stat (path, &xstat) == -1)
120 *timep = xstat.st_mtime;
122 strcpy (path, zi->server_path_prefix);
123 strcat (path, FNAME_MAIN_LOCK);
124 fd = open (path, O_BINARY|O_RDONLY);
130 if (read (fd, buf, 2) == 0)