2 CTDB event daemon - daemon state
4 Copyright (C) Amitay Isaacs 2018
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, see <http://www.gnu.org/licenses/>.
21 #include "system/dir.h"
26 #include "lib/util/debug.h"
27 #include "lib/util/dlinklist.h"
29 #include "common/logging.h"
30 #include "common/run_event.h"
31 #include "common/path.h"
33 #include "event/event_private.h"
36 struct event_event
*prev
, *next
;
39 struct run_event_script_list
*script_list
;
42 struct event_component
{
43 struct event_component
*prev
, *next
;
48 struct run_event_context
*run_ctx
;
51 struct event_event
*event
;
55 struct event_client
*prev
, *next
;
57 struct sock_client_context
*client
;
60 struct event_context
{
61 struct tevent_context
*ev
;
62 struct event_config
*config
;
63 struct run_proc_context
*run_proc_ctx
;
65 const char *script_dir
;
66 const char *debug_script
;
69 struct event_component
*component
;
72 struct event_client
*client
;
76 * event_event functions
79 static struct event_event
*eventd_event_find(struct event_component
*comp
,
80 const char *event_name
)
82 struct event_event
*event
;
84 if (event_name
== NULL
) {
88 for (event
= comp
->event
; event
!= NULL
; event
= event
->next
) {
89 if (strcmp(event
->name
, event_name
) == 0) {
97 static int eventd_event_add(struct event_component
*comp
,
98 const char *event_name
,
99 struct event_event
**result
)
101 struct event_event
*event
;
103 if (event_name
== NULL
) {
107 event
= eventd_event_find(comp
, event_name
);
112 event
= talloc_zero(comp
, struct event_event
);
117 event
->name
= talloc_strdup(event
, event_name
);
118 if (event
->name
== NULL
) {
123 DLIST_ADD_END(comp
->event
, event
);
126 if (result
!= NULL
) {
132 static int eventd_event_set(struct event_component
*comp
,
133 const char *event_name
,
134 struct run_event_script_list
*script_list
)
136 struct event_event
*event
= NULL
;
139 ret
= eventd_event_add(comp
, event_name
, &event
);
144 TALLOC_FREE(event
->script_list
);
145 if (script_list
!= NULL
) {
146 event
->script_list
= talloc_steal(event
, script_list
);
152 static int eventd_event_get(struct event_component
*comp
,
153 const char *event_name
,
154 struct run_event_script_list
**result
)
156 struct event_event
*event
;
158 event
= eventd_event_find(comp
, event_name
);
163 *result
= event
->script_list
;
168 * event_component functions
171 static struct event_component
*eventd_component_find(
172 struct event_context
*eventd
,
173 const char *comp_name
)
175 struct event_component
*comp
;
177 if (comp_name
== NULL
) {
181 for (comp
= eventd
->component
; comp
!= NULL
; comp
= comp
->next
) {
182 if (strcmp(comp
->name
, comp_name
) == 0) {
190 static int eventd_component_add(struct event_context
*eventd
,
191 const char *comp_name
,
192 struct event_component
**result
)
194 struct event_component
*comp
;
197 if (comp_name
== NULL
) {
201 comp
= eventd_component_find(eventd
, comp_name
);
206 comp
= talloc_zero(eventd
, struct event_component
);
211 comp
->name
= talloc_strdup(comp
, comp_name
);
212 if (comp
->name
== NULL
) {
217 comp
->path
= talloc_asprintf(comp
,
221 if (comp
->path
== NULL
) {
226 ret
= run_event_init(eventd
,
227 eventd
->run_proc_ctx
,
229 eventd
->debug_script
,
236 DLIST_ADD_END(eventd
->component
, comp
);
239 if (result
!= NULL
) {
246 * event_client functions
249 static struct event_client
*eventd_client_find(
250 struct event_context
*eventd
,
251 struct sock_client_context
*client
)
253 struct event_client
*e
;
255 for (e
= eventd
->client
; e
!= NULL
; e
= e
->next
) {
256 if (e
->client
== client
) {
264 int eventd_client_add(struct event_context
*eventd
,
265 struct sock_client_context
*client
)
267 struct event_client
*e
;
269 e
= talloc_zero(eventd
, struct event_client
);
276 DLIST_ADD_END(eventd
->client
, e
);
281 void eventd_client_del(struct event_context
*eventd
,
282 struct sock_client_context
*client
)
284 struct event_client
*e
;
286 e
= eventd_client_find(eventd
, client
);
291 DLIST_REMOVE(eventd
->client
, e
);
296 bool eventd_client_exists(struct event_context
*eventd
,
297 struct sock_client_context
*client
)
299 struct event_client
*e
;
301 e
= eventd_client_find(eventd
, client
);
309 /* public functions */
311 int event_context_init(TALLOC_CTX
*mem_ctx
,
312 struct tevent_context
*ev
,
313 struct event_config
*config
,
314 struct event_context
**result
)
316 struct event_context
*eventd
;
317 const char *debug_script
;
320 eventd
= talloc_zero(mem_ctx
, struct event_context
);
321 if (eventd
== NULL
) {
326 eventd
->config
= config
;
328 ret
= run_proc_init(eventd
, ev
, &eventd
->run_proc_ctx
);
334 eventd
->script_dir
= path_etcdir_append(eventd
, "events");
335 if (eventd
->script_dir
== NULL
) {
341 status = directory_exist(eventd->script_dir);
348 debug_script
= event_config_debug_script(config
);
349 if (debug_script
!= NULL
) {
350 eventd
->debug_script
= path_etcdir_append(eventd
,
352 if (eventd
->debug_script
== NULL
) {
353 D_WARNING("Failed to set debug script to %s\n",
362 struct event_config
*eventd_config(struct event_context
*eventd
)
364 return eventd
->config
;
367 int eventd_run_ctx(struct event_context
*eventd
,
368 const char *comp_name
,
369 struct run_event_context
**result
)
371 struct event_component
*comp
;
374 ret
= eventd_component_add(eventd
, comp_name
, &comp
);
379 *result
= comp
->run_ctx
;
383 int eventd_set_event_result(struct event_context
*eventd
,
384 const char *comp_name
,
385 const char *event_name
,
386 struct run_event_script_list
*script_list
)
388 struct event_component
*comp
;
390 comp
= eventd_component_find(eventd
, comp_name
);
395 return eventd_event_set(comp
, event_name
, script_list
);
398 int eventd_get_event_result(struct event_context
*eventd
,
399 const char *comp_name
,
400 const char *event_name
,
401 struct run_event_script_list
**result
)
403 struct event_component
*comp
;
406 ret
= eventd_component_add(eventd
, comp_name
, &comp
);
411 return eventd_event_get(comp
, event_name
, result
);
414 struct ctdb_event_script_list
*eventd_script_list(
416 struct run_event_script_list
*script_list
)
418 struct ctdb_event_script_list
*value
;
422 value
= talloc_zero(mem_ctx
, struct ctdb_event_script_list
);
427 if (script_list
!= NULL
) {
428 num_scripts
= script_list
->num_scripts
;
431 if (num_scripts
<= 0) {
435 value
->script
= talloc_array(value
,
436 struct ctdb_event_script
,
438 if (value
->script
== NULL
) {
442 for (i
=0; i
<num_scripts
; i
++) {
443 struct run_event_script
*rscript
= &script_list
->script
[i
];
444 struct ctdb_event_script
*escript
= &value
->script
[i
];
446 escript
->name
= talloc_strdup(value
, rscript
->name
);
447 if (escript
->name
== NULL
) {
451 escript
->begin
= rscript
->begin
;
452 escript
->end
= rscript
->end
;
453 escript
->result
= rscript
->summary
;
455 if (rscript
->output
== NULL
) {
456 escript
->output
= NULL
;
460 escript
->output
= talloc_strdup(value
, rscript
->output
);
461 if (escript
->output
== NULL
) {
465 value
->num_scripts
= num_scripts
;