#include "config.h"
#include <stdio.h>
#include <errno.h>
#include <signal.h>
#include <string.h>
#include <sys/types.h>
#ifdef HAVE_SYS_WAIT_H
#include <sys/wait.h>
#endif
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#else
#ifdef HAVE_SYS_FCNTL_H
#include <sys/fcntl.h>
#endif
#endif
#include <sys/stat.h>
#if defined(HAVE_UNISTD_H)
#include <unistd.h>
#endif
#if defined(STDC_HEADERS)
#include <stdlib.h>
#endif
#if defined(QNX)
#include <unix.h>
#endif
#if !defined(HAVE_SETSID) && defined(SIGTSTP)
#if defined(HAVE_TERMIOS_H)
# include <termios.h>
#endif
#if !defined(TIOCNOTTY) && defined(HAVE_SGTTY_H)
# include <sgtty.h>
#endif
#endif
#if !defined(SIGCHLD) && defined(SIGCLD)
#define SIGCHLD SIGCLD
#endif
#include "fetchmail.h"
#include "tunable.h"
RETSIGTYPE
sigchld_handler (int sig)
{
extern volatile int lastsig;
pid_t pid;
#if defined(HAVE_WAITPID)
int status;
while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
continue;
#elif defined(HAVE_WAIT3)
#if defined(HAVE_UNION_WAIT) && !defined(__FreeBSD__)
union wait status;
#else
int status;
#endif
while ((pid = wait3(&status, WNOHANG, 0)) > 0)
continue;
#else
int status;
wait(&status);
#endif
lastsig = SIGCHLD;
}
RETSIGTYPE null_signal_handler(int sig) { }
SIGHANDLERTYPE set_signal_handler(int sig, SIGHANDLERTYPE handler)
{
SIGHANDLERTYPE rethandler;
#ifdef HAVE_SIGACTION
struct sigaction sa_new, sa_old;
memset (&sa_new, 0, sizeof sa_new);
sigemptyset (&sa_new.sa_mask);
sa_new.sa_handler = handler;
sa_new.sa_flags = 0;
#ifdef SA_RESTART
if (sig != SIGALRM)
sa_new.sa_flags |= SA_RESTART;
#endif
#ifdef SA_NOCLDSTOP
if (sig == SIGCHLD)
sa_new.sa_flags |= SA_NOCLDSTOP;
#endif
sigaction(sig, &sa_new, &sa_old);
rethandler = sa_old.sa_handler;
#if defined(SIGPWR)
if (sig == SIGCHLD)
sigaction(SIGPWR, &sa_new, NULL);
#endif
#else
rethandler = signal(sig, handler);
#if defined(SIGPWR)
if (sig == SIGCHLD)
signal(SIGPWR, handler);
#endif
siginterrupt(sig, sig == SIGALRM);
#endif
return rethandler;
}
void deal_with_sigchld(void)
{
set_signal_handler(SIGCHLD, sigchld_handler);
}
int
daemonize (const char *logfile, void (*termhook)(int))
{
int fd;
pid_t childpid;
if (getppid() == 1)
goto nottyDetach;
#ifdef SIGTTOU
set_signal_handler(SIGTTOU, SIG_IGN);
#endif
#ifdef SIGTTIN
set_signal_handler(SIGTTIN, SIG_IGN);
#endif
#ifdef SIGTSTP
set_signal_handler(SIGTSTP, SIG_IGN);
#endif
if ((childpid = fork()) < 0) {
report(stderr, "fork (%s)\n", strerror(errno));
return(PS_IOERR);
}
else if (childpid > 0)
exit(0);
#if defined(HAVE_SETSID)
if (setsid() < 0) {
report(stderr, "setsid (%s)\n", strerror(errno));
return(PS_IOERR);
}
#elif defined(SIGTSTP)
#ifndef __EMX__
setpgrp(0, getpid());
#endif
if ((fd = open("/dev/tty", O_RDWR)) >= 0) {
ioctl(fd, TIOCNOTTY, (char *) 0);
close(fd);
}
#else
#ifndef __EMX__
setpgrp();
#endif
set_signal_handler(SIGHUP, SIG_IGN);
if ((childpid = fork()) < 0) {
report(stderr, "fork (%s)\n", strerror(errno));
return(PS_IOERR);
}
else if (childpid > 0) {
exit(0);
}
#endif
nottyDetach:
#if defined(HAVE_GETDTABLESIZE)
for (fd = getdtablesize()-1; fd >= 0; fd--)
#elif defined(NOFILE)
for (fd = NOFILE-1; fd >= 0; fd--)
#else
for (fd = 19; fd >= 0; fd--)
#endif
{
close(fd);
}
if ((fd = open("/dev/null", O_RDWR)) < 0) {
report(stderr, "open: /dev/null (%s)\n", strerror(errno));
return(PS_IOERR);
}
if (logfile)
{
if ((fd = open(logfile, O_CREAT|O_WRONLY|O_APPEND, 0666)) < 0) {
report(stderr, "open %s (%s)\n", logfile, strerror(errno));
return(PS_IOERR);
}
}
else
{
if (dup(fd) < 0) {
report(stderr, "dup (%s)\n", strerror(errno));
return(PS_IOERR);
}
}
if (dup(fd) < 0) {
report(stderr, "dup (%s)\n", strerror(errno));
return(PS_IOERR);
}
#ifdef HAVE_GETCWD
chdir("/");
#endif
#if defined(DEF_UMASK)
umask(DEF_UMASK);
#else
umask(022);
#endif
deal_with_sigchld();
return(0);
}
flag isafile(int fd)
{
struct stat stbuf;
if (isatty(fd) || fstat(fd, &stbuf))
return(0);
else if (stbuf.st_mode & (S_IFREG))
return(1);
return(0);
}