28 #include <qb/qbarray.h> 30 struct mainloop_child_s {
53 crm_trigger_prepare(GSource * source, gint *
timeout)
80 crm_trigger_check(GSource * source)
88 crm_trigger_dispatch(GSource * source, GSourceFunc callback, gpointer userdata)
97 trig->trigger = FALSE;
100 rc = callback(trig->user_data);
102 crm_trace(
"Trigger handler %p not yet complete", trig);
103 trig->running = TRUE;
111 crm_trigger_finalize(GSource * source)
113 crm_trace(
"Trigger %p destroyed", source);
116 static GSourceFuncs crm_trigger_funcs = {
119 crm_trigger_dispatch,
120 crm_trigger_finalize,
124 mainloop_setup_trigger(GSource * source,
int priority,
int (*dispatch) (gpointer user_data),
132 trigger->trigger = FALSE;
133 trigger->user_data = userdata;
136 g_source_set_callback(source, dispatch, trigger, NULL);
139 g_source_set_priority(source, priority);
140 g_source_set_can_recurse(source, FALSE);
142 trigger->id = g_source_attach(source, NULL);
149 crm_trace(
"Trigger handler %p complete", trig);
150 trig->running = FALSE;
161 GSource *source = NULL;
164 source = g_source_new(&crm_trigger_funcs,
sizeof(
crm_trigger_t));
167 return mainloop_setup_trigger(source, priority, dispatch, userdata);
174 source->trigger = TRUE;
187 gs = (GSource *)source;
189 g_source_destroy(gs);
204 typedef struct signal_s {
206 void (*handler) (
int sig);
225 crm_signal_dispatch(GSource * source, GSourceFunc callback, gpointer userdata)
229 if(sig->signal != SIGCHLD) {
231 strsignal(sig->signal), sig->signal,
232 (sig->handler?
"invoking" :
"no"));
235 sig->trigger.trigger = FALSE;
237 sig->handler(sig->signal);
252 mainloop_signal_handler(
int sig)
254 if (sig > 0 && sig < NSIG && crm_signals[sig] != NULL) {
260 static GSourceFuncs crm_signal_funcs = {
264 crm_trigger_finalize,
284 struct sigaction old;
286 if (sigemptyset(&mask) < 0) {
287 crm_err(
"Could not set handler for signal %d: %s",
292 memset(&sa, 0,
sizeof(
struct sigaction));
293 sa.sa_handler = dispatch;
294 sa.sa_flags = SA_RESTART;
297 if (sigaction(sig, &sa, &old) < 0) {
298 crm_err(
"Could not set handler for signal %d: %s",
302 return old.sa_handler;
306 mainloop_destroy_signal_entry(
int sig)
310 crm_signals[sig] = NULL;
330 GSource *source = NULL;
331 int priority = G_PRIORITY_HIGH - 1;
333 if (sig == SIGTERM) {
341 if (sig >= NSIG || sig < 0) {
342 crm_err(
"Signal %d is out of range", sig);
345 }
else if (crm_signals[sig] != NULL && crm_signals[sig]->handler == dispatch) {
346 crm_trace(
"Signal handler for %d is already installed", sig);
349 }
else if (crm_signals[sig] != NULL) {
350 crm_err(
"Different signal handler for %d is already installed", sig);
355 source = g_source_new(&crm_signal_funcs,
sizeof(
crm_signal_t));
357 crm_signals[sig] = (
crm_signal_t *) mainloop_setup_trigger(source, priority, NULL, NULL);
360 crm_signals[sig]->handler = dispatch;
361 crm_signals[sig]->signal = sig;
364 mainloop_destroy_signal_entry(sig);
373 if (siginterrupt(sig, 1) < 0) {
374 crm_perror(LOG_INFO,
"Could not enable system call interruptions for signal %d", sig);
384 if (sig >= NSIG || sig < 0) {
385 crm_err(
"Signal %d is out of range", sig);
389 crm_perror(LOG_ERR,
"Could not uninstall signal handler for signal %d", sig);
392 }
else if (crm_signals[sig] == NULL) {
395 mainloop_destroy_signal_entry(sig);
399 static qb_array_t *gio_map = NULL;
405 qb_array_free(gio_map);
408 for (
int sig = 0; sig < NSIG; ++sig) {
409 mainloop_destroy_signal_entry(sig);
416 struct gio_to_qb_poll {
421 qb_ipcs_dispatch_fn_t fn;
422 enum qb_loop_priority p;
426 gio_read_socket(GIOChannel * gio, GIOCondition condition, gpointer
data)
428 struct gio_to_qb_poll *adaptor = (
struct gio_to_qb_poll *)
data;
429 gint fd = g_io_channel_unix_get_fd(gio);
437 return (adaptor->fn(fd, condition, adaptor->data) == 0);
441 gio_poll_destroy(gpointer
data)
443 struct gio_to_qb_poll *adaptor = (
struct gio_to_qb_poll *)
data;
448 if (adaptor->is_used == 0) {
449 crm_trace(
"Marking adaptor %p unused", adaptor);
463 conv_prio_libqb2glib(
enum qb_loop_priority prio)
465 gint ret = G_PRIORITY_DEFAULT;
468 ret = G_PRIORITY_LOW;
471 ret = G_PRIORITY_HIGH;
474 crm_trace(
"Invalid libqb's loop priority %d, assuming QB_LOOP_MED",
491 static enum qb_ipcs_rate_limit
492 conv_libqb_prio2ratelimit(
enum qb_loop_priority prio)
495 enum qb_ipcs_rate_limit ret = QB_IPCS_RATE_NORMAL;
498 ret = QB_IPCS_RATE_SLOW;
501 ret = QB_IPCS_RATE_FAST;
504 crm_trace(
"Invalid libqb's loop priority %d, assuming QB_LOOP_MED",
514 gio_poll_dispatch_update(
enum qb_loop_priority p, int32_t fd, int32_t evts,
515 void *
data, qb_ipcs_dispatch_fn_t fn, int32_t add)
517 struct gio_to_qb_poll *adaptor;
521 res = qb_array_index(gio_map, fd, (
void **)&adaptor);
523 crm_err(
"Array lookup failed for fd=%d: %d", fd, res);
527 crm_trace(
"Adding fd=%d to mainloop as adaptor %p", fd, adaptor);
529 if (add && adaptor->source) {
530 crm_err(
"Adaptor for descriptor %d is still in-use", fd);
533 if (!add && !adaptor->is_used) {
534 crm_err(
"Adaptor for descriptor %d is not in-use", fd);
539 channel = g_io_channel_unix_new(fd);
541 crm_err(
"No memory left to add fd=%d", fd);
545 if (adaptor->source) {
546 g_source_remove(adaptor->source);
551 evts |= (G_IO_HUP | G_IO_NVAL | G_IO_ERR);
554 adaptor->events = evts;
555 adaptor->data =
data;
559 g_io_add_watch_full(channel, conv_prio_libqb2glib(p), evts,
560 gio_read_socket, adaptor, gio_poll_destroy);
571 g_io_channel_unref(channel);
573 crm_trace(
"Added to mainloop with gsource id=%d", adaptor->source);
574 if (adaptor->source > 0) {
582 gio_poll_dispatch_add(
enum qb_loop_priority p, int32_t fd, int32_t evts,
583 void *
data, qb_ipcs_dispatch_fn_t fn)
585 return gio_poll_dispatch_update(p, fd, evts,
data, fn, QB_TRUE);
589 gio_poll_dispatch_mod(
enum qb_loop_priority p, int32_t fd, int32_t evts,
590 void *
data, qb_ipcs_dispatch_fn_t fn)
592 return gio_poll_dispatch_update(p, fd, evts,
data, fn, QB_FALSE);
596 gio_poll_dispatch_del(int32_t fd)
598 struct gio_to_qb_poll *adaptor;
601 if (qb_array_index(gio_map, fd, (
void **)&adaptor) == 0) {
602 if (adaptor->source) {
603 g_source_remove(adaptor->source);
612 .dispatch_add = gio_poll_dispatch_add,
613 .dispatch_mod = gio_poll_dispatch_mod,
614 .dispatch_del = gio_poll_dispatch_del,
617 static enum qb_ipc_type
618 pick_ipc_type(
enum qb_ipc_type requested)
620 const char *env = getenv(
"PCMK_ipc_type");
622 if (env && strcmp(
"shared-mem", env) == 0) {
624 }
else if (env && strcmp(
"socket", env) == 0) {
625 return QB_IPC_SOCKET;
626 }
else if (env && strcmp(
"posix", env) == 0) {
627 return QB_IPC_POSIX_MQ;
628 }
else if (env && strcmp(
"sysv", env) == 0) {
629 return QB_IPC_SYSV_MQ;
630 }
else if (requested == QB_IPC_NATIVE) {
644 struct qb_ipcs_service_handlers *callbacks)
651 struct qb_ipcs_service_handlers *callbacks,
652 enum qb_loop_priority prio)
655 qb_ipcs_service_t *server = NULL;
657 if (gio_map == NULL) {
658 gio_map = qb_array_create_2(64,
sizeof(
struct gio_to_qb_poll), 1);
661 server = qb_ipcs_create(
name, 0, pick_ipc_type(
type), callbacks);
663 if (server == NULL) {
668 if (prio != QB_LOOP_MED) {
669 qb_ipcs_request_rate_limit(server, conv_libqb_prio2ratelimit(prio));
672 #ifdef HAVE_IPCS_GET_BUFFER_SIZE 679 rc = qb_ipcs_run(server);
692 qb_ipcs_destroy(server);
696 struct mainloop_io_s {
705 int (*dispatch_fn_ipc) (
const char *buffer, ssize_t length, gpointer userdata);
706 int (*dispatch_fn_io) (gpointer userdata);
707 void (*destroy_fn) (gpointer userdata);
712 mainloop_gio_callback(GIOChannel * gio, GIOCondition condition, gpointer
data)
714 gboolean keep = TRUE;
717 CRM_ASSERT(client->fd == g_io_channel_unix_get_fd(gio));
719 if (condition & G_IO_IN) {
727 crm_trace(
"Message acquisition from %s[%p] failed: %s (%ld)",
730 }
else if (client->dispatch_fn_ipc) {
733 crm_trace(
"New message from %s[%p] = %ld (I/O condition=%d)", client->name, client,
rc, condition);
734 if (client->dispatch_fn_ipc(buffer,
rc, client->userdata) < 0) {
735 crm_trace(
"Connection to %s no longer required", client->name);
740 }
while (keep &&
rc > 0 && --max > 0);
743 crm_trace(
"New message from %s[%p] %u", client->name, client, condition);
744 if (client->dispatch_fn_io) {
745 if (client->dispatch_fn_io(client->userdata) < 0) {
746 crm_trace(
"Connection to %s no longer required", client->name);
754 crm_err(
"Connection to %s closed " CRM_XS "client=%p condition=%d",
755 client->name, client, condition);
758 }
else if (condition & (G_IO_HUP | G_IO_NVAL | G_IO_ERR)) {
759 crm_trace(
"The connection %s[%p] has been closed (I/O condition=%d)",
760 client->name, client, condition);
763 }
else if ((condition & G_IO_IN) == 0) {
791 crm_err(
"Strange condition: %d", condition);
801 mainloop_gio_destroy(gpointer c)
804 char *c_name = strdup(client->name);
809 crm_trace(
"Destroying client %s[%p]", c_name, c);
815 if (client->destroy_fn) {
816 void (*destroy_fn) (gpointer userdata) = client->destroy_fn;
818 client->destroy_fn = NULL;
819 destroy_fn(client->userdata);
829 crm_trace(
"Destroyed client %s[%p]", c_name, c);
831 free(client->name); client->name = NULL;
860 CRM_CHECK((ipc != NULL) && (callbacks != NULL),
return EINVAL);
867 if (*source == NULL) {
873 (*source)->ipc = ipc;
874 (*source)->destroy_fn = callbacks->
destroy;
875 (*source)->dispatch_fn_ipc = callbacks->
dispatch;
890 fprintf(stderr,
"Connection to %s failed: %s",
922 if (client == NULL) {
925 client->name = strdup(
name);
926 client->userdata = userdata;
929 client->destroy_fn = callbacks->
destroy;
930 client->dispatch_fn_io = callbacks->
dispatch;
934 client->channel = g_io_channel_unix_new(fd);
936 g_io_add_watch_full(client->channel, priority,
937 (G_IO_IN | G_IO_HUP | G_IO_NVAL | G_IO_ERR), mainloop_gio_callback,
938 client, mainloop_gio_destroy);
949 g_io_channel_unref(client->channel);
950 crm_trace(
"Added connection %d for %s[%p].%d", client->source, client->name, client, fd);
961 if (client != NULL) {
962 crm_trace(
"Removing client %s[%p]", client->name, client);
963 if (client->source) {
967 g_source_remove(client->source);
989 return child->timeout;
995 return child->privatedata;
1001 child->privatedata = NULL;
1008 if (child->timerid != 0) {
1009 crm_trace(
"Removing timer %d", child->timerid);
1010 g_source_remove(child->timerid);
1023 crm_debug(
"Kill pid %d only. leave group intact.", child->pid);
1024 rc = kill(child->pid, SIGKILL);
1026 crm_debug(
"Kill pid %d's group", child->pid);
1027 rc = kill(-child->pid, SIGKILL);
1031 if (errno != ESRCH) {
1032 crm_perror(LOG_ERR,
"kill(%d, KILL) failed", child->pid);
1040 child_timeout_callback(gpointer p)
1046 if (child->timeout) {
1047 crm_crit(
"%s process (PID %d) will not die!", child->desc, (
int)child->pid);
1051 rc = child_kill_helper(child);
1057 child->timeout = TRUE;
1058 crm_warn(
"%s process (PID %d) timed out", child->desc, (
int)child->pid);
1060 child->timerid = g_timeout_add(5000, child_timeout_callback, child);
1072 bool callback_needed =
true;
1074 rc = waitpid(child->pid, &status,
flags);
1076 crm_trace(
"Child process %d (%s) still active",
1077 child->pid, child->desc);
1078 callback_needed =
false;
1080 }
else if (
rc != child->pid) {
1092 crm_notice(
"Wait for child process %d (%s) interrupted: %s",
1095 }
else if (WIFEXITED(status)) {
1096 exitcode = WEXITSTATUS(status);
1097 crm_trace(
"Child process %d (%s) exited with status %d",
1098 child->pid, child->desc, exitcode);
1100 }
else if (WIFSIGNALED(status)) {
1101 signo = WTERMSIG(status);
1102 crm_trace(
"Child process %d (%s) exited with signal %d (%s)",
1103 child->pid, child->desc, signo, strsignal(signo));
1105 #ifdef WCOREDUMP // AIX, SunOS, maybe others 1106 }
else if (WCOREDUMP(status)) {
1108 crm_err(
"Child process %d (%s) dumped core",
1109 child->pid, child->desc);
1113 crm_trace(
"Child process %d (%s) stopped or continued",
1114 child->pid, child->desc);
1115 callback_needed =
false;
1118 if (callback_needed && child->callback) {
1119 child->callback(child, child->pid, core, signo, exitcode);
1121 return callback_needed;
1125 child_death_dispatch(
int signal)
1127 for (GList *iter = child_list; iter; ) {
1128 GList *saved = iter;
1132 if (child_waitpid(child, WNOHANG)) {
1133 crm_trace(
"Removing completed process %d from child list",
1135 child_list = g_list_remove_link(child_list, saved);
1143 child_signal_init(gpointer p)
1150 child_death_dispatch(SIGCHLD);
1162 int waitflags = 0,
rc = 0;
1164 for (iter = child_list; iter != NULL && match == NULL; iter = iter->next) {
1166 if (
pid == child->pid) {
1171 if (match == NULL) {
1175 rc = child_kill_helper(match);
1182 crm_trace(
"Waiting for signal that child process %d completed",
1186 }
else if(
rc != 0) {
1190 waitflags = WNOHANG;
1193 if (!child_waitpid(match, waitflags)) {
1198 child_list = g_list_remove(child_list, match);
1214 static bool need_init = TRUE;
1219 child->timeout = FALSE;
1220 child->privatedata = privatedata;
1221 child->callback = callback;
1222 child->flags =
flags;
1225 child->desc = strdup(desc);
1229 child->timerid = g_timeout_add(
timeout, child_timeout_callback, child);
1232 child_list = g_list_append(child_list, child);
1240 g_timeout_add(1, child_signal_init, NULL);
1251 struct mainloop_timer_s {
1261 mainloop_timer_cb(gpointer user_data)
1264 bool repeat = FALSE;
1265 struct mainloop_timer_s *t = user_data;
1275 crm_trace(
"Invoking callbacks for timer %s", t->name);
1277 if(t->cb(t->userdata) == FALSE) {
1278 crm_trace(
"Timer %s complete", t->name);
1294 if(t && t->id != 0) {
1304 if(t && t->period_ms > 0) {
1305 crm_trace(
"Starting timer %s", t->name);
1306 t->id = g_timeout_add(t->period_ms, mainloop_timer_cb, t);
1313 if(t && t->id != 0) {
1314 crm_trace(
"Stopping timer %s", t->name);
1315 g_source_remove(t->id);
1326 last = t->period_ms;
1327 t->period_ms = period_ms;
1330 if(t && t->id != 0 && last != t->period_ms) {
1348 t->period_ms = period_ms;
1351 t->userdata = userdata;
1352 crm_trace(
"Created timer %s with %p %p", t->name, userdata, t->userdata);
1361 crm_trace(
"Destroying timer %s", t->name);
1373 drain_timeout_cb(gpointer user_data)
1375 bool *timeout_popped = (
bool*) user_data;
1377 *timeout_popped = TRUE;
1390 if ((mloop != NULL) && g_main_loop_is_running(mloop)) {
1391 GMainContext *ctx = g_main_loop_get_context(mloop);
1396 for (
int i = 0; (i < n) && g_main_context_pending(ctx); ++i) {
1397 g_main_context_dispatch(ctx);
1399 g_main_loop_quit(mloop);
1418 bool timeout_popped = FALSE;
1420 GMainContext *ctx = NULL;
1424 ctx = g_main_loop_get_context(mloop);
1426 time_t start_time = time(NULL);
1428 timer = g_timeout_add(timer_ms, drain_timeout_cb, &timeout_popped);
1429 while (!timeout_popped
1430 && check(timer_ms - (time(NULL) - start_time) * 1000)) {
1431 g_main_context_iteration(ctx, TRUE);
1434 if (!timeout_popped && (timer > 0)) {
1435 g_source_remove(timer);
1440 gboolean
crm_signal(
int sig,
void (*dispatch) (
int sig));
void * mainloop_child_userdata(mainloop_child_t *child)
#define CRM_CHECK(expr, failure_action)
pid_t mainloop_child_pid(mainloop_child_t *child)
bool mainloop_timer_running(mainloop_timer_t *t)
bool crm_ipc_connect(crm_ipc_t *client)
Establish an IPC connection to a Pacemaker component.
void(* destroy)(gpointer)
#define crm_notice(fmt, args...)
const char * pcmk_strerror(int rc)
struct signal_s crm_signal_t
#define crm_crit(fmt, args...)
void mainloop_del_fd(mainloop_io_t *client)
void mainloop_del_ipc_server(qb_ipcs_service_t *server)
gboolean mainloop_destroy_signal(int sig)
crm_trigger_t * mainloop_add_trigger(int priority, int(*dispatch)(gpointer user_data), gpointer userdata)
void(* destroy)(gpointer userdata)
int crm_ipc_get_fd(crm_ipc_t *client)
void mainloop_trigger_complete(crm_trigger_t *trig)
const char * mainloop_child_name(mainloop_child_t *child)
void pcmk_quit_main_loop(GMainLoop *mloop, unsigned int n)
Drain some remaining main loop events then quit it.
struct mainloop_timer_s mainloop_timer_t
struct mainloop_io_s mainloop_io_t
struct mainloop_child_s mainloop_child_t
gboolean crm_signal(int sig, void(*dispatch)(int sig))
long crm_ipc_read(crm_ipc_t *client)
mainloop_timer_t * mainloop_timer_add(const char *name, guint period_ms, bool repeat, GSourceFunc cb, void *userdata)
const char * pcmk_rc_str(int rc)
Get a user-friendly description of a return code.
mainloop_io_t * mainloop_add_ipc_client(const char *name, int priority, size_t max_size, void *userdata, struct ipc_client_callbacks *callbacks)
struct qb_ipcs_poll_handlers gio_poll_funcs
guint mainloop_timer_set_period(mainloop_timer_t *t, guint period_ms)
Wrappers for and extensions to glib mainloop.
void mainloop_timer_del(mainloop_timer_t *t)
crm_ipc_t * mainloop_get_ipc_client(mainloop_io_t *client)
const char * crm_ipc_buffer(crm_ipc_t *client)
void mainloop_del_ipc_client(mainloop_io_t *client)
struct trigger_s crm_trigger_t
int(* dispatch)(gpointer userdata)
#define crm_warn(fmt, args...)
int mainloop_child_timeout(mainloop_child_t *child)
#define crm_debug(fmt, args...)
struct crm_ipc_s crm_ipc_t
void mainloop_set_trigger(crm_trigger_t *source)
#define crm_trace(fmt, args...)
mainloop_io_t * mainloop_add_fd(const char *name, int priority, int fd, void *userdata, struct mainloop_fd_callbacks *callbacks)
void mainloop_timer_start(mainloop_timer_t *t)
int pcmk__add_mainloop_ipc(crm_ipc_t *ipc, int priority, void *userdata, struct ipc_client_callbacks *callbacks, mainloop_io_t **source)
Connect to IPC and add it as a main loop source.
qb_ipcs_service_t * mainloop_add_ipc_server(const char *name, enum qb_ipc_type type, struct qb_ipcs_service_handlers *callbacks)
Wrappers for and extensions to libxml2.
void mainloop_clear_child_userdata(mainloop_child_t *child)
gboolean mainloop_child_kill(pid_t pid)
void mainloop_timer_stop(mainloop_timer_t *t)
void mainloop_child_add_with_flags(pid_t pid, int timeout, const char *desc, void *privatedata, enum mainloop_child_flags flags, void(*callback)(mainloop_child_t *p, pid_t pid, int core, int signo, int exitcode))
unsigned int crm_ipc_default_buffer_size(void)
Return pacemaker's default IPC buffer size.
void crm_ipc_destroy(crm_ipc_t *client)
void mainloop_child_add(pid_t pid, int timeout, const char *desc, void *privatedata, void(*callback)(mainloop_child_t *p, pid_t pid, int core, int signo, int exitcode))
bool crm_ipc_connected(crm_ipc_t *client)
unsigned int crm_log_level
void(* sighandler_t)(int)
const char * crm_ipc_name(crm_ipc_t *client)
#define crm_perror(level, fmt, args...)
Send a system error message to both the log and stderr.
qb_ipcs_service_t * mainloop_add_ipc_server_with_prio(const char *name, enum qb_ipc_type type, struct qb_ipcs_service_handlers *callbacks, enum qb_loop_priority prio)
Start server-side API end-point, hooked into the internal event loop.
#define crm_err(fmt, args...)
crm_ipc_t * crm_ipc_new(const char *name, size_t max_size)
Create a new (legacy) object for using Pacemaker daemon IPC.
void mainloop_cleanup(void)
gboolean mainloop_add_signal(int sig, void(*dispatch)(int sig))
gboolean mainloop_destroy_trigger(crm_trigger_t *source)
void pcmk_drain_main_loop(GMainLoop *mloop, guint timer_ms, bool(*check)(guint))
Process main loop events while a certain condition is met.
sighandler_t crm_signal_handler(int sig, sighandler_t dispatch)
void crm_ipc_close(crm_ipc_t *client)
char * crm_strdup_printf(char const *format,...) __attribute__((__format__(__printf__
int(* dispatch)(const char *buffer, ssize_t length, gpointer userdata)
enum crm_ais_msg_types type