25 #include <sys/types.h> 34 #include <sys/resource.h> 36 #ifdef HAVE_SYS_SIGNALFD_H 37 #include <sys/signalfd.h> 46 #if SUPPORT_CIBSECRETS 54 set_fd_opts(
int fd,
int opts)
58 if ((flag = fcntl(fd, F_GETFL)) >= 0) {
59 if (fcntl(fd, F_SETFL, flag | opts) < 0) {
60 crm_err(
"fcntl() write failed");
68 svc_read_output(
int fd,
svc_action_t * op,
bool is_stderr)
73 static const size_t buf_read_len =
sizeof(buf) - 1;
84 crm_trace(
"Reading %s stderr into offset %d", op->
id, len);
89 crm_trace(
"Reading %s stdout into offset %d", op->
id, len);
92 crm_trace(
"Reading %s %s into offset %d", op->
id, is_stderr?
"stderr":
"stdout", len);
96 rc = read(fd, buf, buf_read_len);
98 crm_trace(
"Got %d chars: %.80s", rc, buf);
100 data = realloc_safe(data, len + rc + 1);
101 len += sprintf(data + len,
"%s", buf);
103 }
else if (errno != EINTR) {
111 }
while (rc == buf_read_len || rc < 0);
123 dispatch_stdout(gpointer userdata)
131 dispatch_stderr(gpointer userdata)
139 pipe_out_done(gpointer user_data)
153 pipe_err_done(gpointer user_data)
166 .destroy = pipe_out_done,
171 .destroy = pipe_err_done,
175 set_ocf_env(
const char *key,
const char *value, gpointer user_data)
177 if (
setenv(key, value, 1) != 0) {
178 crm_perror(LOG_ERR,
"setenv failed for key:%s and value:%s", key, value);
183 set_ocf_env_with_prefix(gpointer key, gpointer value, gpointer user_data)
187 snprintf(buffer,
sizeof(buffer),
"OCF_RESKEY_%s", (
char *)key);
188 set_ocf_env(buffer, value, user_data);
199 g_hash_table_foreach(op->
params, set_ocf_env_with_prefix, NULL);
202 set_ocf_env(
"OCF_RA_VERSION_MAJOR",
"1", NULL);
203 set_ocf_env(
"OCF_RA_VERSION_MINOR",
"0", NULL);
208 set_ocf_env(
"OCF_RESOURCE_INSTANCE", op->
rsc, NULL);
211 if (op->
agent != NULL) {
212 set_ocf_env(
"OCF_RESOURCE_TYPE", op->
agent, NULL);
217 set_ocf_env(
"OCF_RESOURCE_PROVIDER", op->
provider, NULL);
226 crm_debug(
"Scheduling another invocation of %s", op->
id);
294 crm_trace(
"%s dispatching stderr", prefix);
305 crm_trace(
"%s dispatching stdout", prefix);
320 "%s - terminated with signal %d", prefix, signo);
327 crm_debug(
"%s - exited with rc=%d", prefix, exitcode);
352 services_handle_exec_error(
svc_action_t * op,
int error)
354 int rc_not_installed, rc_insufficient_priv, rc_exec_error;
381 op->
rc = rc_not_installed;
386 op->
rc = rc_insufficient_priv;
390 op->
rc = rc_exec_error;
405 signal(SIGPIPE, SIG_DFL);
407 #if defined(HAVE_SCHED_SETSCHEDULER) 408 if (sched_getscheduler(0) != SCHED_OTHER) {
409 struct sched_param sp;
411 memset(&sp, 0,
sizeof(sp));
412 sp.sched_priority = 0;
414 if (sched_setscheduler(0, SCHED_OTHER, &sp) == -1) {
415 crm_perror(LOG_ERR,
"Could not reset scheduling policy to SCHED_OTHER for %s", op->
id);
419 if (setpriority(PRIO_PROCESS, 0, 0) == -1) {
420 crm_perror(LOG_ERR,
"Could not reset process priority to 0 for %s", op->
id);
430 for (lpc = getdtablesize() - 1; lpc > STDERR_FILENO; lpc--) {
434 #if SUPPORT_CIBSECRETS 439 crm_info(
"proceeding with the stop operation for %s", op->
rsc);
442 crm_err(
"failed to get secrets for %s, " 443 "considering resource not configured", op->
rsc);
449 add_OCF_env_vars(op);
455 services_handle_exec_error(op, errno);
460 #ifndef HAVE_SYS_SIGNALFD_H 461 static int sigchld_pipe[2] = { -1, -1 };
466 if ((sigchld_pipe[1] >= 0) && (write(sigchld_pipe[1],
"", 1) == -1)) {
479 struct pollfd fds[3];
482 #ifdef HAVE_SYS_SIGNALFD_H 483 sfd = signalfd(-1, mask, SFD_NONBLOCK);
488 sfd = sigchld_pipe[0];
492 fds[0].events = POLLIN;
496 fds[1].events = POLLIN;
500 fds[2].events = POLLIN;
506 int poll_rc = poll(fds, 3, timeout);
509 if (fds[0].revents & POLLIN) {
513 if (fds[1].revents & POLLIN) {
517 if (fds[2].revents & POLLIN) {
518 #ifdef HAVE_SYS_SIGNALFD_H 519 struct signalfd_siginfo fdsi;
522 s = read(sfd, &fdsi,
sizeof(
struct signalfd_siginfo));
523 if (s !=
sizeof(
struct signalfd_siginfo)) {
524 crm_perror(LOG_ERR,
"Read from signal fd %d failed", sfd);
526 }
else if (fdsi.ssi_signo == SIGCHLD) {
531 while (read(sfd, &ch, 1) == 1);
533 wait_rc = waitpid(op->
pid, &status, WNOHANG);
538 }
else if (wait_rc > 0) {
544 }
else if (poll_rc == 0) {
548 }
else if (poll_rc < 0) {
549 if (errno != EINTR) {
555 timeout = op->
timeout - (time(NULL) - start) * 1000;
557 }
while ((op->
timeout < 0 || timeout > 0));
561 int killrc = kill(op->
pid, SIGKILL);
564 if (op->
timeout > 0 && timeout <= 0) {
572 if (killrc && errno != ESRCH) {
573 crm_err(
"kill(%d, KILL) failed: %d", op->
pid, errno);
581 waitpid(op->
pid, &status, 0);
583 }
else if (WIFEXITED(status)) {
585 op->
rc = WEXITSTATUS(status);
586 crm_info(
"Managed %s process %d exited with rc=%d", op->
id, op->
pid, op->
rc);
588 }
else if (WIFSIGNALED(status)) {
589 int signo = WTERMSIG(status);
592 crm_err(
"Managed %s process %d exited with signal=%d", op->
id, op->
pid, signo);
595 if (WCOREDUMP(status)) {
596 crm_err(
"Managed %s process %d dumped core", op->
id, op->
pid);
606 #ifdef HAVE_SYS_SIGNALFD_H 621 #ifdef HAVE_SYS_SIGNALFD_H 624 #define sigchld_cleanup() do { \ 625 if (sigismember(&old_mask, SIGCHLD) == 0) { \ 626 if (sigprocmask(SIG_UNBLOCK, &mask, NULL) < 0) { \ 627 crm_perror(LOG_ERR, "sigprocmask() failed to unblock sigchld"); \ 633 struct sigaction old_sa;
634 #define sigchld_cleanup() do { \ 635 if (sigaction(SIGCHLD, &old_sa, NULL) < 0) { \ 636 crm_perror(LOG_ERR, "sigaction() failed to remove sigchld handler"); \ 638 close(sigchld_pipe[0]); \ 639 close(sigchld_pipe[1]); \ 640 sigchld_pipe[0] = sigchld_pipe[1] = -1; \ 648 services_handle_exec_error(op, rc);
655 if (pipe(stdout_fd) < 0) {
660 services_handle_exec_error(op, rc);
667 if (pipe(stderr_fd) < 0) {
675 services_handle_exec_error(op, rc);
683 #ifdef HAVE_SYS_SIGNALFD_H 685 sigaddset(&mask, SIGCHLD);
686 sigemptyset(&old_mask);
688 if (sigprocmask(SIG_BLOCK, &mask, &old_mask) < 0) {
689 crm_perror(LOG_ERR,
"sigprocmask() failed to block sigchld");
694 if(pipe(sigchld_pipe) == -1) {
698 set_fd_opts(sigchld_pipe[0], O_NONBLOCK);
699 set_fd_opts(sigchld_pipe[1], O_NONBLOCK);
701 sa.sa_handler = sigchld_handler;
703 sigemptyset(&sa.sa_mask);
704 if (sigaction(SIGCHLD, &sa, &old_sa) < 0) {
705 crm_perror(LOG_ERR,
"sigaction() failed to set sigchld handler");
724 services_handle_exec_error(op, rc);
735 if (STDOUT_FILENO != stdout_fd[1]) {
736 if (dup2(stdout_fd[1], STDOUT_FILENO) != STDOUT_FILENO) {
737 crm_err(
"dup2() failed (stdout)");
741 if (STDERR_FILENO != stderr_fd[1]) {
742 if (dup2(stderr_fd[1], STDERR_FILENO) != STDERR_FILENO) {
743 crm_err(
"dup2() failed (stderr)");
752 action_launch_child(op);
767 action_synced_wait(op, pmask);
798 struct dirent **namelist;
799 int entries = 0, lpc = 0;
800 char buffer[PATH_MAX];
802 entries = scandir(root, &namelist, NULL,
alphasort);
807 for (lpc = 0; lpc < entries; lpc++) {
810 if (
'.' == namelist[lpc]->d_name[0]) {
815 snprintf(buffer,
sizeof(buffer),
"%s/%s", root, namelist[lpc]->d_name);
817 if (stat(buffer, &sb)) {
821 if (S_ISDIR(sb.st_mode)) {
827 }
else if (S_ISREG(sb.st_mode)) {
828 if (files == FALSE) {
832 }
else if (executable
833 && (sb.st_mode & S_IXUSR) == 0
834 && (sb.st_mode & S_IXGRP) == 0 && (sb.st_mode & S_IXOTH) == 0) {
840 list = g_list_append(list, strdup(namelist[lpc]->d_name));
865 GList *result = NULL;
866 GList *providers = NULL;
871 snprintf(buffer,
sizeof(buffer),
"%s/resource.d/%s",
OCF_ROOT_DIR, provider);
876 for (gIter = providers; gIter != NULL; gIter = gIter->next) {
877 GList *tmp1 = result;
881 result = g_list_concat(tmp1, tmp2);
884 g_list_free_full(providers, free);
892 GList *plugin_list = NULL;
893 GList *result = NULL;
899 for (gIter = plugin_list; gIter != NULL; gIter = gIter->next) {
900 const char *plugin = gIter->data;
904 if (stat(metadata, &st) == 0) {
905 result = g_list_append(result, strdup(plugin));
910 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)
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
gboolean services_os_action_execute(svc_action_t *op, gboolean synchronous)
#define crm_warn(fmt, args...)
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)
#define PCMK_OCF_REASON_PREFIX
void services_add_inflight_op(svc_action_t *op)
#define crm_perror(level, fmt, args...)
Log a system error message.
#define NAGIOS_PLUGIN_DIR
#define crm_err(fmt, args...)
GList * resources_os_list_nagios_agents(void)
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 *))
void handle_blocked_ops(void)
#define crm_info(fmt, args...)
#define NAGIOS_METADATA_DIR
int mainloop_child_timeout(mainloop_child_t *child)