s3: smbd: Factor out setting up case parameters for a share to a function - conn_setu...
[Samba.git] / ctdb / server / ctdb_banning.c
blob3c711575e8cebae4eb11c830b029a9529e0848a2
1 /*
2 ctdb banning code
4 Copyright (C) Ronnie Sahlberg 2009
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 3 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, see <http://www.gnu.org/licenses/>.
19 #include "replace.h"
20 #include "system/time.h"
21 #include "system/network.h"
22 #include "system/filesys.h"
23 #include "system/wait.h"
25 #include <talloc.h>
26 #include <tevent.h>
28 #include "lib/util/debug.h"
29 #include "lib/util/samba_util.h"
31 #include "ctdb_private.h"
32 #include "ctdb_client.h"
34 #include "common/common.h"
35 #include "common/logging.h"
37 static void ctdb_ban_node_event(struct tevent_context *ev,
38 struct tevent_timer *te,
39 struct timeval t, void *private_data)
41 struct ctdb_context *ctdb = talloc_get_type(private_data, struct ctdb_context);
43 /* Make sure we were able to freeze databases during banning */
44 if (!ctdb_db_all_frozen(ctdb)) {
45 DEBUG(DEBUG_ERR, ("Banning timed out, but not all databases "
46 "frozen yet - banning this node again.\n"));
47 ctdb_ban_self(ctdb);
48 return;
51 DEBUG(DEBUG_ERR,("Banning timed out\n"));
52 ctdb->nodes[ctdb->pnn]->flags &= ~NODE_FLAGS_BANNED;
54 if (ctdb->banning_ctx != NULL) {
55 talloc_free(ctdb->banning_ctx);
56 ctdb->banning_ctx = NULL;
60 int32_t ctdb_control_set_ban_state(struct ctdb_context *ctdb, TDB_DATA indata)
62 struct ctdb_ban_state *bantime = (struct ctdb_ban_state *)indata.dptr;
63 bool already_banned;
65 DEBUG(DEBUG_INFO,("SET BAN STATE\n"));
67 if (bantime->pnn != ctdb->pnn) {
68 DEBUG(DEBUG_WARNING,
69 ("SET_BAN_STATE control for PNN %d ignored\n",
70 bantime->pnn));
71 return -1;
74 already_banned = false;
75 if (ctdb->banning_ctx != NULL) {
76 talloc_free(ctdb->banning_ctx);
77 ctdb->banning_ctx = NULL;
78 already_banned = true;
81 if (bantime->time == 0) {
82 DEBUG(DEBUG_ERR,("Unbanning this node\n"));
83 ctdb->nodes[bantime->pnn]->flags &= ~NODE_FLAGS_BANNED;
84 return 0;
87 if (ctdb->tunable.enable_bans == 0) {
88 DEBUG(DEBUG_ERR,("Bans are disabled - ignoring ban of node %u\n", bantime->pnn));
89 return 0;
92 ctdb->banning_ctx = talloc(ctdb, struct ctdb_ban_state);
93 if (ctdb->banning_ctx == NULL) {
94 DEBUG(DEBUG_CRIT,(__location__ " ERROR Failed to allocate new banning state\n"));
95 return -1;
97 *((struct ctdb_ban_state *)(ctdb->banning_ctx)) = *bantime;
100 DEBUG(DEBUG_ERR,("Banning this node for %d seconds\n", bantime->time));
101 ctdb->nodes[bantime->pnn]->flags |= NODE_FLAGS_BANNED;
103 tevent_add_timer(ctdb->ev, ctdb->banning_ctx,
104 timeval_current_ofs(bantime->time,0),
105 ctdb_ban_node_event, ctdb);
107 if (!already_banned) {
108 ctdb_node_become_inactive(ctdb);
110 return 0;
113 int32_t ctdb_control_get_ban_state(struct ctdb_context *ctdb, TDB_DATA *outdata)
115 struct ctdb_ban_state *bantime;
117 bantime = talloc(outdata, struct ctdb_ban_state);
118 CTDB_NO_MEMORY(ctdb, bantime);
120 if (ctdb->banning_ctx != NULL) {
121 *bantime = *(struct ctdb_ban_state *)(ctdb->banning_ctx);
122 } else {
123 bantime->pnn = ctdb->pnn;
124 bantime->time = 0;
127 outdata->dptr = (uint8_t *)bantime;
128 outdata->dsize = sizeof(struct ctdb_ban_state);
130 return 0;
133 /* Routine to ban ourselves for a while when trouble strikes. */
134 void ctdb_ban_self(struct ctdb_context *ctdb)
136 TDB_DATA data;
137 struct ctdb_ban_state bantime;
139 bantime.pnn = ctdb->pnn;
140 bantime.time = ctdb->tunable.recovery_ban_period;
142 data.dsize = sizeof(bantime);
143 data.dptr = (uint8_t *)&bantime;
145 ctdb_control_set_ban_state(ctdb, data);