1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <fcntl.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_SYS_IOCTL_H
#include <sys/ioctl.h>
#endif
#include "../sys.h"
#include "../tools.h"
#include "daemonize.h"
/* */
/* Daemonize binkd if we know how to do it */
/* Alex Semenyaka, Andrew Kolchoogin */
int already_daemonized = 0;
int binkd_daemonize(int nochdir)
{
if (already_daemonized)
return 0;
#ifdef HAVE_DAEMON
if (daemon(nochdir, 0) == -1)
{ Log(2, "Daemon() failed, %s", strerror(errno)); return -1; }
#elif defined(HAVE_SETSID)
if (fork() != 0) exit(0);
if (setsid() == -1)
#ifdef ultrix
/* Sendmail wisdom has been used */
if ((setpgrp() < 0) || (setsid() < 0))
#endif
{ Log(2, "Setsid() failed, %s", strerror(errno)); }
freopen("/dev/null","r",stdin);
freopen("/dev/null","w",stdout);
freopen("/dev/null","w",stderr);
if (!nochdir)
chdir("/");
#elif defined(HAVE_TIOCNOTTY)
if (fork() != 0) exit(0);
if (setpgrp() < 0)
{ Log(2, "Setpgrp failed, %s", strerror(errno)); }
{ int fd;
if ((fd = open("/dev/tty", O_RDWR)) >= 0)
{ ioctl(fd, TIOCNOTTY, (char*)0);
close(fd);
}
else
{ Log(2, "Cannot open /dev/tty, %s", strerror(errno)); }
}
if (!nochdir)
chdir("/");
freopen("/dev/null","r",stdin);
freopen("/dev/null","w",stdout);
freopen("/dev/null","w",stderr);
#endif /* HAVE_TIOCNOTTY, HAVE_SETSID, HAVE_DAEMON */
/* BinkD is either daemonized here or OS does not support known methods to */
/* do it. */
already_daemonized = 1;
return 0;
}
|