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 "ctdb_private.h"
24 static char *progname
= NULL
;
27 /* CTDB sends SIGTERM, when process must die */
28 static void sigterm(int sig
)
32 /* all the child processes are running in the same process group */
35 kill(-getpid(), SIGKILL
);
42 static int check_executable(const char *path
)
46 if (stat(path
, &st
) != 0) {
47 fprintf(stderr
, "Failed to access '%s' - %s\n",
48 path
, strerror(errno
));
52 if (!(st
.st_mode
& S_IXUSR
)) {
59 static void usage(void)
61 fprintf(stderr
, "\n");
62 fprintf(stderr
, "Usage: %s <log-fd> <output-fd> <script_path> <event> [<args>]\n",
66 int main(int argc
, char *argv
[])
81 log_fd
= atoi(argv
[1]);
82 write_fd
= atoi(argv
[2]);
84 set_close_on_exec(write_fd
);
88 dup2(log_fd
, STDOUT_FILENO
);
89 dup2(log_fd
, STDERR_FILENO
);
92 if (setpgid(0, 0) != 0) {
93 fprintf(stderr
, "Failed to create process group for event script - %s\n",
98 signal(SIGTERM
, sigterm
);
102 fprintf(stderr
, "Failed to fork - %s\n", strerror(errno
));
109 execv(argv
[3], &argv
[3]);
110 if (errno
== EACCES
) {
111 save_errno
= check_executable(argv
[3]);
114 fprintf(stderr
, "Error executing '%s' - %s\n",
115 argv
[3], strerror(errno
));
120 waitpid(pid
, &status
, 0);
121 if (WIFEXITED(status
)) {
122 output
= WEXITSTATUS(status
);
123 if (output
== ENOENT
|| output
== ENOEXEC
) {
126 write(write_fd
, &output
, sizeof(output
));