2 * Copyright (C) 1994-1997, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.10 1997-09-29 09:08:36 adam
8 * Revised locking system to be thread safe for the server.
10 * Revision 1.9 1997/09/25 14:54:43 adam
11 * WIN32 files lock support.
13 * Revision 1.8 1997/09/17 12:19:15 adam
14 * Zebra version corresponds to YAZ version 1.4.
15 * Changed Zebra server so that it doesn't depend on global common_resource.
17 * Revision 1.7 1997/09/04 13:58:04 adam
18 * Added O_BINARY for open calls.
20 * Revision 1.6 1996/10/29 14:06:52 adam
21 * Include zebrautl.h instead of alexutil.h.
23 * Revision 1.5 1996/05/15 11:58:18 adam
24 * Changed some log messages.
26 * Revision 1.4 1996/04/10 16:01:27 quinn
27 * Fixed order of path/filename.
29 * Revision 1.3 1995/12/11 11:43:29 adam
30 * Locking based on fcntl instead of flock.
31 * Setting commitEnable removed. Command line option -n can be used to
32 * prevent commit if commit setting is defined in the configuration file.
34 * Revision 1.2 1995/12/08 16:22:55 adam
35 * Work on update while servers are running. Three lock files introduced.
36 * The servers reload their registers when necessary, but they don't
37 * reestablish result sets yet.
39 * Revision 1.1 1995/12/07 17:38:47 adam
40 * Work locking mechanisms for concurrent updates/commit.
57 int zebra_server_lock_init (ZServerInfo *zi)
59 char path_prefix[1024];
62 zi->server_lock_cmt = NULL;
63 zi->server_lock_org = NULL;
65 zebra_lock_prefix (zi->res, path_prefix);
66 zi->server_path_prefix = xmalloc (strlen(path_prefix)+1);
67 strcpy (zi->server_path_prefix, path_prefix);
69 logf (LOG_DEBUG, "Locking system initialized");
73 int zebra_server_lock_destroy (ZServerInfo *zi)
75 xfree (zi->server_path_prefix);
76 zebra_lock_destroy (zi->server_lock_cmt);
77 zebra_lock_destroy (zi->server_lock_org);
78 logf (LOG_DEBUG, "Locking system destroyed");
82 int zebra_server_lock (ZServerInfo *zi, int commitPhase)
84 if (!zi->server_lock_cmt)
88 strcpy (path, zi->server_path_prefix);
89 strcat (path, FNAME_COMMIT_LOCK);
90 if (!(zi->server_lock_cmt = zebra_lock_create (path, 0)))
92 logf (LOG_FATAL|LOG_ERRNO, "create %s", path);
95 assert (zi->server_lock_org == NULL);
97 strcpy (path, zi->server_path_prefix);
98 strcat (path, FNAME_ORG_LOCK);
99 if (!(zi->server_lock_org = zebra_lock_create (path, 0)))
101 logf (LOG_FATAL|LOG_ERRNO, "create %s", path);
107 logf (LOG_DEBUG, "Server locks org");
108 zebra_lock (zi->server_lock_org);
112 logf (LOG_DEBUG, "Server locks cmt");
113 zebra_lock (zi->server_lock_cmt);
118 void zebra_server_unlock (ZServerInfo *zi, int commitPhase)
120 if (zi->server_lock_org == NULL)
124 logf (LOG_DEBUG, "Server unlocks org");
125 zebra_unlock (zi->server_lock_org);
129 logf (LOG_DEBUG, "Server unlocks cmt");
130 zebra_unlock (zi->server_lock_cmt);
134 int zebra_server_lock_get_state (ZServerInfo *zi, time_t *timep)
141 strcpy (path, zi->server_path_prefix);
142 strcat (path, FNAME_TOUCH_TIME);
143 if (stat (path, &xstat) == -1)
146 *timep = xstat.st_ctime;
148 strcpy (path, zi->server_path_prefix);
149 strcat (path, FNAME_MAIN_LOCK);
150 fd = open (path, O_BINARY|O_RDONLY);
156 if (read (fd, buf, 2) == 0)