create a separate context for non-monitor eventscripts so they dont collide
[sahlberg/ctdb.git] / server / eventscript.c
1 /* 
2    event script handling
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 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
20 #include "includes.h"
21 #include <time.h>
22 #include "system/filesys.h"
23 #include "system/wait.h"
24 #include "system/dir.h"
25 #include "system/locale.h"
26 #include "../include/ctdb_private.h"
27 #include "lib/events/events.h"
28 #include "../common/rb_tree.h"
29
30 static struct {
31         struct timeval start;
32         const char *script_running;
33 } child_state;
34
35 static void ctdb_event_script_timeout(struct event_context *ev, struct timed_event *te, struct timeval t, void *p);
36
37 /*
38   ctdbd sends us a SIGTERM when we should time out the current script
39  */
40 static void sigterm(int sig)
41 {
42         char tbuf[100], buf[200];
43         time_t t;
44
45         DEBUG(DEBUG_ERR,("Timed out running script '%s' after %.1f seconds pid :%d\n", 
46                  child_state.script_running, timeval_elapsed(&child_state.start), getpid()));
47
48         t = time(NULL);
49
50         strftime(tbuf, sizeof(tbuf)-1, "%Y%m%d%H%M%S",  localtime(&t));
51         sprintf(buf, "pstree -p >/tmp/ctdb.event.%s.%d", tbuf, getpid());
52         system(buf);
53
54         DEBUG(DEBUG_ERR,("Logged timedout eventscript : %s\n", buf));
55
56         /* all the child processes will be running in the same process group */
57         kill(-getpgrp(), SIGKILL);
58         _exit(1);
59 }
60
61 struct ctdb_event_script_state {
62         struct ctdb_context *ctdb;
63         pid_t child;
64         void (*callback)(struct ctdb_context *, int, void *);
65         int fd[2];
66         void *private_data;
67         const char *options;
68         struct timed_event *te;
69         struct timeval timeout;
70 };
71
72
73 struct ctdb_monitor_script_status {
74         struct ctdb_monitor_script_status *next;
75         const char *name;
76         struct timeval start;
77         struct timeval finished;
78         int32_t disabled;
79         int32_t status;
80         int32_t timedout;
81         char *output;
82 };
83
84 struct ctdb_monitor_status {
85         struct timeval start;
86         struct timeval finished;
87         int32_t status;
88         struct ctdb_monitor_script_status *scripts;
89         struct ctdb_event_script_state *state;
90 };
91
92
93 /* called from ctdb_logging when we have received output on STDERR from
94  * one of the eventscripts
95  */
96 int ctdb_log_event_script_output(struct ctdb_context *ctdb, char *str, uint16_t len)
97 {
98         struct ctdb_monitor_status *monitoring_status = (struct ctdb_monitor_status *)ctdb->script_monitor_ctx;
99         struct ctdb_monitor_script_status *script;
100
101         if (monitoring_status == NULL) {
102                 return -1;
103         }
104
105         script = monitoring_status->scripts;
106         if (script == NULL) {
107                 return -1;
108         }
109
110         if (script->output == NULL) {
111                 script->output = talloc_asprintf(script, "%*.*s", len, len, str);
112         } else {
113                 script->output = talloc_asprintf_append(script->output, "%*.*s", len, len, str);
114         }
115
116         return 0;
117 }
118
119 /* called from the event script child process when we are starting a new
120  * monitor event
121  */
122 int32_t ctdb_control_event_script_init(struct ctdb_context *ctdb)
123 {
124         struct ctdb_monitor_status *monitoring_status = (struct ctdb_monitor_status *)ctdb->script_monitor_ctx;
125
126         DEBUG(DEBUG_INFO, ("event script init called\n"));
127
128         if (monitoring_status == NULL) {
129                 DEBUG(DEBUG_ERR,(__location__ " Init called when context is NULL\n"));
130                 return 0;
131         }
132
133         monitoring_status->start = timeval_current();   
134
135         return 0;
136 }
137
138
139 /* called from the event script child process when we are star running
140  * an eventscript
141  */
142 int32_t ctdb_control_event_script_start(struct ctdb_context *ctdb, TDB_DATA indata)
143 {
144         const char *name = (const char *)indata.dptr;
145         struct ctdb_monitor_status *monitoring_status = (struct ctdb_monitor_status *)ctdb->script_monitor_ctx;
146         struct ctdb_event_script_state *state;
147         struct ctdb_monitor_script_status *script;
148
149         DEBUG(DEBUG_INFO, ("event script start called : %s\n", name));
150
151         if (monitoring_status == NULL) {
152                 DEBUG(DEBUG_ERR,(__location__ " script_status is NULL when starting to run script %s\n", name));
153                 return -1;
154         }
155
156         script = talloc_zero(monitoring_status, struct ctdb_monitor_script_status);
157         if (script == NULL) {
158                 DEBUG(DEBUG_ERR,(__location__ " Failed to talloc ctdb_monitor_script_status for script %s\n", name));
159                 return -1;
160         }
161
162         script->next  = monitoring_status->scripts;
163         script->name  = talloc_strdup(script, name);
164         CTDB_NO_MEMORY(ctdb, script->name);
165         script->start = timeval_current();
166         monitoring_status->scripts = script;
167
168         state = monitoring_status->state;
169         if (state != NULL) {
170                 /* reset the timeout for the next eventscript */
171                 if (!timeval_is_zero(&state->timeout)) {
172                         if (state->te != NULL) {
173                                 talloc_free(state->te);
174                                 state->te = NULL;
175                         }
176                         state->te = event_add_timed(ctdb->ev, state, timeval_current_ofs(state->timeout.tv_sec, state->timeout.tv_usec), ctdb_event_script_timeout, state);
177                 }
178
179         }
180
181         return 0;
182 }
183
184 /* called from the event script child process when we have finished running
185  * an eventscript
186  */
187 int32_t ctdb_control_event_script_stop(struct ctdb_context *ctdb, TDB_DATA indata)
188 {
189         int32_t res = *((int32_t *)indata.dptr);
190         struct ctdb_monitor_status *monitoring_status = (struct ctdb_monitor_status *)ctdb->script_monitor_ctx;
191         struct ctdb_monitor_script_status *script;
192
193         if (monitoring_status == NULL) {
194                 DEBUG(DEBUG_ERR,(__location__ " script_status is NULL when script finished.\n"));
195                 return -1;
196         }
197
198         script = monitoring_status->scripts;
199         if (script == NULL) {
200                 DEBUG(DEBUG_ERR,(__location__ " script is NULL when the script had finished\n"));
201                 return -1;
202         }
203
204         script->finished = timeval_current();
205         script->status   = res;
206
207         DEBUG(DEBUG_INFO, ("event script stop called for script:%s duration:%.1f status:%d\n", script->name, timeval_elapsed(&script->start), (int)res));
208
209         return 0;
210 }
211
212 /* called from the event script child process when we have a disabled script
213  */
214 int32_t ctdb_control_event_script_disabled(struct ctdb_context *ctdb, TDB_DATA indata)
215 {
216         const char *name = (const char *)indata.dptr;
217         struct ctdb_monitor_status *monitoring_status = (struct ctdb_monitor_status *)ctdb->script_monitor_ctx;
218         struct ctdb_monitor_script_status *script;
219
220         DEBUG(DEBUG_INFO, ("event script disabed called for script %s\n", name));
221
222         if (monitoring_status == NULL) {
223                 DEBUG(DEBUG_ERR,(__location__ " script_status is NULL when script finished.\n"));
224                 return -1;
225         }
226
227         script = monitoring_status->scripts;
228         if (script == NULL) {
229                 DEBUG(DEBUG_ERR,(__location__ " script is NULL when the script had finished\n"));
230                 return -1;
231         }
232
233         script->finished = timeval_current();
234         script->status   = 0;
235         script->disabled = 1;
236
237         return 0;
238 }
239
240 /* called from the event script child process when we have completed a
241  * monitor event
242  */
243 int32_t ctdb_control_event_script_finished(struct ctdb_context *ctdb)
244 {
245         struct ctdb_monitor_status *monitoring_status = (struct ctdb_monitor_status *)ctdb->script_monitor_ctx;
246
247         DEBUG(DEBUG_INFO, ("event script finished called\n"));
248
249         if (monitoring_status == NULL) {
250                 DEBUG(DEBUG_ERR,(__location__ " script_status is NULL when monitoring event finished\n"));
251                 return -1;
252         }
253
254         monitoring_status->finished = timeval_current();        
255         monitoring_status->status   = MONITOR_SCRIPT_OK;
256
257         if (ctdb->last_monitor_ctx) {
258                 talloc_free(ctdb->last_monitor_ctx);
259                 ctdb->last_monitor_ctx = NULL;
260         }
261         ctdb->last_monitor_ctx = talloc_steal(ctdb, ctdb->script_monitor_ctx);
262         ctdb->script_monitor_ctx = NULL;
263
264         return 0;
265 }
266
267 static struct ctdb_monitoring_wire *marshall_monitoring_scripts(TALLOC_CTX *mem_ctx, struct ctdb_monitoring_wire *monitoring_scripts, struct ctdb_monitor_script_status *script)
268 {
269         struct ctdb_monitoring_script_wire script_wire;
270         size_t size;
271
272         if (script == NULL) {
273                 return monitoring_scripts;
274         }
275         monitoring_scripts = marshall_monitoring_scripts(mem_ctx, monitoring_scripts, script->next);
276         if (monitoring_scripts == NULL) {
277                 return NULL;
278         }
279
280         bzero(&script_wire, sizeof(struct ctdb_monitoring_script_wire));
281         strncpy(script_wire.name, script->name, MAX_SCRIPT_NAME);
282         script_wire.start    = script->start;
283         script_wire.finished = script->finished;
284         script_wire.disabled = script->disabled;
285         script_wire.status   = script->status;
286         script_wire.timedout = script->timedout;
287         if (script->output != NULL) {
288                 strncpy(script_wire.output, script->output, MAX_SCRIPT_OUTPUT);
289         }
290
291         size = talloc_get_size(monitoring_scripts);
292         monitoring_scripts = talloc_realloc_size(mem_ctx, monitoring_scripts, size + sizeof(struct ctdb_monitoring_script_wire));
293         if (monitoring_scripts == NULL) {
294                 DEBUG(DEBUG_ERR,(__location__ " Failed to talloc_resize monitoring_scripts blob\n"));
295                 return NULL;
296         }
297
298         memcpy(&monitoring_scripts->scripts[monitoring_scripts->num_scripts], &script_wire, sizeof(script_wire));
299         monitoring_scripts->num_scripts++;
300         
301         return monitoring_scripts;
302 }
303
304 int32_t ctdb_control_get_event_script_status(struct ctdb_context *ctdb, TDB_DATA *outdata)
305 {
306         struct ctdb_monitor_status *monitoring_status = (struct ctdb_monitor_status *)ctdb->last_monitor_ctx;
307         struct ctdb_monitoring_wire *monitoring_scripts;
308
309         if (monitoring_status == NULL) {
310                 DEBUG(DEBUG_ERR,(__location__ " last_monitor_ctx is NULL when reading status\n"));
311                 return -1;
312         }
313
314         monitoring_scripts = talloc_size(outdata, offsetof(struct ctdb_monitoring_wire, scripts));
315         if (monitoring_scripts == NULL) {
316                 DEBUG(DEBUG_ERR,(__location__ " failed to talloc monitoring_scripts structure\n"));
317                 return -1;
318         }
319         
320         monitoring_scripts->num_scripts = 0;
321         monitoring_scripts = marshall_monitoring_scripts(outdata, monitoring_scripts, monitoring_status->scripts);
322         if (monitoring_scripts == NULL) {
323                 DEBUG(DEBUG_ERR,(__location__ " Monitoring scritps is NULL. can not return data to client\n"));
324                 return -1;
325         }
326
327         outdata->dsize = talloc_get_size(monitoring_scripts);
328         outdata->dptr  = (uint8_t *)monitoring_scripts;
329
330         return 0;
331 }
332
333 struct ctdb_script_tree_item {
334         const char *name;
335         int32_t is_enabled;
336 };
337
338 struct ctdb_script_list {
339         struct ctdb_script_list *next;
340         const char *name;
341         int32_t is_enabled;
342 };
343
344 static struct ctdb_script_list *ctdb_get_script_list(struct ctdb_context *ctdb, TALLOC_CTX *mem_ctx)
345 {
346         DIR *dir;
347         struct dirent *de;
348         struct stat st;
349         trbt_tree_t *tree;
350         struct ctdb_script_list *head, *tail, *new_item;
351         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
352         struct ctdb_script_tree_item *tree_item;
353         int count;
354
355         /*
356           the service specific event scripts 
357         */
358         if (stat(ctdb->event_script_dir, &st) != 0 && 
359             errno == ENOENT) {
360                 DEBUG(DEBUG_CRIT,("No event script directory found at '%s'\n", ctdb->event_script_dir));
361                 talloc_free(tmp_ctx);
362                 return NULL;
363         }
364
365         /* create a tree to store all the script names in */
366         tree = trbt_create(tmp_ctx, 0);
367
368         /* scan all directory entries and insert all valid scripts into the 
369            tree
370         */
371         dir = opendir(ctdb->event_script_dir);
372         if (dir == NULL) {
373                 DEBUG(DEBUG_CRIT,("Failed to open event script directory '%s'\n", ctdb->event_script_dir));
374                 talloc_free(tmp_ctx);
375                 return NULL;
376         }
377
378         count = 0;
379         while ((de=readdir(dir)) != NULL) {
380                 int namlen;
381                 unsigned num;
382                 char *str;
383
384                 namlen = strlen(de->d_name);
385
386                 if (namlen < 3) {
387                         continue;
388                 }
389
390                 if (de->d_name[namlen-1] == '~') {
391                         /* skip files emacs left behind */
392                         continue;
393                 }
394
395                 if (de->d_name[2] != '.') {
396                         continue;
397                 }
398
399                 if (sscanf(de->d_name, "%02u.", &num) != 1) {
400                         continue;
401                 }
402
403                 /* Make sure the event script is executable */
404                 str = talloc_asprintf(tree, "%s/%s", ctdb->event_script_dir, de->d_name);
405                 if (stat(str, &st) != 0) {
406                         DEBUG(DEBUG_ERR,("Could not stat event script %s. Ignoring this event script\n", str));
407                         continue;
408                 }
409
410
411                 tree_item = talloc(tree, struct ctdb_script_tree_item);
412                 if (tree_item == NULL) {
413                         DEBUG(DEBUG_ERR, (__location__ " Failed to allocate new tree item\n"));
414                         talloc_free(tmp_ctx);
415                         return NULL;
416                 }
417         
418                 tree_item->is_enabled = 1;
419                 if (!(st.st_mode & S_IXUSR)) {
420                         DEBUG(DEBUG_INFO,("Event script %s is not executable. Ignoring this event script\n", str));
421                         tree_item->is_enabled = 0;
422                 }
423
424                 tree_item->name = talloc_strdup(tree_item, de->d_name);
425                 if (tree_item->name == NULL) {
426                         DEBUG(DEBUG_ERR,(__location__ " Failed to allocate script name.\n"));
427                         talloc_free(tmp_ctx);
428                         return NULL;
429                 }
430
431                 /* store the event script in the tree */
432                 trbt_insert32(tree, (num<<16)|count++, tree_item);
433         }
434         closedir(dir);
435
436
437         head = NULL;
438         tail = NULL;
439
440         /* fetch the scripts from the tree one by one and add them to the linked
441            list
442          */
443         while ((tree_item=trbt_findfirstarray32(tree, 1)) != NULL) {
444
445                 new_item = talloc(tmp_ctx, struct ctdb_script_list);
446                 if (new_item == NULL) {
447                         DEBUG(DEBUG_ERR, (__location__ " Failed to allocate new list item\n"));
448                         talloc_free(tmp_ctx);
449                         return NULL;
450                 }
451
452                 new_item->next = NULL;
453                 new_item->name = talloc_steal(new_item, tree_item->name);
454                 new_item->is_enabled = tree_item->is_enabled;
455
456                 if (head == NULL) {
457                         head = new_item;
458                         tail = new_item;
459                 } else {
460                         tail->next = new_item;
461                         tail = new_item;
462                 }
463
464                 talloc_steal(mem_ctx, new_item);
465
466                 /* remove this script from the tree */
467                 talloc_free(tree_item);
468         }       
469
470         talloc_free(tmp_ctx);
471         return head;
472 }
473
474
475
476 /*
477   run the event script - varargs version
478   this function is called and run in the context of a forked child
479   which allows it to do blocking calls such as system()
480  */
481 static int ctdb_event_script_v(struct ctdb_context *ctdb, const char *options)
482 {
483         char *cmdstr;
484         int ret;
485         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
486         struct ctdb_script_list *scripts, *current;
487         int is_monitor = 0;
488
489         if (!strcmp(options, "monitor")) {
490                 is_monitor = 1;
491         }
492
493         if (is_monitor == 1) {
494                 /* This is running in the forked child process. At this stage
495                  * we want to switch from being a ctdb daemon into being a
496                  * client and connect to the real local daemon.
497                  */
498                 if (switch_from_server_to_client(ctdb) != 0) {
499                         DEBUG(DEBUG_CRIT, (__location__ "ERROR: failed to switch eventscript child into client mode. shutting down.\n"));
500                         _exit(1);
501                 }
502
503                 if (ctdb_ctrl_event_script_init(ctdb) != 0) {
504                         DEBUG(DEBUG_ERR,(__location__ " Failed to init event script monitoring\n"));
505                         talloc_free(tmp_ctx);
506                         return -1;
507                 }
508         }
509
510         if (ctdb->recovery_mode != CTDB_RECOVERY_NORMAL) {
511                 /* we guarantee that only some specifically allowed event scripts are run
512                    while in recovery */
513           const char *allowed_scripts[] = {"startrecovery", "shutdown", "releaseip", "stopped" };
514                 int i;
515                 for (i=0;i<ARRAY_SIZE(allowed_scripts);i++) {
516                         if (strncmp(options, allowed_scripts[i], strlen(allowed_scripts[i])) == 0) break;
517                 }
518                 if (i == ARRAY_SIZE(allowed_scripts)) {
519                         DEBUG(DEBUG_ERR,("Refusing to run event scripts with option '%s' while in recovery\n",
520                                  options));
521                         talloc_free(tmp_ctx);
522                         return -1;
523                 }
524         }
525
526         if (setpgid(0,0) != 0) {
527                 DEBUG(DEBUG_ERR,("Failed to create process group for event scripts - %s\n",
528                          strerror(errno)));
529                 talloc_free(tmp_ctx);
530                 return -1;              
531         }
532
533         signal(SIGTERM, sigterm);
534
535         child_state.start = timeval_current();
536         child_state.script_running = "startup";
537
538         scripts = ctdb_get_script_list(ctdb, tmp_ctx);
539
540         /* fetch the scripts from the tree one by one and execute
541            them
542          */
543         for (current=scripts; current; current=current->next) {
544                 /* we dont run disabled scripts, we just report they are disabled */
545                 cmdstr = talloc_asprintf(tmp_ctx, "%s/%s %s", 
546                                 ctdb->event_script_dir,
547                                 current->name, options);
548                 CTDB_NO_MEMORY(ctdb, cmdstr);
549
550                 DEBUG(DEBUG_INFO,("Executing event script %s\n",cmdstr));
551
552                 child_state.start = timeval_current();
553                 child_state.script_running = cmdstr;
554
555                 if (is_monitor == 1) {
556                         if (ctdb_ctrl_event_script_start(ctdb, current->name) != 0) {
557                                 DEBUG(DEBUG_ERR,(__location__ " Failed to start event script monitoring\n"));
558                                 talloc_free(tmp_ctx);
559                                 return -1;
560                         }
561
562                         if (!current->is_enabled) {
563                                 if (ctdb_ctrl_event_script_disabled(ctdb, current->name) != 0) {
564                                         DEBUG(DEBUG_ERR,(__location__ " Failed to report disabled eventscript\n"));
565                                         talloc_free(tmp_ctx);
566                                         return -1;
567                                 }
568                         }
569
570                 }
571
572                 if (!current->is_enabled) {
573                         continue;
574                 }
575
576                 ret = system(cmdstr);
577                 /* if the system() call was successful, translate ret into the
578                    return code from the command
579                 */
580                 if (ret != -1) {
581                         ret = WEXITSTATUS(ret);
582                 }
583                 if (ret == 127) {
584                         ret = 0;
585                         DEBUG(DEBUG_ERR,("Script %s returned status 127. Someone just deleted it?\n", cmdstr));
586                 }
587  
588                 if (is_monitor == 1) {
589                         if (ctdb_ctrl_event_script_stop(ctdb, ret) != 0) {
590                                 DEBUG(DEBUG_ERR,(__location__ " Failed to stop event script monitoring\n"));
591                                 talloc_free(tmp_ctx);
592                                 return -1;
593                         }
594                 }
595
596                 /* return an error if the script failed */
597                 if (ret != 0) {
598                         DEBUG(DEBUG_ERR,("Event script %s failed with error %d\n", cmdstr, ret));
599                         if (is_monitor == 1) {
600                                 if (ctdb_ctrl_event_script_finished(ctdb) != 0) {
601                                         DEBUG(DEBUG_ERR,(__location__ " Failed to finish event script monitoring\n"));
602                                         talloc_free(tmp_ctx);
603                                         return -1;
604                                 }
605                         }
606
607                         talloc_free(tmp_ctx);
608                         return ret;
609                 }
610         }
611
612         child_state.start = timeval_current();
613         child_state.script_running = "finished";
614         
615         if (is_monitor == 1) {
616                 if (ctdb_ctrl_event_script_finished(ctdb) != 0) {
617                         DEBUG(DEBUG_ERR,(__location__ " Failed to finish event script monitoring\n"));
618                         talloc_free(tmp_ctx);
619                         return -1;
620                 }
621         }
622
623         talloc_free(tmp_ctx);
624         return 0;
625 }
626
627 /* called when child is finished */
628 static void ctdb_event_script_handler(struct event_context *ev, struct fd_event *fde, 
629                                       uint16_t flags, void *p)
630 {
631         struct ctdb_event_script_state *state = 
632                 talloc_get_type(p, struct ctdb_event_script_state);
633         struct ctdb_context *ctdb = state->ctdb;
634         signed char rt = -1;
635
636         read(state->fd[0], &rt, sizeof(rt));
637
638         DEBUG(DEBUG_INFO,(__location__ " Eventscript %s finished with state %d\n", state->options, rt));
639
640         if (state->callback) {
641                 state->callback(ctdb, rt, state->private_data);
642                 state->callback = NULL;
643         }
644
645         talloc_set_destructor(state, NULL);
646         talloc_free(state);
647         ctdb->event_script_timeouts = 0;
648 }
649
650 static void ctdb_ban_self(struct ctdb_context *ctdb, uint32_t ban_period)
651 {
652         TDB_DATA data;
653         struct ctdb_ban_time bantime;
654
655         bantime.pnn  = ctdb->pnn;
656         bantime.time = ban_period;
657
658         data.dsize = sizeof(bantime);
659         data.dptr  = (uint8_t *)&bantime;
660
661         ctdb_control_set_ban_state(ctdb, data);
662 }
663
664
665 /* called when child times out */
666 static void ctdb_event_script_timeout(struct event_context *ev, struct timed_event *te, 
667                                       struct timeval t, void *p)
668 {
669         struct ctdb_event_script_state *state = talloc_get_type(p, struct ctdb_event_script_state);
670         void *private_data = state->private_data;
671         struct ctdb_context *ctdb = state->ctdb;
672         char *options;
673         struct ctdb_monitor_status *monitoring_status = (struct ctdb_monitor_status *)ctdb->script_monitor_ctx;
674
675         state->te = NULL;
676
677         DEBUG(DEBUG_ERR,("Event script timed out : %s count : %u  pid : %d\n", state->options, ctdb->event_script_timeouts, state->child));
678         if (kill(state->child, 0) != 0) {
679                 DEBUG(DEBUG_ERR,("Event script child process already dead, errno %s(%d)\n", strerror(errno), errno));
680                 if (state->callback) {
681                         state->callback(ctdb, 0, private_data);
682                         state->callback = NULL;
683                 }
684                 talloc_set_destructor(state, NULL);
685                 talloc_free(state);
686                 return;
687         }
688
689         options = talloc_strdup(ctdb, state->options);
690         CTDB_NO_MEMORY_VOID(ctdb, options);
691
692         talloc_free(state);
693         if (!strcmp(options, "monitor")) {
694                 /* if it is a monitor event, we allow it to "hang" a few times
695                    before we declare it a failure and ban ourself (and make
696                    ourself unhealthy)
697                 */
698                 DEBUG(DEBUG_ERR, (__location__ " eventscript for monitor event timedout.\n"));
699
700                 ctdb->event_script_timeouts++;
701                 if (ctdb->event_script_timeouts > ctdb->tunable.script_ban_count) {
702                         if (ctdb->tunable.script_unhealthy_on_timeout != 0) {
703                                 DEBUG(DEBUG_ERR, ("Maximum timeout count %u reached for eventscript. Making node unhealthy\n", ctdb->tunable.script_ban_count));
704                                 if (state->callback) {
705                                         state->callback(ctdb, -ETIME, private_data);
706                                         state->callback = NULL;
707                                 }
708                         } else {
709                                 ctdb->event_script_timeouts = 0;
710                                 DEBUG(DEBUG_ERR, ("Maximum timeout count %u reached for eventscript. Banning self for %d seconds\n", ctdb->tunable.script_ban_count, ctdb->tunable.recovery_ban_period));
711                                 ctdb_ban_self(ctdb, ctdb->tunable.recovery_ban_period);
712                                 if (state->callback) {
713                                         state->callback(ctdb, -1, private_data);
714                                         state->callback = NULL;
715                                 }
716                         }
717                 } else {
718                         if (state->callback) {
719                                 state->callback(ctdb, 0, private_data);
720                                 state->callback = NULL;
721                         }
722                 }
723         } else if (!strcmp(options, "startup")) {
724                 DEBUG(DEBUG_ERR, (__location__ " eventscript for startup event timedout.\n"));
725                 if (state->callback) {
726                         state->callback(ctdb, -1, private_data);
727                         state->callback = NULL;
728                 }
729         } else {
730                 /* if it is not a monitor event we ban ourself immediately */
731                 DEBUG(DEBUG_ERR, (__location__ " eventscript for NON-monitor/NON-startup event timedout. Immediately banning ourself for %d seconds\n", ctdb->tunable.recovery_ban_period));
732                 ctdb_ban_self(ctdb, ctdb->tunable.recovery_ban_period);
733                 if (state->callback) {
734                         state->callback(ctdb, -1, private_data);
735                         state->callback = NULL;
736                 }
737         }
738
739         if ((!strcmp(options, "monitor")) && (monitoring_status != NULL)) {
740                 struct ctdb_monitor_script_status *script;
741
742                 script = monitoring_status->scripts;
743                 if (script != NULL) {
744                         script->timedout = 1;
745                 }
746                 monitoring_status->status = MONITOR_SCRIPT_TIMEOUT;
747                 if (ctdb->last_monitor_ctx) {
748                         talloc_free(ctdb->last_monitor_ctx);
749                         ctdb->last_monitor_ctx = NULL;
750                 }
751                 ctdb->last_monitor_ctx = talloc_steal(ctdb, ctdb->script_monitor_ctx);
752                 ctdb->script_monitor_ctx = NULL;
753         }
754
755         talloc_free(options);
756 }
757
758 /*
759   destroy a running event script
760  */
761 static int event_script_destructor(struct ctdb_event_script_state *state)
762 {
763         DEBUG(DEBUG_ERR,(__location__ " Sending SIGTERM to child pid:%d\n", state->child));
764
765         if (state->callback) {
766                 state->callback(state->ctdb, 0, state->private_data);
767                 state->callback = NULL;
768         }
769
770         if (kill(state->child, SIGTERM) != 0) {
771                 DEBUG(DEBUG_ERR,("Failed to kill child process for eventscript, errno %s(%d)\n", strerror(errno), errno));
772         }
773
774         return 0;
775 }
776
777 /*
778   run the event script in the background, calling the callback when 
779   finished
780  */
781 static int ctdb_event_script_callback_v(struct ctdb_context *ctdb, 
782                                         struct timeval timeout,
783                                         void (*callback)(struct ctdb_context *, int, void *),
784                                         void *private_data,
785                                         const char *fmt, va_list ap)
786 {
787         struct ctdb_monitor_status *monitoring_status;
788         struct ctdb_event_script_state *state;
789         int ret;
790
791         if (!strcmp(fmt, "monitor")) {
792                 if (ctdb->script_monitor_ctx != NULL) {
793                         talloc_free(ctdb->script_monitor_ctx);
794                         ctdb->script_monitor_ctx = NULL;
795                 }
796                 monitoring_status = talloc_zero(ctdb, struct ctdb_monitor_status);
797         } else {
798                 if (ctdb->event_script_ctx == NULL) {
799                         ctdb->event_script_ctx = talloc_zero(ctdb, struct ctdb_monitor_status);
800                 }
801                 monitoring_status = ctdb->event_script_ctx;
802         }
803
804         if (monitoring_status == NULL) {
805                 DEBUG(DEBUG_ERR, (__location__ " ERROR: Failed to talloc script_monitoring context\n"));
806                 return -1;
807         }
808
809         state = talloc(monitoring_status, struct ctdb_event_script_state);
810         if (state == NULL) {
811                 DEBUG(DEBUG_ERR,(__location__ " could not allocate state\n"));
812                 return -1;
813         }
814         monitoring_status->state = state;
815
816         state->ctdb = ctdb;
817         state->callback = callback;
818         state->private_data = private_data;
819         state->options = talloc_vasprintf(state, fmt, ap);
820         state->timeout = timeout;
821         state->te = NULL;
822         if (state->options == NULL) {
823                 DEBUG(DEBUG_ERR, (__location__ " could not allocate state->options\n"));
824                 talloc_free(state);
825                 return -1;
826         }
827
828         DEBUG(DEBUG_INFO,(__location__ " Starting eventscript %s\n", state->options));
829         
830         ret = pipe(state->fd);
831         if (ret != 0) {
832                 talloc_free(state);
833                 return -1;
834         }
835
836         state->child = fork();
837
838         if (state->child == (pid_t)-1) {
839                 close(state->fd[0]);
840                 close(state->fd[1]);
841                 talloc_free(state);
842                 return -1;
843         }
844
845         if (state->child == 0) {
846                 signed char rt;
847
848                 close(state->fd[0]);
849                 set_close_on_exec(state->fd[1]);
850
851                 rt = ctdb_event_script_v(ctdb, state->options);
852                 while ((ret = write(state->fd[1], &rt, sizeof(rt))) != sizeof(rt)) {
853                         write(state->fd[1], &rt, sizeof(rt));
854                         usleep(100000);
855                 }
856                 _exit(rt);
857         }
858
859         talloc_set_destructor(state, event_script_destructor);
860         if (!strcmp(fmt, "monitor")) {
861                 ctdb->script_monitor_ctx = monitoring_status;
862         } else {
863                 ctdb->event_script_ctx  = monitoring_status;
864         }
865
866         close(state->fd[1]);
867         set_close_on_exec(state->fd[0]);
868
869         DEBUG(DEBUG_DEBUG, (__location__ " Created PIPE FD:%d to child eventscript process\n", state->fd[0]));
870
871         event_add_fd(ctdb->ev, state, state->fd[0], EVENT_FD_READ|EVENT_FD_AUTOCLOSE,
872                      ctdb_event_script_handler, state);
873
874         if (!timeval_is_zero(&state->timeout)) {
875                 state->te = event_add_timed(ctdb->ev, state, timeval_current_ofs(state->timeout.tv_sec, state->timeout.tv_usec), ctdb_event_script_timeout, state);
876         } else {
877                 DEBUG(DEBUG_ERR, (__location__ " eventscript %s called with no timeout\n", state->options));
878         }
879
880         return 0;
881 }
882
883
884 /*
885   run the event script in the background, calling the callback when 
886   finished
887  */
888 int ctdb_event_script_callback(struct ctdb_context *ctdb, 
889                                struct timeval timeout,
890                                TALLOC_CTX *mem_ctx,
891                                void (*callback)(struct ctdb_context *, int, void *),
892                                void *private_data,
893                                const char *fmt, ...)
894 {
895         va_list ap;
896         int ret;
897
898         va_start(ap, fmt);
899         ret = ctdb_event_script_callback_v(ctdb, timeout, callback, private_data, fmt, ap);
900         va_end(ap);
901
902         return ret;
903 }
904
905
906 struct callback_status {
907         bool done;
908         int status;
909 };
910
911 /*
912   called when ctdb_event_script() finishes
913  */
914 static void event_script_callback(struct ctdb_context *ctdb, int status, void *private_data)
915 {
916         struct callback_status *s = (struct callback_status *)private_data;
917         s->done = true;
918         s->status = status;
919 }
920
921 /*
922   run the event script, waiting for it to complete. Used when the caller doesn't want to 
923   continue till the event script has finished.
924  */
925 int ctdb_event_script(struct ctdb_context *ctdb, const char *fmt, ...)
926 {
927         va_list ap;
928         int ret;
929         struct callback_status status;
930
931         va_start(ap, fmt);
932         ret = ctdb_event_script_callback_v(ctdb, 
933                         timeval_set(ctdb->tunable.script_timeout, 0),
934                         event_script_callback, &status, fmt, ap);
935         va_end(ap);
936
937         if (ret != 0) {
938                 return ret;
939         }
940
941         status.status = -1;
942         status.done = false;
943
944         while (status.done == false && event_loop_once(ctdb->ev) == 0) /* noop */;
945
946         return status.status;
947 }
948
949
950 struct eventscript_callback_state {
951         struct ctdb_req_control *c;
952 };
953
954 /*
955   called when takeip event finishes
956  */
957 static void run_eventscripts_callback(struct ctdb_context *ctdb, int status, 
958                                  void *private_data)
959 {
960         struct eventscript_callback_state *state = 
961                 talloc_get_type(private_data, struct eventscript_callback_state);
962
963         ctdb_enable_monitoring(ctdb);
964
965         if (status != 0) {
966                 DEBUG(DEBUG_ERR,(__location__ " Failed to forcibly run eventscripts\n"));
967                 ctdb_request_control_reply(ctdb, state->c, NULL, status, NULL);
968                 talloc_free(state);
969                 return;
970         }
971
972         /* the control succeeded */
973         ctdb_request_control_reply(ctdb, state->c, NULL, 0, NULL);
974         talloc_free(state);
975         return;
976 }
977
978 /*
979   A control to force running of the eventscripts from the ctdb client tool
980 */
981 int32_t ctdb_run_eventscripts(struct ctdb_context *ctdb,
982                 struct ctdb_req_control *c,
983                 TDB_DATA indata, bool *async_reply)
984 {
985         int ret;
986         struct eventscript_callback_state *state;
987
988         /* kill off any previous invokations of forced eventscripts */
989         if (ctdb->eventscripts_ctx) {
990                 talloc_free(ctdb->eventscripts_ctx);
991         }
992         ctdb->eventscripts_ctx = talloc_new(ctdb);
993         CTDB_NO_MEMORY(ctdb, ctdb->eventscripts_ctx);
994
995         state = talloc(ctdb->eventscripts_ctx, struct eventscript_callback_state);
996         CTDB_NO_MEMORY(ctdb, state);
997
998         state->c = talloc_steal(state, c);
999
1000         DEBUG(DEBUG_NOTICE,("Forced running of eventscripts with arguments %s\n", indata.dptr));
1001
1002         if (ctdb->recovery_mode != CTDB_RECOVERY_NORMAL) {
1003                 DEBUG(DEBUG_ERR, (__location__ " Aborted running eventscript \"%s\" while in RECOVERY mode\n", indata.dptr));
1004                 return -1;
1005         }
1006
1007         ctdb_disable_monitoring(ctdb);
1008
1009         ret = ctdb_event_script_callback(ctdb, 
1010                          timeval_set(ctdb->tunable.script_timeout, 0),
1011                          state, run_eventscripts_callback, state,
1012                          "%s", (const char *)indata.dptr);
1013
1014         if (ret != 0) {
1015                 ctdb_enable_monitoring(ctdb);
1016                 DEBUG(DEBUG_ERR,(__location__ " Failed to run eventscripts with arguments %s\n", indata.dptr));
1017                 talloc_free(state);
1018                 return -1;
1019         }
1020
1021         /* tell ctdb_control.c that we will be replying asynchronously */
1022         *async_reply = true;
1023
1024         return 0;
1025 }
1026
1027
1028
1029 int32_t ctdb_control_enable_script(struct ctdb_context *ctdb, TDB_DATA indata)
1030 {
1031         const char *script;
1032         struct stat st;
1033         char *filename;
1034         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1035
1036         script = (char *)indata.dptr;
1037         if (indata.dsize == 0) {
1038                 DEBUG(DEBUG_ERR,(__location__ " No script specified.\n"));
1039                 talloc_free(tmp_ctx);
1040                 return -1;
1041         }
1042         if (indata.dptr[indata.dsize - 1] != '\0') {
1043                 DEBUG(DEBUG_ERR,(__location__ " String is not null terminated.\n"));
1044                 talloc_free(tmp_ctx);
1045                 return -1;
1046         }
1047         if (index(script,'/') != NULL) {
1048                 DEBUG(DEBUG_ERR,(__location__ " Script name contains '/'. Failed to enable script %s\n", script));
1049                 talloc_free(tmp_ctx);
1050                 return -1;
1051         }
1052
1053
1054         if (stat(ctdb->event_script_dir, &st) != 0 && 
1055             errno == ENOENT) {
1056                 DEBUG(DEBUG_CRIT,("No event script directory found at '%s'\n", ctdb->event_script_dir));
1057                 talloc_free(tmp_ctx);
1058                 return -1;
1059         }
1060
1061
1062         filename = talloc_asprintf(tmp_ctx, "%s/%s", ctdb->event_script_dir, script);
1063         if (filename == NULL) {
1064                 DEBUG(DEBUG_ERR,(__location__ " Failed to create script path\n"));
1065                 talloc_free(tmp_ctx);
1066                 return -1;
1067         }
1068
1069         if (stat(filename, &st) != 0) {
1070                 DEBUG(DEBUG_ERR,("Could not stat event script %s. Failed to enable script.\n", filename));
1071                 talloc_free(tmp_ctx);
1072                 return -1;
1073         }
1074
1075         if (chmod(filename, st.st_mode | S_IXUSR) == -1) {
1076                 DEBUG(DEBUG_ERR,("Could not chmod %s. Failed to enable script.\n", filename));
1077                 talloc_free(tmp_ctx);
1078                 return -1;
1079         }
1080
1081         talloc_free(tmp_ctx);
1082         return 0;
1083 }
1084
1085 int32_t ctdb_control_disable_script(struct ctdb_context *ctdb, TDB_DATA indata)
1086 {
1087         const char *script;
1088         struct stat st;
1089         char *filename;
1090         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1091
1092         script = (char *)indata.dptr;
1093         if (indata.dsize == 0) {
1094                 DEBUG(DEBUG_ERR,(__location__ " No script specified.\n"));
1095                 talloc_free(tmp_ctx);
1096                 return -1;
1097         }
1098         if (indata.dptr[indata.dsize - 1] != '\0') {
1099                 DEBUG(DEBUG_ERR,(__location__ " String is not null terminated.\n"));
1100                 talloc_free(tmp_ctx);
1101                 return -1;
1102         }
1103         if (index(script,'/') != NULL) {
1104                 DEBUG(DEBUG_ERR,(__location__ " Script name contains '/'. Failed to disable script %s\n", script));
1105                 talloc_free(tmp_ctx);
1106                 return -1;
1107         }
1108
1109
1110         if (stat(ctdb->event_script_dir, &st) != 0 && 
1111             errno == ENOENT) {
1112                 DEBUG(DEBUG_CRIT,("No event script directory found at '%s'\n", ctdb->event_script_dir));
1113                 talloc_free(tmp_ctx);
1114                 return -1;
1115         }
1116
1117
1118         filename = talloc_asprintf(tmp_ctx, "%s/%s", ctdb->event_script_dir, script);
1119         if (filename == NULL) {
1120                 DEBUG(DEBUG_ERR,(__location__ " Failed to create script path\n"));
1121                 talloc_free(tmp_ctx);
1122                 return -1;
1123         }
1124
1125         if (stat(filename, &st) != 0) {
1126                 DEBUG(DEBUG_ERR,("Could not stat event script %s. Failed to disable script.\n", filename));
1127                 talloc_free(tmp_ctx);
1128                 return -1;
1129         }
1130
1131         if (chmod(filename, st.st_mode & ~(S_IXUSR|S_IXGRP|S_IXOTH)) == -1) {
1132                 DEBUG(DEBUG_ERR,("Could not chmod %s. Failed to disable script.\n", filename));
1133                 talloc_free(tmp_ctx);
1134                 return -1;
1135         }
1136
1137         talloc_free(tmp_ctx);
1138         return 0;
1139 }