<commit_before>/* $Id$ */

#include <sys/param.h>
#include <sys/ioctl.h>
#include <sys/stat.h>

#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "psc_util/daemon.h"

/* a daemon startup routine, by <NAME>
 * sever all ties to the launching process
 */

void daemon_start(void){
	/* Ignore the terminal stop signals */
#ifdef SIGTTOU
	signal(SIGTTOU, SIG_IGN);
#endif
#ifdef SIGTTIN
	signal(SIGTTIN, SIG_IGN);
#endif
#ifdef SIGTSTP
	signal(SIGTSTP, SIG_IGN);
#endif

	/* If we were not started in the background, fork and
	 * let the parent exit.  This also guarantees the first child
	 * is not a process group leader. */
	pid_t childpid = 0;
	if ( (childpid = fork()) < 0){
		/* using err() instead of psc_log
		 * because logging may not be initialized at this point */
		err(errno, "Can't fork first child");
	} else if (childpid > 0)
		exit(0); /* parent exits */


	/* Disassociate from controlling terminal and process group. */
	/* Ensure the process can't reacquire a new controlling terminal. */
	int fd = 0;

#ifdef	SIGTSTP	/* BSD */

	if ( (fd = open("/dev/tty", O_RDWR)) >= 0) {
		ioctl(fd, TIOCNOTTY, (char *)NULL); /* lose controlling tty */
		close(fd);
	}

	if (0!=setpgid(getpid(), getpid())){
		err(errno, "Can't change process group");
	}

#else /* System V */

	if (setpgrp() == -1){
		err(errno, "Can't change process group");
	}

	signal(SIGHUP, SIG_IGN); /* immune from pgrp leader death */

	if ( (childpid = fork()) < 0){
		err(errno, "Can't fork second child");
	} else if (childpid > 0)
		exit(0); /* first child dies */

	/* begin second child... */
#endif


	/* Move to a "safe" directory   */
	if (0!=chdir("/tmp")){
		err(errno, "Failed to CD to /tmp");
	}

	/* Clear any inherited file mode creation mask. */
	umask(0);

	/* Close any open files descriptors. */
	for (fd = 0; fd < NOFILE; fd++)
		close(fd);
	errno = 0; /* probably got set to EBADF from a close */
}

void write_pidfile(const char *dname, int id){
	char  pidfile_name[MAXPATHLEN], pidStr[10];
	FILE *fp;
	int   fd;

	/* look for an existing PID file, and read the PID from it */
	if (0!=id)
		snprintf(pidfile_name, MAXPATHLEN, "%s/%s-%d.pid",
			 PIDFILE_DIR, dname, id);
	else
		snprintf(pidfile_name, MAXPATHLEN, "%s/%s.pid",
			 PIDFILE_DIR, dname);

	if (NULL==(fp=fopen(pidfile_name, "w+")))
		err(errno, "failed to open pid file (%s)", pidfile_name);

	/* lock it, to avoid race conditions */
	if (0>(fd = fileno(fp)))<commit_msg>use daemon(3) instead of homegrown<commit_after>/* $Id$ */

#include <sys/param.h>
#include <sys/ioctl.h>
#include <sys/stat.h>

#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "psc_util/daemon.h"

void write_pidfile(const char *dname, int id)
{
	char  pidfile_name[MAXPATHLEN], pidStr[10];
	FILE *fp;
	int   fd;

	/* look for an existing PID file, and read the PID from it */
	if (0!=id)
		snprintf(pidfile_name, MAXPATHLEN, "%s/%s-%d.pid",
			 PIDFILE_DIR, dname, id);
	else
		snprintf(pidfile_name, MAXPATHLEN, "%s/%s.pid",
			 PIDFILE_DIR, dname);

	if (NULL==(fp=fopen(pidfile_name, "w+")))
		err(errno, "failed to open pid file (%s)", pidfile_name);

	/* lock it, to avoid race conditions */
	if (0>(fd = fileno(fp)))
{'fraction_non_alphanumeric': 0.16696165191740414, 'fraction_numerical': 0.007079646017699115, 'mean_word_length': 3.7360335195530725, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5755241', 'n_tokens_mistral': 1357, 'n_tokens_neox': 1280, 'n_words': 392}