/*
 * Copyright (c) 1997, 2001 Hellmuth Michaelis. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 *---------------------------------------------------------------------------
 *
 *	nifmon - background process handling
 *	------------------------------------
 *
 *      last edit-date: [Wed May  2 16:29:24 2001]
 *
 *---------------------------------------------------------------------------*/

#include "nifmon.h"

/*---------------------------------------------------------------------------*
 *	check if another instance of us is already running
 *---------------------------------------------------------------------------*/
void
check_pid(char *pidfn)
{
	FILE *fp;
	
	/* check if another lock-file already exists */

	if ((fp = fopen(pidfn, "r")) != NULL)
	{
		/* lockfile found, check */

		int oldpid;

		/* read pid from file */

		if ((fscanf(fp, "%d", &oldpid)) != 1)
		{
			perror
				("ERROR, reading pid from lockfile failed, terminating!");
			exit(1);
		}

		/* check if process got from file is still alive */

		if ((kill(oldpid, 0)) != 0)
		{
			/* process does not exist */

			/* close file */

			fclose(fp);

			/* remove file */

			unlink(pidfn);
		}
		else
		{
			/* process is still alive */

			fprintf(stderr,
				"ERROR, another daemon is already running, pid = %d, terminating!",
				oldpid);
			exit(1);
		}
	}
}

/*---------------------------------------------------------------------------*
 *	establish and init process lock file
 *---------------------------------------------------------------------------*/
void
write_pid(char *pidfn)
{
	FILE *fp;

	/* write my pid into lock-file */

	if ((fp = fopen(pidfn, "w")) == NULL)
	{
		perror("ERROR, can't open lockfile for writing, terminating");
		do_exit(1);
	}

	if ((fprintf(fp, "%d", (int) getpid())) == EOF)
	{
		perror("ERROR, can't write pid to lockfile, terminating");
		do_exit(1);
	}

	fsync(fileno(fp));

	fclose(fp);
}

/*---------------------------------------------------------------------------*
 *	become a daemon
 *---------------------------------------------------------------------------*/
void
daemonize(void)
{
	int fd;
	char *tp;
		
	switch (fork())
	{
		case -1:	/* error */
			perror("ERROR, daemonize/fork");
			exit(1);
		case 0:	/* child */
			break;
		default:	/* parent */
			exit(0);
	}

	chdir("/");	/* go away from mounted dir */
	
	/* new session / no control tty */

	if (setsid() == -1)
	{
		perror("ERROR, setsid");
		exit(1);
	}

	if ((fd = open(rdev, O_RDWR, 0)) != -1)
	{
		if (!isatty(fd))
		{
			perror("ERROR, device is not a tty!");
			exit(1);
		}
		if ((dup2(fd, STDIN_FILENO)) == -1)
		{
			perror("ERROR, dup2 stdin");
			exit(1);
		}
		if ((dup2(fd, STDOUT_FILENO)) == -1)
		{
			perror("ERROR, dup2 stdout");
			exit(1);
		}
		if ((dup2(fd, STDERR_FILENO)) == -1)
		{
			perror("ERROR, dup2 stderr");
			exit(1);
		}
	}
	else
	{
		perror("ERROR, cannot open redirected device");
		exit(1);
	}

	if (fd > 2)
	{
		if ((close(fd)) == -1)
		{
			perror("ERROR, close in daemonize");
			exit(1);
		}
	}

	/* curses output && fork NEEDS controlling tty */

	if ((ioctl(STDIN_FILENO, TIOCSCTTY, (char *) NULL)) < 0)
	{
		perror
			("ERROR, cannot setup tty as controlling terminal");
		exit(1);
	}

	/* in case there is no environment ... */

	if (((tp = getenv("TERM")) == NULL) || (*tp == '\0'))
	{
		if (has_term == 0)
		{
			perror
				("ERROR, no environment variable TERM found and -t not specified!");
			exit(1);
		}

		if ((setenv("TERM", term, 1)) != 0)
		{
			perror("ERROR, setenv TERM failed");
			exit(1);
		}
	}
}

/* EOF */


syntax highlighted by Code2HTML, v. 0.9.1