This commit is contained in:
2021-06-15 16:38:30 +01:00
commit 8c5c5c0f45
12 changed files with 468 additions and 0 deletions

65
pdetach.c Normal file
View File

@@ -0,0 +1,65 @@
#include <unistd.h>
#include <signal.h>
#include <sysexits.h>
#ifdef SIGTSTP
#include <sys/file.h>
#include <sys/ioctl.h>
#endif
/* normally included from paths.h */
#ifndef _PATH_TTY
#define _PATH_TTY "/dev/tty"
#endif
void pdetach(void)
{
int pid;
int fd;
/* if we are started from init, no need to become daemon */
if(getppid() == 1)
return;
#ifdef SIGTTOU
signal(SIGTTOU, SIG_IGN);
#endif
#ifdef SIGTTIN
signal(SIGTTIN, SIG_IGN);
#endif
#ifdef SIGTSTP
signal(SIGTSTP, SIG_IGN);
#endif
if((pid = fork()) < 0)
exit(EX_OSERR);
else if(pid > 0)
exit(0);
#if defined(SIGTSTP) && defined(TIOCNOTTY)
if(setpgid(0, getpid()) == -1)
exit(EX_OSERR);
if((fd = open(_PATH_TTY, O_RDWR)) >= 0)
{
ioctl(fd, TIOCNOTTY, NULL);
close(fd);
}
#else
if(setpgrp() == -1)
exit(EX_OSERR);
signal(SIGHUP, SIG_IGN);
if((pid = fork()) < 0)
exit(OS_ERR);
else if(pid > 0)
exit(0);
#endif
}