mirror of
https://github.com/samba-team/samba.git
synced 2025-01-24 02:04:21 +03:00
ctdb-daemon: Add ctdb_vfork_with_logging()
This will be used to spawn lightweight helper processes to run eventscripts. Signed-off-by: Amitay Isaacs <amitay@gmail.com> Reviewed-by: Martin Schwenke <martin@meltin.net>
This commit is contained in:
parent
7aa20ccb5c
commit
2879404388
@ -1461,6 +1461,15 @@ struct ctdb_log_state *ctdb_fork_with_logging(TALLOC_CTX *mem_ctx,
|
||||
const char *log_prefix,
|
||||
void (*logfn)(const char *, uint16_t, void *),
|
||||
void *logfn_private, pid_t *pid);
|
||||
struct ctdb_log_state *ctdb_vfork_with_logging(TALLOC_CTX *mem_ctx,
|
||||
struct ctdb_context *ctdb,
|
||||
const char *log_prefix,
|
||||
const char *helper,
|
||||
int helper_argc,
|
||||
const char **helper_argv,
|
||||
void (*logfn)(const char *, uint16_t, void *),
|
||||
void *logfn_private, pid_t *pid);
|
||||
|
||||
|
||||
int32_t ctdb_control_process_exists(struct ctdb_context *ctdb, pid_t pid);
|
||||
struct ctdb_client *ctdb_find_client_by_pid(struct ctdb_context *ctdb, pid_t pid);
|
||||
|
@ -528,6 +528,84 @@ free_log:
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/*
|
||||
* vfork + exec, redirecting child output to logging and specified callback.
|
||||
*/
|
||||
struct ctdb_log_state *ctdb_vfork_with_logging(TALLOC_CTX *mem_ctx,
|
||||
struct ctdb_context *ctdb,
|
||||
const char *log_prefix,
|
||||
const char *helper,
|
||||
int helper_argc,
|
||||
const char **helper_argv,
|
||||
void (*logfn)(const char *, uint16_t, void *),
|
||||
void *logfn_private, pid_t *pid)
|
||||
{
|
||||
int p[2];
|
||||
struct ctdb_log_state *log;
|
||||
struct tevent_fd *fde;
|
||||
char **argv;
|
||||
int i;
|
||||
|
||||
log = talloc_zero(mem_ctx, struct ctdb_log_state);
|
||||
CTDB_NO_MEMORY_NULL(ctdb, log);
|
||||
|
||||
log->ctdb = ctdb;
|
||||
log->prefix = log_prefix;
|
||||
log->logfn = logfn;
|
||||
log->logfn_private = logfn_private;
|
||||
|
||||
if (pipe(p) != 0) {
|
||||
DEBUG(DEBUG_ERR, (__location__ " Failed to setup pipe for child logging\n"));
|
||||
goto free_log;
|
||||
}
|
||||
|
||||
argv = talloc_array(mem_ctx, char *, helper_argc + 2);
|
||||
if (argv == NULL) {
|
||||
DEBUG(DEBUG_ERR, (__location__ "Failed to allocate memory for helper\n"));
|
||||
goto free_log;
|
||||
}
|
||||
argv[0] = discard_const(helper);
|
||||
argv[1] = talloc_asprintf(argv, "%d", p[1]);
|
||||
if (argv[1] == NULL) {
|
||||
DEBUG(DEBUG_ERR, (__location__ "Failed to allocate memory for helper\n"));
|
||||
talloc_free(argv);
|
||||
goto free_log;
|
||||
}
|
||||
|
||||
for (i=0; i<helper_argc; i++) {
|
||||
argv[i+2] = discard_const(helper_argv[i]);
|
||||
}
|
||||
|
||||
*pid = vfork();
|
||||
if (*pid == 0) {
|
||||
execv(helper, argv);
|
||||
_exit(1);
|
||||
}
|
||||
close(p[1]);
|
||||
|
||||
if (*pid < 0) {
|
||||
DEBUG(DEBUG_ERR, (__location__ "vfork failed for helper process\n"));
|
||||
close(p[0]);
|
||||
goto free_log;
|
||||
}
|
||||
|
||||
ctdb_track_child(ctdb, *pid);
|
||||
|
||||
log->pfd = p[0];
|
||||
set_close_on_exec(log->pfd);
|
||||
talloc_set_destructor(log, log_context_destructor);
|
||||
fde = tevent_add_fd(ctdb->ev, log, log->pfd, EVENT_FD_READ,
|
||||
ctdb_log_handler, log);
|
||||
tevent_fd_set_auto_close(fde);
|
||||
|
||||
return log;
|
||||
|
||||
free_log:
|
||||
talloc_free(log);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
setup for logging of child process stdout
|
||||
*/
|
||||
|
Loading…
x
Reference in New Issue
Block a user