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 43 set_fd_opts(
int fd,
int opts)
47 if ((flag = fcntl(fd, F_GETFL)) >= 0) {
48 if (fcntl(fd, F_SETFL, flag | opts) < 0) {
49 crm_err(
"fcntl() write failed");
57 svc_read_output(
int fd,
svc_action_t * op,
bool is_stderr)
62 static const size_t buf_read_len =
sizeof(buf) - 1;
73 crm_trace(
"Reading %s stderr into offset %d", op->
id, len);
78 crm_trace(
"Reading %s stdout into offset %d", op->
id, len);
81 crm_trace(
"Reading %s %s into offset %d", op->
id, is_stderr?
"stderr":
"stdout", len);
85 rc = read(fd, buf, buf_read_len);
87 crm_trace(
"Got %d chars: %.80s", rc, buf);
89 data = realloc_safe(data, len + rc + 1);
90 len += sprintf(data + len,
"%s", buf);
92 }
else if (errno != EINTR) {
100 }
while (rc == buf_read_len || rc < 0);
112 dispatch_stdout(gpointer userdata)
120 dispatch_stderr(gpointer userdata)
128 pipe_out_done(gpointer user_data)
142 pipe_err_done(gpointer user_data)
155 .destroy = pipe_out_done,
160 .destroy = pipe_err_done,
164 set_ocf_env(
const char *key,
const char *value, gpointer user_data)
166 if (
setenv(key, value, 1) != 0) {
167 crm_perror(LOG_ERR,
"setenv failed for key:%s and value:%s", key, value);
172 set_ocf_env_with_prefix(gpointer key, gpointer value, gpointer user_data)
176 snprintf(buffer,
sizeof(buffer),
"OCF_RESKEY_%s", (
char *)key);
177 set_ocf_env(buffer, value, user_data);
188 g_hash_table_foreach(op->
params, set_ocf_env_with_prefix, NULL);
191 set_ocf_env(
"OCF_RA_VERSION_MAJOR",
"1", NULL);
192 set_ocf_env(
"OCF_RA_VERSION_MINOR",
"0", NULL);
197 set_ocf_env(
"OCF_RESOURCE_INSTANCE", op->
rsc, NULL);
200 if (op->
agent != NULL) {
201 set_ocf_env(
"OCF_RESOURCE_TYPE", op->
agent, NULL);
206 set_ocf_env(
"OCF_RESOURCE_PROVIDER", op->
provider, NULL);
215 crm_debug(
"Scheduling another invocation of %s", op->
id);
283 crm_trace(
"%s dispatching stderr", prefix);
294 crm_trace(
"%s dispatching stdout", prefix);
309 "%s - terminated with signal %d", prefix, signo);
316 crm_debug(
"%s - exited with rc=%d", prefix, exitcode);
341 services_handle_exec_error(
svc_action_t * op,
int error)
343 int rc_not_installed, rc_insufficient_priv, rc_exec_error;
370 op->
rc = rc_not_installed;
375 op->
rc = rc_insufficient_priv;
379 op->
rc = rc_exec_error;
394 signal(SIGPIPE, SIG_DFL);
396 #if defined(HAVE_SCHED_SETSCHEDULER) 397 if (sched_getscheduler(0) != SCHED_OTHER) {
398 struct sched_param sp;
400 memset(&sp, 0,
sizeof(sp));
401 sp.sched_priority = 0;
403 if (sched_setscheduler(0, SCHED_OTHER, &sp) == -1) {
404 crm_perror(LOG_ERR,
"Could not reset scheduling policy to SCHED_OTHER for %s", op->
id);
408 if (setpriority(PRIO_PROCESS, 0, 0) == -1) {
409 crm_perror(LOG_ERR,
"Could not reset process priority to 0 for %s", op->
id);
419 for (lpc = getdtablesize() - 1; lpc > STDERR_FILENO; lpc--) {
423 #if SUPPORT_CIBSECRETS 428 crm_info(
"proceeding with the stop operation for %s", op->
rsc);
431 crm_err(
"failed to get secrets for %s, " 432 "considering resource not configured", op->
rsc);
438 add_OCF_env_vars(op);
444 services_handle_exec_error(op, errno);
449 #ifndef HAVE_SYS_SIGNALFD_H 450 static int sigchld_pipe[2] = { -1, -1 };
455 if ((sigchld_pipe[1] >= 0) && (write(sigchld_pipe[1],
"", 1) == -1)) {
468 struct pollfd fds[3];
471 #ifdef HAVE_SYS_SIGNALFD_H 472 sfd = signalfd(-1, mask, SFD_NONBLOCK);
477 sfd = sigchld_pipe[0];
481 fds[0].events = POLLIN;
485 fds[1].events = POLLIN;
489 fds[2].events = POLLIN;
495 int poll_rc = poll(fds, 3, timeout);
498 if (fds[0].revents & POLLIN) {
502 if (fds[1].revents & POLLIN) {
506 if (fds[2].revents & POLLIN) {
507 #ifdef HAVE_SYS_SIGNALFD_H 508 struct signalfd_siginfo fdsi;
511 s = read(sfd, &fdsi,
sizeof(
struct signalfd_siginfo));
512 if (s !=
sizeof(
struct signalfd_siginfo)) {
513 crm_perror(LOG_ERR,
"Read from signal fd %d failed", sfd);
515 }
else if (fdsi.ssi_signo == SIGCHLD) {
520 while (read(sfd, &ch, 1) == 1);
522 wait_rc = waitpid(op->
pid, &status, WNOHANG);
527 }
else if (wait_rc > 0) {
533 }
else if (poll_rc == 0) {
537 }
else if (poll_rc < 0) {
538 if (errno != EINTR) {
544 timeout = op->
timeout - (time(NULL) - start) * 1000;
546 }
while ((op->
timeout < 0 || timeout > 0));
550 int killrc = kill(op->
pid, SIGKILL);
553 if (op->
timeout > 0 && timeout <= 0) {
561 if (killrc && errno != ESRCH) {
562 crm_err(
"kill(%d, KILL) failed: %d", op->
pid, errno);
570 waitpid(op->
pid, &status, 0);
572 }
else if (WIFEXITED(status)) {
574 op->
rc = WEXITSTATUS(status);
575 crm_info(
"Managed %s process %d exited with rc=%d", op->
id, op->
pid, op->
rc);
577 }
else if (WIFSIGNALED(status)) {
578 int signo = WTERMSIG(status);
581 crm_err(
"Managed %s process %d exited with signal=%d", op->
id, op->
pid, signo);
584 if (WCOREDUMP(status)) {
585 crm_err(
"Managed %s process %d dumped core", op->
id, op->
pid);
595 #ifdef HAVE_SYS_SIGNALFD_H 610 #ifdef HAVE_SYS_SIGNALFD_H 613 #define sigchld_cleanup() do { \ 614 if (sigismember(&old_mask, SIGCHLD) == 0) { \ 615 if (sigprocmask(SIG_UNBLOCK, &mask, NULL) < 0) { \ 616 crm_perror(LOG_ERR, "sigprocmask() failed to unblock sigchld"); \ 622 struct sigaction old_sa;
623 #define sigchld_cleanup() do { \ 624 if (sigaction(SIGCHLD, &old_sa, NULL) < 0) { \ 625 crm_perror(LOG_ERR, "sigaction() failed to remove sigchld handler"); \ 627 close(sigchld_pipe[0]); \ 628 close(sigchld_pipe[1]); \ 629 sigchld_pipe[0] = sigchld_pipe[1] = -1; \ 637 services_handle_exec_error(op, rc);
644 if (pipe(stdout_fd) < 0) {
649 services_handle_exec_error(op, rc);
656 if (pipe(stderr_fd) < 0) {
664 services_handle_exec_error(op, rc);
672 #ifdef HAVE_SYS_SIGNALFD_H 674 sigaddset(&mask, SIGCHLD);
675 sigemptyset(&old_mask);
677 if (sigprocmask(SIG_BLOCK, &mask, &old_mask) < 0) {
678 crm_perror(LOG_ERR,
"sigprocmask() failed to block sigchld");
683 if(pipe(sigchld_pipe) == -1) {
687 set_fd_opts(sigchld_pipe[0], O_NONBLOCK);
688 set_fd_opts(sigchld_pipe[1], O_NONBLOCK);
690 sa.sa_handler = sigchld_handler;
692 sigemptyset(&sa.sa_mask);
693 if (sigaction(SIGCHLD, &sa, &old_sa) < 0) {
694 crm_perror(LOG_ERR,
"sigaction() failed to set sigchld handler");
713 services_handle_exec_error(op, rc);
724 if (STDOUT_FILENO != stdout_fd[1]) {
725 if (dup2(stdout_fd[1], STDOUT_FILENO) != STDOUT_FILENO) {
726 crm_err(
"dup2() failed (stdout)");
730 if (STDERR_FILENO != stderr_fd[1]) {
731 if (dup2(stderr_fd[1], STDERR_FILENO) != STDERR_FILENO) {
732 crm_err(
"dup2() failed (stderr)");
741 action_launch_child(op);
756 action_synced_wait(op, pmask);
787 struct dirent **namelist;
788 int entries = 0, lpc = 0;
789 char buffer[PATH_MAX];
791 entries = scandir(root, &namelist, NULL,
alphasort);
796 for (lpc = 0; lpc < entries; lpc++) {
799 if (
'.' == namelist[lpc]->d_name[0]) {
804 snprintf(buffer,
sizeof(buffer),
"%s/%s", root, namelist[lpc]->d_name);
806 if (stat(buffer, &sb)) {
810 if (S_ISDIR(sb.st_mode)) {
816 }
else if (S_ISREG(sb.st_mode)) {
817 if (files == FALSE) {
821 }
else if (executable
822 && (sb.st_mode & S_IXUSR) == 0
823 && (sb.st_mode & S_IXGRP) == 0 && (sb.st_mode & S_IXOTH) == 0) {
829 list = g_list_append(list, strdup(namelist[lpc]->d_name));
854 GList *result = NULL;
855 GList *providers = NULL;
860 snprintf(buffer,
sizeof(buffer),
"%s/resource.d/%s",
OCF_ROOT_DIR, provider);
865 for (gIter = providers; gIter != NULL; gIter = gIter->next) {
866 GList *tmp1 = result;
870 result = g_list_concat(tmp1, tmp2);
873 g_list_free_full(providers, free);
881 GList *plugin_list = NULL;
882 GList *result = NULL;
888 for (gIter = plugin_list; gIter != NULL; gIter = gIter->next) {
889 const char *plugin = gIter->data;
893 if (stat(metadata, &st) == 0) {
894 result = g_list_append(result, strdup(plugin));
899 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)