Blame keepalived/core/daemon.c

Packit c22fc9
/*
Packit c22fc9
 * Soft:        Keepalived is a failover program for the LVS project
Packit c22fc9
 *              <www.linuxvirtualserver.org>. It monitor & manipulate
Packit c22fc9
 *              a loadbalanced server pool using multi-layer checks.
Packit c22fc9
 *
Packit c22fc9
 * Part:        Main program structure.
Packit c22fc9
 *
Packit c22fc9
 * Author:      Alexandre Cassen, <acassen@linux-vs.org>
Packit c22fc9
 *
Packit c22fc9
 *              This program is distributed in the hope that it will be useful,
Packit c22fc9
 *              but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit c22fc9
 *              MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Packit c22fc9
 *              See the GNU General Public License for more details.
Packit c22fc9
 *
Packit c22fc9
 *              This program is free software; you can redistribute it and/or
Packit c22fc9
 *              modify it under the terms of the GNU General Public License
Packit c22fc9
 *              as published by the Free Software Foundation; either version
Packit c22fc9
 *              2 of the License, or (at your option) any later version.
Packit c22fc9
 *
Packit c22fc9
 * Copyright (C) 2001-2017 Alexandre Cassen, <acassen@gmail.com>
Packit c22fc9
 */
Packit c22fc9
Packit c22fc9
#include "config.h"
Packit c22fc9
Packit c22fc9
#include <stdlib.h>
Packit c22fc9
#include <unistd.h>
Packit c22fc9
#include <stdbool.h>
Packit c22fc9
Packit c22fc9
#include "daemon.h"
Packit c22fc9
#include "logger.h"
Packit c22fc9
#include "utils.h"
Packit c22fc9
Packit c22fc9
/* Daemonization function coming from zebra source code */
Packit c22fc9
pid_t
Packit c22fc9
xdaemon(bool nochdir, bool noclose, bool exitflag)
Packit c22fc9
{
Packit c22fc9
	pid_t pid;
Packit c22fc9
	int ret;
Packit c22fc9
Packit c22fc9
#ifdef ENABLE_LOG_TO_FILE
Packit c22fc9
	if (log_file_name)
Packit c22fc9
		flush_log_file();
Packit c22fc9
#endif
Packit c22fc9
Packit c22fc9
	/* In case of fork is error. */
Packit c22fc9
	pid = fork();
Packit c22fc9
	if (pid < 0) {
Packit c22fc9
		log_message(LOG_INFO, "xdaemon: fork error");
Packit c22fc9
		return -1;
Packit c22fc9
	}
Packit c22fc9
Packit c22fc9
	/* In case of this is parent process. */
Packit c22fc9
	if (pid != 0) {
Packit c22fc9
		if (!exitflag)
Packit c22fc9
			exit(0);
Packit c22fc9
		else
Packit c22fc9
			return pid;
Packit c22fc9
	}
Packit c22fc9
Packit c22fc9
	/* Become session leader and get pid. */
Packit c22fc9
	pid = setsid();
Packit c22fc9
	if (pid < -1) {
Packit c22fc9
		log_message(LOG_INFO, "xdaemon: setsid error");
Packit c22fc9
		return -1;
Packit c22fc9
	}
Packit c22fc9
Packit c22fc9
	/* Change directory to root. */
Packit c22fc9
	if (!nochdir) {
Packit c22fc9
		ret = chdir("/");
Packit c22fc9
		if (ret < 0) {
Packit c22fc9
			log_message(LOG_INFO, "xdaemon: chdir error");
Packit c22fc9
		}
Packit c22fc9
	}
Packit c22fc9
Packit c22fc9
	/* File descriptor close. */
Packit c22fc9
	if (!noclose)
Packit c22fc9
		set_std_fd(true);
Packit c22fc9
Packit c22fc9
	return 0;
Packit c22fc9
}