4 /* Portions of this file are subject to the following copyright(s). See
5 * the Net-SNMP's COPYING file for more details and other copyrights
8 /***********************************************************
9 Copyright 1992 by Carnegie Mellon University
13 Permission to use, copy, modify, and distribute this software and its
14 documentation for any purpose and without fee is hereby granted,
15 provided that the above copyright notice appear in all copies and that
16 both that copyright notice and this permission notice appear in
17 supporting documentation, and that the name of CMU not be
18 used in advertising or publicity pertaining to distribution of the
19 software without specific, written prior permission.
21 CMU DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
22 ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
23 CMU BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
24 ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
25 WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
26 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
28 ******************************************************************/
30 * Portions of this file are copyrighted by:
31 * Copyright © 2003 Sun Microsystems, Inc. All rights reserved.
32 * Use is subject to license terms specified in the COPYING file
33 * distributed with the Net-SNMP package.
36 * System dependent routines go here
38 #include <net-snmp/net-snmp-config.h>
39 #undef PACKAGE_BUGREPORT
42 #undef PACKAGE_TARNAME
44 #undef PACKAGE_VERSION
47 #ifdef NEED_NETSNMP_DAEMONIZE
60 #if TIME_WITH_SYS_TIME
62 # include <sys/timeb.h>
64 # include <sys/time.h>
69 # include <sys/time.h>
75 #include <sys/types.h>
78 #include <netinet/in.h>
85 #include <sys/socket.h>
92 #include <sys/sockio.h>
96 #include <sys/ioctl.h>
104 #include <sys/file.h>
112 #include <sys/param.h>
114 #if HAVE_SYS_SYSCTL_H
115 #include <sys/sysctl.h>
128 #ifdef HAVE_SYS_STAT_H
129 #include <sys/stat.h>
135 #if defined(hpux10) || defined(hpux11)
136 #include <sys/pstat.h>
139 #if HAVE_SYS_UTSNAME_H
140 #include <sys/utsname.h>
143 #if HAVE_SYS_SYSTEMCFG_H
144 #include <sys/systemcfg.h>
147 #if HAVE_SYS_SYSTEMINFO_H
148 #include <sys/systeminfo.h>
151 #include <net-snmp/types.h>
152 #include <net-snmp/output_api.h>
153 #include <net-snmp/utilities.h>
154 #include <net-snmp/library/system.h> /* for "internal" definitions */
156 #include <net-snmp/library/snmp_api.h>
157 #include <net-snmp/library/read_config.h> /* for get_temp_file_pattern() */
160 # define IFF_LOOPBACK 0
163 #ifdef INADDR_LOOPBACK
164 # define LOOPBACK INADDR_LOOPBACK
166 # define LOOPBACK 0x7f000001
170 * fork current process into the background.
172 * This function forks a process into the background, in order to
173 * become a daemon process. It does a few things along the way:
175 * - becoming a process/session group leader, and forking a second time so
176 * that process/session group leader can exit.
178 * - changing the working directory to /
180 * - closing stdin, stdout and stderr (unless stderr_log is set) and
181 * redirecting them to /dev/null
183 * @param quit_immediately : indicates if the parent process should
184 * exit after a successful fork.
185 * @param stderr_log : indicates if stderr is being used for
186 * logging and shouldn't be closed
187 * @returns -1 : fork error
188 * 0 : child process returning
189 * >0 : parent process returning. returned value is the child PID.
192 netsnmp_daemonize(int quit_immediately, int stderr_log)
197 DEBUGMSGT(("daemonize","daemonizing...\n"));
198 #ifdef HAVE_WORKING_FORK
200 * Fork to return control to the invoking process and to
201 * guarantee that we aren't a process group leader.
207 DEBUGMSGT(("daemonize","first fork returned %d.\n", i));
209 snmp_log(LOG_ERR,"first fork failed (errno %d) in "
210 "netsnmp_daemonize()\n", saved_errno);
213 if (quit_immediately) {
214 DEBUGMSGT(("daemonize","parent exiting\n"));
220 /* Become a process/session group leader. */
224 * Fork to let the process/session group leader exit.
226 if ((i = fork()) != 0) {
228 DEBUGMSGT(("daemonize","second fork returned %d.\n", i));
230 snmp_log(LOG_ERR,"second fork failed (errno %d) in "
231 "netsnmp_daemonize()\n", saved_errno);
240 DEBUGMSGT(("daemonize","child continuing\n"));
242 /* Avoid keeping any directory in use. */
247 * Close inherited file descriptors to avoid
248 * keeping unnecessary references.
255 * Redirect std{in,out,err} to /dev/null, just in
258 open("/dev/null", O_RDWR);
265 #endif /* HAVE_WORKING_FORK */
269 #else /* !NEED_NETSNMP_DAEMONIZE */
270 int netsnp_daemonize_bs;