From: Harlan Stenn Date: Sat, 1 Nov 2008 04:27:50 +0000 (-0500) Subject: [Bug 1068] Older versions of netsnmp do not have netsnmp_daemonize() X-Git-Tag: NTP_4_2_5P137~3 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=fc0a35a8c19751aceb44e8bddebb05b7a5c16bc7;p=thirdparty%2Fntp.git [Bug 1068] Older versions of netsnmp do not have netsnmp_daemonize() bk: 490bdac6nM-YK2lrF9lWaBkoIeuZ1A --- diff --git a/configure.ac b/configure.ac index d92f1dc0b..87a86b12f 100644 --- a/configure.ac +++ b/configure.ac @@ -3842,12 +3842,24 @@ case "$ans" in save_CFLAGS=$CFLAGS save_CPPFLAGS=$CPPFLAGS + save_LIBS=$LIBS CFLAGS=$SNMP_CFLAGS CPPFLAGS=$SNMP_CPPFLAGS + AC_CHECK_HEADER([net-snmp/net-snmp-config.h], [MAKE_NTPSNMPD=ntpsnmpd], [AC_MSG_WARN([net-snmp-config present but net-snmp headers are not available!])]) + + # Do this last, as we're messing up LIBS. + + # check -lnetsnmp for netsnmp_daemonize + LIBS=`$PATH_NET_SNMP_CONFIG --netsnmp-libs` + AC_CHECK_LIB([netsnmp], [netsnmp_daemonize], [], + [AC_DEFINE([NEED_NETSNMP_DAEMONIZE], [1], + [We need to provide netsnmp_daemonize()])]) + CFLAGS=$save_CFLAGS CPPFLAGS=$save_CPPFLAGS + LIBS=$save_LIBS ;; *) AC_MSG_WARN([Cannot build ntpsnmpd as desired - net-snmp-config cannot be found]) diff --git a/ntpsnmpd/Makefile.am b/ntpsnmpd/Makefile.am index 0279bce66..7e8f57f36 100644 --- a/ntpsnmpd/Makefile.am +++ b/ntpsnmpd/Makefile.am @@ -2,7 +2,7 @@ AUTOMAKE_OPTIONS= sbin_PROGRAMS= @MAKE_NTPSNMPD@ EXTRA_PROGRAMS= ntpsnmpd -ntpsnmpd_SOURCES= ntpsnmpd.c ntpSnmpSubagentObject.c \ +ntpsnmpd_SOURCES= netsnmp_daemonize.c ntpsnmpd.c ntpSnmpSubagentObject.c \ ntpsnmpd-opts.c ntpsnmpd-opts.h noinst_HEADERS= ntpSnmpSubagentObject.h # HMS: we probably want a version.o file here, too. diff --git a/ntpsnmpd/netsnmp_daemonize.c b/ntpsnmpd/netsnmp_daemonize.c new file mode 100644 index 000000000..d4da614ac --- /dev/null +++ b/ntpsnmpd/netsnmp_daemonize.c @@ -0,0 +1,266 @@ +/* + * system.c + */ +/* Portions of this file are subject to the following copyright(s). See + * the Net-SNMP's COPYING file for more details and other copyrights + * that may apply: + */ +/*********************************************************** + Copyright 1992 by Carnegie Mellon University + + All Rights Reserved + +Permission to use, copy, modify, and distribute this software and its +documentation for any purpose and without fee is hereby granted, +provided that the above copyright notice appear in all copies and that +both that copyright notice and this permission notice appear in +supporting documentation, and that the name of CMU not be +used in advertising or publicity pertaining to distribution of the +software without specific, written prior permission. + +CMU DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING +ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL +CMU BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR +ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, +WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, +ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS +SOFTWARE. +******************************************************************/ +/* + * Portions of this file are copyrighted by: + * Copyright © 2003 Sun Microsystems, Inc. All rights reserved. + * Use is subject to license terms specified in the COPYING file + * distributed with the Net-SNMP package. + */ +/* + * System dependent routines go here + */ +#include +#undef PACKAGE_BUGREPORT +#undef PACKAGE_NAME +#undef PACKAGE_STRING +#undef PACKAGE_TARNAME +#undef PACKAGE_VERSION +#include + +#ifdef NEED_NETSNMP_DAEMONIZE + +#include +#include +#include + +#if HAVE_UNISTD_H +#include +#endif +#if HAVE_STDLIB_H +#include +#endif + +#if TIME_WITH_SYS_TIME +# ifdef WIN32 +# include +# else +# include +# endif +# include +#else +# if HAVE_SYS_TIME_H +# include +# else +# include +# endif +#endif + +#include + +#if HAVE_NETINET_IN_H +#include +#endif + +#if HAVE_WINSOCK_H +#include +#endif +#if HAVE_SYS_SOCKET_H +#include +#endif +#if HAVE_NET_IF_H +#include +#endif + +#if HAVE_SYS_SOCKIO_H +#include +#endif + +#if HAVE_SYS_IOCTL_H +#include +#endif + +#ifdef HAVE_NLIST_H +#include +#endif + +#if HAVE_SYS_FILE_H +#include +#endif + +#if HAVE_KSTAT_H +#include +#endif + +#if HAVE_SYS_PARAM_H +#include +#endif +#if HAVE_SYS_SYSCTL_H +#include +#endif + +#if HAVE_STRING_H +#include +#else +#include +#endif + +#if HAVE_DMALLOC_H +#include +#endif + +#ifdef HAVE_SYS_STAT_H +#include +#endif +#if HAVE_FCNTL_H +#include +#endif + +#if defined(hpux10) || defined(hpux11) +#include +#endif + +#if HAVE_SYS_UTSNAME_H +#include +#endif + +#if HAVE_SYS_SYSTEMCFG_H +#include +#endif + +#if HAVE_SYS_SYSTEMINFO_H +#include +#endif + +#include +#include +#include +#include /* for "internal" definitions */ + +#include +#include /* for get_temp_file_pattern() */ + +#ifndef IFF_LOOPBACK +# define IFF_LOOPBACK 0 +#endif + +#ifdef INADDR_LOOPBACK +# define LOOPBACK INADDR_LOOPBACK +#else +# define LOOPBACK 0x7f000001 +#endif + +/** + * fork current process into the background. + * + * This function forks a process into the background, in order to + * become a daemon process. It does a few things along the way: + * + * - becoming a process/session group leader, and forking a second time so + * that process/session group leader can exit. + * + * - changing the working directory to / + * + * - closing stdin, stdout and stderr (unless stderr_log is set) and + * redirecting them to /dev/null + * + * @param quit_immediately : indicates if the parent process should + * exit after a successful fork. + * @param stderr_log : indicates if stderr is being used for + * logging and shouldn't be closed + * @returns -1 : fork error + * 0 : child process returning + * >0 : parent process returning. returned value is the child PID. + */ +int +netsnmp_daemonize(int quit_immediately, int stderr_log) +{ + int i = 0; + DEBUGMSGT(("daemonize","deamonizing...\n")); +#if HAVE_FORK + /* + * Fork to return control to the invoking process and to + * guarantee that we aren't a process group leader. + */ + i = fork(); + if (i != 0) { + /* Parent. */ + DEBUGMSGT(("daemonize","first fork returned %d.\n", i)); + if(i == -1) { + snmp_log(LOG_ERR,"first fork failed (errno %d) in " + "netsnmp_daemonize()\n", errno); + return -1; + } + if (quit_immediately) { + DEBUGMSGT(("daemonize","parent exiting\n")); + exit(0); + } + } else { + /* Child. */ +#ifdef HAVE_SETSID + /* Become a process/session group leader. */ + setsid(); +#endif + /* + * Fork to let the process/session group leader exit. + */ + if ((i = fork()) != 0) { + DEBUGMSGT(("daemonize","second fork returned %d.\n", i)); + if(i == -1) { + snmp_log(LOG_ERR,"second fork failed (errno %d) in " + "netsnmp_daemonize()\n", errno); + } + /* Parent. */ + exit(0); + } +#ifndef WIN32 + else { + /* Child. */ + + DEBUGMSGT(("daemonize","child continuing\n")); + + /* Avoid keeping any directory in use. */ + chdir("/"); + + if (!stderr_log) { + /* + * Close inherited file descriptors to avoid + * keeping unnecessary references. + */ + close(0); + close(1); + close(2); + + /* + * Redirect std{in,out,err} to /dev/null, just in + * case. + */ + open("/dev/null", O_RDWR); + dup(0); + dup(0); + } + } +#endif /* !WIN32 */ + } +#endif /* HAVE_FORK */ + return i; +} + +#else /* !NEED_NETSNMP_DAEMONIZE */ +int netsnp_daemonize_bs; +#endif