rename ctdb.h to ctdb_protocol.h
[sahlberg/ctdb.git] / server / ctdb_banning.c
1 /* 
2    ctdb banning code
3
4    Copyright (C) Ronnie Sahlberg  2009
5
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.
10    
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.
15    
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/>.
18 */
19 #include "includes.h"
20 #include "lib/events/events.h"
21 #include "lib/tdb/include/tdb.h"
22 #include "system/time.h"
23 #include "system/network.h"
24 #include "system/filesys.h"
25 #include "system/wait.h"
26 #include "include/ctdb_protocol.h"
27 #include "include/ctdb_private.h"
28
29
30 static void
31 ctdb_ban_node_event(struct event_context *ev, struct timed_event *te, 
32                                struct timeval t, void *private_data)
33 {
34         struct ctdb_context *ctdb = talloc_get_type(private_data, struct ctdb_context);
35
36         DEBUG(DEBUG_ERR,("Banning timedout\n"));
37         ctdb->nodes[ctdb->pnn]->flags &= ~NODE_FLAGS_BANNED;
38
39         if (ctdb->banning_ctx != NULL) {
40                 talloc_free(ctdb->banning_ctx);
41                 ctdb->banning_ctx = NULL;
42         }
43 }
44
45 int32_t ctdb_control_set_ban_state(struct ctdb_context *ctdb, TDB_DATA indata)
46 {
47         struct ctdb_ban_time *bantime = (struct ctdb_ban_time *)indata.dptr;
48
49         DEBUG(DEBUG_INFO,("SET BAN STATE\n"));
50
51         if (bantime->pnn != ctdb->pnn) {
52                 if (bantime->pnn < 0 || bantime->pnn >= ctdb->num_nodes) {
53                         DEBUG(DEBUG_ERR,(__location__ " ERROR: Invalid ban request. PNN:%d is invalid. Max nodes %d\n", bantime->pnn, ctdb->num_nodes));
54                         return -1;
55                 }
56                 if (bantime->time == 0) {
57                         DEBUG(DEBUG_INFO,("unbanning node %d\n", bantime->pnn));
58                         ctdb->nodes[bantime->pnn]->flags &= ~NODE_FLAGS_BANNED;
59                 } else {
60                         DEBUG(DEBUG_INFO,("banning node %d\n", bantime->pnn));
61                         if (ctdb->tunable.enable_bans == 0) {
62                                 DEBUG(DEBUG_INFO,("Bans are disabled - ignoring ban of node %u\n", bantime->pnn));
63                                 return 0;
64                         }
65
66                         ctdb->nodes[bantime->pnn]->flags |= NODE_FLAGS_BANNED;
67                 }
68                 return 0;
69         }
70
71         if (ctdb->banning_ctx != NULL) {
72                 talloc_free(ctdb->banning_ctx);
73                 ctdb->banning_ctx = NULL;
74         }
75
76         if (bantime->time == 0) {
77                 DEBUG(DEBUG_ERR,("Unbanning this node\n"));
78                 ctdb->nodes[bantime->pnn]->flags &= ~NODE_FLAGS_BANNED;
79                 return 0;
80         }
81
82         if (ctdb->tunable.enable_bans == 0) {
83                 DEBUG(DEBUG_ERR,("Bans are disabled - ignoring ban of node %u\n", bantime->pnn));
84                 return 0;
85         }
86
87         ctdb->banning_ctx = talloc(ctdb, struct ctdb_ban_time);
88         if (ctdb->banning_ctx == NULL) {
89                 DEBUG(DEBUG_CRIT,(__location__ " ERROR Failed to allocate new banning state\n"));
90                 return -1;
91         }
92         *((struct ctdb_ban_time *)(ctdb->banning_ctx)) = *bantime;
93
94
95         DEBUG(DEBUG_ERR,("Banning this node for %d seconds\n", bantime->time));
96         ctdb->nodes[bantime->pnn]->flags |= NODE_FLAGS_BANNED;
97
98         event_add_timed(ctdb->ev, ctdb->banning_ctx, timeval_current_ofs(bantime->time,0), ctdb_ban_node_event, ctdb);
99         
100         return 0;
101 }
102
103 int32_t ctdb_control_get_ban_state(struct ctdb_context *ctdb, TDB_DATA *outdata)
104 {
105         struct ctdb_ban_time *bantime;
106
107         bantime = talloc(outdata, struct ctdb_ban_time);
108         CTDB_NO_MEMORY(ctdb, bantime);
109
110         if (ctdb->banning_ctx != NULL) {
111                 *bantime = *(struct ctdb_ban_time *)(ctdb->banning_ctx);
112         } else {
113                 bantime->pnn = ctdb->pnn;
114                 bantime->time = 0;
115         }
116
117         outdata->dptr  = (uint8_t *)bantime;
118         outdata->dsize = sizeof(struct ctdb_ban_time);
119
120         return 0;
121 }
122
123 /* Routine to ban ourselves for a while when trouble strikes. */
124 void ctdb_ban_self(struct ctdb_context *ctdb)
125 {
126         TDB_DATA data;
127         struct ctdb_ban_time bantime;
128
129         bantime.pnn  = ctdb->pnn;
130         bantime.time = ctdb->tunable.recovery_ban_period;
131
132         data.dsize = sizeof(bantime);
133         data.dptr  = (uint8_t *)&bantime;
134
135         ctdb_control_set_ban_state(ctdb, data);
136 }