/*
 *
 *
 *    daemon.cc
 *
 *    C++ source code for class my::daemon member functions and/or static
 *    member data.
 *
 *    =========================================================================
 *
 *    Copyright (C) 1999-2013 by Matt Roberts, KK5JY.
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License as published by the Free Software Foundation; either
 *    version 2.1 of the License, or (at your option) any later version.
 *
 *    This library 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
 *    Lesser General Public License for more details.
 *
 *    You should have received a copy of the GNU Lesser General Public
 *    License along with this library; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *    =========================================================================
 *
 *
 */


#include <my++/daemon.h>
#include <cstdlib>

my::daemon::daemon (const deference q_defer) {
	rootdir = "";
	parent = false;
	if (q_defer == no_defer)
		daemonize ();
}

my::daemon::daemon (bool q_defer) {
	rootdir = "";
	parent = false;
	if (!q_defer)
		daemonize ();
}


my::daemon::daemon (const std::string &rd) {
	rootdir = rd;
	parent = false;
	daemonize();
}

my::daemon::daemon (const std::string &rd, bool q_defer) {
	rootdir = rd;
	parent = false;
	if (!q_defer)
		daemonize ();
}

my::daemon::daemon (const std::string &rd, const deference q_defer) {
	rootdir = rd;
	parent = false;
	if (!q_defer == no_defer)
		daemonize ();
}

my::daemon::daemon (void) {
	rootdir = "";
	parent = false;
	daemonize ();
}


void chdir_wrapper(const std::string &dn) {
	if (chdir(dn.c_str()) != 0) {
		// TODO: warn, or something
	}
}


/* here's the meat of the class.  This detaches the current process. */
void my::daemon::daemonize (bool doreturn) {
	pid_t fpid;

	/* call ::fork() to spawn a child process */

	fpid = fork ();

	if (fpid < 0) {      /* if the returned PID < 0, an error has occured   */
		pid = 0;         /* set the stored PID to zero                      */
		throw daemon::exception (this, errno); /* throw an appropriate exception */
	}

	/* if the PID > 0, it is the child PID, being returned to the parent   */
	/* so do whatever cleanup is needed (virtual function doBeforeExit())  */
	/* and then exit the program.  If a process returns from the con-      */
	/* structor (or this function called after deferral), without exiting, */
	/* the process is the child process                                    */

	if (fpid != 0) {
		parent = true;
		pid = fpid;
		if (doreturn)
			exit (exit_action (fpid));
	}

	/* the child process will continue here */

	pid = getpid ();              /* store the child's PID         */
	setsid ();                    /* become session leader         */
	if (rootdir.length())         /* if a root dir was specified   */
		chdir_wrapper(rootdir);   /* change to root directory      */
	umask (0);                    /* clear file mode creation mask */
	return;                       /* return normally               */
}
