head 1.5; access; symbols LMTP2NNTP_1_4_1:1.5 LMTP2NNTP_1_4_0:1.5 LMTP2NNTP_1_3_0:1.5 LMTP2NNTP_1_3b2:1.5 LMTP2NNTP_1_3b1:1.5 LMTP2NNTP_1_3a3:1.5 LMTP2NNTP_1_3a2:1.5 LMTP2NNTP_1_3a1:1.5 LMTP2NNTP_1_2_0:1.5 LMTP2NNTP_1_2b4:1.5 LMTP2NNTP_1_2b3:1.4 LMTP2NNTP_1_2b2:1.4 LMTP2NNTP_1_2b1:1.3 LMTP2NNTP_1_2a8:1.3 LMTP2NNTP_1_2a7:1.3 LMTP2NNTP_1_2a6:1.1 LMTP2NNTP_1_2a5:1.1 LMTP2NNTP_1_2a4:1.1 LMTP2NNTP_1_2a3:1.1 SOURCE_RESTRUCTURING_AFTER:1.1; locks; strict; comment @ * @; 1.5 date 2003.02.12.16.16.29; author rse; state Exp; branches; next 1.4; 1.4 date 2003.02.10.10.13.12; author thl; state Exp; branches; next 1.3; 1.3 date 2003.01.30.19.45.20; author rse; state Exp; branches; next 1.2; 1.2 date 2003.01.30.19.42.13; author rse; state Exp; branches; next 1.1; 1.1 date 2001.12.31.11.09.52; author thl; state Exp; branches; next ; desc @@ 1.5 log @finish DMalloc support @ text @/* ** OSSP lmtp2nntp - Mail to News Gateway ** Copyright (c) 2001-2003 Ralf S. Engelschall ** Copyright (c) 2001-2003 The OSSP Project ** Copyright (c) 2001-2003 Cable & Wireless Germany ** ** This file is part of OSSP lmtp2nntp, an LMTP speaking local ** mailer which forwards mails as Usenet news articles via NNTP. ** It can be found at http://www.ossp.org/pkg/tool/lmtp2nntp/. ** ** This program is free software; you can redistribute it and/or ** modify it under the terms of the GNU General Public License ** as published by the Free Software Foundation; either version ** 2.0 of the License, or (at your option) any later version. ** ** This program is distributed in the hope that it will be useful, ** but WITHOUT ANY WARRANTY; without even the implied warranty of ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU ** General Public License for more details. ** ** You should have received a copy of the GNU General Public License ** along with this file; if not, write to the Free Software ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 ** USA, or contact the OSSP project . ** ** lmtp2nntp_daemon.c: daemonize current process */ #include "config.h" #include #include #include #include #include #ifdef HAVE_TERMIOS_H #include #endif #if defined(HAVE_DMALLOC_H) && defined(WITH_DMALLOC) #include "dmalloc.h" #endif #include "lmtp2nntp_daemon.h" /* * See "Unix Programming Frequently Asked Questions": * http://www.erlenstar.demon.co.uk/unix/faq_2.html#SEC10 */ int daemonize(void) { int fd; int rc; /* * if we are started from init, * no need to become daemon. */ if (getppid() == 1) return 0; /* * Ignore tty related signals */ #ifdef SIGTTOU signal(SIGTTOU, SIG_IGN); #endif #ifdef SIGTTIN signal(SIGTTIN, SIG_IGN); #endif #ifdef SIGTSTP signal(SIGTSTP, SIG_IGN); #endif /* * fork so the parent can exit, this returns control to the command line * or shell invoking your program. This step is required so that the new * process is guaranteed not to be a process group leader (The next step, * setsid, would fail if you're a process group leader). */ rc = fork(); switch (rc) { case -1: return -1; case 0: break; default: _exit(0); /* exit original process */ } /* * setsid to become a process group and session group leader. Since a * controlling terminal is associated with a session, and this new session * has not yet acquired a controlling terminal our process now has no * controlling terminal, which is a Good Thing for daemons. */ #ifdef HAVE_SETSID if (setsid() == -1) return -1; #else if (setpgid(0, getpid()) == -1) return -1; #ifndef _PATH_TTY #define _PATH_TTY "/dev/tty" #endif if ((fd = open(_PATH_TTY, O_RDWR)) == -1) return -1; ioctl(fd, TIOCNOTTY, NULL); close(fd); #endif /* * fork again so the parent, (the session group leader), can exit. This * means that we, as a non-session group leader, can never regain a * controlling terminal. */ rc = fork(); switch (rc) { case -1: return -1; case 0: break; default: _exit(0); /* exit original process */ } /* * chdir("/") to ensure that our process doesn't keep any directory in * use. Failure to do this could make it so that an administrator couldn't * unmount a filesystem, because it was our current directory. * [Equivalently, we could change to any directory containing files * important to the daemon's operation.] */ chdir("/"); /* * give us complete control over the permissions of anything we write. We * don't know what umask we may have inherited. [This step is optional] */ umask(0); /* * close fds 0, 1, and 2. This releases the standard in, out, and error we * inherited from our parent process. We have no way of knowing where * these fds might have been redirected to. */ if ((fd = open("/dev/null", O_RDWR, 0)) != -1) { dup2(fd, STDIN_FILENO); dup2(fd, STDOUT_FILENO); dup2(fd, STDERR_FILENO); if (fd > 2) close(fd); } return 0; } @ 1.4 log @extend copyright messages based on CVS information @ text @d39 3 @ 1.3 log @remove trailing whitespaces from source tree @ text @d3 3 a5 3 ** Copyright (c) 2002-2003 Ralf S. Engelschall ** Copyright (c) 2002-2003 The OSSP Project ** Copyright (c) 2002-2003 Cable & Wireless Germany @ 1.2 log @Apply the standard OSSP copyright header, document ASCII art and fix URL to lmtp2nntp homepage. @ text @d52 2 a53 2 /* * if we are started from init, d76 1 a76 1 * setsid, would fail if you're a process group leader). d89 1 a89 1 * controlling terminal, which is a Good Thing for daemons. d100 1 a100 1 if ((fd = open(_PATH_TTY, O_RDWR)) == -1) d109 1 a109 1 * controlling terminal. d123 1 a123 1 * important to the daemon's operation.] d129 1 a129 1 * don't know what umask we may have inherited. [This step is optional] d136 1 a136 1 * these fds might have been redirected to. @ 1.1 log @Mega-Commit: Finally restructure the lmtp2nntp source tree in order to clean it up. We especially use a consistent prefix for all inlined sources. @ text @d2 4 a5 2 ** daemon.c -- daemonize current process ** Copyright (c) 1999-2000 Ralf S. Engelschall, All Rights Reserved. d7 20 a26 2 ** See "Unix Programming Frequently Asked Questions": ** http://www.erlenstar.demon.co.uk/unix/faq_2.html#SEC10 d41 5 @