14 #include <sys/types.h>
23 #include <sys/resource.h>
25 #ifdef HAVE_SYS_SIGNALFD_H
26 #include <sys/signalfd.h>
35 #if SUPPORT_CIBSECRETS
40 svc_read_output(
int fd,
svc_action_t * op,
bool is_stderr)
45 static const size_t buf_read_len =
sizeof(buf) - 1;
56 crm_trace(
"Reading %s stderr into offset %d", op->
id, len);
61 crm_trace(
"Reading %s stdout into offset %d", op->
id, len);
64 crm_trace(
"Reading %s %s into offset %d", op->
id, is_stderr?
"stderr":
"stdout", len);
68 rc = read(fd, buf, buf_read_len);
71 crm_trace(
"Got %d chars: %.80s", rc, buf);
72 data = realloc_safe(data, len + rc + 1);
73 len += sprintf(data + len,
"%s", buf);
75 }
else if (errno != EINTR) {
83 }
while (rc == buf_read_len || rc < 0);
95 dispatch_stdout(gpointer userdata)
103 dispatch_stderr(gpointer userdata)
111 pipe_out_done(gpointer user_data)
125 pipe_err_done(gpointer user_data)
138 .destroy = pipe_out_done,
143 .destroy = pipe_err_done,
147 set_ocf_env(
const char *key,
const char *value, gpointer user_data)
149 if (
setenv(key, value, 1) != 0) {
150 crm_perror(LOG_ERR,
"setenv failed for key:%s and value:%s", key, value);
155 set_ocf_env_with_prefix(gpointer key, gpointer value, gpointer user_data)
159 snprintf(buffer,
sizeof(buffer),
"OCF_RESKEY_%s", (
char *)key);
160 set_ocf_env(buffer, value, user_data);
164 set_alert_env(gpointer key, gpointer value, gpointer user_data)
169 rc =
setenv(key, value, 1);
176 (
char*)key, (value? (
char*)value :
""));
178 crm_trace(
"setenv %s=%s", (
char*)key, (value? (
char*)value :
""));
191 void (*env_setter)(gpointer, gpointer, gpointer) = NULL;
192 if (op->
agent == NULL) {
193 env_setter = set_alert_env;
196 env_setter = set_ocf_env_with_prefix;
199 if (env_setter != NULL && op->
params != NULL) {
200 g_hash_table_foreach(op->
params, env_setter, NULL);
203 if (env_setter == NULL || env_setter == set_alert_env) {
207 set_ocf_env(
"OCF_RA_VERSION_MAJOR",
"1", NULL);
208 set_ocf_env(
"OCF_RA_VERSION_MINOR",
"0", NULL);
213 set_ocf_env(
"OCF_RESOURCE_INSTANCE", op->
rsc, NULL);
216 if (op->
agent != NULL) {
217 set_ocf_env(
"OCF_RESOURCE_TYPE", op->
agent, NULL);
222 set_ocf_env(
"OCF_RESOURCE_PROVIDER", op->
provider, NULL);
227 pipe_in_single_parameter(gpointer key, gpointer value, gpointer user_data)
231 int ret, total = 0, len = strlen(buffer);
240 }
while ((errno == EINTR) && (total < len));
255 g_hash_table_foreach(op->
params, pipe_in_single_parameter, (gpointer) op);
264 crm_debug(
"Scheduling another invocation of %s", op->
id);
330 crm_trace(
"%s dispatching stderr", prefix);
341 crm_trace(
"%s dispatching stdout", prefix);
362 crm_info(
"%s - terminated with signal %d", prefix, signo);
367 crm_warn(
"%s - terminated with signal %d", prefix, signo);
374 crm_debug(
"%s - exited with rc=%d", prefix, exitcode);
399 services_handle_exec_error(
svc_action_t * op,
int error)
401 int rc_not_installed, rc_insufficient_priv, rc_exec_error;
430 op->
rc = rc_not_installed;
435 op->
rc = rc_insufficient_priv;
439 op->
rc = rc_exec_error;
454 signal(SIGPIPE, SIG_DFL);
456 #if defined(HAVE_SCHED_SETSCHEDULER)
457 if (sched_getscheduler(0) != SCHED_OTHER) {
458 struct sched_param sp;
460 memset(&sp, 0,
sizeof(sp));
461 sp.sched_priority = 0;
463 if (sched_setscheduler(0, SCHED_OTHER, &sp) == -1) {
464 crm_perror(LOG_ERR,
"Could not reset scheduling policy to SCHED_OTHER for %s", op->
id);
468 if (setpriority(PRIO_PROCESS, 0, 0) == -1) {
469 crm_perror(LOG_ERR,
"Could not reset process priority to 0 for %s", op->
id);
479 for (lpc = getdtablesize() - 1; lpc > STDERR_FILENO; lpc--) {
483 #if SUPPORT_CIBSECRETS
488 crm_info(
"proceeding with the stop operation for %s", op->
rsc);
491 crm_err(
"failed to get secrets for %s, "
492 "considering resource not configured", op->
rsc);
498 add_action_env_vars(op);
501 if (op->
opaque->
uid && (geteuid() == 0)) {
511 if (setgroups(0, NULL) < 0) {
512 crm_perror(LOG_ERR,
"Could not set child groups");
527 services_handle_exec_error(op, errno);
532 #ifndef HAVE_SYS_SIGNALFD_H
533 static int sigchld_pipe[2] = { -1, -1 };
538 if ((sigchld_pipe[1] >= 0) && (write(sigchld_pipe[1],
"", 1) == -1)) {
551 struct pollfd fds[3];
554 #ifdef HAVE_SYS_SIGNALFD_H
555 sfd = signalfd(-1, mask, SFD_NONBLOCK);
560 sfd = sigchld_pipe[0];
564 fds[0].events = POLLIN;
568 fds[1].events = POLLIN;
572 fds[2].events = POLLIN;
578 int poll_rc = poll(fds, 3, timeout);
581 if (fds[0].revents & POLLIN) {
585 if (fds[1].revents & POLLIN) {
589 if (fds[2].revents & POLLIN) {
590 #ifdef HAVE_SYS_SIGNALFD_H
591 struct signalfd_siginfo fdsi;
594 s = read(sfd, &fdsi,
sizeof(
struct signalfd_siginfo));
595 if (s !=
sizeof(
struct signalfd_siginfo)) {
596 crm_perror(LOG_ERR,
"Read from signal fd %d failed", sfd);
598 }
else if (fdsi.ssi_signo == SIGCHLD) {
603 while (read(sfd, &ch, 1) == 1) ;
605 wait_rc = waitpid(op->
pid, &status, WNOHANG);
610 }
else if (wait_rc < 0){
611 if (errno == ECHILD) {
624 }
else if (poll_rc == 0) {
628 }
else if (poll_rc < 0) {
629 if (errno != EINTR) {
635 timeout = op->
timeout - (time(NULL) - start) * 1000;
637 }
while ((op->
timeout < 0 || timeout > 0));
643 if (op->
timeout > 0 && timeout <= 0) {
653 if (wait_rc == 0 && waitpid(op->
pid, &status, WNOHANG) == 0) {
654 if (kill(op->
pid, SIGKILL)) {
655 crm_err(
"kill(%d, KILL) failed: %d", op->
pid, errno);
658 while (waitpid(op->
pid, &status, 0) == (pid_t) -1 && errno == EINTR) ;
661 }
else if (WIFEXITED(status)) {
663 op->
rc = WEXITSTATUS(status);
664 crm_info(
"Managed %s process %d exited with rc=%d", op->
id, op->
pid, op->
rc);
666 }
else if (WIFSIGNALED(status)) {
667 int signo = WTERMSIG(status);
670 crm_err(
"Managed %s process %d exited with signal=%d", op->
id, op->
pid, signo);
673 if (WCOREDUMP(status)) {
674 crm_err(
"Managed %s process %d dumped core", op->
id, op->
pid);
687 #ifdef HAVE_SYS_SIGNALFD_H
699 int stdin_fd[2] = {-1, -1};
702 sigset_t *pmask = NULL;
704 #ifdef HAVE_SYS_SIGNALFD_H
707 #define sigchld_cleanup() do { \
708 if (sigismember(&old_mask, SIGCHLD) == 0) { \
709 if (sigprocmask(SIG_UNBLOCK, &mask, NULL) < 0) { \
710 crm_perror(LOG_ERR, "sigprocmask() failed to unblock sigchld"); \
716 struct sigaction old_sa;
717 #define sigchld_cleanup() do { \
718 if (sigaction(SIGCHLD, &old_sa, NULL) < 0) { \
719 crm_perror(LOG_ERR, "sigaction() failed to remove sigchld handler"); \
721 close(sigchld_pipe[0]); \
722 close(sigchld_pipe[1]); \
723 sigchld_pipe[0] = sigchld_pipe[1] = -1; \
731 services_handle_exec_error(op, rc);
738 if (pipe(stdout_fd) < 0) {
743 services_handle_exec_error(op, rc);
750 if (pipe(stderr_fd) < 0) {
758 services_handle_exec_error(op, rc);
766 if (pipe(stdin_fd) < 0) {
776 services_handle_exec_error(op, rc);
785 #ifdef HAVE_SYS_SIGNALFD_H
787 sigaddset(&mask, SIGCHLD);
788 sigemptyset(&old_mask);
790 if (sigprocmask(SIG_BLOCK, &mask, &old_mask) < 0) {
791 crm_perror(LOG_ERR,
"sigprocmask() failed to block sigchld");
796 if(pipe(sigchld_pipe) == -1) {
802 crm_warn(
"Could not set pipe input non-blocking: %s " CRM_XS " rc=%d",
807 crm_warn(
"Could not set pipe output non-blocking: %s " CRM_XS " rc=%d",
811 sa.sa_handler = sigchld_handler;
813 sigemptyset(&sa.sa_mask);
814 if (sigaction(SIGCHLD, &sa, &old_sa) < 0) {
815 crm_perror(LOG_ERR,
"sigaction() failed to set sigchld handler");
831 if (stdin_fd[0] >= 0) {
837 services_handle_exec_error(op, rc);
848 if (stdin_fd[1] >= 0) {
851 if (STDOUT_FILENO != stdout_fd[1]) {
852 if (dup2(stdout_fd[1], STDOUT_FILENO) != STDOUT_FILENO) {
853 crm_err(
"dup2() failed (stdout)");
857 if (STDERR_FILENO != stderr_fd[1]) {
858 if (dup2(stderr_fd[1], STDERR_FILENO) != STDERR_FILENO) {
859 crm_err(
"dup2() failed (stderr)");
863 if ((stdin_fd[0] >= 0) &&
864 (STDIN_FILENO != stdin_fd[0])) {
865 if (dup2(stdin_fd[0], STDIN_FILENO) != STDIN_FILENO) {
866 crm_err(
"dup2() failed (stdin)");
875 action_launch_child(op);
882 if (stdin_fd[0] >= 0) {
889 crm_warn(
"Could not set child output non-blocking: %s "
897 crm_warn(
"Could not set child error output non-blocking: %s "
908 crm_warn(
"Could not set child input non-blocking: %s "
912 pipe_in_action_stdin_parameters(op);
924 action_synced_wait(op, pmask);
955 struct dirent **namelist;
956 int entries = 0, lpc = 0;
957 char buffer[PATH_MAX];
959 entries = scandir(root, &namelist, NULL,
alphasort);
964 for (lpc = 0; lpc < entries; lpc++) {
967 if (
'.' == namelist[lpc]->d_name[0]) {
972 snprintf(buffer,
sizeof(buffer),
"%s/%s", root, namelist[lpc]->d_name);
974 if (stat(buffer, &sb)) {
978 if (S_ISDIR(sb.st_mode)) {
984 }
else if (S_ISREG(sb.st_mode)) {
985 if (files == FALSE) {
989 }
else if (executable
990 && (sb.st_mode & S_IXUSR) == 0
991 && (sb.st_mode & S_IXGRP) == 0 && (sb.st_mode & S_IXOTH) == 0) {
997 list = g_list_append(list, strdup(namelist[lpc]->d_name));
1015 GList *gIter = NULL;
1016 GList *result = NULL;
1017 GList *providers = NULL;
1022 snprintf(buffer,
sizeof(buffer),
"%s/resource.d/%s",
OCF_ROOT_DIR, provider);
1027 for (gIter = providers; gIter != NULL; gIter = gIter->next) {
1028 GList *tmp1 = result;
1032 result = g_list_concat(tmp1, tmp2);
1035 g_list_free_full(providers, free);
1043 gboolean rc = FALSE;
1046 if (provider == NULL || agent == NULL) {
1051 if (stat(buf, &st) == 0) {
1063 GList *plugin_list = NULL;
1064 GList *result = NULL;
1065 GList *gIter = NULL;
1070 for (gIter = plugin_list; gIter != NULL; gIter = gIter->next) {
1071 const char *plugin = gIter->data;
1075 if (stat(metadata, &st) == 0) {
1076 result = g_list_append(result, strdup(plugin));
1081 g_list_free_full(plugin_list, free);
1089 gboolean rc = FALSE;
1097 if (stat(buf, &st) == 0) {
const char * pcmk_strerror(int rc)
void services_action_free(svc_action_t *op)
gboolean services__ocf_agent_exists(const char *provider, const char *agent)
mainloop_io_t * mainloop_add_fd(const char *name, int priority, int fd, void *userdata, struct mainloop_fd_callbacks *callbacks)
#define sigchld_cleanup()
#define crm_log_output(level, prefix, output)
mainloop_io_t * stderr_gsource
GList * resources_os_list_ocf_agents(const char *provider)
GList * resources_os_list_ocf_providers(void)
int alphasort(const void *dirent1, const void *dirent2)
gboolean recurring_action_timer(gpointer data)
void(* fork_callback)(svc_action_t *op)
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(* dispatch)(gpointer userdata)
gboolean services_os_action_execute(svc_action_t *op)
G_GNUC_INTERNAL GList * resources_os_list_nagios_agents(void)
Wrappers for and extensions to glib mainloop.
void services_action_cleanup(svc_action_t *op)
enum svc_action_flags flags
#define crm_warn(fmt, args...)
#define PCMK_RESOURCE_CLASS_OCF
gboolean cancel_recurring_action(svc_action_t *op)
svc_action_private_t * opaque
#define crm_debug(fmt, args...)
void * mainloop_child_userdata(mainloop_child_t *child)
gboolean operation_finalize(svc_action_t *op)
G_GNUC_INTERNAL gboolean services__nagios_agent_exists(const char *agent)
#define crm_trace(fmt, args...)
int setenv(const char *name, const char *value, int why)
int crm_set_nonblocking(int fd)
void(* callback)(svc_action_t *op)
#define PCMK_OCF_REASON_PREFIX
#define PCMK_RESOURCE_CLASS_STONITH
void services_add_inflight_op(svc_action_t *op)
void services_untrack_op(svc_action_t *op)
int replace_secret_params(const char *rsc_id, GHashTable *params)
#define PCMK_RESOURCE_CLASS_NAGIOS
#define PCMK_RESOURCE_CLASS_LSB
#define crm_perror(level, fmt, args...)
Log a system error message.
#define NAGIOS_PLUGIN_DIR
#define crm_err(fmt, args...)
void mainloop_clear_child_userdata(mainloop_child_t *child)
GList * get_directory_list(const char *root, gboolean files, gboolean executable)
Get a list of files or directories in a given path.
mainloop_io_t * stdout_gsource
void mainloop_del_fd(mainloop_io_t *client)
#define safe_str_eq(a, b)
gboolean services_action_async(svc_action_t *op, void(*action_callback)(svc_action_t *))
char * crm_strdup_printf(char const *format,...) __attribute__((__format__(__printf__
#define crm_info(fmt, args...)
#define NAGIOS_METADATA_DIR
int mainloop_child_timeout(mainloop_child_t *child)