2 ctdb event script helper
4 Copyright (C) Amitay Isaacs 2013
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, see <http://www.gnu.org/licenses/>.
21 #include "system/filesys.h"
22 #include "system/network.h"
23 #include "system/wait.h"
27 #include "lib/util/blocking.h"
29 #include "ctdb_private.h"
31 #include "common/system.h"
33 static char *progname
= NULL
;
36 /* CTDB sends SIGTERM, when process must die */
37 static void sigterm(int sig
)
41 /* all the child processes are running in the same process group */
44 kill(-getpid(), SIGKILL
);
51 static int check_executable(const char *path
)
55 if (stat(path
, &st
) != 0) {
56 fprintf(stderr
, "Failed to access '%s' - %s\n",
57 path
, strerror(errno
));
61 if (!(st
.st_mode
& S_IXUSR
)) {
68 static void usage(void)
70 fprintf(stderr
, "\n");
71 fprintf(stderr
, "Usage: %s <log-fd> <output-fd> <script_path> <event> [<args>]\n",
75 int main(int argc
, char *argv
[])
79 int status
, output
, ret
;
90 log_fd
= atoi(argv
[1]);
91 write_fd
= atoi(argv
[2]);
93 set_close_on_exec(write_fd
);
97 dup2(log_fd
, STDOUT_FILENO
);
98 dup2(log_fd
, STDERR_FILENO
);
101 if (setpgid(0, 0) != 0) {
102 fprintf(stderr
, "Failed to create process group for event script - %s\n",
107 signal(SIGTERM
, sigterm
);
111 int save_errno
= errno
;
112 fprintf(stderr
, "Failed to fork - %s\n", strerror(errno
));
113 sys_write(write_fd
, &save_errno
, sizeof(save_errno
));
118 ret
= check_executable(argv
[3]);
122 ret
= execv(argv
[3], &argv
[3]);
124 int save_errno
= errno
;
125 fprintf(stderr
, "Error executing '%s' - %s\n",
126 argv
[3], strerror(save_errno
));
128 /* This should never happen */
132 ret
= waitpid(pid
, &status
, 0);
135 fprintf(stderr
, "waitpid() failed - %s\n", strerror(errno
));
136 sys_write(write_fd
, &output
, sizeof(output
));
139 if (WIFEXITED(status
)) {
140 output
= WEXITSTATUS(status
);
141 /* Only errors should be returned as -ve values */
142 if (output
== ENOENT
|| output
== ENOEXEC
) {
145 sys_write(write_fd
, &output
, sizeof(output
));
148 if (WIFSIGNALED(status
)) {
150 fprintf(stderr
, "Process terminated with signal - %d\n",
152 sys_write(write_fd
, &output
, sizeof(output
));
156 fprintf(stderr
, "waitpid() status=%d\n", status
);