14 #include <sys/types.h> 22 #include <sys/resource.h> 24 #ifdef HAVE_SYS_SIGNALFD_H 25 #include <sys/signalfd.h> 34 #if SUPPORT_CIBSECRETS 39 svc_read_output(
int fd,
svc_action_t * op,
bool is_stderr)
44 static const size_t buf_read_len =
sizeof(buf) - 1;
55 crm_trace(
"Reading %s stderr into offset %d", op->
id, len);
60 crm_trace(
"Reading %s stdout into offset %d", op->
id, len);
63 crm_trace(
"Reading %s %s into offset %d", op->
id, is_stderr?
"stderr":
"stdout", len);
67 rc = read(fd, buf, buf_read_len);
69 crm_trace(
"Got %d chars: %.80s", rc, buf);
71 data = realloc_safe(data, len + rc + 1);
72 len += sprintf(data + len,
"%s", buf);
74 }
else if (errno != EINTR) {
82 }
while (rc == buf_read_len || rc < 0);
94 dispatch_stdout(gpointer userdata)
102 dispatch_stderr(gpointer userdata)
110 pipe_out_done(gpointer user_data)
124 pipe_err_done(gpointer user_data)
137 .destroy = pipe_out_done,
142 .destroy = pipe_err_done,
146 set_ocf_env(
const char *key,
const char *value, gpointer user_data)
148 if (
setenv(key, value, 1) != 0) {
149 crm_perror(LOG_ERR,
"setenv failed for key:%s and value:%s", key, value);
154 set_ocf_env_with_prefix(gpointer key, gpointer value, gpointer user_data)
158 snprintf(buffer,
sizeof(buffer),
"OCF_RESKEY_%s", (
char *)key);
159 set_ocf_env(buffer, value, user_data);
163 set_alert_env(gpointer key, gpointer value, gpointer user_data)
168 rc =
setenv(key, value, 1);
175 (
char*)key, (value? (
char*)value :
""));
177 crm_trace(
"setenv %s=%s", (
char*)key, (value? (
char*)value :
""));
190 void (*env_setter)(gpointer, gpointer, gpointer) = NULL;
191 if (op->
agent == NULL) {
192 env_setter = set_alert_env;
195 env_setter = set_ocf_env_with_prefix;
198 if (env_setter != NULL && op->
params != NULL) {
199 g_hash_table_foreach(op->
params, env_setter, NULL);
202 if (env_setter == NULL || env_setter == set_alert_env) {
206 set_ocf_env(
"OCF_RA_VERSION_MAJOR",
"1", NULL);
207 set_ocf_env(
"OCF_RA_VERSION_MINOR",
"0", NULL);
212 set_ocf_env(
"OCF_RESOURCE_INSTANCE", op->
rsc, NULL);
215 if (op->
agent != NULL) {
216 set_ocf_env(
"OCF_RESOURCE_TYPE", op->
agent, NULL);
221 set_ocf_env(
"OCF_RESOURCE_PROVIDER", op->
provider, NULL);
230 crm_debug(
"Scheduling another invocation of %s", op->
id);
296 crm_trace(
"%s dispatching stderr", prefix);
307 crm_trace(
"%s dispatching stdout", prefix);
322 "%s - terminated with signal %d", prefix, signo);
329 crm_debug(
"%s - exited with rc=%d", prefix, exitcode);
354 services_handle_exec_error(
svc_action_t * op,
int error)
356 int rc_not_installed, rc_insufficient_priv, rc_exec_error;
385 op->
rc = rc_not_installed;
390 op->
rc = rc_insufficient_priv;
394 op->
rc = rc_exec_error;
409 signal(SIGPIPE, SIG_DFL);
411 #if defined(HAVE_SCHED_SETSCHEDULER) 412 if (sched_getscheduler(0) != SCHED_OTHER) {
413 struct sched_param sp;
415 memset(&sp, 0,
sizeof(sp));
416 sp.sched_priority = 0;
418 if (sched_setscheduler(0, SCHED_OTHER, &sp) == -1) {
419 crm_perror(LOG_ERR,
"Could not reset scheduling policy to SCHED_OTHER for %s", op->
id);
423 if (setpriority(PRIO_PROCESS, 0, 0) == -1) {
424 crm_perror(LOG_ERR,
"Could not reset process priority to 0 for %s", op->
id);
434 for (lpc = getdtablesize() - 1; lpc > STDERR_FILENO; lpc--) {
438 #if SUPPORT_CIBSECRETS 443 crm_info(
"proceeding with the stop operation for %s", op->
rsc);
446 crm_err(
"failed to get secrets for %s, " 447 "considering resource not configured", op->
rsc);
453 add_action_env_vars(op);
456 if (op->
opaque->
uid && (geteuid() == 0)) {
472 services_handle_exec_error(op, errno);
477 #ifndef HAVE_SYS_SIGNALFD_H 478 static int sigchld_pipe[2] = { -1, -1 };
483 if ((sigchld_pipe[1] >= 0) && (write(sigchld_pipe[1],
"", 1) == -1)) {
496 struct pollfd fds[3];
499 #ifdef HAVE_SYS_SIGNALFD_H 500 sfd = signalfd(-1, mask, SFD_NONBLOCK);
505 sfd = sigchld_pipe[0];
509 fds[0].events = POLLIN;
513 fds[1].events = POLLIN;
517 fds[2].events = POLLIN;
523 int poll_rc = poll(fds, 3, timeout);
526 if (fds[0].revents & POLLIN) {
530 if (fds[1].revents & POLLIN) {
534 if (fds[2].revents & POLLIN) {
535 #ifdef HAVE_SYS_SIGNALFD_H 536 struct signalfd_siginfo fdsi;
539 s = read(sfd, &fdsi,
sizeof(
struct signalfd_siginfo));
540 if (s !=
sizeof(
struct signalfd_siginfo)) {
541 crm_perror(LOG_ERR,
"Read from signal fd %d failed", sfd);
543 }
else if (fdsi.ssi_signo == SIGCHLD) {
548 while (read(sfd, &ch, 1) == 1) ;
550 wait_rc = waitpid(op->
pid, &status, WNOHANG);
555 }
else if (wait_rc < 0){
556 if (errno == ECHILD) {
569 }
else if (poll_rc == 0) {
573 }
else if (poll_rc < 0) {
574 if (errno != EINTR) {
580 timeout = op->
timeout - (time(NULL) - start) * 1000;
582 }
while ((op->
timeout < 0 || timeout > 0));
588 if (op->
timeout > 0 && timeout <= 0) {
598 if (wait_rc == 0 && waitpid(op->
pid, &status, WNOHANG) == 0) {
599 if (kill(op->
pid, SIGKILL)) {
600 crm_err(
"kill(%d, KILL) failed: %d", op->
pid, errno);
603 while (waitpid(op->
pid, &status, 0) == (pid_t) -1 && errno == EINTR) ;
606 }
else if (WIFEXITED(status)) {
608 op->
rc = WEXITSTATUS(status);
609 crm_info(
"Managed %s process %d exited with rc=%d", op->
id, op->
pid, op->
rc);
611 }
else if (WIFSIGNALED(status)) {
612 int signo = WTERMSIG(status);
615 crm_err(
"Managed %s process %d exited with signal=%d", op->
id, op->
pid, signo);
618 if (WCOREDUMP(status)) {
619 crm_err(
"Managed %s process %d dumped core", op->
id, op->
pid);
629 #ifdef HAVE_SYS_SIGNALFD_H 645 #ifdef HAVE_SYS_SIGNALFD_H 648 #define sigchld_cleanup() do { \ 649 if (sigismember(&old_mask, SIGCHLD) == 0) { \ 650 if (sigprocmask(SIG_UNBLOCK, &mask, NULL) < 0) { \ 651 crm_perror(LOG_ERR, "sigprocmask() failed to unblock sigchld"); \ 657 struct sigaction old_sa;
658 #define sigchld_cleanup() do { \ 659 if (sigaction(SIGCHLD, &old_sa, NULL) < 0) { \ 660 crm_perror(LOG_ERR, "sigaction() failed to remove sigchld handler"); \ 662 close(sigchld_pipe[0]); \ 663 close(sigchld_pipe[1]); \ 664 sigchld_pipe[0] = sigchld_pipe[1] = -1; \ 672 services_handle_exec_error(op, rc);
679 if (pipe(stdout_fd) < 0) {
684 services_handle_exec_error(op, rc);
691 if (pipe(stderr_fd) < 0) {
699 services_handle_exec_error(op, rc);
707 #ifdef HAVE_SYS_SIGNALFD_H 709 sigaddset(&mask, SIGCHLD);
710 sigemptyset(&old_mask);
712 if (sigprocmask(SIG_BLOCK, &mask, &old_mask) < 0) {
713 crm_perror(LOG_ERR,
"sigprocmask() failed to block sigchld");
718 if(pipe(sigchld_pipe) == -1) {
724 crm_warn(
"Could not set pipe input non-blocking: %s " CRM_XS " rc=%d",
729 crm_warn(
"Could not set pipe output non-blocking: %s " CRM_XS " rc=%d",
733 sa.sa_handler = sigchld_handler;
735 sigemptyset(&sa.sa_mask);
736 if (sigaction(SIGCHLD, &sa, &old_sa) < 0) {
737 crm_perror(LOG_ERR,
"sigaction() failed to set sigchld handler");
755 services_handle_exec_error(op, rc);
766 if (STDOUT_FILENO != stdout_fd[1]) {
767 if (dup2(stdout_fd[1], STDOUT_FILENO) != STDOUT_FILENO) {
768 crm_err(
"dup2() failed (stdout)");
772 if (STDERR_FILENO != stderr_fd[1]) {
773 if (dup2(stderr_fd[1], STDERR_FILENO) != STDERR_FILENO) {
774 crm_err(
"dup2() failed (stderr)");
783 action_launch_child(op);
794 crm_warn(
"Could not set child output non-blocking: %s " 802 crm_warn(
"Could not set child error output non-blocking: %s " 808 action_synced_wait(op, pmask);
839 struct dirent **namelist;
840 int entries = 0, lpc = 0;
841 char buffer[PATH_MAX];
843 entries = scandir(root, &namelist, NULL,
alphasort);
848 for (lpc = 0; lpc < entries; lpc++) {
851 if (
'.' == namelist[lpc]->d_name[0]) {
856 snprintf(buffer,
sizeof(buffer),
"%s/%s", root, namelist[lpc]->d_name);
858 if (stat(buffer, &sb)) {
862 if (S_ISDIR(sb.st_mode)) {
868 }
else if (S_ISREG(sb.st_mode)) {
869 if (files == FALSE) {
873 }
else if (executable
874 && (sb.st_mode & S_IXUSR) == 0
875 && (sb.st_mode & S_IXGRP) == 0 && (sb.st_mode & S_IXOTH) == 0) {
881 list = g_list_append(list, strdup(namelist[lpc]->d_name));
906 GList *result = NULL;
907 GList *providers = NULL;
912 snprintf(buffer,
sizeof(buffer),
"%s/resource.d/%s",
OCF_ROOT_DIR, provider);
917 for (gIter = providers; gIter != NULL; gIter = gIter->next) {
918 GList *tmp1 = result;
922 result = g_list_concat(tmp1, tmp2);
925 g_list_free_full(providers, free);
933 GList *plugin_list = NULL;
934 GList *result = NULL;
940 for (gIter = plugin_list; gIter != NULL; gIter = gIter->next) {
941 const char *plugin = gIter->data;
945 if (stat(metadata, &st) == 0) {
946 result = g_list_append(result, strdup(plugin));
951 g_list_free_full(plugin_list, free);
int replace_secret_params(char *rsc_id, GHashTable *params)
void(* callback)(svc_action_t *op)
void services_action_free(svc_action_t *op)
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)
const char * pcmk_strerror(int rc)
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 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)
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.
GList * resources_os_list_lsb_agents(void)
void services_action_cleanup(svc_action_t *op)
int(* dispatch)(gpointer userdata)
#define do_crm_log_unlikely(level, fmt, args...)
Log a message that is likely to be filtered out.
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)
#define crm_trace(fmt, args...)
int setenv(const char *name, const char *value, int why)
int crm_set_nonblocking(int fd)
#define PCMK_OCF_REASON_PREFIX
void services_add_inflight_op(svc_action_t *op)
void services_untrack_op(svc_action_t *op)
#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)
char * crm_strdup_printf(char const *format,...) __attribute__((__format__(__printf__
gboolean services_action_async(svc_action_t *op, void(*action_callback)(svc_action_t *))
#define crm_info(fmt, args...)
#define NAGIOS_METADATA_DIR
int mainloop_child_timeout(mainloop_child_t *child)