21#include <sys/resource.h>
30static void close_pipe(
int fildes[]);
40#ifdef HAVE_SYS_SIGNALFD_H
44#include <sys/signalfd.h>
47struct sigchld_data_s {
55sigchld_setup(
struct sigchld_data_s *
data)
57 sigemptyset(&(
data->mask));
58 sigaddset(&(
data->mask), SIGCHLD);
60 sigemptyset(&(
data->old_mask));
63 if (sigprocmask(SIG_BLOCK, &(
data->mask), &(
data->old_mask)) < 0) {
64 crm_info(
"Wait for child process completion failed: %s "
69 data->ignored =
false;
76sigchld_open(
struct sigchld_data_s *
data)
82 fd = signalfd(-1, &(
data->mask), SFD_NONBLOCK);
84 crm_info(
"Wait for child process completion failed: %s "
101sigchld_received(
int fd,
int pid,
struct sigchld_data_s *
data)
103 struct signalfd_siginfo fdsi;
109 s = read(fd, &fdsi,
sizeof(
struct signalfd_siginfo));
110 if (s !=
sizeof(
struct signalfd_siginfo)) {
111 crm_info(
"Wait for child process completion failed: %s "
114 }
else if (fdsi.ssi_signo == SIGCHLD) {
115 if (fdsi.ssi_pid ==
pid) {
124 data->ignored =
true;
133sigchld_cleanup(
struct sigchld_data_s *
data)
136 if ((sigismember(&(
data->old_mask), SIGCHLD) == 0)
137 && (sigprocmask(SIG_UNBLOCK, &(
data->mask), NULL) < 0)) {
138 crm_warn(
"Could not clean up after child process completion: %s",
143 if (
data->ignored && kill(getpid(), SIGCHLD) != 0) {
144 crm_warn(
"Could not resend ignored SIGCHLD to ourselves: %s",
153struct sigchld_data_s {
156 struct sigaction old_sa;
170 crm_info(
"Wait for child process completion failed: %s "
176sigchld_setup(
struct sigchld_data_s *
data)
180 data->pipe_fd[0] =
data->pipe_fd[1] = -1;
182 if (pipe(
data->pipe_fd) == -1) {
183 crm_info(
"Wait for child process completion failed: %s "
190 crm_info(
"Could not set pipe input non-blocking: %s " QB_XS
" rc=%d",
195 crm_info(
"Could not set pipe output non-blocking: %s " QB_XS
" rc=%d",
201 data->sa.sa_flags = 0;
202 sigemptyset(&(
data->sa.sa_mask));
203 if (sigaction(SIGCHLD, &(
data->sa), &(
data->old_sa)) < 0) {
204 crm_info(
"Wait for child process completion failed: %s "
208 data->ignored =
false;
216sigchld_open(
struct sigchld_data_s *
data)
219 return data->pipe_fd[0];
230sigchld_received(
int fd,
int pid,
struct sigchld_data_s *
data)
239 while (read(fd, &ch, 1) == 1) ;
244sigchld_cleanup(
struct sigchld_data_s *
data)
247 if (sigaction(SIGCHLD, &(
data->old_sa), NULL) < 0) {
248 crm_warn(
"Could not clean up after child process completion: %s",
252 close_pipe(
data->pipe_fd);
255 if (
data->ignored && kill(getpid(), SIGCHLD) != 0) {
256 crm_warn(
"Could not resend ignored SIGCHLD to ourselves: %s",
270close_pipe(
int fildes[])
272 if (fildes[0] >= 0) {
276 if (fildes[1] >= 0) {
282#define out_type(is_stderr) ((is_stderr)? "stderr" : "stdout")
285#define MAX_OUTPUT (10 * 1024 * 1024)
288svc_read_output(
int fd,
svc_action_t * op,
bool is_stderr)
293 size_t discarded = 0;
295 static const size_t buf_read_len =
sizeof(buf) - 1;
305 crm_trace(
"Reading %s stderr into offset %zu", op->
id, len);
307 }
else if (is_stderr == FALSE && op->
stdout_data) {
310 crm_trace(
"Reading %s stdout into offset %zu", op->
id, len);
318 rc = read(fd, buf, buf_read_len);
322 crm_trace(
"Received %zd bytes of %s %s: %.80s",
324 data = pcmk__realloc(
data, len + rc + 1);
325 strcpy(
data + len, buf);
331 }
else if (errno != EINTR) {
335 }
while ((rc == buf_read_len) || (rc < 0));
338 crm_warn(
"Truncated %s %s to %zu bytes (discarded %zu)",
352dispatch_stdout(gpointer userdata)
360dispatch_stderr(gpointer userdata)
368pipe_out_done(gpointer user_data)
382pipe_err_done(gpointer user_data)
394 .dispatch = dispatch_stdout,
395 .destroy = pipe_out_done,
399 .dispatch = dispatch_stderr,
400 .destroy = pipe_err_done,
404set_ocf_env(
const char *key,
const char *value, gpointer user_data)
406 if (setenv(key, value, 1) != 0) {
407 crm_perror(LOG_ERR,
"setenv failed for key:%s and value:%s", key, value);
412set_ocf_env_with_prefix(gpointer key, gpointer value, gpointer user_data)
416 snprintf(buffer,
sizeof(buffer), strcmp(key,
"OCF_CHECK_LEVEL") != 0 ?
"OCF_RESKEY_%s" :
"%s", (
char *)key);
417 set_ocf_env(buffer, value, user_data);
421set_alert_env(gpointer key, gpointer value, gpointer user_data)
426 rc = setenv(key, value, 1);
433 (
char*)key, (value? (
char*)value :
""));
435 crm_trace(
"setenv %s=%s", (
char*)key, (value? (
char*)value :
""));
448 void (*env_setter)(gpointer, gpointer, gpointer) = NULL;
449 if (op->
agent == NULL) {
450 env_setter = set_alert_env;
453 env_setter = set_ocf_env_with_prefix;
456 if (env_setter != NULL && op->
params != NULL) {
457 g_hash_table_foreach(op->
params, env_setter, NULL);
460 if (env_setter == NULL || env_setter == set_alert_env) {
470 set_ocf_env(
"OCF_RESOURCE_INSTANCE", op->
rsc, NULL);
473 if (op->
agent != NULL) {
474 set_ocf_env(
"OCF_RESOURCE_TYPE", op->
agent, NULL);
479 set_ocf_env(
"OCF_RESOURCE_PROVIDER", op->
provider, NULL);
484pipe_in_single_parameter(gpointer key, gpointer value, gpointer user_data)
488 size_t len = strlen(buffer);
498 }
while ((errno == EINTR) && (total < len));
512 g_hash_table_foreach(op->
params, pipe_in_single_parameter, (gpointer) op);
521 crm_debug(
"Scheduling another invocation of %s", op->
id);
611 op->
id, op->
pid, (is_stderr?
"stderr" :
"stdout"));
612 svc_read_output(fd, op, is_stderr);
633 strcpy(prefix + strlen(prefix) - strlen(
"error output"),
"output");
639#define EXIT_REASON_MAX_LEN 128
644 const char *reason_start = NULL;
645 const char *reason_end = NULL;
662 if ((reason_start == NULL) || (reason_start[0] ==
'\n')
663 || (reason_start[0] ==
'\0')) {
668 reason_end = strchr(reason_start,
'\n');
669 if (reason_end == NULL) {
670 reason_end = reason_start + strlen(reason_start);
708 finish_op_output(op,
true);
709 finish_op_output(op,
false);
714 crm_debug(
"%s[%d] exited with status %d", op->
id, op->
pid, exitcode);
717 parse_exit_reason_from_stderr(op);
722 crm_info(
"%s %s[%d] timed out after %s",
726 "%s did not complete within %s",
733 crm_info(
"%s[%d] terminated with signal %d (%s)",
734 op->
id, op->
pid, signo, strsignal(signo));
738 crm_info(
"%s[%d] terminated with signal %d (%s)",
739 op->
id, op->
pid, signo, strsignal(signo));
741 "%s interrupted by %s signal",
764 if ((op == NULL) || (op->
standard == NULL)) {
795 if ((op == NULL) || (op->
standard == NULL)) {
826 if ((op == NULL) || (op->
standard == NULL)) {
858 if ((op == NULL) || (op->
standard == NULL)) {
926exit_child(
const svc_action_t *op,
int exit_status,
const char *exit_reason)
928 if ((op != NULL) && (exit_reason != NULL)
947 signal(SIGPIPE, SIG_DFL);
949 if (sched_getscheduler(0) != SCHED_OTHER) {
950 struct sched_param sp;
952 memset(&sp, 0,
sizeof(sp));
953 sp.sched_priority = 0;
955 if (sched_setscheduler(0, SCHED_OTHER, &sp) == -1) {
956 crm_info(
"Could not reset scheduling policy for %s", op->
id);
960 if (setpriority(PRIO_PROCESS, 0, 0) == -1) {
961 crm_info(
"Could not reset process priority for %s", op->
id);
983#if PCMK__ENABLE_CIBSECRETS
987 crm_info(
"Proceeding with stop operation for %s "
988 "despite being unable to load CIB secrets (%s)",
991 crm_err(
"Considering %s unconfigured "
992 "because unable to load CIB secrets: %s",
995 "Unable to load CIB secrets");
1000 add_action_env_vars(op);
1003 if (op->
opaque->
uid && (geteuid() == 0)) {
1007 crm_err(
"Considering %s unauthorized because could not set "
1008 "child group to %d: %s",
1011 "Could not set group for child process");
1016 if (setgroups(0, NULL) < 0) {
1017 crm_err(
"Considering %s unauthorized because could not "
1018 "clear supplementary groups: %s", op->
id, strerror(errno));
1020 "Could not clear supplementary groups for child process");
1025 crm_err(
"Considering %s unauthorized because could not set user "
1026 "to %d: %s", op->
id, op->
opaque->
uid, strerror(errno));
1028 "Could not set user for child process");
1038 crm_err(
"Unable to execute %s: %s", op->
id, strerror(rc));
1039 exit_child(op, op->
rc,
"Child process was unable to execute file");
1054 time_t start = time(NULL);
1055 struct pollfd fds[3];
1057 const char *wait_reason = NULL;
1060 fds[0].events = POLLIN;
1064 fds[1].events = POLLIN;
1067 fds[2].fd = sigchld_open(
data);
1068 fds[2].events = POLLIN;
1073 int poll_rc = poll(fds, 3,
timeout);
1078 if (fds[0].revents & POLLIN) {
1082 if (fds[1].revents & POLLIN) {
1086 if ((fds[2].revents & POLLIN)
1087 && sigchld_received(fds[2].fd, op->
pid,
data)) {
1088 wait_rc = waitpid(op->
pid, &status, WNOHANG);
1090 if ((wait_rc > 0) || ((wait_rc < 0) && (errno == ECHILD))) {
1094 }
else if (wait_rc < 0) {
1096 crm_info(
"Wait for completion of %s[%d] failed: %s "
1097 QB_XS
" source=waitpid",
1098 op->
id, op->
pid, wait_reason);
1101#ifndef HAVE_SYS_SIGNALFD_H
1109 data->ignored =
true;
1114 }
else if (poll_rc == 0) {
1119 }
else if ((poll_rc < 0) && (errno != EINTR)) {
1121 crm_info(
"Wait for completion of %s[%d] failed: %s "
1122 QB_XS
" source=poll", op->
id, op->
pid, wait_reason);
1131 finish_op_output(op,
true);
1132 finish_op_output(op,
false);
1134 sigchld_close(fds[2].fd);
1141 "%s did not exit within specified timeout",
1143 crm_info(
"%s[%d] timed out after %dms",
1153 if ((wait_rc == 0) && (waitpid(op->
pid, &status, WNOHANG) == 0)) {
1154 if (kill(op->
pid, SIGKILL)) {
1155 crm_warn(
"Could not kill rogue child %s[%d]: %s",
1159 while ((waitpid(op->
pid, &status, 0) == (pid_t) -1)
1160 && (errno == EINTR)) {
1165 }
else if (WIFEXITED(status)) {
1167 parse_exit_reason_from_stderr(op);
1170 }
else if (WIFSIGNALED(status)) {
1171 int signo = WTERMSIG(status);
1176 crm_info(
"%s[%d] terminated with signal %d (%s)",
1177 op->
id, op->
pid, signo, strsignal(signo));
1180 if (WCOREDUMP(status)) {
1188 "Unable to wait for child to complete");
1213 int stdin_fd[2] = {-1, -1};
1216 struct sigchld_data_s
data = { .ignored =
false };
1221 crm_info(
"Cannot execute '%s': %s " QB_XS
" stat rc=%d",
1227 if (pipe(stdout_fd) < 0) {
1229 crm_info(
"Cannot execute '%s': %s " QB_XS
" pipe(stdout) rc=%d",
1235 if (pipe(stderr_fd) < 0) {
1238 close_pipe(stdout_fd);
1240 crm_info(
"Cannot execute '%s': %s " QB_XS
" pipe(stderr) rc=%d",
1247 if (pipe(stdin_fd) < 0) {
1250 close_pipe(stdout_fd);
1251 close_pipe(stderr_fd);
1253 crm_info(
"Cannot execute '%s': %s " QB_XS
" pipe(stdin) rc=%d",
1261 close_pipe(stdin_fd);
1262 close_pipe(stdout_fd);
1263 close_pipe(stderr_fd);
1264 sigchld_cleanup(&
data);
1266 "Could not manage signals for child process");
1274 close_pipe(stdin_fd);
1275 close_pipe(stdout_fd);
1276 close_pipe(stderr_fd);
1278 crm_info(
"Cannot execute '%s': %s " QB_XS
" fork rc=%d",
1282 sigchld_cleanup(&
data);
1288 close(stdout_fd[0]);
1289 close(stderr_fd[0]);
1290 if (stdin_fd[1] >= 0) {
1293 if (STDOUT_FILENO != stdout_fd[1]) {
1294 if (dup2(stdout_fd[1], STDOUT_FILENO) != STDOUT_FILENO) {
1295 crm_warn(
"Can't redirect output from '%s': %s "
1299 close(stdout_fd[1]);
1301 if (STDERR_FILENO != stderr_fd[1]) {
1302 if (dup2(stderr_fd[1], STDERR_FILENO) != STDERR_FILENO) {
1303 crm_warn(
"Can't redirect error output from '%s': %s "
1307 close(stderr_fd[1]);
1309 if ((stdin_fd[0] >= 0) &&
1310 (STDIN_FILENO != stdin_fd[0])) {
1311 if (dup2(stdin_fd[0], STDIN_FILENO) != STDIN_FILENO) {
1312 crm_warn(
"Can't redirect input to '%s': %s "
1320 sigchld_cleanup(&
data);
1323 action_launch_child(op);
1328 close(stdout_fd[1]);
1329 close(stderr_fd[1]);
1330 if (stdin_fd[0] >= 0) {
1337 crm_info(
"Could not set '%s' output non-blocking: %s "
1345 crm_info(
"Could not set '%s' error output non-blocking: %s "
1356 crm_info(
"Could not set '%s' input non-blocking: %s "
1360 pipe_in_action_stdin_parameters(op);
1372 wait_for_sync_result(op, &
data);
1373 sigchld_cleanup(&
data);
1380 async_action_complete);
1405 struct dirent **namelist = NULL;
1406 int entries = 0, lpc = 0;
1407 char buffer[PATH_MAX];
1409 entries = scandir(root, &namelist, NULL, alphasort);
1414 for (lpc = 0; lpc < entries; lpc++) {
1417 if (
'.' == namelist[lpc]->d_name[0]) {
1418 free(namelist[lpc]);
1422 snprintf(buffer,
sizeof(buffer),
"%s/%s", root, namelist[lpc]->d_name);
1424 if (stat(buffer, &sb)) {
1428 if (S_ISDIR(sb.st_mode)) {
1430 free(namelist[lpc]);
1434 }
else if (S_ISREG(sb.st_mode)) {
1435 if (files == FALSE) {
1436 free(namelist[lpc]);
1439 }
else if (executable
1440 && (sb.st_mode & S_IXUSR) == 0
1441 && (sb.st_mode & S_IXGRP) == 0 && (sb.st_mode & S_IXOTH) == 0) {
1442 free(namelist[lpc]);
1447 list = g_list_append(list, strdup(namelist[lpc]->d_name));
1449 free(namelist[lpc]);
1460 char *dirs = strdup(root);
1463 if (pcmk__str_empty(dirs)) {
1468 for (dir = strtok(dirs,
":"); dir != NULL; dir = strtok(NULL,
":")) {
#define PCMK_ACTION_STATUS
uint32_t pcmk_get_ra_caps(const char *standard)
Get capabilities of a resource agent standard.
#define PCMK_OCF_MAJOR_VERSION
#define PCMK_RESOURCE_CLASS_OCF
#define PCMK_OCF_MINOR_VERSION
#define PCMK_RESOURCE_CLASS_LSB
int pcmk__substitute_secrets(const char *rsc_id, GHashTable *params)
guint pcmk__create_timer(guint interval_ms, GSourceFunc fn, gpointer data)
void pcmk_common_cleanup(void)
Free all memory used by libcrmcommon.
#define pcmk_is_set(g, f)
Convenience alias for pcmk_all_flags_set(), to check single flag.
int pcmk__set_nonblocking(int fd)
void pcmk__close_fds_in_child(bool)
const char * pcmk__readable_interval(guint interval_ms)
#define crm_info(fmt, args...)
#define crm_warn(fmt, args...)
#define crm_log_output(level, prefix, output)
#define crm_perror(level, fmt, args...)
Send a system error message to both the log and stderr.
#define CRM_CHECK(expr, failure_action)
#define crm_debug(fmt, args...)
#define crm_err(fmt, args...)
#define crm_trace(fmt, args...)
Wrappers for and extensions to glib mainloop.
struct mainloop_child_s mainloop_child_t
int mainloop_child_timeout(mainloop_child_t *child)
void mainloop_child_add_with_flags(pid_t pid, int timeout, const char *desc, void *userdata, enum mainloop_child_flags, void(*callback)(mainloop_child_t *p, pid_t pid, int core, int signo, int exitcode))
void * mainloop_child_userdata(mainloop_child_t *child)
void(* sighandler_t)(int)
void mainloop_del_fd(mainloop_io_t *client)
struct mainloop_io_s mainloop_io_t
@ mainloop_leave_pid_group
void mainloop_clear_child_userdata(mainloop_child_t *child)
mainloop_io_t * mainloop_add_fd(const char *name, int priority, int fd, void *userdata, struct mainloop_fd_callbacks *callbacks)
pcmk__action_result_t result
const char * pcmk_rc_str(int rc)
Get a user-friendly description of a return code.
@ PCMK_OCF_INSUFFICIENT_PRIV
Insufficient privileges.
@ PCMK_OCF_NOT_CONFIGURED
Parameter invalid (inherently)
@ PCMK_OCF_NOT_INSTALLED
Dependencies not available locally.
@ PCMK_OCF_UNKNOWN_ERROR
Unspecified error.
@ PCMK_OCF_INVALID_PARAM
Parameter invalid (in local context)
@ PCMK_EXEC_CANCELLED
Action was cancelled.
@ PCMK_EXEC_NOT_INSTALLED
Agent or dependency not available locally.
@ PCMK_EXEC_DONE
Action completed, result is known.
@ PCMK_EXEC_ERROR
Execution failed, may be retried.
@ PCMK_EXEC_TIMEOUT
Action did not complete in time.
#define pcmk__assert(expr)
const char * services__action_kind(const svc_action_t *action)
gboolean cancel_recurring_action(svc_action_t *op)
void services__set_cancelled(svc_action_t *action)
void services_untrack_op(const svc_action_t *op)
void services_add_inflight_op(svc_action_t *op)
@ PCMK_LSB_STATUS_NOT_INSTALLED
@ PCMK_LSB_STATUS_INSUFFICIENT_PRIV
@ PCMK_LSB_STATUS_UNKNOWN
gboolean services_action_async(svc_action_t *op, void(*action_callback)(svc_action_t *))
Request asynchronous execution of an action.
#define PCMK_OCF_REASON_PREFIX
void services_action_free(svc_action_t *op)
@ PCMK_LSB_NOT_CONFIGURED
void services_action_cleanup(svc_action_t *op)
void services__format_result(svc_action_t *action, int agent_status, enum pcmk_exec_status exec_status, const char *format,...) G_GNUC_PRINTF(4
void services__set_result(svc_action_t *action, int agent_status, enum pcmk_exec_status exec_status, const char *exit_reason)
volatile struct sigchld_data_s * last_sigchld_data
int services__authorization_error(const svc_action_t *op)
void services__handle_exec_error(svc_action_t *op, int error)
int services__execute_file(svc_action_t *op)
#define out_type(is_stderr)
int services__not_installed_error(const svc_action_t *op)
int services__finalize_async_op(svc_action_t *op)
gboolean recurring_action_timer(gpointer data)
int services__configuration_error(const svc_action_t *op, bool is_fatal)
#define EXIT_REASON_MAX_LEN
GList * services_os_get_directory_list(const char *root, gboolean files, gboolean executable)
GList * services_os_get_single_directory_list(const char *root, gboolean files, gboolean executable)
int services__generic_error(const svc_action_t *op)
char * crm_strdup_printf(char const *format,...) G_GNUC_PRINTF(1
void(* fork_callback)(svc_action_t *op)
mainloop_io_t * stdout_gsource
mainloop_io_t * stderr_gsource
void(* callback)(svc_action_t *op)
Object for executing external actions.
char * provider
Resource provider for resource actions that require it, otherwise NULL.
char * agent
Resource agent name for resource actions, otherwise NULL.
char * standard
Resource standard for resource actions, otherwise NULL.
int rc
Exit status of action (set by library upon completion)
char * rsc
XML ID of resource being executed for resource actions, otherwise NULL.
char * action
Name of action being executed for resource actions, otherwise NULL.
enum svc_action_flags flags
Flag group of enum svc_action_flags.
char * stderr_data
Action stderr (set by library)
int timeout
Action timeout (in milliseconds)
char * stdout_data
Action stdout (set by library)
guint interval_ms
Action interval for recurring resource actions, otherwise 0.
svc_action_private_t * opaque
This field should be treated as internal to Pacemaker.