X-Git-Url: https://git.lttng.org/?a=blobdiff_plain;ds=sidebyside;f=src%2Fcommon%2Fdaemonize.c;h=16af5a3f78ccec5da76033a48a7d0a04bd0c38f2;hb=HEAD;hp=40cef3840000a9a44848d779f531a9ea400ae983;hpb=f3630ec4ac23f0bc4f2e9b438310eadb5d3ebcb4;p=lttng-tools.git diff --git a/src/common/daemonize.c b/src/common/daemonize.c deleted file mode 100644 index 40cef3840..000000000 --- a/src/common/daemonize.c +++ /dev/null @@ -1,132 +0,0 @@ -/* - * Copyright (C) 2013 - David Goulet - * Copyright (C) 2014 - Mathieu Desnoyers - * - * This program is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License, version 2 only, as - * published by the Free Software Foundation. - * - * 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, write to the Free Software Foundation, Inc., 51 - * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#define _LGPL_SOURCE -#include -#include -#include -#include -#include - -#include - -#include -#include - -LTTNG_HIDDEN -int lttng_daemonize(pid_t *child_ppid, int *completion_flag, - int close_fds) -{ - int ret; - pid_t pid; - - /* Get parent pid of this process. */ - *child_ppid = getppid(); - - pid = fork(); - if (pid < 0) { - PERROR("fork"); - goto error; - } else if (pid == 0) { - int fd; - pid_t sid; - - /* Child */ - - /* - * Get the newly created parent pid so we can signal - * that process when we are ready to operate. - */ - *child_ppid = getppid(); - - sid = setsid(); - if (sid < 0) { - PERROR("setsid"); - goto error; - } - - /* - * Try to change directory to /. If we can't well at - * least notify. - */ - ret = chdir("/"); - if (ret < 0) { - PERROR("chdir"); - } - - if (close_fds) { - fd = open(_PATH_DEVNULL, O_RDWR, 0); - if (fd < 0) { - PERROR("open %s", _PATH_DEVNULL); - /* - * Let 0, 1 and 2 open since we can't - * bind them to /dev/null. - */ - } else { - (void) dup2(fd, STDIN_FILENO); - (void) dup2(fd, STDOUT_FILENO); - (void) dup2(fd, STDERR_FILENO); - if (fd > 2) { - ret = close(fd); - if (ret < 0) { - PERROR("close"); - } - } - } - } - goto end; - } else { - /* Parent */ - - /* - * Waiting for child to notify this parent that it can - * exit. Note that sleep() is interrupted before the 1 - * second delay as soon as the signal is received, so it - * will not cause visible delay for the user. - */ - while (!CMM_LOAD_SHARED(*completion_flag)) { - int status; - pid_t ret; - - /* - * Check if child exists without blocking. If - * so, we have to stop this parent process and - * return an error. - */ - ret = waitpid(pid, &status, WNOHANG); - if (ret < 0 || (ret != 0 && WIFEXITED(status))) { - /* The child exited somehow or was not valid. */ - goto error; - } - sleep(1); - } - - /* - * From this point on, the parent can exit and the child - * is now an operational session daemon ready to serve - * clients and applications. - */ - exit(EXIT_SUCCESS); - } - -end: - return 0; - -error: - return -1; -}