Annotation of embedaddon/ntp/ntpsnmpd/netsnmp_daemonize.c, revision 1.1
1.1 ! misho 1: /*
! 2: * system.c
! 3: */
! 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
! 6: * that may apply:
! 7: */
! 8: /***********************************************************
! 9: Copyright 1992 by Carnegie Mellon University
! 10:
! 11: All Rights Reserved
! 12:
! 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.
! 20:
! 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
! 27: SOFTWARE.
! 28: ******************************************************************/
! 29: /*
! 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.
! 34: */
! 35: /*
! 36: * System dependent routines go here
! 37: */
! 38: #include <net-snmp/net-snmp-config.h>
! 39: #undef PACKAGE_BUGREPORT
! 40: #undef PACKAGE_NAME
! 41: #undef PACKAGE_STRING
! 42: #undef PACKAGE_TARNAME
! 43: #undef PACKAGE_VERSION
! 44: #include <config.h>
! 45:
! 46: #ifdef NEED_NETSNMP_DAEMONIZE
! 47:
! 48: #include <stdio.h>
! 49: #include <ctype.h>
! 50: #include <errno.h>
! 51:
! 52: #if HAVE_UNISTD_H
! 53: #include <unistd.h>
! 54: #endif
! 55: #if HAVE_STDLIB_H
! 56: #include <stdlib.h>
! 57: #endif
! 58:
! 59: #if TIME_WITH_SYS_TIME
! 60: # ifdef WIN32
! 61: # include <sys/timeb.h>
! 62: # else
! 63: # include <sys/time.h>
! 64: # endif
! 65: # include <time.h>
! 66: #else
! 67: # if HAVE_SYS_TIME_H
! 68: # include <sys/time.h>
! 69: # else
! 70: # include <time.h>
! 71: # endif
! 72: #endif
! 73:
! 74: #include <sys/types.h>
! 75:
! 76: #if HAVE_NETINET_IN_H
! 77: #include <netinet/in.h>
! 78: #endif
! 79:
! 80: #if HAVE_WINSOCK_H
! 81: #include <winsock.h>
! 82: #endif
! 83: #if HAVE_SYS_SOCKET_H
! 84: #include <sys/socket.h>
! 85: #endif
! 86: #if HAVE_NET_IF_H
! 87: #include <net/if.h>
! 88: #endif
! 89:
! 90: #if HAVE_SYS_SOCKIO_H
! 91: #include <sys/sockio.h>
! 92: #endif
! 93:
! 94: #if HAVE_SYS_IOCTL_H
! 95: #include <sys/ioctl.h>
! 96: #endif
! 97:
! 98: #ifdef HAVE_NLIST_H
! 99: #include <nlist.h>
! 100: #endif
! 101:
! 102: #if HAVE_SYS_FILE_H
! 103: #include <sys/file.h>
! 104: #endif
! 105:
! 106: #if HAVE_KSTAT_H
! 107: #include <kstat.h>
! 108: #endif
! 109:
! 110: #if HAVE_SYS_PARAM_H
! 111: #include <sys/param.h>
! 112: #endif
! 113: #if HAVE_SYS_SYSCTL_H
! 114: #include <sys/sysctl.h>
! 115: #endif
! 116:
! 117: #if HAVE_STRING_H
! 118: #include <string.h>
! 119: #else
! 120: #include <strings.h>
! 121: #endif
! 122:
! 123: #if HAVE_DMALLOC_H
! 124: #include <dmalloc.h>
! 125: #endif
! 126:
! 127: #ifdef HAVE_SYS_STAT_H
! 128: #include <sys/stat.h>
! 129: #endif
! 130: #if HAVE_FCNTL_H
! 131: #include <fcntl.h>
! 132: #endif
! 133:
! 134: #if defined(hpux10) || defined(hpux11)
! 135: #include <sys/pstat.h>
! 136: #endif
! 137:
! 138: #if HAVE_SYS_UTSNAME_H
! 139: #include <sys/utsname.h>
! 140: #endif
! 141:
! 142: #if HAVE_SYS_SYSTEMCFG_H
! 143: #include <sys/systemcfg.h>
! 144: #endif
! 145:
! 146: #if HAVE_SYS_SYSTEMINFO_H
! 147: #include <sys/systeminfo.h>
! 148: #endif
! 149:
! 150: #include <net-snmp/types.h>
! 151: #include <net-snmp/output_api.h>
! 152: #include <net-snmp/utilities.h>
! 153: #include <net-snmp/library/system.h> /* for "internal" definitions */
! 154:
! 155: #include <net-snmp/library/snmp_api.h>
! 156: #include <net-snmp/library/read_config.h> /* for get_temp_file_pattern() */
! 157:
! 158: #ifndef IFF_LOOPBACK
! 159: # define IFF_LOOPBACK 0
! 160: #endif
! 161:
! 162: #ifdef INADDR_LOOPBACK
! 163: # define LOOPBACK INADDR_LOOPBACK
! 164: #else
! 165: # define LOOPBACK 0x7f000001
! 166: #endif
! 167:
! 168: /**
! 169: * fork current process into the background.
! 170: *
! 171: * This function forks a process into the background, in order to
! 172: * become a daemon process. It does a few things along the way:
! 173: *
! 174: * - becoming a process/session group leader, and forking a second time so
! 175: * that process/session group leader can exit.
! 176: *
! 177: * - changing the working directory to /
! 178: *
! 179: * - closing stdin, stdout and stderr (unless stderr_log is set) and
! 180: * redirecting them to /dev/null
! 181: *
! 182: * @param quit_immediately : indicates if the parent process should
! 183: * exit after a successful fork.
! 184: * @param stderr_log : indicates if stderr is being used for
! 185: * logging and shouldn't be closed
! 186: * @returns -1 : fork error
! 187: * 0 : child process returning
! 188: * >0 : parent process returning. returned value is the child PID.
! 189: */
! 190: int
! 191: netsnmp_daemonize(int quit_immediately, int stderr_log)
! 192: {
! 193: int i = 0;
! 194: DEBUGMSGT(("daemonize","deamonizing...\n"));
! 195: #if HAVE_WORKING_FORK
! 196: /*
! 197: * Fork to return control to the invoking process and to
! 198: * guarantee that we aren't a process group leader.
! 199: */
! 200: i = fork();
! 201: if (i != 0) {
! 202: /* Parent. */
! 203: DEBUGMSGT(("daemonize","first fork returned %d.\n", i));
! 204: if(i == -1) {
! 205: snmp_log(LOG_ERR,"first fork failed (errno %d) in "
! 206: "netsnmp_daemonize()\n", errno);
! 207: return -1;
! 208: }
! 209: if (quit_immediately) {
! 210: DEBUGMSGT(("daemonize","parent exiting\n"));
! 211: exit(0);
! 212: }
! 213: } else {
! 214: /* Child. */
! 215: #ifdef HAVE_SETSID
! 216: /* Become a process/session group leader. */
! 217: setsid();
! 218: #endif
! 219: /*
! 220: * Fork to let the process/session group leader exit.
! 221: */
! 222: if ((i = fork()) != 0) {
! 223: DEBUGMSGT(("daemonize","second fork returned %d.\n", i));
! 224: if(i == -1) {
! 225: snmp_log(LOG_ERR,"second fork failed (errno %d) in "
! 226: "netsnmp_daemonize()\n", errno);
! 227: }
! 228: /* Parent. */
! 229: exit(0);
! 230: }
! 231: #ifndef WIN32
! 232: else {
! 233: /* Child. */
! 234:
! 235: DEBUGMSGT(("daemonize","child continuing\n"));
! 236:
! 237: /* Avoid keeping any directory in use. */
! 238: chdir("/");
! 239:
! 240: if (!stderr_log) {
! 241: /*
! 242: * Close inherited file descriptors to avoid
! 243: * keeping unnecessary references.
! 244: */
! 245: close(0);
! 246: close(1);
! 247: close(2);
! 248:
! 249: /*
! 250: * Redirect std{in,out,err} to /dev/null, just in
! 251: * case.
! 252: */
! 253: open("/dev/null", O_RDWR);
! 254: dup(0);
! 255: dup(0);
! 256: }
! 257: }
! 258: #endif /* !WIN32 */
! 259: }
! 260: #endif /* HAVE_WORKING_FORK */
! 261: return i;
! 262: }
! 263:
! 264: #else /* !NEED_NETSNMP_DAEMONIZE */
! 265: int netsnp_daemonize_bs;
! 266: #endif
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>