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);
177 g_hash_table_foreach(op->
params, set_ocf_env_with_prefix, NULL);
180 set_ocf_env(
"OCF_RA_VERSION_MAJOR",
"1", NULL);
181 set_ocf_env(
"OCF_RA_VERSION_MINOR",
"0", NULL);
186 set_ocf_env(
"OCF_RESOURCE_INSTANCE", op->
rsc, NULL);
189 if (op->
agent != NULL) {
190 set_ocf_env(
"OCF_RESOURCE_TYPE", op->
agent, NULL);
195 set_ocf_env(
"OCF_RESOURCE_PROVIDER", op->
provider, NULL);
204 crm_debug(
"Scheduling another invocation of %s", op->
id);
270 crm_trace(
"%s dispatching stderr", prefix);
281 crm_trace(
"%s dispatching stdout", prefix);
298 crm_info(
"%s - terminated with signal %d", prefix, signo);
303 crm_warn(
"%s - terminated with signal %d", prefix, signo);
310 crm_debug(
"%s - exited with rc=%d", prefix, exitcode);
335 services_handle_exec_error(
svc_action_t * op,
int error)
337 int rc_not_installed, rc_insufficient_priv, rc_exec_error;
366 op->
rc = rc_not_installed;
371 op->
rc = rc_insufficient_priv;
375 op->
rc = rc_exec_error;
390 signal(SIGPIPE, SIG_DFL);
392 #if defined(HAVE_SCHED_SETSCHEDULER) 393 if (sched_getscheduler(0) != SCHED_OTHER) {
394 struct sched_param sp;
396 memset(&sp, 0,
sizeof(sp));
397 sp.sched_priority = 0;
399 if (sched_setscheduler(0, SCHED_OTHER, &sp) == -1) {
400 crm_perror(LOG_ERR,
"Could not reset scheduling policy to SCHED_OTHER for %s", op->
id);
404 if (setpriority(PRIO_PROCESS, 0, 0) == -1) {
405 crm_perror(LOG_ERR,
"Could not reset process priority to 0 for %s", op->
id);
415 for (lpc = getdtablesize() - 1; lpc > STDERR_FILENO; lpc--) {
419 #if SUPPORT_CIBSECRETS 424 crm_info(
"proceeding with the stop operation for %s", op->
rsc);
427 crm_err(
"failed to get secrets for %s, " 428 "considering resource not configured", op->
rsc);
434 add_action_env_vars(op);
437 if (op->
opaque->
uid && (geteuid() == 0)) {
447 if (setgroups(0, NULL) < 0) {
448 crm_perror(LOG_ERR,
"Could not set child groups");
463 services_handle_exec_error(op, errno);
468 #ifndef HAVE_SYS_SIGNALFD_H 469 static int sigchld_pipe[2] = { -1, -1 };
474 if ((sigchld_pipe[1] >= 0) && (write(sigchld_pipe[1],
"", 1) == -1)) {
487 struct pollfd fds[3];
490 #ifdef HAVE_SYS_SIGNALFD_H 491 sfd = signalfd(-1, mask, SFD_NONBLOCK);
496 sfd = sigchld_pipe[0];
500 fds[0].events = POLLIN;
504 fds[1].events = POLLIN;
508 fds[2].events = POLLIN;
514 int poll_rc = poll(fds, 3, timeout);
517 if (fds[0].revents & POLLIN) {
521 if (fds[1].revents & POLLIN) {
525 if (fds[2].revents & POLLIN) {
526 #ifdef HAVE_SYS_SIGNALFD_H 527 struct signalfd_siginfo fdsi;
530 s = read(sfd, &fdsi,
sizeof(
struct signalfd_siginfo));
531 if (s !=
sizeof(
struct signalfd_siginfo)) {
532 crm_perror(LOG_ERR,
"Read from signal fd %d failed", sfd);
534 }
else if (fdsi.ssi_signo == SIGCHLD) {
539 while (read(sfd, &ch, 1) == 1) ;
541 wait_rc = waitpid(op->
pid, &status, WNOHANG);
546 }
else if (wait_rc < 0){
547 if (errno == ECHILD) {
560 }
else if (poll_rc == 0) {
564 }
else if (poll_rc < 0) {
565 if (errno != EINTR) {
571 timeout = op->
timeout - (time(NULL) - start) * 1000;
573 }
while ((op->
timeout < 0 || timeout > 0));
579 if (op->
timeout > 0 && timeout <= 0) {
589 if (wait_rc == 0 && waitpid(op->
pid, &status, WNOHANG) == 0) {
590 if (kill(op->
pid, SIGKILL)) {
591 crm_err(
"kill(%d, KILL) failed: %d", op->
pid, errno);
594 while (waitpid(op->
pid, &status, 0) == (pid_t) -1 && errno == EINTR) ;
597 }
else if (WIFEXITED(status)) {
599 op->
rc = WEXITSTATUS(status);
600 crm_info(
"Managed %s process %d exited with rc=%d", op->
id, op->
pid, op->
rc);
602 }
else if (WIFSIGNALED(status)) {
603 int signo = WTERMSIG(status);
606 crm_err(
"Managed %s process %d exited with signal=%d", op->
id, op->
pid, signo);
609 if (WCOREDUMP(status)) {
610 crm_err(
"Managed %s process %d dumped core", op->
id, op->
pid);
620 #ifdef HAVE_SYS_SIGNALFD_H 636 #ifdef HAVE_SYS_SIGNALFD_H 639 #define sigchld_cleanup() do { \ 640 if (sigismember(&old_mask, SIGCHLD) == 0) { \ 641 if (sigprocmask(SIG_UNBLOCK, &mask, NULL) < 0) { \ 642 crm_perror(LOG_ERR, "sigprocmask() failed to unblock sigchld"); \ 648 struct sigaction old_sa;
649 #define sigchld_cleanup() do { \ 650 if (sigaction(SIGCHLD, &old_sa, NULL) < 0) { \ 651 crm_perror(LOG_ERR, "sigaction() failed to remove sigchld handler"); \ 653 close(sigchld_pipe[0]); \ 654 close(sigchld_pipe[1]); \ 655 sigchld_pipe[0] = sigchld_pipe[1] = -1; \ 663 services_handle_exec_error(op, rc);
670 if (pipe(stdout_fd) < 0) {
675 services_handle_exec_error(op, rc);
682 if (pipe(stderr_fd) < 0) {
690 services_handle_exec_error(op, rc);
698 #ifdef HAVE_SYS_SIGNALFD_H 700 sigaddset(&mask, SIGCHLD);
701 sigemptyset(&old_mask);
703 if (sigprocmask(SIG_BLOCK, &mask, &old_mask) < 0) {
704 crm_perror(LOG_ERR,
"sigprocmask() failed to block sigchld");
709 if(pipe(sigchld_pipe) == -1) {
715 crm_warn(
"Could not set pipe input non-blocking: %s " CRM_XS " rc=%d",
720 crm_warn(
"Could not set pipe output non-blocking: %s " CRM_XS " rc=%d",
724 sa.sa_handler = sigchld_handler;
726 sigemptyset(&sa.sa_mask);
727 if (sigaction(SIGCHLD, &sa, &old_sa) < 0) {
728 crm_perror(LOG_ERR,
"sigaction() failed to set sigchld handler");
746 services_handle_exec_error(op, rc);
757 if (STDOUT_FILENO != stdout_fd[1]) {
758 if (dup2(stdout_fd[1], STDOUT_FILENO) != STDOUT_FILENO) {
759 crm_err(
"dup2() failed (stdout)");
763 if (STDERR_FILENO != stderr_fd[1]) {
764 if (dup2(stderr_fd[1], STDERR_FILENO) != STDERR_FILENO) {
765 crm_err(
"dup2() failed (stderr)");
774 action_launch_child(op);
785 crm_warn(
"Could not set child output non-blocking: %s " 793 crm_warn(
"Could not set child error output non-blocking: %s " 799 action_synced_wait(op, pmask);
830 struct dirent **namelist;
831 int entries = 0, lpc = 0;
832 char buffer[PATH_MAX];
834 entries = scandir(root, &namelist, NULL,
alphasort);
839 for (lpc = 0; lpc < entries; lpc++) {
842 if (
'.' == namelist[lpc]->d_name[0]) {
847 snprintf(buffer,
sizeof(buffer),
"%s/%s", root, namelist[lpc]->d_name);
849 if (stat(buffer, &sb)) {
853 if (S_ISDIR(sb.st_mode)) {
859 }
else if (S_ISREG(sb.st_mode)) {
860 if (files == FALSE) {
864 }
else if (executable
865 && (sb.st_mode & S_IXUSR) == 0
866 && (sb.st_mode & S_IXGRP) == 0 && (sb.st_mode & S_IXOTH) == 0) {
872 list = g_list_append(list, strdup(namelist[lpc]->d_name));
891 GList *result = NULL;
892 GList *providers = NULL;
897 snprintf(buffer,
sizeof(buffer),
"%s/resource.d/%s",
OCF_ROOT_DIR, provider);
902 for (gIter = providers; gIter != NULL; gIter = gIter->next) {
903 GList *tmp1 = result;
907 result = g_list_concat(tmp1, tmp2);
910 g_list_free_full(providers, free);
918 GList *plugin_list = NULL;
919 GList *result = NULL;
925 for (gIter = plugin_list; gIter != NULL; gIter = gIter->next) {
926 const char *plugin = gIter->data;
930 if (stat(metadata, &st) == 0) {
931 result = g_list_append(result, strdup(plugin));
936 g_list_free_full(plugin_list, free);
void(* callback)(svc_action_t *op)
const char * pcmk_strerror(int rc)
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)
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.
void services_action_cleanup(svc_action_t *op)
int(* dispatch)(gpointer userdata)
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)
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)