test of a change to make ctdbd use "status" event instead of the "monitor" event.
[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                 /* Allow a setting where we run the actual monitor event
545                    from an external source and replace it with
546                    a "status" event that just picks up the actual
547                    status of the event asynchronously.
548                 */
549                 if ((ctdb->tunable.use_status_events_for_monitoring != 0) 
550                 && (!strcmp(options, "status"))) {
551                         cmdstr = talloc_asprintf(tmp_ctx, "%s/%s %s", 
552                                         ctdb->event_script_dir,
553                                         current->name, "status");
554                 } else {
555                         cmdstr = talloc_asprintf(tmp_ctx, "%s/%s %s", 
556                                         ctdb->event_script_dir,
557                                         current->name, options);
558                 }
559                 CTDB_NO_MEMORY(ctdb, cmdstr);
560
561                 DEBUG(DEBUG_INFO,("Executing event script %s\n",cmdstr));
562
563                 child_state.start = timeval_current();
564                 child_state.script_running = cmdstr;
565
566                 if (is_monitor == 1) {
567                         if (ctdb_ctrl_event_script_start(ctdb, current->name) != 0) {
568                                 DEBUG(DEBUG_ERR,(__location__ " Failed to start event script monitoring\n"));
569                                 talloc_free(tmp_ctx);
570                                 return -1;
571                         }
572
573                         if (!current->is_enabled) {
574                                 if (ctdb_ctrl_event_script_disabled(ctdb, current->name) != 0) {
575                                         DEBUG(DEBUG_ERR,(__location__ " Failed to report disabled eventscript\n"));
576                                         talloc_free(tmp_ctx);
577                                         return -1;
578                                 }
579                         }
580
581                 }
582
583                 if (!current->is_enabled) {
584                         continue;
585                 }
586
587                 ret = system(cmdstr);
588                 /* if the system() call was successful, translate ret into the
589                    return code from the command
590                 */
591                 if (ret != -1) {
592                         ret = WEXITSTATUS(ret);
593                 }
594                 if (ret == 127) {
595                         ret = 0;
596                         DEBUG(DEBUG_ERR,("Script %s returned status 127. Someone just deleted it?\n", cmdstr));
597                 }
598  
599                 if (is_monitor == 1) {
600                         if (ctdb_ctrl_event_script_stop(ctdb, ret) != 0) {
601                                 DEBUG(DEBUG_ERR,(__location__ " Failed to stop event script monitoring\n"));
602                                 talloc_free(tmp_ctx);
603                                 return -1;
604                         }
605                 }
606
607                 /* return an error if the script failed */
608                 if (ret != 0) {
609                         DEBUG(DEBUG_ERR,("Event script %s failed with error %d\n", cmdstr, ret));
610                         if (is_monitor == 1) {
611                                 if (ctdb_ctrl_event_script_finished(ctdb) != 0) {
612                                         DEBUG(DEBUG_ERR,(__location__ " Failed to finish event script monitoring\n"));
613                                         talloc_free(tmp_ctx);
614                                         return -1;
615                                 }
616                         }
617
618                         talloc_free(tmp_ctx);
619                         return ret;
620                 }
621         }
622
623         child_state.start = timeval_current();
624         child_state.script_running = "finished";
625         
626         if (is_monitor == 1) {
627                 if (ctdb_ctrl_event_script_finished(ctdb) != 0) {
628                         DEBUG(DEBUG_ERR,(__location__ " Failed to finish event script monitoring\n"));
629                         talloc_free(tmp_ctx);
630                         return -1;
631                 }
632         }
633
634         talloc_free(tmp_ctx);
635         return 0;
636 }
637
638 /* called when child is finished */
639 static void ctdb_event_script_handler(struct event_context *ev, struct fd_event *fde, 
640                                       uint16_t flags, void *p)
641 {
642         struct ctdb_event_script_state *state = 
643                 talloc_get_type(p, struct ctdb_event_script_state);
644         struct ctdb_context *ctdb = state->ctdb;
645         signed char rt = -1;
646
647         read(state->fd[0], &rt, sizeof(rt));
648
649         DEBUG(DEBUG_INFO,(__location__ " Eventscript %s finished with state %d\n", state->options, rt));
650
651         if (state->callback) {
652                 state->callback(ctdb, rt, state->private_data);
653                 state->callback = NULL;
654         }
655
656         talloc_set_destructor(state, NULL);
657         talloc_free(state);
658         ctdb->event_script_timeouts = 0;
659 }
660
661 static void ctdb_ban_self(struct ctdb_context *ctdb, uint32_t ban_period)
662 {
663         TDB_DATA data;
664         struct ctdb_ban_time bantime;
665
666         bantime.pnn  = ctdb->pnn;
667         bantime.time = ban_period;
668
669         data.dsize = sizeof(bantime);
670         data.dptr  = (uint8_t *)&bantime;
671
672         ctdb_control_set_ban_state(ctdb, data);
673 }
674
675
676 /* called when child times out */
677 static void ctdb_event_script_timeout(struct event_context *ev, struct timed_event *te, 
678                                       struct timeval t, void *p)
679 {
680         struct ctdb_event_script_state *state = talloc_get_type(p, struct ctdb_event_script_state);
681         void *private_data = state->private_data;
682         struct ctdb_context *ctdb = state->ctdb;
683         char *options;
684         struct ctdb_monitor_status *monitoring_status = (struct ctdb_monitor_status *)ctdb->script_monitor_ctx;
685
686         state->te = NULL;
687
688         DEBUG(DEBUG_ERR,("Event script timed out : %s count : %u  pid : %d\n", state->options, ctdb->event_script_timeouts, state->child));
689         if (kill(state->child, 0) != 0) {
690                 DEBUG(DEBUG_ERR,("Event script child process already dead, errno %s(%d)\n", strerror(errno), errno));
691                 if (state->callback) {
692                         state->callback(ctdb, 0, private_data);
693                         state->callback = NULL;
694                 }
695                 talloc_set_destructor(state, NULL);
696                 talloc_free(state);
697                 return;
698         }
699
700         options = talloc_strdup(ctdb, state->options);
701         CTDB_NO_MEMORY_VOID(ctdb, options);
702
703         if (!strcmp(options, "monitor")) {
704                 /* if it is a monitor event, we allow it to "hang" a few times
705                    before we declare it a failure and ban ourself (and make
706                    ourself unhealthy)
707                 */
708                 DEBUG(DEBUG_ERR, (__location__ " eventscript for monitor event timedout.\n"));
709
710                 ctdb->event_script_timeouts++;
711                 if (ctdb->event_script_timeouts > ctdb->tunable.script_ban_count) {
712                         if (ctdb->tunable.script_unhealthy_on_timeout != 0) {
713                                 DEBUG(DEBUG_ERR, ("Maximum timeout count %u reached for eventscript. Making node unhealthy\n", ctdb->tunable.script_ban_count));
714                                 if (state->callback) {
715                                         state->callback(ctdb, -ETIME, private_data);
716                                         state->callback = NULL;
717                                 }
718                         } else {
719                                 ctdb->event_script_timeouts = 0;
720                                 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));
721                                 ctdb_ban_self(ctdb, ctdb->tunable.recovery_ban_period);
722                                 if (state->callback) {
723                                         state->callback(ctdb, -1, private_data);
724                                         state->callback = NULL;
725                                 }
726                         }
727                 } else {
728                         if (state->callback) {
729                                 state->callback(ctdb, 0, private_data);
730                                 state->callback = NULL;
731                         }
732                 }
733         } else if (!strcmp(options, "startup")) {
734                 DEBUG(DEBUG_ERR, (__location__ " eventscript for startup event timedout.\n"));
735                 if (state->callback) {
736                         state->callback(ctdb, -1, private_data);
737                         state->callback = NULL;
738                 }
739         } else {
740                 /* if it is not a monitor event we ban ourself immediately */
741                 DEBUG(DEBUG_ERR, (__location__ " eventscript for NON-monitor/NON-startup event timedout. Immediately banning ourself for %d seconds\n", ctdb->tunable.recovery_ban_period));
742                 ctdb_ban_self(ctdb, ctdb->tunable.recovery_ban_period);
743                 if (state->callback) {
744                         state->callback(ctdb, -1, private_data);
745                         state->callback = NULL;
746                 }
747         }
748
749         if ((!strcmp(options, "monitor")) && (monitoring_status != NULL)) {
750                 struct ctdb_monitor_script_status *script;
751
752                 script = monitoring_status->scripts;
753                 if (script != NULL) {
754                         script->timedout = 1;
755                 }
756                 monitoring_status->status = MONITOR_SCRIPT_TIMEOUT;
757                 if (ctdb->last_monitor_ctx) {
758                         talloc_free(ctdb->last_monitor_ctx);
759                         ctdb->last_monitor_ctx = NULL;
760                 }
761                 ctdb->last_monitor_ctx = talloc_steal(ctdb, ctdb->script_monitor_ctx);
762                 ctdb->script_monitor_ctx = NULL;
763         }
764
765         talloc_free(state);
766         talloc_free(options);
767 }
768
769 /*
770   destroy a running event script
771  */
772 static int event_script_destructor(struct ctdb_event_script_state *state)
773 {
774         DEBUG(DEBUG_ERR,(__location__ " Sending SIGTERM to child pid:%d\n", state->child));
775
776         if (state->callback) {
777                 state->callback(state->ctdb, 0, state->private_data);
778                 state->callback = NULL;
779         }
780
781         if (kill(state->child, SIGTERM) != 0) {
782                 DEBUG(DEBUG_ERR,("Failed to kill child process for eventscript, errno %s(%d)\n", strerror(errno), errno));
783         }
784
785         return 0;
786 }
787
788 /*
789   run the event script in the background, calling the callback when 
790   finished
791  */
792 static int ctdb_event_script_callback_v(struct ctdb_context *ctdb, 
793                                         struct timeval timeout,
794                                         void (*callback)(struct ctdb_context *, int, void *),
795                                         void *private_data,
796                                         const char *fmt, va_list ap)
797 {
798         struct ctdb_monitor_status *monitoring_status;
799         struct ctdb_event_script_state *state;
800         int ret;
801
802         if (!strcmp(fmt, "monitor")) {
803                 if (ctdb->script_monitor_ctx != NULL) {
804                         talloc_free(ctdb->script_monitor_ctx);
805                         ctdb->script_monitor_ctx = NULL;
806                 }
807                 monitoring_status = talloc_zero(ctdb, struct ctdb_monitor_status);
808         } else {
809                 if (ctdb->event_script_ctx == NULL) {
810                         ctdb->event_script_ctx = talloc_zero(ctdb, struct ctdb_monitor_status);
811                 }
812                 monitoring_status = ctdb->event_script_ctx;
813         }
814
815         if (monitoring_status == NULL) {
816                 DEBUG(DEBUG_ERR, (__location__ " ERROR: Failed to talloc script_monitoring context\n"));
817                 return -1;
818         }
819
820         state = talloc(monitoring_status, struct ctdb_event_script_state);
821         if (state == NULL) {
822                 DEBUG(DEBUG_ERR,(__location__ " could not allocate state\n"));
823                 return -1;
824         }
825         monitoring_status->state = state;
826
827         state->ctdb = ctdb;
828         state->callback = callback;
829         state->private_data = private_data;
830         state->options = talloc_vasprintf(state, fmt, ap);
831         state->timeout = timeout;
832         state->te = NULL;
833         if (state->options == NULL) {
834                 DEBUG(DEBUG_ERR, (__location__ " could not allocate state->options\n"));
835                 talloc_free(state);
836                 return -1;
837         }
838
839         DEBUG(DEBUG_INFO,(__location__ " Starting eventscript %s\n", state->options));
840         
841         ret = pipe(state->fd);
842         if (ret != 0) {
843                 talloc_free(state);
844                 return -1;
845         }
846
847         state->child = fork();
848
849         if (state->child == (pid_t)-1) {
850                 close(state->fd[0]);
851                 close(state->fd[1]);
852                 talloc_free(state);
853                 return -1;
854         }
855
856         if (state->child == 0) {
857                 signed char rt;
858
859                 close(state->fd[0]);
860                 set_close_on_exec(state->fd[1]);
861
862                 rt = ctdb_event_script_v(ctdb, state->options);
863                 while ((ret = write(state->fd[1], &rt, sizeof(rt))) != sizeof(rt)) {
864                         write(state->fd[1], &rt, sizeof(rt));
865                         usleep(100000);
866                 }
867                 _exit(rt);
868         }
869
870         talloc_set_destructor(state, event_script_destructor);
871         if (!strcmp(fmt, "monitor")) {
872                 ctdb->script_monitor_ctx = monitoring_status;
873         } else {
874                 ctdb->event_script_ctx  = monitoring_status;
875         }
876
877         close(state->fd[1]);
878         set_close_on_exec(state->fd[0]);
879
880         DEBUG(DEBUG_DEBUG, (__location__ " Created PIPE FD:%d to child eventscript process\n", state->fd[0]));
881
882         event_add_fd(ctdb->ev, state, state->fd[0], EVENT_FD_READ|EVENT_FD_AUTOCLOSE,
883                      ctdb_event_script_handler, state);
884
885         if (!timeval_is_zero(&state->timeout)) {
886                 state->te = event_add_timed(ctdb->ev, state, timeval_current_ofs(state->timeout.tv_sec, state->timeout.tv_usec), ctdb_event_script_timeout, state);
887         } else {
888                 DEBUG(DEBUG_ERR, (__location__ " eventscript %s called with no timeout\n", state->options));
889         }
890
891         return 0;
892 }
893
894
895 /*
896   run the event script in the background, calling the callback when 
897   finished
898  */
899 int ctdb_event_script_callback(struct ctdb_context *ctdb, 
900                                struct timeval timeout,
901                                TALLOC_CTX *mem_ctx,
902                                void (*callback)(struct ctdb_context *, int, void *),
903                                void *private_data,
904                                const char *fmt, ...)
905 {
906         va_list ap;
907         int ret;
908
909         va_start(ap, fmt);
910         ret = ctdb_event_script_callback_v(ctdb, timeout, callback, private_data, fmt, ap);
911         va_end(ap);
912
913         return ret;
914 }
915
916
917 struct callback_status {
918         bool done;
919         int status;
920 };
921
922 /*
923   called when ctdb_event_script() finishes
924  */
925 static void event_script_callback(struct ctdb_context *ctdb, int status, void *private_data)
926 {
927         struct callback_status *s = (struct callback_status *)private_data;
928         s->done = true;
929         s->status = status;
930 }
931
932 /*
933   run the event script, waiting for it to complete. Used when the caller doesn't want to 
934   continue till the event script has finished.
935  */
936 int ctdb_event_script(struct ctdb_context *ctdb, const char *fmt, ...)
937 {
938         va_list ap;
939         int ret;
940         struct callback_status status;
941
942         va_start(ap, fmt);
943         ret = ctdb_event_script_callback_v(ctdb, 
944                         timeval_set(ctdb->tunable.script_timeout, 0),
945                         event_script_callback, &status, fmt, ap);
946         va_end(ap);
947
948         if (ret != 0) {
949                 return ret;
950         }
951
952         status.status = -1;
953         status.done = false;
954
955         while (status.done == false && event_loop_once(ctdb->ev) == 0) /* noop */;
956
957         return status.status;
958 }
959
960
961 struct eventscript_callback_state {
962         struct ctdb_req_control *c;
963 };
964
965 /*
966   called when takeip event finishes
967  */
968 static void run_eventscripts_callback(struct ctdb_context *ctdb, int status, 
969                                  void *private_data)
970 {
971         struct eventscript_callback_state *state = 
972                 talloc_get_type(private_data, struct eventscript_callback_state);
973
974         ctdb_enable_monitoring(ctdb);
975
976         if (status != 0) {
977                 DEBUG(DEBUG_ERR,(__location__ " Failed to forcibly run eventscripts\n"));
978                 ctdb_request_control_reply(ctdb, state->c, NULL, status, NULL);
979                 talloc_free(state);
980                 return;
981         }
982
983         /* the control succeeded */
984         ctdb_request_control_reply(ctdb, state->c, NULL, 0, NULL);
985         talloc_free(state);
986         return;
987 }
988
989 /*
990   A control to force running of the eventscripts from the ctdb client tool
991 */
992 int32_t ctdb_run_eventscripts(struct ctdb_context *ctdb,
993                 struct ctdb_req_control *c,
994                 TDB_DATA indata, bool *async_reply)
995 {
996         int ret;
997         struct eventscript_callback_state *state;
998
999         /* kill off any previous invokations of forced eventscripts */
1000         if (ctdb->eventscripts_ctx) {
1001                 talloc_free(ctdb->eventscripts_ctx);
1002         }
1003         ctdb->eventscripts_ctx = talloc_new(ctdb);
1004         CTDB_NO_MEMORY(ctdb, ctdb->eventscripts_ctx);
1005
1006         state = talloc(ctdb->eventscripts_ctx, struct eventscript_callback_state);
1007         CTDB_NO_MEMORY(ctdb, state);
1008
1009         state->c = talloc_steal(state, c);
1010
1011         DEBUG(DEBUG_NOTICE,("Forced running of eventscripts with arguments %s\n", indata.dptr));
1012
1013         if (ctdb->recovery_mode != CTDB_RECOVERY_NORMAL) {
1014                 DEBUG(DEBUG_ERR, (__location__ " Aborted running eventscript \"%s\" while in RECOVERY mode\n", indata.dptr));
1015                 return -1;
1016         }
1017
1018         ctdb_disable_monitoring(ctdb);
1019
1020         ret = ctdb_event_script_callback(ctdb, 
1021                          timeval_set(ctdb->tunable.script_timeout, 0),
1022                          state, run_eventscripts_callback, state,
1023                          "%s", (const char *)indata.dptr);
1024
1025         if (ret != 0) {
1026                 ctdb_enable_monitoring(ctdb);
1027                 DEBUG(DEBUG_ERR,(__location__ " Failed to run eventscripts with arguments %s\n", indata.dptr));
1028                 talloc_free(state);
1029                 return -1;
1030         }
1031
1032         /* tell ctdb_control.c that we will be replying asynchronously */
1033         *async_reply = true;
1034
1035         return 0;
1036 }
1037
1038
1039
1040 int32_t ctdb_control_enable_script(struct ctdb_context *ctdb, TDB_DATA indata)
1041 {
1042         const char *script;
1043         struct stat st;
1044         char *filename;
1045         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1046
1047         script = (char *)indata.dptr;
1048         if (indata.dsize == 0) {
1049                 DEBUG(DEBUG_ERR,(__location__ " No script specified.\n"));
1050                 talloc_free(tmp_ctx);
1051                 return -1;
1052         }
1053         if (indata.dptr[indata.dsize - 1] != '\0') {
1054                 DEBUG(DEBUG_ERR,(__location__ " String is not null terminated.\n"));
1055                 talloc_free(tmp_ctx);
1056                 return -1;
1057         }
1058         if (index(script,'/') != NULL) {
1059                 DEBUG(DEBUG_ERR,(__location__ " Script name contains '/'. Failed to enable script %s\n", script));
1060                 talloc_free(tmp_ctx);
1061                 return -1;
1062         }
1063
1064
1065         if (stat(ctdb->event_script_dir, &st) != 0 && 
1066             errno == ENOENT) {
1067                 DEBUG(DEBUG_CRIT,("No event script directory found at '%s'\n", ctdb->event_script_dir));
1068                 talloc_free(tmp_ctx);
1069                 return -1;
1070         }
1071
1072
1073         filename = talloc_asprintf(tmp_ctx, "%s/%s", ctdb->event_script_dir, script);
1074         if (filename == NULL) {
1075                 DEBUG(DEBUG_ERR,(__location__ " Failed to create script path\n"));
1076                 talloc_free(tmp_ctx);
1077                 return -1;
1078         }
1079
1080         if (stat(filename, &st) != 0) {
1081                 DEBUG(DEBUG_ERR,("Could not stat event script %s. Failed to enable script.\n", filename));
1082                 talloc_free(tmp_ctx);
1083                 return -1;
1084         }
1085
1086         if (chmod(filename, st.st_mode | S_IXUSR) == -1) {
1087                 DEBUG(DEBUG_ERR,("Could not chmod %s. Failed to enable script.\n", filename));
1088                 talloc_free(tmp_ctx);
1089                 return -1;
1090         }
1091
1092         talloc_free(tmp_ctx);
1093         return 0;
1094 }
1095
1096 int32_t ctdb_control_disable_script(struct ctdb_context *ctdb, TDB_DATA indata)
1097 {
1098         const char *script;
1099         struct stat st;
1100         char *filename;
1101         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1102
1103         script = (char *)indata.dptr;
1104         if (indata.dsize == 0) {
1105                 DEBUG(DEBUG_ERR,(__location__ " No script specified.\n"));
1106                 talloc_free(tmp_ctx);
1107                 return -1;
1108         }
1109         if (indata.dptr[indata.dsize - 1] != '\0') {
1110                 DEBUG(DEBUG_ERR,(__location__ " String is not null terminated.\n"));
1111                 talloc_free(tmp_ctx);
1112                 return -1;
1113         }
1114         if (index(script,'/') != NULL) {
1115                 DEBUG(DEBUG_ERR,(__location__ " Script name contains '/'. Failed to disable script %s\n", script));
1116                 talloc_free(tmp_ctx);
1117                 return -1;
1118         }
1119
1120
1121         if (stat(ctdb->event_script_dir, &st) != 0 && 
1122             errno == ENOENT) {
1123                 DEBUG(DEBUG_CRIT,("No event script directory found at '%s'\n", ctdb->event_script_dir));
1124                 talloc_free(tmp_ctx);
1125                 return -1;
1126         }
1127
1128
1129         filename = talloc_asprintf(tmp_ctx, "%s/%s", ctdb->event_script_dir, script);
1130         if (filename == NULL) {
1131                 DEBUG(DEBUG_ERR,(__location__ " Failed to create script path\n"));
1132                 talloc_free(tmp_ctx);
1133                 return -1;
1134         }
1135
1136         if (stat(filename, &st) != 0) {
1137                 DEBUG(DEBUG_ERR,("Could not stat event script %s. Failed to disable script.\n", filename));
1138                 talloc_free(tmp_ctx);
1139                 return -1;
1140         }
1141
1142         if (chmod(filename, st.st_mode & ~(S_IXUSR|S_IXGRP|S_IXOTH)) == -1) {
1143                 DEBUG(DEBUG_ERR,("Could not chmod %s. Failed to disable script.\n", filename));
1144                 talloc_free(tmp_ctx);
1145                 return -1;
1146         }
1147
1148         talloc_free(tmp_ctx);
1149         return 0;
1150 }