6 /* Portions of this file are subject to the following copyright(s). See
7 * the Net-SNMP's COPYING file for more details and other copyrights
10 /***********************************************************
11 Copyright 1992 by Carnegie Mellon University
15 Permission to use, copy, modify, and distribute this software and its
16 documentation for any purpose and without fee is hereby granted,
17 provided that the above copyright notice appear in all copies and that
18 both that copyright notice and this permission notice appear in
19 supporting documentation, and that the name of CMU not be
20 used in advertising or publicity pertaining to distribution of the
21 software without specific, written prior permission.
23 CMU DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
24 ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
25 CMU BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
26 ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
27 WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
28 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
30 ******************************************************************/
32 * Portions of this file are copyrighted by:
33 * Copyright © 2003 Sun Microsystems, Inc. All rights reserved.
34 * Use is subject to license terms specified in the COPYING file
35 * distributed with the Net-SNMP package.
38 * System dependent routines go here
40 #include <net-snmp/net-snmp-config.h>
41 #undef PACKAGE_BUGREPORT
44 #undef PACKAGE_TARNAME
45 #undef PACKAGE_VERSION
48 #ifdef NEED_NETSNMP_DAEMONIZE
61 #if TIME_WITH_SYS_TIME
63 # include <sys/timeb.h>
65 # include <sys/time.h>
70 # include <sys/time.h>
76 #include <sys/types.h>
79 #include <netinet/in.h>
86 #include <sys/socket.h>
93 #include <sys/sockio.h>
97 #include <sys/ioctl.h>
105 #include <sys/file.h>
113 #include <sys/param.h>
115 #if HAVE_SYS_SYSCTL_H
116 #include <sys/sysctl.h>
129 #ifdef HAVE_SYS_STAT_H
130 #include <sys/stat.h>
136 #if defined(hpux10) || defined(hpux11)
137 #include <sys/pstat.h>
140 #if HAVE_SYS_UTSNAME_H
141 #include <sys/utsname.h>
144 #if HAVE_SYS_SYSTEMCFG_H
145 #include <sys/systemcfg.h>
148 #if HAVE_SYS_SYSTEMINFO_H
149 #include <sys/systeminfo.h>
152 #include <net-snmp/types.h>
153 #include <net-snmp/output_api.h>
154 #include <net-snmp/utilities.h>
155 #include <net-snmp/library/system.h> /* for "internal" definitions */
157 #include <net-snmp/library/snmp_api.h>
158 #include <net-snmp/library/read_config.h> /* for get_temp_file_pattern() */
161 # define IFF_LOOPBACK 0
164 #ifdef INADDR_LOOPBACK
165 # define LOOPBACK INADDR_LOOPBACK
167 # define LOOPBACK 0x7f000001
171 * fork current process into the background.
173 * This function forks a process into the background, in order to
174 * become a daemon process. It does a few things along the way:
176 * - becoming a process/session group leader, and forking a second time so
177 * that process/session group leader can exit.
179 * - changing the working directory to /
181 * - closing stdin, stdout and stderr (unless stderr_log is set) and
182 * redirecting them to /dev/null
184 * @param quit_immediately : indicates if the parent process should
185 * exit after a successful fork.
186 * @param stderr_log : indicates if stderr is being used for
187 * logging and shouldn't be closed
188 * @returns -1 : fork error
189 * 0 : child process returning
190 * >0 : parent process returning. returned value is the child PID.
193 netsnmp_daemonize(int quit_immediately
, int stderr_log
)
196 DEBUGMSGT(("daemonize","deamonizing...\n"));
199 * Fork to return control to the invoking process and to
200 * guarantee that we aren't a process group leader.
205 DEBUGMSGT(("daemonize","first fork returned %d.\n", i
));
207 snmp_log(LOG_ERR
,"first fork failed (errno %d) in "
208 "netsnmp_daemonize()\n", errno
);
211 if (quit_immediately
) {
212 DEBUGMSGT(("daemonize","parent exiting\n"));
218 /* Become a process/session group leader. */
222 * Fork to let the process/session group leader exit.
224 if ((i
= fork()) != 0) {
225 DEBUGMSGT(("daemonize","second fork returned %d.\n", i
));
227 snmp_log(LOG_ERR
,"second fork failed (errno %d) in "
228 "netsnmp_daemonize()\n", errno
);
237 DEBUGMSGT(("daemonize","child continuing\n"));
239 /* Avoid keeping any directory in use. */
244 * Close inherited file descriptors to avoid
245 * keeping unnecessary references.
252 * Redirect std{in,out,err} to /dev/null, just in
255 open("/dev/null", O_RDWR
);
262 #endif /* HAVE_FORK */
266 #else /* !NEED_NETSNMP_DAEMONIZE */
267 int netsnp_daemonize_bs
;