16 #include <sys/types.h> 25 #include <sys/resource.h> 34 static void close_pipe(
int fildes[]);
44 #ifdef HAVE_SYS_SIGNALFD_H 48 #include <sys/signalfd.h> 51 struct sigchld_data_s {
59 sigchld_setup(
struct sigchld_data_s *
data)
61 sigemptyset(&(
data->mask));
62 sigaddset(&(
data->mask), SIGCHLD);
64 sigemptyset(&(
data->old_mask));
67 if (sigprocmask(SIG_BLOCK, &(
data->mask), &(
data->old_mask)) < 0) {
68 crm_info(
"Wait for child process completion failed: %s " 73 data->ignored =
false;
80 sigchld_open(
struct sigchld_data_s *
data)
86 fd = signalfd(-1, &(
data->mask), SFD_NONBLOCK);
88 crm_info(
"Wait for child process completion failed: %s " 105 sigchld_received(
int fd,
int pid,
struct sigchld_data_s *
data)
107 struct signalfd_siginfo fdsi;
113 s = read(fd, &fdsi,
sizeof(
struct signalfd_siginfo));
114 if (s !=
sizeof(
struct signalfd_siginfo)) {
115 crm_info(
"Wait for child process completion failed: %s " 118 }
else if (fdsi.ssi_signo == SIGCHLD) {
119 if (fdsi.ssi_pid ==
pid) {
128 data->ignored =
true;
137 sigchld_cleanup(
struct sigchld_data_s *
data)
140 if ((sigismember(&(
data->old_mask), SIGCHLD) == 0)
141 && (sigprocmask(SIG_UNBLOCK, &(
data->mask), NULL) < 0)) {
142 crm_warn(
"Could not clean up after child process completion: %s",
147 if (
data->ignored && kill(getpid(), SIGCHLD) != 0) {
148 crm_warn(
"Could not resend ignored SIGCHLD to ourselves: %s",
153 #else // HAVE_SYS_SIGNALFD_H not defined 157 struct sigchld_data_s {
160 struct sigaction old_sa;
168 sigchld_handler(
void)
174 crm_info(
"Wait for child process completion failed: %s " 180 sigchld_setup(
struct sigchld_data_s *
data)
184 data->pipe_fd[0] =
data->pipe_fd[1] = -1;
186 if (pipe(
data->pipe_fd) == -1) {
187 crm_info(
"Wait for child process completion failed: %s " 194 crm_info(
"Could not set pipe input non-blocking: %s " CRM_XS " rc=%d",
199 crm_info(
"Could not set pipe output non-blocking: %s " CRM_XS " rc=%d",
205 data->sa.sa_flags = 0;
206 sigemptyset(&(
data->sa.sa_mask));
207 if (sigaction(SIGCHLD, &(
data->sa), &(
data->old_sa)) < 0) {
208 crm_info(
"Wait for child process completion failed: %s " 212 data->ignored =
false;
220 sigchld_open(
struct sigchld_data_s *
data)
223 return data->pipe_fd[0];
227 sigchld_close(
int fd)
234 sigchld_received(
int fd,
int pid,
struct sigchld_data_s *
data)
243 while (read(fd, &ch, 1) == 1) ;
248 sigchld_cleanup(
struct sigchld_data_s *
data)
251 if (sigaction(SIGCHLD, &(
data->old_sa), NULL) < 0) {
252 crm_warn(
"Could not clean up after child process completion: %s",
256 close_pipe(
data->pipe_fd);
259 if (
data->ignored && kill(getpid(), SIGCHLD) != 0) {
260 crm_warn(
"Could not resend ignored SIGCHLD to ourselves: %s",
274 close_pipe(
int fildes[])
276 if (fildes[0] >= 0) {
280 if (fildes[1] >= 0) {
286 #define out_type(is_stderr) ((is_stderr)? "stderr" : "stdout") 289 #define MAX_OUTPUT (10 * 1024 * 1024) 292 svc_read_output(
int fd,
svc_action_t * op,
bool is_stderr)
297 size_t discarded = 0;
299 static const size_t buf_read_len =
sizeof(buf) - 1;
309 crm_trace(
"Reading %s stderr into offset %lld",
310 op->
id, (
long long) len);
312 }
else if (is_stderr == FALSE && op->
stdout_data) {
315 crm_trace(
"Reading %s stdout into offset %lld",
316 op->
id, (
long long) len);
324 rc = read(fd, buf, buf_read_len);
328 crm_trace(
"Received %lld bytes of %s %s: %.80s",
329 (
long long) rc, op->
id,
out_type(is_stderr), buf);
330 data = pcmk__realloc(
data, len + rc + 1);
331 strcpy(
data + len, buf);
337 }
else if (errno != EINTR) {
341 }
while ((rc == buf_read_len) || (rc < 0));
344 crm_warn(
"Truncated %s %s to %lld bytes (discarded %lld)",
346 (
long long) discarded);
359 dispatch_stdout(gpointer userdata)
367 dispatch_stderr(gpointer userdata)
375 pipe_out_done(gpointer user_data)
389 pipe_err_done(gpointer user_data)
402 .destroy = pipe_out_done,
407 .destroy = pipe_err_done,
411 set_ocf_env(
const char *key,
const char *value, gpointer user_data)
413 if (setenv(key, value, 1) != 0) {
414 crm_perror(LOG_ERR,
"setenv failed for key:%s and value:%s", key, value);
419 set_ocf_env_with_prefix(gpointer key, gpointer value, gpointer user_data)
423 snprintf(buffer,
sizeof(buffer), strcmp(key,
"OCF_CHECK_LEVEL") != 0 ?
"OCF_RESKEY_%s" :
"%s", (
char *)key);
424 set_ocf_env(buffer, value, user_data);
428 set_alert_env(gpointer key, gpointer value, gpointer user_data)
433 rc = setenv(key, value, 1);
440 (
char*)key, (value? (
char*)value :
""));
442 crm_trace(
"setenv %s=%s", (
char*)key, (value? (
char*)value :
""));
455 void (*env_setter)(gpointer, gpointer, gpointer) = NULL;
456 if (op->
agent == NULL) {
457 env_setter = set_alert_env;
460 env_setter = set_ocf_env_with_prefix;
463 if (env_setter != NULL && op->
params != NULL) {
464 g_hash_table_foreach(op->
params, env_setter, NULL);
467 if (env_setter == NULL || env_setter == set_alert_env) {
477 set_ocf_env(
"OCF_RESOURCE_INSTANCE", op->
rsc, NULL);
480 if (op->
agent != NULL) {
481 set_ocf_env(
"OCF_RESOURCE_TYPE", op->
agent, NULL);
486 set_ocf_env(
"OCF_RESOURCE_PROVIDER", op->
provider, NULL);
491 pipe_in_single_parameter(gpointer key, gpointer value, gpointer user_data)
495 size_t len = strlen(buffer);
505 }
while ((errno == EINTR) && (total < len));
519 g_hash_table_foreach(op->
params, pipe_in_single_parameter, (gpointer) op);
528 crm_debug(
"Scheduling another invocation of %s", op->
id);
618 op->
id, op->
pid, (is_stderr?
"stderr" :
"stdout"));
619 svc_read_output(fd, op, is_stderr);
640 strcpy(prefix + strlen(prefix) - strlen(
"error output"),
"output");
646 #define EXIT_REASON_MAX_LEN 128 651 const char *reason_start = NULL;
652 const char *reason_end = NULL;
669 if ((reason_start == NULL) || (reason_start[0] ==
'\n')
670 || (reason_start[0] ==
'\0')) {
675 reason_end = strchr(reason_start,
'\n');
676 if (reason_end == NULL) {
677 reason_end = reason_start + strlen(reason_start);
715 finish_op_output(op,
true);
716 finish_op_output(op,
false);
721 crm_debug(
"%s[%d] exited with status %d", op->
id, op->
pid, exitcode);
724 parse_exit_reason_from_stderr(op);
729 crm_info(
"%s %s[%d] timed out after %s",
733 "%s did not complete within %s",
740 crm_info(
"%s[%d] terminated with signal %d (%s)",
741 op->
id, op->
pid, signo, strsignal(signo));
745 crm_info(
"%s[%d] terminated with signal %d (%s)",
746 op->
id, op->
pid, signo, strsignal(signo));
748 "%s interrupted by %s signal",
771 if ((op == NULL) || (op->
standard == NULL)) {
808 if ((op == NULL) || (op->
standard == NULL)) {
845 if ((op == NULL) || (op->
standard == NULL)) {
883 if ((op == NULL) || (op->
standard == NULL)) {
957 exit_child(
const svc_action_t *op,
int exit_status,
const char *exit_reason)
959 if ((op != NULL) && (exit_reason != NULL)
977 signal(SIGPIPE, SIG_DFL);
979 #if defined(HAVE_SCHED_SETSCHEDULER) 980 if (sched_getscheduler(0) != SCHED_OTHER) {
981 struct sched_param sp;
983 memset(&sp, 0,
sizeof(sp));
984 sp.sched_priority = 0;
986 if (sched_setscheduler(0, SCHED_OTHER, &sp) == -1) {
987 crm_info(
"Could not reset scheduling policy for %s", op->
id);
991 if (setpriority(PRIO_PROCESS, 0, 0) == -1) {
992 crm_info(
"Could not reset process priority for %s", op->
id);
1010 #if SUPPORT_CIBSECRETS 1014 crm_info(
"Proceeding with stop operation for %s " 1015 "despite being unable to load CIB secrets (%s)",
1018 crm_err(
"Considering %s unconfigured " 1019 "because unable to load CIB secrets: %s",
1022 "Unable to load CIB secrets");
1027 add_action_env_vars(op);
1030 if (op->
opaque->
uid && (geteuid() == 0)) {
1034 crm_err(
"Considering %s unauthorized because could not set " 1035 "child group to %d: %s",
1038 "Could not set group for child process");
1043 if (setgroups(0, NULL) < 0) {
1044 crm_err(
"Considering %s unauthorized because could not " 1045 "clear supplementary groups: %s", op->
id, strerror(errno));
1047 "Could not clear supplementary groups for child process");
1052 crm_err(
"Considering %s unauthorized because could not set user " 1053 "to %d: %s", op->
id, op->
opaque->
uid, strerror(errno));
1055 "Could not set user for child process");
1065 crm_err(
"Unable to execute %s: %s", op->
id, strerror(rc));
1066 exit_child(op, op->
rc,
"Child process was unable to execute file");
1081 time_t start = time(NULL);
1082 struct pollfd fds[3];
1084 const char *wait_reason = NULL;
1087 fds[0].events = POLLIN;
1091 fds[1].events = POLLIN;
1094 fds[2].fd = sigchld_open(
data);
1095 fds[2].events = POLLIN;
1100 int poll_rc = poll(fds, 3,
timeout);
1105 if (fds[0].revents & POLLIN) {
1109 if (fds[1].revents & POLLIN) {
1113 if ((fds[2].revents & POLLIN)
1114 && sigchld_received(fds[2].fd, op->
pid,
data)) {
1115 wait_rc = waitpid(op->
pid, &status, WNOHANG);
1117 if ((wait_rc > 0) || ((wait_rc < 0) && (errno == ECHILD))) {
1121 }
else if (wait_rc < 0) {
1123 crm_info(
"Wait for completion of %s[%d] failed: %s " 1124 CRM_XS " source=waitpid",
1125 op->
id, op->
pid, wait_reason);
1128 #ifndef HAVE_SYS_SIGNALFD_H 1136 data->ignored =
true;
1141 }
else if (poll_rc == 0) {
1146 }
else if ((poll_rc < 0) && (errno != EINTR)) {
1148 crm_info(
"Wait for completion of %s[%d] failed: %s " 1149 CRM_XS " source=poll", op->
id, op->
pid, wait_reason);
1158 finish_op_output(op,
true);
1159 finish_op_output(op,
false);
1161 sigchld_close(fds[2].fd);
1168 "%s did not exit within specified timeout",
1170 crm_info(
"%s[%d] timed out after %dms",
1180 if ((wait_rc == 0) && (waitpid(op->
pid, &status, WNOHANG) == 0)) {
1181 if (kill(op->
pid, SIGKILL)) {
1182 crm_warn(
"Could not kill rogue child %s[%d]: %s",
1186 while ((waitpid(op->
pid, &status, 0) == (pid_t) -1)
1187 && (errno == EINTR)) {
1192 }
else if (WIFEXITED(status)) {
1194 parse_exit_reason_from_stderr(op);
1197 }
else if (WIFSIGNALED(status)) {
1198 int signo = WTERMSIG(status);
1203 crm_info(
"%s[%d] terminated with signal %d (%s)",
1204 op->
id, op->
pid, signo, strsignal(signo));
1207 if (WCOREDUMP(status)) {
1215 "Unable to wait for child to complete");
1240 int stdin_fd[2] = {-1, -1};
1243 struct sigchld_data_s
data = { .ignored =
false };
1254 if (pipe(stdout_fd) < 0) {
1256 crm_info(
"Cannot execute '%s': %s " CRM_XS " pipe(stdout) rc=%d",
1262 if (pipe(stderr_fd) < 0) {
1265 close_pipe(stdout_fd);
1267 crm_info(
"Cannot execute '%s': %s " CRM_XS " pipe(stderr) rc=%d",
1274 if (pipe(stdin_fd) < 0) {
1277 close_pipe(stdout_fd);
1278 close_pipe(stderr_fd);
1288 close_pipe(stdin_fd);
1289 close_pipe(stdout_fd);
1290 close_pipe(stderr_fd);
1291 sigchld_cleanup(&
data);
1293 "Could not manage signals for child process");
1301 close_pipe(stdin_fd);
1302 close_pipe(stdout_fd);
1303 close_pipe(stderr_fd);
1309 sigchld_cleanup(&
data);
1315 close(stdout_fd[0]);
1316 close(stderr_fd[0]);
1317 if (stdin_fd[1] >= 0) {
1320 if (STDOUT_FILENO != stdout_fd[1]) {
1321 if (dup2(stdout_fd[1], STDOUT_FILENO) != STDOUT_FILENO) {
1322 crm_warn(
"Can't redirect output from '%s': %s " 1326 close(stdout_fd[1]);
1328 if (STDERR_FILENO != stderr_fd[1]) {
1329 if (dup2(stderr_fd[1], STDERR_FILENO) != STDERR_FILENO) {
1330 crm_warn(
"Can't redirect error output from '%s': %s " 1334 close(stderr_fd[1]);
1336 if ((stdin_fd[0] >= 0) &&
1337 (STDIN_FILENO != stdin_fd[0])) {
1338 if (dup2(stdin_fd[0], STDIN_FILENO) != STDIN_FILENO) {
1339 crm_warn(
"Can't redirect input to '%s': %s " 1347 sigchld_cleanup(&
data);
1350 action_launch_child(op);
1355 close(stdout_fd[1]);
1356 close(stderr_fd[1]);
1357 if (stdin_fd[0] >= 0) {
1364 crm_info(
"Could not set '%s' output non-blocking: %s " 1372 crm_info(
"Could not set '%s' error output non-blocking: %s " 1383 crm_info(
"Could not set '%s' input non-blocking: %s " 1387 pipe_in_action_stdin_parameters(op);
1399 wait_for_sync_result(op, &
data);
1400 sigchld_cleanup(&
data);
1407 async_action_complete);
1432 struct dirent **namelist;
1433 int entries = 0, lpc = 0;
1434 char buffer[PATH_MAX];
1436 entries = scandir(root, &namelist, NULL, alphasort);
1441 for (lpc = 0; lpc < entries; lpc++) {
1444 if (
'.' == namelist[lpc]->d_name[0]) {
1445 free(namelist[lpc]);
1449 snprintf(buffer,
sizeof(buffer),
"%s/%s", root, namelist[lpc]->d_name);
1451 if (stat(buffer, &sb)) {
1455 if (S_ISDIR(sb.st_mode)) {
1457 free(namelist[lpc]);
1461 }
else if (S_ISREG(sb.st_mode)) {
1462 if (files == FALSE) {
1463 free(namelist[lpc]);
1466 }
else if (executable
1467 && (sb.st_mode & S_IXUSR) == 0
1468 && (sb.st_mode & S_IXGRP) == 0 && (sb.st_mode & S_IXOTH) == 0) {
1469 free(namelist[lpc]);
1474 list = g_list_append(list, strdup(namelist[lpc]->d_name));
1476 free(namelist[lpc]);
1487 char *dirs = strdup(root);
1490 if (pcmk__str_empty(dirs)) {
1495 for (dir = strtok(dirs,
":"); dir != NULL; dir = strtok(NULL,
":")) {
int rc
Exit status of action (set by library upon completion)
#define CRM_CHECK(expr, failure_action)
void(* callback)(svc_action_t *op)
int pcmk__set_nonblocking(int fd)
void services_action_free(svc_action_t *op)
guint interval_ms
Action interval for recurring resource actions, otherwise 0.
mainloop_io_t * mainloop_add_fd(const char *name, int priority, int fd, void *userdata, struct mainloop_fd_callbacks *callbacks)
char * standard
Resource standard for resource actions, otherwise NULL.
#define crm_log_output(level, prefix, output)
mainloop_io_t * stderr_gsource
int pcmk__substitute_secrets(const char *rsc_id, GHashTable *params)
gboolean recurring_action_timer(gpointer data)
struct mainloop_io_s mainloop_io_t
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))
struct mainloop_child_s mainloop_child_t
GList * services_os_get_directory_list(const char *root, gboolean files, gboolean executable)
int services__generic_error(const svc_action_t *op)
#define PCMK_RESOURCE_CLASS_OCF
char * rsc
XML ID of resource being executed for resource actions, otherwise NULL.
const char * services__action_kind(const svc_action_t *action)
int timeout
Action timeout (in milliseconds)
Action did not complete in time.
const char * pcmk_rc_str(int rc)
Get a user-friendly description of a return code.
Wrappers for and extensions to glib mainloop.
#define PCMK_ACTION_STATUS
void services_action_cleanup(svc_action_t *op)
int(* dispatch)(gpointer userdata)
Dispatch function for mainloop file descriptor with data ready.
volatile struct sigchld_data_s * last_sigchld_data
enum svc_action_flags flags
Flag group of enum svc_action_flags.
#define crm_warn(fmt, args...)
void services__set_cancelled(svc_action_t *action)
gboolean cancel_recurring_action(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
svc_action_private_t * opaque
This field should be treated as internal to Pacemaker.
#define crm_debug(fmt, args...)
void * mainloop_child_userdata(mainloop_child_t *child)
void services_untrack_op(const svc_action_t *op)
Parameter invalid (in local context)
char * stdout_data
Action stdout (set by library)
Parameter invalid (inherently)
#define crm_trace(fmt, args...)
#define PCMK_OCF_MAJOR_VERSION
int services__finalize_async_op(svc_action_t *op)
int services__authorization_error(const svc_action_t *op)
char * crm_strdup_printf(char const *format,...) G_GNUC_PRINTF(1
GList * services_os_get_single_directory_list(const char *root, gboolean files, gboolean executable)
Object for executing external actions.
#define pcmk_is_set(g, f)
Convenience alias for pcmk_all_flags_set(), to check single flag.
void services__set_result(svc_action_t *action, int agent_status, enum pcmk_exec_status exec_status, const char *exit_reason)
char * agent
Resource agent name for resource actions, otherwise NULL.
Action completed, result is known.
#define PCMK_OCF_MINOR_VERSION
Dependencies not available locally.
#define PCMK_OCF_REASON_PREFIX
#define EXIT_REASON_MAX_LEN
#define pcmk__assert(expr)
void services_add_inflight_op(svc_action_t *op)
int services__not_installed_error(const svc_action_t *op)
void(* sighandler_t)(int)
#define out_type(is_stderr)
char * action
Name of action being executed for resource actions, otherwise NULL.
pcmk__action_result_t result
#define crm_perror(level, fmt, args...)
Send a system error message to both the log and stderr.
#define crm_err(fmt, args...)
void mainloop_clear_child_userdata(mainloop_child_t *child)
#define PCMK_RESOURCE_CLASS_LSB
void services__handle_exec_error(svc_action_t *op, int error)
void pcmk__close_fds_in_child(bool)
mainloop_io_t * stdout_gsource
Agent or dependency not available locally.
void mainloop_del_fd(mainloop_io_t *client)
const char * pcmk__readable_interval(guint interval_ms)
uint32_t pcmk_get_ra_caps(const char *standard)
Get capabilities of a resource agent standard.
#define PCMK_RESOURCE_CLASS_NAGIOS
int services__configuration_error(const svc_action_t *op, bool is_fatal)
gboolean services_action_async(svc_action_t *op, void(*action_callback)(svc_action_t *))
Request asynchronous execution of an action.
char * provider
Resource provider for resource actions that require it, otherwise NULL.
void(* fork_callback)(svc_action_t *op)
Execution failed, may be retried.
#define crm_info(fmt, args...)
int services__execute_file(svc_action_t *op)
int mainloop_child_timeout(mainloop_child_t *child)
char * stderr_data
Action stderr (set by library)