child_common.c revision 06f10b2a0ebb26f2460cd445f8040e9205de7500
/*
SSSD
Common helper functions to be used in child processes
Authors:
Sumit Bose <sbose@redhat.com>
Copyright (C) 2009 Red Hat
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <fcntl.h>
#include <tevent.h>
#include <errno.h>
#include "util/find_uid.h"
#include "util/child_common.h"
struct sss_sigchild_ctx {
struct tevent_context *ev;
int options;
};
struct sss_child_ctx {
void *pvt;
struct sss_sigchild_ctx *sigchld_ctx;
};
struct tevent_context *ev,
struct sss_sigchild_ctx **child_ctx)
{
struct sss_sigchild_ctx *sigchld_ctx;
struct tevent_signal *tes;
if (!sigchld_ctx) {
"fatal error initializing sss_sigchild_ctx\n");
return ENOMEM;
}
"fatal error initializing children hash table: [%s]\n",
return ret;
}
BlockSignals(false, SIGCHLD);
return EIO;
}
*child_ctx = sigchld_ctx;
return EOK;
}
static int sss_child_destructor(void *ptr)
{
struct sss_child_ctx *child_ctx;
int error;
"failed to delete child_ctx from hash table [%d]: %s\n",
}
return 0;
}
struct sss_sigchild_ctx *sigchld_ctx,
void *pvt,
struct sss_child_ctx **child_ctx)
{
struct sss_child_ctx *child;
int error;
return ENOMEM;
}
if (error != HASH_SUCCESS) {
return ENOMEM;
}
return EOK;
}
struct sss_child_cb_pvt {
struct sss_child_ctx *child_ctx;
int wait_status;
};
struct tevent_immediate *imm,
void *pvt)
{
struct sss_child_cb_pvt *cb_pvt;
struct sss_child_ctx *child_ctx;
int error;
"failed to delete child_ctx from hash table [%d]: %s\n",
}
}
}
struct tevent_signal *se,
int signum,
int count,
void *siginfo,
void *private_data)
{
struct sss_sigchild_ctx *sigchld_ctx;
struct tevent_immediate *imm;
struct sss_child_cb_pvt *invoke_pvt;
struct sss_child_ctx *child_ctx;
int error;
int wait_status;
do {
do {
errno = 0;
if (pid == -1) {
return;
} else if (pid == 0) continue;
if (error == HASH_SUCCESS) {
"Out of memory invoking SIGCHLD callback\n");
return;
}
if (invoke_pvt == NULL) {
"out of memory invoking SIGCHLD callback\n");
return;
}
} else if (error == HASH_ERROR_KEY_NOT_FOUND) {
"BUG: waitpid() returned [%d] but it was not in the table. "
"This could be due to a linked library creating processes "
"without registering them with the sigchld handler\n",
pid);
/* We will simply ignore this and return to the loop
* This will prevent a zombie, but may cause unexpected
* behavior in the code that was trying to handle this
* pid.
*/
} else {
"SIGCHLD hash table error [%d]: %s\n",
/* This is bad, but we should try to check for other
* children anyway, to avoid potential zombies.
*/
}
} while (pid != 0);
}
struct sss_child_ctx_old {
struct tevent_signal *sige;
int child_status;
void *pvt;
};
struct sss_child_ctx_old **_child_ctx)
{
struct sss_child_ctx_old *child_ctx;
"Setting up signal handler up for pid [%d]\n", pid);
return ENOMEM;
}
/* Error setting up signal handler */
return ENOMEM;
}
if (_child_ctx != NULL) {
*_child_ctx = child_ctx;
}
return EOK;
}
{
/* We still want to wait for the child to finish, but the caller is not
* interested in the result anymore (e.g. timeout was reached). */
if (ret == -1) {
}
}
/* Async communication with the child process via a pipe */
struct write_pipe_state {
int fd;
};
struct tevent_context *ev,
{
struct tevent_req *req;
struct write_pipe_state *state;
goto fail;
}
return req;
fail:
return NULL;
}
{
struct write_pipe_state);
if (flags & TEVENT_FD_READ) {
"write_pipe_done called with TEVENT_FD_READ,"
" this should not happen.\n");
return;
}
errno = 0;
return;
}
return;
}
return;
}
{
return EOK;
}
struct read_pipe_state {
int fd;
};
{
struct tevent_req *req;
struct read_pipe_state *state;
goto fail;
}
return req;
fail:
return NULL;
}
{
struct read_pipe_state);
if (flags & TEVENT_FD_WRITE) {
" this should not happen.\n");
return;
}
buf,
if (size == -1) {
return;
} else if (size > 0) {
return;
}
return;
} else if (size == 0) {
return;
} else {
"unexpected return value of read [%zd].\n", size);
return;
}
}
{
struct read_pipe_state *state;
return EOK;
}
/* The pipes to communicate with the child must be nonblocking */
void fd_nonblocking(int fd)
{
int flags;
int ret;
if (flags == -1) {
return;
}
}
return;
}
struct tevent_immediate *imm,
void *pvt);
{
struct sss_child_ctx_old *child_ctx;
struct tevent_immediate *imm;
if (count <= 0) {
"SIGCHLD handler called with invalid child count\n");
return;
}
errno = 0;
if (ret == -1) {
} else if (ret == 0) {
"waitpid did not found a child with changed status.\n");
} else {
"child [%d] failed with status [%d].\n", ret,
} else {
"child [%d] finished successfully.\n", ret);
}
"child [%d] was terminated by signal [%d].\n", ret,
} else {
"child [%d] was stopped by signal [%d].\n", ret,
}
"child [%d] was resumed by delivery of SIGCONT.\n",
ret);
}
return;
}
/* Invoke the callback in a tevent_immediate handler
* so that it is safe to free the tevent_signal *
*/
"Out of memory invoking sig handler callback\n");
return;
}
}
return;
}
struct tevent_immediate *imm,
void *pvt)
{
struct sss_child_ctx_old *child_ctx =
}
/* Stop monitoring for this child */
}
int child_debug_fd,
const char *binary,
char ***_argv)
{
/*
* program name, debug_level, debug_timestamps,
* debug_microseconds and NULL
*/
char ** argv;
/* Save the current state in case an interrupt changes it */
bool child_debug_to_file = debug_to_file;
bool child_debug_stderr = debug_to_stderr;
if (child_debug_to_file) argc++;
if (child_debug_stderr) argc++;
/*
* program name, debug_level, debug_to_file, debug_timestamps,
* debug_microseconds and NULL
*/
return ENOMEM;
}
goto fail;
}
if (child_debug_stderr) {
goto fail;
}
}
if (child_debug_to_file) {
goto fail;
}
}
goto fail;
}
goto fail;
}
goto fail;
}
if (argc != 0) {
goto fail;
}
return EOK;
fail:
return ret;
}
int *pipefd_to_child, int *pipefd_from_child,
{
int ret;
char **argv;
if (ret == -1) {
return err;
}
close(pipefd_from_child[0]);
if (ret == -1) {
return err;
}
return ret;
}
return err;
}
{
int ret;
if (readfd != -1) {
}
}
if (writefd != -1) {
}
}
}
int child_io_destructor(void *ptr)
{
int ret;
}
}
}
}
return EOK;
}