4 Copyright (C) Andrew Tridgell 2007
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include "lib/events/events.h"
22 #include "lib/tdb/include/tdb.h"
23 #include "system/network.h"
24 #include "system/filesys.h"
25 #include "system/wait.h"
26 #include "../include/ctdb_private.h"
27 #include "lib/util/dlinklist.h"
34 static int ctdb_lock_all_databases(struct ctdb_context *ctdb)
36 struct ctdb_db_context *ctdb_db;
37 for (ctdb_db=ctdb->db_list;ctdb_db;ctdb_db=ctdb_db->next) {
38 if (tdb_lockall(ctdb_db->ltdb->tdb) != 0) {
46 a list of control requests waiting for a freeze lock child to get
49 struct ctdb_freeze_waiter {
50 struct ctdb_freeze_waiter *next, *prev;
51 struct ctdb_context *ctdb;
52 struct ctdb_req_control *c;
56 /* a handle to a freeze lock child process */
57 struct ctdb_freeze_handle {
58 struct ctdb_context *ctdb;
61 struct ctdb_freeze_waiter *waiters;
65 destroy a freeze handle
67 static int ctdb_freeze_handle_destructor(struct ctdb_freeze_handle *h)
69 h->ctdb->freeze_mode = CTDB_FREEZE_NONE;
70 kill(h->child, SIGKILL);
71 waitpid(h->child, NULL, 0);
76 called when the child writes its status to us
78 static void ctdb_freeze_lock_handler(struct event_context *ev, struct fd_event *fde,
79 uint16_t flags, void *private_data)
81 struct ctdb_freeze_handle *h = talloc_get_type(private_data, struct ctdb_freeze_handle);
83 struct ctdb_freeze_waiter *w;
85 if (h->ctdb->freeze_mode == CTDB_FREEZE_FROZEN) {
86 DEBUG(0,("freeze child died - unfreezing\n"));
91 if (read(h->fd, &status, sizeof(status)) != sizeof(status)) {
92 DEBUG(0,("read error from freeze lock child\n"));
97 DEBUG(0,("Failed to get locks in ctdb_freeze_child\n"));
98 /* we didn't get the locks - destroy the handle */
103 h->ctdb->freeze_mode = CTDB_FREEZE_FROZEN;
105 /* notify the waiters */
106 while ((w = h->ctdb->freeze_handle->waiters)) {
108 DLIST_REMOVE(h->ctdb->freeze_handle->waiters, w);
114 create a child which gets locks on all the open databases, then calls the callback telling the parent
117 static struct ctdb_freeze_handle *ctdb_freeze_lock(struct ctdb_context *ctdb)
119 struct ctdb_freeze_handle *h;
121 struct fd_event *fde;
123 h = talloc_zero(ctdb, struct ctdb_freeze_handle);
124 CTDB_NO_MEMORY_VOID(ctdb, h);
128 /* use socketpair() instead of pipe() so we have bi-directional fds */
129 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fd) != 0) {
130 DEBUG(0,("Failed to create pipe for ctdb_freeze_lock\n"));
136 if (h->child == -1) {
137 DEBUG(0,("Failed to fork child for ctdb_freeze_lock\n"));
146 ret = ctdb_lock_all_databases(ctdb);
150 write(fd[1], &ret, sizeof(ret));
151 /* the read here means we will die if the parent exits */
152 read(fd[1], &ret, sizeof(ret));
156 talloc_set_destructor(h, ctdb_freeze_handle_destructor);
162 fde = event_add_fd(ctdb->ev, h, h->fd, EVENT_FD_READ|EVENT_FD_AUTOCLOSE,
163 ctdb_freeze_lock_handler, h);
165 DEBUG(0,("Failed to setup fd event for ctdb_freeze_lock\n"));
175 destroy a waiter for a freeze mode change
177 static int ctdb_freeze_waiter_destructor(struct ctdb_freeze_waiter *w)
179 DLIST_REMOVE(w->ctdb->freeze_handle->waiters, w);
180 ctdb_request_control_reply(w->ctdb, w->c, NULL, w->status, NULL);
185 start the freeze process
187 void ctdb_start_freeze(struct ctdb_context *ctdb)
189 if (ctdb->freeze_mode == CTDB_FREEZE_FROZEN) {
190 /* we're already frozen */
194 /* if there isn't a freeze lock child then create one */
195 if (!ctdb->freeze_handle) {
196 ctdb->freeze_handle = ctdb_freeze_lock(ctdb);
197 CTDB_NO_MEMORY_VOID(ctdb, ctdb->freeze_handle);
198 ctdb->freeze_mode = CTDB_FREEZE_PENDING;
205 int32_t ctdb_control_freeze(struct ctdb_context *ctdb, struct ctdb_req_control *c, bool *async_reply)
207 struct ctdb_freeze_waiter *w;
209 if (ctdb->freeze_mode == CTDB_FREEZE_FROZEN) {
210 /* we're already frozen */
214 ctdb_start_freeze(ctdb);
216 /* add ourselves to list of waiters */
217 w = talloc(ctdb->freeze_handle, struct ctdb_freeze_waiter);
218 CTDB_NO_MEMORY(ctdb, w);
220 w->c = talloc_steal(w, c);
222 talloc_set_destructor(w, ctdb_freeze_waiter_destructor);
223 DLIST_ADD(ctdb->freeze_handle->waiters, w);
225 /* we won't reply till later */
232 block until we are frozen, used during daemon startup
234 bool ctdb_blocking_freeze(struct ctdb_context *ctdb)
236 ctdb_start_freeze(ctdb);
238 /* block until frozen */
239 while (ctdb->freeze_mode == CTDB_FREEZE_PENDING) {
240 event_loop_once(ctdb->ev);
243 return ctdb->freeze_mode == CTDB_FREEZE_FROZEN;
251 int32_t ctdb_control_thaw(struct ctdb_context *ctdb)
253 talloc_free(ctdb->freeze_handle);
254 ctdb->freeze_handle = NULL;
255 ctdb_call_resend_all(ctdb);