Blame liblttng-ust-fork/ustfork.c

Packit c04fcb
/*
Packit c04fcb
 * Copyright (C) 2009  Pierre-Marc Fournier
Packit c04fcb
 * Copyright (C) 2011-2012  Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Packit c04fcb
 *
Packit c04fcb
 * This library is free software; you can redistribute it and/or
Packit c04fcb
 * modify it under the terms of the GNU Lesser General Public
Packit c04fcb
 * License as published by the Free Software Foundation; version 2.1 of
Packit c04fcb
 * the License.
Packit c04fcb
 *
Packit c04fcb
 * This library is distributed in the hope that it will be useful,
Packit c04fcb
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit c04fcb
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit c04fcb
 * Lesser General Public License for more details.
Packit c04fcb
 *
Packit c04fcb
 * You should have received a copy of the GNU Lesser General Public
Packit c04fcb
 * License along with this library; if not, write to the Free Software
Packit c04fcb
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
Packit c04fcb
 */
Packit c04fcb
Packit c04fcb
#define _GNU_SOURCE
Packit c04fcb
#include <lttng/ust-dlfcn.h>
Packit c04fcb
#include <unistd.h>
Packit c04fcb
#include <stdio.h>
Packit c04fcb
#include <signal.h>
Packit c04fcb
#include <sched.h>
Packit c04fcb
#include <stdarg.h>
Packit c04fcb
#include <errno.h>
Packit c04fcb
Packit c04fcb
#include <lttng/ust.h>
Packit c04fcb
Packit c04fcb
pid_t fork(void)
Packit c04fcb
{
Packit c04fcb
	static pid_t (*plibc_func)(void) = NULL;
Packit c04fcb
	sigset_t sigset;
Packit c04fcb
	pid_t retval;
Packit c04fcb
Packit c04fcb
	if (plibc_func == NULL) {
Packit c04fcb
		plibc_func = dlsym(RTLD_NEXT, "fork");
Packit c04fcb
		if (plibc_func == NULL) {
Packit c04fcb
			fprintf(stderr, "libustfork: unable to find \"fork\" symbol\n");
Packit c04fcb
			errno = ENOSYS;
Packit c04fcb
			return -1;
Packit c04fcb
		}
Packit c04fcb
	}
Packit c04fcb
Packit c04fcb
	ust_before_fork(&sigset);
Packit c04fcb
	/* Do the real fork */
Packit c04fcb
	retval = plibc_func();
Packit c04fcb
	if (retval == 0) {
Packit c04fcb
		/* child */
Packit c04fcb
		ust_after_fork_child(&sigset);
Packit c04fcb
	} else {
Packit c04fcb
		ust_after_fork_parent(&sigset);
Packit c04fcb
	}
Packit c04fcb
	return retval;
Packit c04fcb
}
Packit c04fcb
Packit c04fcb
int daemon(int nochdir, int noclose)
Packit c04fcb
{
Packit c04fcb
	static int (*plibc_func)(int nochdir, int noclose) = NULL;
Packit c04fcb
	sigset_t sigset;
Packit c04fcb
	int retval;
Packit c04fcb
Packit c04fcb
	if (plibc_func == NULL) {
Packit c04fcb
		plibc_func = dlsym(RTLD_NEXT, "daemon");
Packit c04fcb
		if (plibc_func == NULL) {
Packit c04fcb
			fprintf(stderr, "libustfork: unable to find \"daemon\" symbol\n");
Packit c04fcb
			errno = ENOSYS;
Packit c04fcb
			return -1;
Packit c04fcb
		}
Packit c04fcb
	}
Packit c04fcb
Packit c04fcb
	ust_before_fork(&sigset);
Packit c04fcb
	/* Do the real daemon call */
Packit c04fcb
	retval = plibc_func(nochdir, noclose);
Packit c04fcb
	if (retval == 0) {
Packit c04fcb
		/* child, parent called _exit() directly */
Packit c04fcb
		ust_after_fork_child(&sigset);
Packit c04fcb
	} else {
Packit c04fcb
		/* on error in the parent */
Packit c04fcb
		ust_after_fork_parent(&sigset);
Packit c04fcb
	}
Packit c04fcb
	return retval;
Packit c04fcb
}
Packit c04fcb
Packit c04fcb
#ifdef __linux__
Packit c04fcb
Packit c04fcb
struct user_desc;
Packit c04fcb
Packit c04fcb
struct ustfork_clone_info {
Packit c04fcb
	int (*fn)(void *);
Packit c04fcb
	void *arg;
Packit c04fcb
	sigset_t sigset;
Packit c04fcb
};
Packit c04fcb
Packit c04fcb
static int clone_fn(void *arg)
Packit c04fcb
{
Packit c04fcb
	struct ustfork_clone_info *info = (struct ustfork_clone_info *) arg;
Packit c04fcb
Packit c04fcb
	/* clone is now done and we are in child */
Packit c04fcb
	ust_after_fork_child(&info->sigset);
Packit c04fcb
	return info->fn(info->arg);
Packit c04fcb
}
Packit c04fcb
Packit c04fcb
int clone(int (*fn)(void *), void *child_stack, int flags, void *arg, ...)
Packit c04fcb
{
Packit c04fcb
	static int (*plibc_func)(int (*fn)(void *), void *child_stack,
Packit c04fcb
			int flags, void *arg, pid_t *ptid,
Packit c04fcb
			struct user_desc *tls, pid_t *ctid) = NULL;
Packit c04fcb
	/* var args */
Packit c04fcb
	pid_t *ptid;
Packit c04fcb
	struct user_desc *tls;
Packit c04fcb
	pid_t *ctid;
Packit c04fcb
	/* end of var args */
Packit c04fcb
	va_list ap;
Packit c04fcb
	int retval;
Packit c04fcb
Packit c04fcb
	va_start(ap, arg);
Packit c04fcb
	ptid = va_arg(ap, pid_t *);
Packit c04fcb
	tls = va_arg(ap, struct user_desc *);
Packit c04fcb
	ctid = va_arg(ap, pid_t *);
Packit c04fcb
	va_end(ap);
Packit c04fcb
Packit c04fcb
	if (plibc_func == NULL) {
Packit c04fcb
		plibc_func = dlsym(RTLD_NEXT, "clone");
Packit c04fcb
		if (plibc_func == NULL) {
Packit c04fcb
			fprintf(stderr, "libustfork: unable to find \"clone\" symbol.\n");
Packit c04fcb
			errno = ENOSYS;
Packit c04fcb
			return -1;
Packit c04fcb
		}
Packit c04fcb
	}
Packit c04fcb
Packit c04fcb
	if (flags & CLONE_VM) {
Packit c04fcb
		/*
Packit c04fcb
		 * Creating a thread, no need to intervene, just pass on
Packit c04fcb
		 * the arguments.
Packit c04fcb
		 */
Packit c04fcb
		retval = plibc_func(fn, child_stack, flags, arg, ptid,
Packit c04fcb
				tls, ctid);
Packit c04fcb
	} else {
Packit c04fcb
		/* Creating a real process, we need to intervene. */
Packit c04fcb
		struct ustfork_clone_info info = { .fn = fn, .arg = arg };
Packit c04fcb
Packit c04fcb
		ust_before_fork(&info.sigset);
Packit c04fcb
		retval = plibc_func(clone_fn, child_stack, flags, &info,
Packit c04fcb
				ptid, tls, ctid);
Packit c04fcb
		/* The child doesn't get here. */
Packit c04fcb
		ust_after_fork_parent(&info.sigset);
Packit c04fcb
	}
Packit c04fcb
	return retval;
Packit c04fcb
}
Packit c04fcb
Packit c04fcb
#elif defined (__FreeBSD__)
Packit c04fcb
Packit c04fcb
pid_t rfork(int flags)
Packit c04fcb
{
Packit c04fcb
	static pid_t (*plibc_func)(void) = NULL;
Packit c04fcb
	sigset_t sigset;
Packit c04fcb
	pid_t retval;
Packit c04fcb
Packit c04fcb
	if (plibc_func == NULL) {
Packit c04fcb
		plibc_func = dlsym(RTLD_NEXT, "rfork");
Packit c04fcb
		if (plibc_func == NULL) {
Packit c04fcb
			fprintf(stderr, "libustfork: unable to find \"rfork\" symbol\n");
Packit c04fcb
			errno = ENOSYS;
Packit c04fcb
			return -1;
Packit c04fcb
		}
Packit c04fcb
	}
Packit c04fcb
Packit c04fcb
	ust_before_fork(&sigset);
Packit c04fcb
	/* Do the real rfork */
Packit c04fcb
	retval = plibc_func();
Packit c04fcb
	if (retval == 0) {
Packit c04fcb
		/* child */
Packit c04fcb
		ust_after_fork_child(&sigset);
Packit c04fcb
	} else {
Packit c04fcb
		ust_after_fork_parent(&sigset);
Packit c04fcb
	}
Packit c04fcb
	return retval;
Packit c04fcb
}
Packit c04fcb
Packit c04fcb
/*
Packit c04fcb
 * On BSD, no need to override vfork, because it runs in the context of
Packit c04fcb
 * the parent, with parent waiting until execve or exit is executed in
Packit c04fcb
 * the child.
Packit c04fcb
 */
Packit c04fcb
Packit c04fcb
#else
Packit c04fcb
#warning "Unknown OS. You might want to ensure that fork/clone/vfork/fork handling is complete."
Packit c04fcb
#endif