| 1 |
#include "atheme-compat.h" |
| 2 |
|
| 3 |
#if (CURRENT_ABI_REVISION < 730000) |
| 4 |
# include "conf.h" |
| 5 |
# include "datastream.h" |
| 6 |
#endif |
| 7 |
|
| 8 |
#ifndef _WIN32 |
| 9 |
|
| 10 |
static struct update_command_state { |
| 11 |
connection_t *out, *err; |
| 12 |
pid_t pid; |
| 13 |
int running; |
| 14 |
} update_command_proc; |
| 15 |
|
| 16 |
static char *command = NULL; |
| 17 |
|
| 18 |
static void |
| 19 |
update_command_finished(pid_t pid, int status, void *data) |
| 20 |
{ |
| 21 |
if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) |
| 22 |
slog(LG_ERROR, "ERROR: Database update command failed with error %d", WEXITSTATUS(status)); |
| 23 |
|
| 24 |
update_command_proc.running = 0; |
| 25 |
} |
| 26 |
|
| 27 |
static void |
| 28 |
update_command_recvq_handler(connection_t *cptr, int err) |
| 29 |
{ |
| 30 |
char buf[BUFSIZE]; |
| 31 |
int count; |
| 32 |
|
| 33 |
count = recvq_getline(cptr, buf, sizeof(buf) - 1); |
| 34 |
if (count <= 0) |
| 35 |
return; |
| 36 |
if (buf[count-1] == '\n') |
| 37 |
count--; |
| 38 |
if (count == 0) |
| 39 |
buf[count++] = ' '; |
| 40 |
buf[count] = '\0'; |
| 41 |
|
| 42 |
if (err) |
| 43 |
{ |
| 44 |
slog(LG_ERROR, "ERROR: database update command said: %s", buf); |
| 45 |
} |
| 46 |
else |
| 47 |
slog(LG_DEBUG, "db update command stdout: %s", buf); |
| 48 |
} |
| 49 |
|
| 50 |
static void |
| 51 |
update_command_stdout_handler(connection_t *cptr) |
| 52 |
{ |
| 53 |
update_command_recvq_handler(cptr, 0); |
| 54 |
} |
| 55 |
|
| 56 |
static void |
| 57 |
update_command_stderr_handler(connection_t *cptr) |
| 58 |
{ |
| 59 |
update_command_recvq_handler(cptr, 1); |
| 60 |
} |
| 61 |
|
| 62 |
static void |
| 63 |
on_db_save(void *unused) |
| 64 |
{ |
| 65 |
int stdout_pipes[2], stderr_pipes[2]; |
| 66 |
pid_t pid; |
| 67 |
int errno1; |
| 68 |
|
| 69 |
if (!command) |
| 70 |
return; |
| 71 |
|
| 72 |
if (update_command_proc.running) |
| 73 |
{ |
| 74 |
slog(LG_ERROR, "ERROR: database update command is still running"); |
| 75 |
return; |
| 76 |
} |
| 77 |
|
| 78 |
if (pipe(stdout_pipes) == -1) |
| 79 |
{ |
| 80 |
int err = errno; |
| 81 |
slog(LG_ERROR, "ERROR: Couldn't create pipe for database update command: %s", strerror(err)); |
| 82 |
return; |
| 83 |
} |
| 84 |
|
| 85 |
if (pipe(stderr_pipes) == -1) |
| 86 |
{ |
| 87 |
int err = errno; |
| 88 |
slog(LG_ERROR, "ERROR: Couldn't create pipe for database update command: %s", strerror(err)); |
| 89 |
close(stdout_pipes[0]); |
| 90 |
close(stdout_pipes[1]); |
| 91 |
return; |
| 92 |
} |
| 93 |
|
| 94 |
pid = fork(); |
| 95 |
|
| 96 |
switch (pid) |
| 97 |
{ |
| 98 |
case -1: |
| 99 |
errno1 = errno; |
| 100 |
slog(LG_ERROR, "Failed to fork for database update command: %s", strerror(errno1)); |
| 101 |
return; |
| 102 |
case 0: |
| 103 |
connection_close_all_fds(); |
| 104 |
close(stdout_pipes[0]); |
| 105 |
close(stderr_pipes[0]); |
| 106 |
dup2(stdout_pipes[1], 1); |
| 107 |
dup2(stderr_pipes[1], 2); |
| 108 |
close(stdout_pipes[1]); |
| 109 |
close(stderr_pipes[1]); |
| 110 |
execl("/bin/sh", "sh", "-c", command, NULL); |
| 111 |
write(2, "Failed to exec /bin/sh\n", 23); |
| 112 |
_exit(255); |
| 113 |
return; |
| 114 |
default: |
| 115 |
close(stdout_pipes[1]); |
| 116 |
close(stderr_pipes[1]); |
| 117 |
update_command_proc.out = connection_add("update_command_stdout", stdout_pipes[0], 0, recvq_put, NULL); |
| 118 |
update_command_proc.err = connection_add("update_command_stderr", stderr_pipes[0], 0, recvq_put, NULL); |
| 119 |
update_command_proc.out->recvq_handler = update_command_stdout_handler; |
| 120 |
update_command_proc.err->recvq_handler = update_command_stderr_handler; |
| 121 |
update_command_proc.pid = pid; |
| 122 |
update_command_proc.running = 1; |
| 123 |
childproc_add(pid, "db_update", update_command_finished, NULL); |
| 124 |
break; |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
static void |
| 129 |
mod_init(module_t *const restrict m) |
| 130 |
{ |
| 131 |
hook_add_event("db_saved"); |
| 132 |
hook_add_db_saved(on_db_save); |
| 133 |
|
| 134 |
add_dupstr_conf_item("db_update_command", &conf_gi_table, 0, &command, NULL); |
| 135 |
} |
| 136 |
|
| 137 |
static void |
| 138 |
mod_deinit(const module_unload_intent_t intent) |
| 139 |
{ |
| 140 |
hook_del_db_saved(on_db_save); |
| 141 |
|
| 142 |
del_conf_item("db_update_command", &conf_gi_table); |
| 143 |
} |
| 144 |
|
| 145 |
SIMPLE_DECLARE_MODULE_V1("contrib/on_db_save", MODULE_UNLOAD_CAPABILITY_OK) |
| 146 |
|
| 147 |
#endif |