ede2c492e75f8a5a8dfd5565b6d9dd067741bb57
[vlendec/samba-autobuild/.git] / ctdb / server / ctdb_control.c
1 /* 
2    ctdb_control protocol code
3
4    Copyright (C) Andrew Tridgell  2007
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 2 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, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20 #include "includes.h"
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"
28 #include "db_wrap.h"
29
30 struct ctdb_control_state {
31         struct ctdb_context *ctdb;
32         uint32_t reqid;
33         ctdb_control_callback_fn_t callback;
34         void *private_data;
35         unsigned flags;
36 };
37
38 /*
39   process a control request
40  */
41 static int32_t ctdb_control_dispatch(struct ctdb_context *ctdb, 
42                                      struct ctdb_req_control *c,
43                                      TDB_DATA indata,
44                                      TDB_DATA *outdata, uint32_t srcnode,
45                                      const char **errormsg,
46                                      bool *async_reply)
47 {
48         uint32_t opcode = c->opcode;
49         uint64_t srvid = c->srvid;
50         uint32_t client_id = c->client_id;
51
52         switch (opcode) {
53         case CTDB_CONTROL_PROCESS_EXISTS: {
54                 CHECK_CONTROL_DATA_SIZE(sizeof(pid_t));
55                 return kill(*(pid_t *)indata.dptr, 0);
56         }
57
58         case CTDB_CONTROL_SET_DEBUG: {
59                 CHECK_CONTROL_DATA_SIZE(sizeof(uint32_t));
60                 LogLevel = *(uint32_t *)indata.dptr;
61                 return 0;
62         }
63
64         case CTDB_CONTROL_GET_DEBUG: {
65                 CHECK_CONTROL_DATA_SIZE(0);
66                 outdata->dptr = (uint8_t *)&LogLevel;
67                 outdata->dsize = sizeof(LogLevel);
68                 return 0;
69         }
70
71         case CTDB_CONTROL_STATISTICS: {
72                 CHECK_CONTROL_DATA_SIZE(0);
73                 ctdb->statistics.memory_used = talloc_total_size(ctdb);
74                 ctdb->statistics.frozen = (ctdb->freeze_mode == CTDB_FREEZE_FROZEN);
75                 ctdb->statistics.recovering = (ctdb->recovery_mode == CTDB_RECOVERY_ACTIVE);
76                 outdata->dptr = (uint8_t *)&ctdb->statistics;
77                 outdata->dsize = sizeof(ctdb->statistics);
78                 return 0;
79         }
80
81         case CTDB_CONTROL_GET_ALL_TUNABLES: {
82                 CHECK_CONTROL_DATA_SIZE(0);
83                 outdata->dptr = (uint8_t *)&ctdb->tunable;
84                 outdata->dsize = sizeof(ctdb->tunable);
85                 return 0;
86         }
87
88         case CTDB_CONTROL_DUMP_MEMORY: {
89                 CHECK_CONTROL_DATA_SIZE(0);
90                 talloc_report_full(ctdb, stdout);
91                 return 0;
92         }
93
94         case CTDB_CONTROL_STATISTICS_RESET: {
95                 CHECK_CONTROL_DATA_SIZE(0);
96                 ZERO_STRUCT(ctdb->statistics);
97                 return 0;
98         }
99
100         case CTDB_CONTROL_GETVNNMAP:
101                 return ctdb_control_getvnnmap(ctdb, opcode, indata, outdata);
102
103         case CTDB_CONTROL_GET_DBMAP:
104                 return ctdb_control_getdbmap(ctdb, opcode, indata, outdata);
105
106         case CTDB_CONTROL_GET_NODEMAP:
107                 return ctdb_control_getnodemap(ctdb, opcode, indata, outdata);
108
109         case CTDB_CONTROL_SETVNNMAP:
110                 return ctdb_control_setvnnmap(ctdb, opcode, indata, outdata);
111
112         case CTDB_CONTROL_PULL_DB: 
113                 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_control_pulldb));
114                 return ctdb_control_pull_db(ctdb, indata, outdata);
115
116         case CTDB_CONTROL_SET_DMASTER: 
117                 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_control_set_dmaster));
118                 return ctdb_control_set_dmaster(ctdb, indata);
119
120         case CTDB_CONTROL_PUSH_DB:
121                 return ctdb_control_push_db(ctdb, indata);
122
123         case CTDB_CONTROL_GET_RECMODE: {
124                 return ctdb->recovery_mode;
125         }
126
127         case CTDB_CONTROL_SET_RECMASTER: {
128                 CHECK_CONTROL_DATA_SIZE(sizeof(uint32_t));
129                 if (ctdb->freeze_mode != CTDB_FREEZE_FROZEN) {
130                         DEBUG(0,("Attempt to set recmaster when not frozen\n"));
131                         return -1;
132                 }
133                 ctdb->recovery_master = ((uint32_t *)(&indata.dptr[0]))[0];
134                 return 0;
135         }
136
137         case CTDB_CONTROL_GET_RECMASTER:
138                 return ctdb->recovery_master;
139
140         case CTDB_CONTROL_GET_PID:
141                 return getpid();
142
143         case CTDB_CONTROL_GET_VNN:
144                 return ctdb->vnn;
145
146         case CTDB_CONTROL_PING:
147                 CHECK_CONTROL_DATA_SIZE(0);
148                 return ctdb->statistics.num_clients;
149
150         case CTDB_CONTROL_GET_DBNAME: {
151                 uint32_t db_id;
152                 struct ctdb_db_context *ctdb_db;
153
154                 CHECK_CONTROL_DATA_SIZE(sizeof(db_id));
155                 db_id = *(uint32_t *)indata.dptr;
156                 ctdb_db = find_ctdb_db(ctdb, db_id);
157                 if (ctdb_db == NULL) return -1;
158                 outdata->dptr = discard_const(ctdb_db->db_name);
159                 outdata->dsize = strlen(ctdb_db->db_name)+1;
160                 return 0;
161         }
162
163         case CTDB_CONTROL_GETDBPATH: {
164                 uint32_t db_id;
165                 struct ctdb_db_context *ctdb_db;
166
167                 CHECK_CONTROL_DATA_SIZE(sizeof(db_id));
168                 db_id = *(uint32_t *)indata.dptr;
169                 ctdb_db = find_ctdb_db(ctdb, db_id);
170                 if (ctdb_db == NULL) return -1;
171                 outdata->dptr = discard_const(ctdb_db->db_path);
172                 outdata->dsize = strlen(ctdb_db->db_path)+1;
173                 return 0;
174         }
175
176         case CTDB_CONTROL_DB_ATTACH:
177                 return ctdb_control_db_attach(ctdb, indata, outdata);
178
179         case CTDB_CONTROL_SET_CALL: {
180                 struct ctdb_control_set_call *sc = 
181                         (struct ctdb_control_set_call *)indata.dptr;
182                 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_control_set_call));
183                 return ctdb_daemon_set_call(ctdb, sc->db_id, sc->fn, sc->id);
184         }
185
186         case CTDB_CONTROL_TRAVERSE_START:
187                 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_traverse_start));
188                 return ctdb_control_traverse_start(ctdb, indata, outdata, srcnode);
189
190         case CTDB_CONTROL_TRAVERSE_ALL:
191                 return ctdb_control_traverse_all(ctdb, indata, outdata);
192
193         case CTDB_CONTROL_TRAVERSE_DATA:
194                 return ctdb_control_traverse_data(ctdb, indata, outdata);
195
196         case CTDB_CONTROL_REGISTER_SRVID:
197                 return daemon_register_message_handler(ctdb, client_id, srvid);
198
199         case CTDB_CONTROL_DEREGISTER_SRVID:
200                 return daemon_deregister_message_handler(ctdb, client_id, srvid);
201
202         case CTDB_CONTROL_ENABLE_SEQNUM:
203                 CHECK_CONTROL_DATA_SIZE(sizeof(uint32_t));
204                 return ctdb_ltdb_enable_seqnum(ctdb, *(uint32_t *)indata.dptr);
205
206         case CTDB_CONTROL_UPDATE_SEQNUM:
207                 CHECK_CONTROL_DATA_SIZE(sizeof(uint32_t));              
208                 return ctdb_ltdb_update_seqnum(ctdb, *(uint32_t *)indata.dptr, srcnode);
209
210         case CTDB_CONTROL_FREEZE:
211                 CHECK_CONTROL_DATA_SIZE(0);
212                 return ctdb_control_freeze(ctdb, c, async_reply);
213
214         case CTDB_CONTROL_THAW:
215                 CHECK_CONTROL_DATA_SIZE(0);
216                 return ctdb_control_thaw(ctdb);
217
218         case CTDB_CONTROL_SET_RECMODE:
219                 CHECK_CONTROL_DATA_SIZE(sizeof(uint32_t));              
220                 return ctdb_control_set_recmode(ctdb, c, indata, async_reply, errormsg);
221
222         case CTDB_CONTROL_SET_MONMODE:
223                 CHECK_CONTROL_DATA_SIZE(sizeof(uint32_t));              
224                 ctdb->monitoring_mode = *(uint32_t *)indata.dptr;
225                 return 0;
226
227         case CTDB_CONTROL_GET_MONMODE: 
228                 return ctdb->monitoring_mode;
229
230         case CTDB_CONTROL_SHUTDOWN:
231                 ctdb_release_all_ips(ctdb);
232                 ctdb->methods->shutdown(ctdb);
233                 ctdb_event_script(ctdb, "shutdown");
234                 DEBUG(0,("shutting down\n"));
235                 exit(0);
236
237         case CTDB_CONTROL_MAX_RSN: 
238                 CHECK_CONTROL_DATA_SIZE(sizeof(uint32_t));
239                 return ctdb_control_max_rsn(ctdb, indata, outdata);
240
241         case CTDB_CONTROL_SET_RSN_NONEMPTY: 
242                 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_control_set_rsn_nonempty));
243                 return ctdb_control_set_rsn_nonempty(ctdb, indata, outdata);
244
245         case CTDB_CONTROL_TAKEOVER_IP:
246                 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_public_ip));
247                 return ctdb_control_takeover_ip(ctdb, c, indata, async_reply);
248
249         case CTDB_CONTROL_RELEASE_IP:
250                 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_public_ip));
251                 return ctdb_control_release_ip(ctdb, c, indata, async_reply);
252
253         case CTDB_CONTROL_GET_PUBLIC_IPS:
254                 CHECK_CONTROL_DATA_SIZE(0);
255                 return ctdb_control_get_public_ips(ctdb, c, outdata);
256
257         case CTDB_CONTROL_DELETE_LOW_RSN: 
258                 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_control_delete_low_rsn));
259                 return ctdb_control_delete_low_rsn(ctdb, indata, outdata);
260
261         case CTDB_CONTROL_TCP_CLIENT: 
262                 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_control_tcp));
263                 return ctdb_control_tcp_client(ctdb, client_id, srcnode, indata);
264
265         case CTDB_CONTROL_STARTUP: 
266                 CHECK_CONTROL_DATA_SIZE(0);
267                 return ctdb_control_startup(ctdb, srcnode);
268
269         case CTDB_CONTROL_TCP_ADD: 
270                 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_control_tcp_vnn));
271                 return ctdb_control_tcp_add(ctdb, indata);
272
273         case CTDB_CONTROL_TCP_REMOVE: 
274                 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_control_tcp_vnn));
275                 return ctdb_control_tcp_remove(ctdb, indata);
276
277         case CTDB_CONTROL_SET_TUNABLE:
278                 return ctdb_control_set_tunable(ctdb, indata);
279
280         case CTDB_CONTROL_GET_TUNABLE:
281                 return ctdb_control_get_tunable(ctdb, indata, outdata);
282
283         case CTDB_CONTROL_LIST_TUNABLES:
284                 return ctdb_control_list_tunables(ctdb, outdata);
285
286         case CTDB_CONTROL_MODIFY_FLAGS:
287                 CHECK_CONTROL_DATA_SIZE(sizeof(struct ctdb_node_modflags));
288                 return ctdb_control_modflags(ctdb, indata);
289
290         default:
291                 DEBUG(0,(__location__ " Unknown CTDB control opcode %u\n", opcode));
292                 return -1;
293         }
294 }
295
296
297 /*
298   send a reply for a ctdb control
299  */
300 void ctdb_request_control_reply(struct ctdb_context *ctdb, struct ctdb_req_control *c,
301                                 TDB_DATA *outdata, int32_t status, const char *errormsg)
302 {
303         struct ctdb_reply_control *r;
304         size_t len;
305         
306         /* some controls send no reply */
307         if (c->flags & CTDB_CTRL_FLAG_NOREPLY) {
308                 return;
309         }
310
311         len = offsetof(struct ctdb_reply_control, data) + (outdata?outdata->dsize:0);
312         if (errormsg) {
313                 len += strlen(errormsg);
314         }
315         r = ctdb_transport_allocate(ctdb, ctdb, CTDB_REPLY_CONTROL, len, struct ctdb_reply_control);
316         CTDB_NO_MEMORY_VOID(ctdb, r);
317
318         r->hdr.destnode     = c->hdr.srcnode;
319         r->hdr.reqid        = c->hdr.reqid;
320         r->status           = status;
321         r->datalen          = outdata?outdata->dsize:0;
322         if (outdata && outdata->dsize) {
323                 memcpy(&r->data[0], outdata->dptr, outdata->dsize);
324         }
325         if (errormsg) {
326                 r->errorlen = strlen(errormsg);
327                 memcpy(&r->data[r->datalen], errormsg, r->errorlen);
328         }
329         
330         ctdb_queue_packet(ctdb, &r->hdr);       
331
332         talloc_free(r);
333 }
334
335 /*
336   called when a CTDB_REQ_CONTROL packet comes in
337 */
338 void ctdb_request_control(struct ctdb_context *ctdb, struct ctdb_req_header *hdr)
339 {
340         struct ctdb_req_control *c = (struct ctdb_req_control *)hdr;
341         TDB_DATA data, *outdata;
342         int32_t status;
343         bool async_reply = False;
344         const char *errormsg = NULL;
345
346         data.dptr = &c->data[0];
347         data.dsize = c->datalen;
348
349         outdata = talloc_zero(c, TDB_DATA);
350
351         status = ctdb_control_dispatch(ctdb, c, data, outdata, hdr->srcnode, 
352                                        &errormsg, &async_reply);
353
354         if (!async_reply) {
355                 ctdb_request_control_reply(ctdb, c, outdata, status, errormsg);
356         }
357 }
358
359 /*
360   called when a CTDB_REPLY_CONTROL packet comes in
361 */
362 void ctdb_reply_control(struct ctdb_context *ctdb, struct ctdb_req_header *hdr)
363 {
364         struct ctdb_reply_control *c = (struct ctdb_reply_control *)hdr;
365         TDB_DATA data;
366         struct ctdb_control_state *state;
367         const char *errormsg = NULL;
368
369         state = ctdb_reqid_find(ctdb, hdr->reqid, struct ctdb_control_state);
370         if (state == NULL) {
371                 DEBUG(0,("vnn %u Invalid reqid %u in ctdb_reply_control\n",
372                          ctdb->vnn, hdr->reqid));
373                 return;
374         }
375
376         if (hdr->reqid != state->reqid) {
377                 /* we found a record  but it was the wrong one */
378                 DEBUG(0, ("Dropped orphaned control reply with reqid:%u\n", hdr->reqid));
379                 return;
380         }
381
382         data.dptr = &c->data[0];
383         data.dsize = c->datalen;
384         if (c->errorlen) {
385                 errormsg = talloc_strndup(state, 
386                                           (char *)&c->data[c->datalen], c->errorlen);
387         }
388
389         /* make state a child of the packet, so it goes away when the packet
390            is freed. */
391         talloc_steal(hdr, state);
392
393         state->callback(ctdb, c->status, data, errormsg, state->private_data);
394 }
395
396 static int ctdb_control_destructor(struct ctdb_control_state *state)
397 {
398         ctdb_reqid_remove(state->ctdb, state->reqid);
399         return 0;
400 }
401
402 /*
403   handle a timeout of a control
404  */
405 static void ctdb_control_timeout(struct event_context *ev, struct timed_event *te, 
406                        struct timeval t, void *private_data)
407 {
408         struct ctdb_control_state *state = talloc_get_type(private_data, struct ctdb_control_state);
409         TALLOC_CTX *tmp_ctx = talloc_new(ev);
410
411         state->ctdb->statistics.timeouts.control++;
412
413         talloc_steal(tmp_ctx, state);
414
415         state->callback(state->ctdb, -1, tdb_null,
416                         "ctdb_control timed out", 
417                         state->private_data);
418         talloc_free(tmp_ctx);
419 }
420
421
422 /*
423   send a control message to a node
424  */
425 int ctdb_daemon_send_control(struct ctdb_context *ctdb, uint32_t destnode,
426                              uint64_t srvid, uint32_t opcode, uint32_t client_id,
427                              uint32_t flags,
428                              TDB_DATA data,
429                              ctdb_control_callback_fn_t callback,
430                              void *private_data)
431 {
432         struct ctdb_req_control *c;
433         struct ctdb_control_state *state;
434         size_t len;
435
436         if (((destnode == CTDB_BROADCAST_VNNMAP) || 
437              (destnode == CTDB_BROADCAST_ALL) ||
438              (destnode == CTDB_BROADCAST_CONNECTED)) && 
439             !(flags & CTDB_CTRL_FLAG_NOREPLY)) {
440                 DEBUG(0,("Attempt to broadcast control without NOREPLY\n"));
441                 return -1;
442         }
443
444         if (destnode != CTDB_BROADCAST_VNNMAP && 
445             destnode != CTDB_BROADCAST_ALL && 
446             destnode != CTDB_BROADCAST_CONNECTED && 
447             (!ctdb_validate_vnn(ctdb, destnode) || 
448              (ctdb->nodes[destnode]->flags & NODE_FLAGS_DISCONNECTED))) {
449                 if (!(flags & CTDB_CTRL_FLAG_NOREPLY)) {
450                         callback(ctdb, -1, tdb_null, "ctdb_control to disconnected node", private_data);
451                 }
452                 return 0;
453         }
454
455         /* the state is made a child of private_data if possible. This means any reply
456            will be discarded if the private_data goes away */
457         state = talloc(private_data?private_data:ctdb, struct ctdb_control_state);
458         CTDB_NO_MEMORY(ctdb, state);
459
460         state->reqid = ctdb_reqid_new(ctdb, state);
461         state->callback = callback;
462         state->private_data = private_data;
463         state->ctdb = ctdb;
464         state->flags = flags;
465
466         talloc_set_destructor(state, ctdb_control_destructor);
467
468         len = offsetof(struct ctdb_req_control, data) + data.dsize;
469         c = ctdb_transport_allocate(ctdb, state, CTDB_REQ_CONTROL, len, 
470                                     struct ctdb_req_control);
471         CTDB_NO_MEMORY(ctdb, c);
472         talloc_set_name_const(c, "ctdb_req_control packet");
473
474         c->hdr.destnode     = destnode;
475         c->hdr.reqid        = state->reqid;
476         c->opcode           = opcode;
477         c->client_id        = client_id;
478         c->flags            = flags;
479         c->srvid            = srvid;
480         c->datalen          = data.dsize;
481         if (data.dsize) {
482                 memcpy(&c->data[0], data.dptr, data.dsize);
483         }
484
485         ctdb_queue_packet(ctdb, &c->hdr);       
486
487         if (flags & CTDB_CTRL_FLAG_NOREPLY) {
488                 talloc_free(state);
489                 return 0;
490         }
491
492         if (ctdb->tunable.control_timeout) {
493                 event_add_timed(ctdb->ev, state, 
494                                 timeval_current_ofs(ctdb->tunable.control_timeout, 0), 
495                                 ctdb_control_timeout, state);
496         }
497
498         talloc_free(c);
499         return 0;
500 }