blob: cfc7bce65235c12ecd10e32634ad8c086fb4dd40 [file] [log] [blame]
Wes Hardakerc0485231994-10-13 20:55:30 +00001/*
Wes Hardaker36e94be2001-11-22 02:56:52 +00002 * snmpd.c
Wes Hardakerc0485231994-10-13 20:55:30 +00003 */
Wes Hardaker7bc5c4e2003-01-14 13:56:18 +00004/** @defgroup agent The Net-SNMP agent
Wes Hardaker36e94be2001-11-22 02:56:52 +00005 * The snmp agent responds to SNMP queries from management stations
6 */
Wes Hardaker56f8a5f2003-08-10 18:38:10 +00007/* Portions of this file are subject to the following copyrights. See
8 * the Net-SNMP's COPYING file for more details and other copyrights
9 * that may apply:
10 */
Wes Hardaker36e94be2001-11-22 02:56:52 +000011/*
Wes Hardaker6d1f6022002-04-20 07:08:20 +000012 * Copyright 1988, 1989 by Carnegie Mellon University
13 *
14 * All Rights Reserved
15 *
16 * Permission to use, copy, modify, and distribute this software and its
17 * documentation for any purpose and without fee is hereby granted,
18 * provided that the above copyright notice appear in all copies and that
19 * both that copyright notice and this permission notice appear in
20 * supporting documentation, and that the name of CMU not be
21 * used in advertising or publicity pertaining to distribution of the
22 * software without specific, written prior permission.
23 *
24 * CMU DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
25 * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
26 * CMU BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
27 * ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
28 * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
29 * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
30 * SOFTWARE.
31 * *****************************************************************
32 */
Wes Hardakera36188e2003-07-02 17:58:55 +000033/*
34 * Copyright © 2003 Sun Microsystems, Inc. All rights reserved.
35 * Use is subject to license terms specified in the COPYING file
36 * distributed with the Net-SNMP package.
37 */
Wes Hardakereb6d4fc2002-01-04 21:00:48 +000038#include <net-snmp/net-snmp-config.h>
Wes Hardaker3b14c592011-02-23 00:01:18 +000039#include <net-snmp/net-snmp-features.h>
Bart Van Asschef13b7102010-01-23 15:07:35 +000040#include <net-snmp/types.h>
Wes Hardakerf86050d1995-09-29 20:44:13 +000041
Michael Slifcakdcad2c62004-01-27 16:54:25 +000042#if HAVE_IO_H
43#include <io.h>
44#endif
Wes Hardaker238b7ba1997-01-20 14:09:25 +000045#include <stdio.h>
46#include <errno.h>
Wes Hardakera1a822e1998-03-26 15:22:30 +000047#if HAVE_STRING_H
Wes Hardaker238b7ba1997-01-20 14:09:25 +000048#include <string.h>
Wes Hardakera1a822e1998-03-26 15:22:30 +000049#else
50#include <strings.h>
Wes Hardaker238b7ba1997-01-20 14:09:25 +000051#endif
52#if HAVE_STDLIB_H
53#include <stdlib.h>
54#endif
55#if HAVE_UNISTD_H
56#include <unistd.h>
57#endif
Wes Hardakerc0485231994-10-13 20:55:30 +000058#include <sys/types.h>
Wes Hardakerf86050d1995-09-29 20:44:13 +000059#if HAVE_NETINET_IN_H
Wes Hardakerc0485231994-10-13 20:55:30 +000060#include <netinet/in.h>
Wes Hardakerf86050d1995-09-29 20:44:13 +000061#endif
Wes Hardaker238b7ba1997-01-20 14:09:25 +000062#if HAVE_ARPA_INET_H
63#include <arpa/inet.h>
64#endif
Wes Hardakerf86050d1995-09-29 20:44:13 +000065#if TIME_WITH_SYS_TIME
Bart Van Assche337360e2010-01-24 11:41:03 +000066# include <sys/time.h>
Wes Hardakerf86050d1995-09-29 20:44:13 +000067# include <time.h>
68#else
69# if HAVE_SYS_TIME_H
70# include <sys/time.h>
71# else
72# include <time.h>
73# endif
74#endif
Wes Hardaker7a2cb581997-08-14 14:38:17 +000075#if HAVE_SYS_SELECT_H
76#include <sys/select.h>
77#endif
Michael Slifcakfd869ca1999-07-30 11:00:28 +000078#if HAVE_SYS_SOCKET_H
Wes Hardakerc0485231994-10-13 20:55:30 +000079#include <sys/socket.h>
Michael Slifcakfd869ca1999-07-30 11:00:28 +000080#endif
81#if HAVE_NET_IF_H
Wes Hardakerc0485231994-10-13 20:55:30 +000082#include <net/if.h>
Michael Slifcakfd869ca1999-07-30 11:00:28 +000083#endif
Wes Hardaker92ba1631999-07-23 17:44:18 +000084#if HAVE_INET_MIB2_H
85#include <inet/mib2.h>
86#endif
Wes Hardakerf86050d1995-09-29 20:44:13 +000087#if HAVE_SYS_IOCTL_H
Wes Hardakerc0485231994-10-13 20:55:30 +000088#include <sys/ioctl.h>
Wes Hardakerf86050d1995-09-29 20:44:13 +000089#endif
90#if HAVE_SYS_FILE_H
Wes Hardakerc0485231994-10-13 20:55:30 +000091#include <sys/file.h>
Wes Hardakerf86050d1995-09-29 20:44:13 +000092#endif
Wes Hardaker1dfa53c1996-05-01 19:37:35 +000093#ifdef HAVE_FCNTL_H
94#include <fcntl.h>
95#endif
Michael Slifcak1b78b761999-11-19 01:58:47 +000096#if HAVE_SYS_WAIT_H
Wes Hardaker238b7ba1997-01-20 14:09:25 +000097#include <sys/wait.h>
Michael Slifcak1b78b761999-11-19 01:58:47 +000098#endif
Wes Hardakera5b59551998-01-16 17:44:56 +000099#include <signal.h>
Wes Hardaker238b7ba1997-01-20 14:09:25 +0000100#ifdef HAVE_SYS_PARAM_H
101#include <sys/param.h>
102#endif
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000103#if HAVE_PROCESS_H /* Win32-getpid */
Wes Hardakerd7d053b2001-03-15 15:46:12 +0000104#include <process.h>
105#endif
John Naylon04fda2e2001-08-06 11:29:49 +0000106#if HAVE_LIMITS_H
107#include <limits.h>
108#endif
Wes Hardaker9743e9f2001-10-11 21:01:50 +0000109#if HAVE_PWD_H
110#include <pwd.h>
111#endif
Wes Hardaker15ddba02001-12-05 16:28:59 +0000112#if HAVE_GRP_H
113#include <grp.h>
114#endif
Bart Van Assche13b46d02010-10-28 08:44:08 +0000115#ifdef HAVE_CRTDBG_H
116#include <crtdbg.h>
117#endif
John Naylon04fda2e2001-08-06 11:29:49 +0000118
119#ifndef PATH_MAX
120# ifdef _POSIX_PATH_MAX
121# define PATH_MAX _POSIX_PATH_MAX
122# else
123# define PATH_MAX 255
124# endif
125#endif
Wes Hardakerc8c147b1999-12-08 23:29:52 +0000126
127#ifndef FD_SET
Wes Hardaker238b7ba1997-01-20 14:09:25 +0000128typedef long fd_mask;
129#define NFDBITS (sizeof(fd_mask) * NBBY) /* bits per mask */
130#define FD_SET(n, p) ((p)->fds_bits[(n)/NFDBITS] |= (1 << ((n) % NFDBITS)))
131#define FD_CLR(n, p) ((p)->fds_bits[(n)/NFDBITS] &= ~(1 << ((n) % NFDBITS)))
132#define FD_ISSET(n, p) ((p)->fds_bits[(n)/NFDBITS] & (1 << ((n) % NFDBITS)))
133#define FD_ZERO(p) memset((p), 0, sizeof(*(p)))
Wes Hardaker268218c1995-10-13 22:45:28 +0000134#endif
Wes Hardakerc0485231994-10-13 20:55:30 +0000135
Dave Shield1f17c992002-02-08 15:45:13 +0000136#include <net-snmp/net-snmp-includes.h>
137#include <net-snmp/agent/net-snmp-agent-includes.h>
138
Dave Shield2decc2f2005-04-29 16:12:33 +0000139#include <net-snmp/library/fd_event_manager.h>
Dave Shield23d9c652009-04-21 15:33:42 +0000140#include <net-snmp/library/large_fd_set.h>
Dave Shield2decc2f2005-04-29 16:12:33 +0000141
Wes Hardakerc0485231994-10-13 20:55:30 +0000142#include "m2m.h"
Magnus Fromreide3f2e7882011-02-02 07:09:27 +0000143#include <net-snmp/agent/agent_module_config.h>
Wes Hardaker91033072002-02-07 21:33:23 +0000144#include <net-snmp/agent/mib_module_config.h>
Wes Hardaker8c6ab571998-06-10 15:53:53 +0000145
Wes Hardaker522909f1997-03-05 14:06:41 +0000146#include "snmpd.h"
Wes Hardakerc0485231994-10-13 20:55:30 +0000147
Magnus Fromreide1f40adb2011-02-01 07:19:58 +0000148#include <net-snmp/agent/mib_modules.h>
Wes Hardaker87bed831999-04-06 22:13:53 +0000149
Wes Hardaker40342b72002-01-14 16:05:52 +0000150#include <net-snmp/agent/agent_trap.h>
Wes Hardaker87bed831999-04-06 22:13:53 +0000151
Wes Hardaker7a649e82002-01-11 22:12:21 +0000152#include <net-snmp/agent/table.h>
153#include <net-snmp/agent/table_iterator.h>
Wes Hardaker87bed831999-04-06 22:13:53 +0000154
155/*
Dave Shield8b2adde2002-02-15 15:28:29 +0000156 * Include winservice.h to support Windows Service
157 */
158#ifdef WIN32
159#include <windows.h>
160#include <tchar.h>
161#include <net-snmp/library/winservice.h>
Michael Slifcak78d401c2004-02-27 12:50:16 +0000162
Michael Slifcak0ef01642004-02-09 12:06:24 +0000163#define WIN32SERVICE
Michael Slifcak78d401c2004-02-27 12:50:16 +0000164
Dave Shield8b2adde2002-02-15 15:28:29 +0000165#endif
166
Wes Hardaker3b14c592011-02-23 00:01:18 +0000167netsnmp_feature_want(logging_file)
168netsnmp_feature_want(logging_stdio)
169netsnmp_feature_want(logging_syslog)
170
Dave Shield8b2adde2002-02-15 15:28:29 +0000171/*
Wes Hardaker87bed831999-04-06 22:13:53 +0000172 * Globals.
173 */
Thomas Anderse7563bf2006-09-15 00:48:50 +0000174#ifdef NETSNMP_USE_LIBWRAP
Wes Hardaker5f1ce641998-03-28 00:10:05 +0000175#include <tcpd.h>
Thomas Anderse7563bf2006-09-15 00:48:50 +0000176#endif /* NETSNMP_USE_LIBWRAP */
Wes Hardaker5f1ce641998-03-28 00:10:05 +0000177
Wes Hardaker87bed831999-04-06 22:13:53 +0000178#define TIMETICK 500000L
Wes Hardaker87bed831999-04-06 22:13:53 +0000179
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000180int snmp_dump_packet;
Magnus Fromreide5a4072f2009-04-04 10:07:39 +0000181static int reconfig = 0;
Dave Shieldf542e4a2002-12-12 16:37:26 +0000182int Facility = LOG_DAEMON;
Wes Hardaker87bed831999-04-06 22:13:53 +0000183
Michael Slifcak0ef01642004-02-09 12:06:24 +0000184#ifdef WIN32SERVICE
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000185/*
186 * SNMP Agent Status
187 */
Dave Shield8b2adde2002-02-15 15:28:29 +0000188#define AGENT_RUNNING 1
189#define AGENT_STOPPED 0
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000190int agent_status = AGENT_STOPPED;
Alex Burger327de412005-11-10 02:33:17 +0000191/* app_name_long used for Event Log (syslog), SCM, registry etc */
Bart Van Asscheb6d21352010-03-20 19:36:28 +0000192LPCTSTR app_name_long = _T("Net-SNMP Agent"); /* Application Name */
Dave Shield8b2adde2002-02-15 15:28:29 +0000193#endif
Wes Hardakerc0485231994-10-13 20:55:30 +0000194
Alex Burger327de412005-11-10 02:33:17 +0000195const char *app_name = "snmpd";
196
Robert Story7627ec72005-11-15 16:37:58 +0000197extern int netsnmp_running;
Magnus Fromreideadff1602010-10-16 22:32:38 +0000198#ifdef USING_UTIL_FUNCS_RESTART_MODULE
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000199extern char **argvrestartp;
200extern char *argvrestart;
201extern char *argvrestartname;
Magnus Fromreideadff1602010-10-16 22:32:38 +0000202#endif /* USING_UTIL_FUNCS_RESTART_MODULE */
Wes Hardaker87bed831999-04-06 22:13:53 +0000203
Dave Shieldbe290001999-12-13 12:43:14 +0000204#ifdef USING_SMUX_MODULE
Niels Baggesen08b58392005-03-15 06:14:06 +0000205#include <mibgroup/smux/smux.h>
Dave Shieldc3fb5c82007-03-22 19:12:22 +0000206#endif /* USING_SMUX_MODULE */
Wes Hardaker87bed831999-04-06 22:13:53 +0000207
208/*
209 * Prototypes.
210 */
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000211int snmp_read_packet(int);
212int snmp_input(int, netsnmp_session *, int, netsnmp_pdu *,
213 void *);
214static void usage(char *);
215static void SnmpTrapNodeDown(void);
216static int receive(void);
Michael Slifcak0ef01642004-02-09 12:06:24 +0000217#ifdef WIN32SERVICE
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000218void StopSnmpAgent(void);
219int SnmpDaemonMain(int argc, TCHAR * argv[]);
220int __cdecl _tmain(int argc, TCHAR * argv[]);
Dave Shield8b2adde2002-02-15 15:28:29 +0000221#else
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000222int main(int, char **);
Dave Shield8b2adde2002-02-15 15:28:29 +0000223#endif
Wes Hardaker40b2bf11999-01-18 18:47:36 +0000224
Dave Shieldf542e4a2002-12-12 16:37:26 +0000225/*
226 * These definitions handle 4.2 systems without additional syslog facilities.
227 */
228#ifndef LOG_CONS
229#define LOG_CONS 0 /* Don't bother if not defined... */
230#endif
231#ifndef LOG_PID
232#define LOG_PID 0 /* Don't bother if not defined... */
233#endif
234#ifndef LOG_LOCAL0
235#define LOG_LOCAL0 0
236#endif
237#ifndef LOG_LOCAL1
238#define LOG_LOCAL1 0
239#endif
240#ifndef LOG_LOCAL2
241#define LOG_LOCAL2 0
242#endif
243#ifndef LOG_LOCAL3
244#define LOG_LOCAL3 0
245#endif
246#ifndef LOG_LOCAL4
247#define LOG_LOCAL4 0
248#endif
249#ifndef LOG_LOCAL5
250#define LOG_LOCAL5 0
251#endif
252#ifndef LOG_LOCAL6
253#define LOG_LOCAL6 0
254#endif
255#ifndef LOG_LOCAL7
256#define LOG_LOCAL7 0
257#endif
258#ifndef LOG_DAEMON
259#define LOG_DAEMON 0
260#endif
261
262
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000263static void
264usage(char *prog)
Wes Hardakeracee5211995-10-06 23:32:04 +0000265{
Michael Slifcak0ef01642004-02-09 12:06:24 +0000266#ifdef WIN32SERVICE
Magnus Fromreide05ea7cd2009-11-12 08:26:06 +0000267 printf("\nUsage: %s [-register] [-quiet] [OPTIONS] [LISTENING ADDRESSES]"
268 "\n %s [-unregister] [-quiet]", prog, prog);
Dave Shield8b2adde2002-02-15 15:28:29 +0000269#else
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000270 printf("\nUsage: %s [OPTIONS] [LISTENING ADDRESSES]", prog);
Dave Shield8b2adde2002-02-15 15:28:29 +0000271#endif
Magnus Fromreide05ea7cd2009-11-12 08:26:06 +0000272 printf("\n"
Dave Shieldcefe9302010-06-14 15:51:14 +0000273 "\n\tVersion: %s\n%s"
274 "\t\t\t (config search path: %s)\n%s%s",
275 netsnmp_get_version(),
Magnus Fromreide05ea7cd2009-11-12 08:26:06 +0000276 "\tWeb: http://www.net-snmp.org/\n"
277 "\tEmail: net-snmp-coders@lists.sourceforge.net\n"
278 "\n -a\t\t\tlog addresses\n"
279 " -A\t\t\tappend to the logfile rather than truncating it\n"
280 " -c FILE[,...]\t\tread FILE(s) as configuration file(s)\n"
Dave Shieldcefe9302010-06-14 15:51:14 +0000281 " -C\t\t\tdo not read the default configuration files\n",
282 get_configuration_directory(),
Magnus Fromreide05ea7cd2009-11-12 08:26:06 +0000283 " -d\t\t\tdump sent and received SNMP packets\n"
Niels Baggesen131c93b2014-05-06 06:20:28 +0200284#ifndef NETSNMP_DISABLE_DEBUGGING
Jan Safraneke6fb38b2010-07-20 07:32:59 +0000285 " -D[TOKEN[,...]]\tturn on debugging output for the given TOKEN(s)\n"
Wes Hardakerf5b1d0f2008-02-15 04:39:29 +0000286 "\t\t\t (try ALL for extremely verbose output)\n"
Magnus Fromreide05ea7cd2009-11-12 08:26:06 +0000287 "\t\t\t Don't put space(s) between -D and TOKEN(s).\n"
Niels Baggesen131c93b2014-05-06 06:20:28 +0200288#endif
Dave Shieldcefe9302010-06-14 15:51:14 +0000289 " -f\t\t\tdo not fork from the shell\n",
John Naylon1542e432001-12-19 12:38:20 +0000290#if HAVE_UNISTD_H
Magnus Fromreide05ea7cd2009-11-12 08:26:06 +0000291 " -g GID\t\tchange to this numeric gid after opening\n"
292 "\t\t\t transport endpoints\n"
John Naylon1542e432001-12-19 12:38:20 +0000293#endif
Magnus Fromreide05ea7cd2009-11-12 08:26:06 +0000294 " -h, --help\t\tdisplay this usage message\n"
295 " -H\t\t\tdisplay configuration file directives understood\n"
296 " -I [-]INITLIST\tlist of mib modules to initialize (or not)\n"
297 "\t\t\t (run snmpd with -Dmib_init for a list)\n"
Dave Shieldcefe9302010-06-14 15:51:14 +0000298 " -L <LOGOPTS>\t\ttoggle options controlling where to log to\n");
Dave Shielde7551852003-10-24 12:49:22 +0000299 snmp_log_options_usage("\t", stdout);
Magnus Fromreide05ea7cd2009-11-12 08:26:06 +0000300 printf(" -m MIBLIST\t\tuse MIBLIST instead of the default MIB list\n"
Dave Shieldc2de2f22010-03-19 22:56:34 +0000301 " -M DIRLIST\t\tuse DIRLIST as the list of locations to look for MIBs\n"
Dave Shieldcefe9302010-06-14 15:51:14 +0000302 "\t\t\t (default %s)\n%s%s",
303#ifndef NETSNMP_DISABLE_MIB_LOADING
304 netsnmp_get_mib_directory(),
305#else
306 "MIBs not loaded",
307#endif
Magnus Fromreide05ea7cd2009-11-12 08:26:06 +0000308 " -p FILE\t\tstore process id in FILE\n"
309 " -q\t\t\tprint information in a more parsable format\n"
310 " -r\t\t\tdo not exit if files only accessible to root\n"
311 "\t\t\t cannot be opened\n"
Michael Slifcak0ef01642004-02-09 12:06:24 +0000312#ifdef WIN32SERVICE
Magnus Fromreide05ea7cd2009-11-12 08:26:06 +0000313 " -register\t\tregister as a Windows service\n"
314 " \t\t\t (followed by -quiet to prevent message popups)\n"
315 " \t\t\t (followed by the startup parameter list)\n"
316 " \t\t\t Note that some parameters are not relevant when running as a service\n"
Dave Shield8b2adde2002-02-15 15:28:29 +0000317#endif
John Naylon1542e432001-12-19 12:38:20 +0000318#if HAVE_UNISTD_H
Magnus Fromreide05ea7cd2009-11-12 08:26:06 +0000319 " -u UID\t\tchange to this uid (numeric or textual) after\n"
320 "\t\t\t opening transport endpoints\n"
John Naylon1542e432001-12-19 12:38:20 +0000321#endif
Michael Slifcak0ef01642004-02-09 12:06:24 +0000322#ifdef WIN32SERVICE
Magnus Fromreide05ea7cd2009-11-12 08:26:06 +0000323 " -unregister\t\tunregister as a Windows service\n"
324 " \t\t\t (followed -quiet to prevent message popups)\n"
Dave Shield8b2adde2002-02-15 15:28:29 +0000325#endif
Magnus Fromreide05ea7cd2009-11-12 08:26:06 +0000326 " -v, --version\t\tdisplay version information\n"
327 " -V\t\t\tverbose display\n"
John Naylon1542e432001-12-19 12:38:20 +0000328#if defined(USING_AGENTX_SUBAGENT_MODULE)|| defined(USING_AGENTX_MASTER_MODULE)
Magnus Fromreide05ea7cd2009-11-12 08:26:06 +0000329 " -x ADDRESS\t\tuse ADDRESS as AgentX address\n"
Wes Hardakerabf3f572000-10-26 00:44:22 +0000330#endif
331#ifdef USING_AGENTX_SUBAGENT_MODULE
Magnus Fromreide05ea7cd2009-11-12 08:26:06 +0000332 " -X\t\t\trun as an AgentX subagent rather than as an\n"
333 "\t\t\t SNMP master agent\n"
Wes Hardakeracee5211995-10-06 23:32:04 +0000334#endif
Dave Shieldcefe9302010-06-14 15:51:14 +0000335 ,
Magnus Fromreide05ea7cd2009-11-12 08:26:06 +0000336 "\nDeprecated options:\n"
337 " -l FILE\t\tuse -Lf <FILE> instead\n"
338 " -P\t\t\tuse -p instead\n"
339 " -s\t\t\tuse -Lsd instead\n"
340 " -S d|i|0-7\t\tuse -Ls <facility> instead\n"
Dave Shieldcefe9302010-06-14 15:51:14 +0000341 "\n"
Dave Shield0b34cf42010-03-22 14:36:59 +0000342 );
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000343 exit(1);
Wes Hardakeracee5211995-10-06 23:32:04 +0000344}
345
John Naylon1542e432001-12-19 12:38:20 +0000346static void
347version(void)
348{
Magnus Fromreide05ea7cd2009-11-12 08:26:06 +0000349 printf("\nNET-SNMP version: %s\n"
350 "Web: http://www.net-snmp.org/\n"
351 "Email: net-snmp-coders@lists.sourceforge.net\n\n",
352 netsnmp_get_version());
John Naylon1542e432001-12-19 12:38:20 +0000353 exit(0);
354}
355
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000356RETSIGTYPE
Wes Hardaker40b2bf11999-01-18 18:47:36 +0000357SnmpdShutDown(int a)
Wes Hardakercaf84271998-09-03 01:50:34 +0000358{
Michael Slifcak0ef01642004-02-09 12:06:24 +0000359#ifdef WIN32SERVICE
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000360 extern netsnmp_session *main_session;
Robert Storydde1ade2002-04-27 02:14:57 +0000361#endif
Robert Story7627ec72005-11-15 16:37:58 +0000362 netsnmp_running = 0;
Michael Slifcak0ef01642004-02-09 12:06:24 +0000363#ifdef WIN32SERVICE
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000364 /*
365 * In case of windows, select() in receive() function will not return
366 * on signal. Thats why following function is called, which closes the
367 * socket descriptors and causes the select() to return
368 */
369 snmp_close(main_session);
Dave Shield8b2adde2002-02-15 15:28:29 +0000370#endif
Wes Hardakercaf84271998-09-03 01:50:34 +0000371}
372
Niels Baggesenc2be2681999-08-18 09:51:57 +0000373#ifdef SIGHUP
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000374RETSIGTYPE
Niels Baggesenc2be2681999-08-18 09:51:57 +0000375SnmpdReconfig(int a)
376{
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000377 reconfig = 1;
378 signal(SIGHUP, SnmpdReconfig);
Niels Baggesenc2be2681999-08-18 09:51:57 +0000379}
380#endif
381
Dave Shield63142221999-09-21 16:32:27 +0000382#ifdef SIGUSR1
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000383extern void dump_registry(void);
384RETSIGTYPE
Dave Shield63142221999-09-21 16:32:27 +0000385SnmpdDump(int a)
386{
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000387 dump_registry();
388 signal(SIGUSR1, SnmpdDump);
Dave Shield63142221999-09-21 16:32:27 +0000389}
390#endif
391
Michael Slifcak6790e992004-03-02 12:18:16 +0000392RETSIGTYPE
393SnmpdCatchRandomSignal(int a)
394{
395 /* Disable all logs and log the error via syslog */
396 snmp_disable_log();
Wes Hardaker3b14c592011-02-23 00:01:18 +0000397#ifndef NETSNMP_FEATURE_REMOVE_LOGGING_SYSLOG
Michael Slifcak6790e992004-03-02 12:18:16 +0000398 snmp_enable_syslog();
Wes Hardaker3b14c592011-02-23 00:01:18 +0000399#endif /* NETSNMP_FEATURE_REMOVE_LOGGING_SYSLOG */
Michael Slifcak6790e992004-03-02 12:18:16 +0000400 snmp_log(LOG_ERR, "Exiting on signal %d\n", a);
Wes Hardaker3b14c592011-02-23 00:01:18 +0000401#ifndef NETSNMP_FEATURE_REMOVE_LOGGING_SYSLOG
Michael Slifcak6790e992004-03-02 12:18:16 +0000402 snmp_disable_syslog();
Wes Hardaker3b14c592011-02-23 00:01:18 +0000403#endif /* NETSNMP_FEATURE_REMOVE_LOGGING_SYSLOG */
Michael Slifcak6790e992004-03-02 12:18:16 +0000404 exit(1);
405}
Dave Shield63142221999-09-21 16:32:27 +0000406
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000407static void
Wes Hardakerc2eafd11999-06-10 01:31:42 +0000408SnmpTrapNodeDown(void)
Wes Hardakera5b59551998-01-16 17:44:56 +0000409{
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000410 send_easy_trap(SNMP_TRAP_ENTERPRISESPECIFIC, 2);
411 /*
412 * XXX 2 - Node Down #define it as NODE_DOWN_TRAP
413 */
Dave Shielddd0f2371999-06-18 16:14:58 +0000414}
415
Wes Hardaker87bed831999-04-06 22:13:53 +0000416/*******************************************************************-o-******
Dave Shield8b2adde2002-02-15 15:28:29 +0000417 * main - Non Windows
Michael Slifcak8784cbc2004-09-09 10:22:23 +0000418 * SnmpDaemonMain - Windows to support windows service
Wes Hardaker87bed831999-04-06 22:13:53 +0000419 *
420 * Parameters:
421 * argc
422 * *argv[]
423 *
424 * Returns:
425 * 0 Always succeeds. (?)
426 *
427 *
428 * Setup and start the agent daemon.
429 *
430 * Also successfully EXITs with zero for some options.
431 */
John Naylon1542e432001-12-19 12:38:20 +0000432int
Michael Slifcak0ef01642004-02-09 12:06:24 +0000433#ifdef WIN32SERVICE
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000434SnmpDaemonMain(int argc, TCHAR * argv[])
Dave Shield8b2adde2002-02-15 15:28:29 +0000435#else
Wes Hardaker40b2bf11999-01-18 18:47:36 +0000436main(int argc, char *argv[])
Dave Shield8b2adde2002-02-15 15:28:29 +0000437#endif
Wes Hardakerc0485231994-10-13 20:55:30 +0000438{
Wes Hardaker8b14d732006-09-01 23:17:28 +0000439 char options[128] = "aAc:CdD::fhHI:l:L:m:M:n:p:P:qrsS:UvV-:Y:";
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000440 int arg, i, ret;
Robert Storyd004d1b2005-09-27 15:34:40 +0000441 int dont_fork = 0, do_help = 0;
Robert Story69b77f42005-11-20 19:39:13 +0000442 int log_set = 0;
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000443 int agent_mode = -1;
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000444 char *pid_file = NULL;
Niels Baggesen59d5f7a2003-11-02 12:50:39 +0000445 char option_compatability[] = "-Le";
Michael Slifcak1b78b761999-11-19 01:58:47 +0000446#if HAVE_GETPID
Wes Hardakera36188e2003-07-02 17:58:55 +0000447 int fd;
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000448 FILE *PID;
Michael Slifcak1b78b761999-11-19 01:58:47 +0000449#endif
Wes Hardakeraebff771995-07-10 18:27:00 +0000450
Alex Burgerd3dd2a52005-08-01 11:39:44 +0000451#ifndef WIN32
Robert Story7bf3d202005-07-07 16:25:39 +0000452 /*
453 * close all non-standard file descriptors we may have
454 * inherited from the shell.
455 */
456 for (i = getdtablesize() - 1; i > 2; --i) {
457 (void) close(i);
458 }
Robert Storya34441b2005-08-18 21:50:41 +0000459#endif /* #WIN32 */
Robert Story7bf3d202005-07-07 16:25:39 +0000460
461 /*
462 * register signals ASAP to prevent default action (usually core)
463 * for signals during startup...
464 */
465#ifdef SIGTERM
466 DEBUGMSGTL(("signal", "registering SIGTERM signal handler\n"));
467 signal(SIGTERM, SnmpdShutDown);
468#endif
469#ifdef SIGINT
470 DEBUGMSGTL(("signal", "registering SIGINT signal handler\n"));
471 signal(SIGINT, SnmpdShutDown);
472#endif
473#ifdef SIGHUP
Wes Hardaker5e8c26a2007-05-17 13:53:50 +0000474 signal(SIGHUP, SIG_IGN); /* do not terminate on early SIGHUP */
Robert Story7bf3d202005-07-07 16:25:39 +0000475#endif
476#ifdef SIGUSR1
477 DEBUGMSGTL(("signal", "registering SIGUSR1 signal handler\n"));
478 signal(SIGUSR1, SnmpdDump);
479#endif
480#ifdef SIGPIPE
481 DEBUGMSGTL(("signal", "registering SIGPIPE signal handler\n"));
482 signal(SIGPIPE, SIG_IGN); /* 'Inline' failure of wayward readers */
483#endif
484#ifdef SIGXFSZ
485 signal(SIGXFSZ, SnmpdCatchRandomSignal);
486#endif
487
Thomas Anderse7563bf2006-09-15 00:48:50 +0000488#ifdef NETSNMP_NO_ROOT_ACCESS
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000489 /*
490 * Default to no.
491 */
John Naylonbb401c12002-05-15 12:53:01 +0000492 netsnmp_ds_set_boolean(NETSNMP_DS_APPLICATION_ID,
493 NETSNMP_DS_AGENT_NO_ROOT_ACCESS, 1);
Wes Hardaker45aeab11999-11-17 19:55:41 +0000494#endif
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000495 /*
496 * Default to NOT running an AgentX master.
497 */
John Naylonbb401c12002-05-15 12:53:01 +0000498 netsnmp_ds_set_boolean(NETSNMP_DS_APPLICATION_ID,
499 NETSNMP_DS_AGENT_AGENTX_MASTER, 0);
Robert Story87cc9fa2002-07-19 21:14:33 +0000500 netsnmp_ds_set_int(NETSNMP_DS_APPLICATION_ID,
501 NETSNMP_DS_AGENT_AGENTX_TIMEOUT, -1);
502 netsnmp_ds_set_int(NETSNMP_DS_APPLICATION_ID,
503 NETSNMP_DS_AGENT_AGENTX_RETRIES, -1);
Wes Hardaker87bed831999-04-06 22:13:53 +0000504
Robert Story19617e32004-03-02 04:52:45 +0000505 netsnmp_ds_set_int(NETSNMP_DS_APPLICATION_ID,
506 NETSNMP_DS_AGENT_CACHE_TIMEOUT, 5);
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000507 /*
508 * Add some options if they are available.
509 */
John Naylon1542e432001-12-19 12:38:20 +0000510#if HAVE_UNISTD_H
511 strcat(options, "g:u:");
512#endif
513#if defined(USING_AGENTX_SUBAGENT_MODULE)|| defined(USING_AGENTX_MASTER_MODULE)
514 strcat(options, "x:");
515#endif
516#ifdef USING_AGENTX_SUBAGENT_MODULE
517 strcat(options, "X");
Wes Hardakerabf3f572000-10-26 00:44:22 +0000518#endif
519
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000520 /*
Dave Shieldea810232003-07-09 12:25:18 +0000521 * This is incredibly ugly, but it's probably the simplest way
522 * to handle the old '-L' option as well as the new '-Lx' style
523 */
524 for (i=0; i<argc; i++) {
525 if (!strcmp(argv[i], "-L"))
526 argv[i] = option_compatability;
527 }
528
Wes Hardaker3b14c592011-02-23 00:01:18 +0000529#ifndef NETSNMP_FEATURE_REMOVE_LOGGING_SYSLOG
Alex Burger327de412005-11-10 02:33:17 +0000530#ifdef WIN32
531 snmp_log_syslogname(app_name_long);
532#else
Alex Burger8233c6d2004-10-04 22:14:34 +0000533 snmp_log_syslogname(app_name);
Alex Burger327de412005-11-10 02:33:17 +0000534#endif
Wes Hardaker3b14c592011-02-23 00:01:18 +0000535#endif /* NETSNMP_FEATURE_REMOVE_LOGGING_SYSLOG */
Robert Storyd004d1b2005-09-27 15:34:40 +0000536 netsnmp_ds_set_string(NETSNMP_DS_LIBRARY_ID,
537 NETSNMP_DS_LIB_APPTYPE, app_name);
Alex Burger2f97d7b2004-09-29 17:35:44 +0000538
Dave Shieldea810232003-07-09 12:25:18 +0000539 /*
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000540 * Now process options normally.
541 */
John Naylon1542e432001-12-19 12:38:20 +0000542 while ((arg = getopt(argc, argv, options)) != EOF) {
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000543 switch (arg) {
Wes Hardaker310607d2002-02-16 08:17:42 +0000544 case '-':
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000545 if (strcasecmp(optarg, "help") == 0) {
546 usage(argv[0]);
547 }
548 if (strcasecmp(optarg, "version") == 0) {
549 version();
550 }
Wes Hardaker310607d2002-02-16 08:17:42 +0000551
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000552 handle_long_opt(optarg);
553 break;
Wes Hardaker310607d2002-02-16 08:17:42 +0000554
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000555 case 'a':
556 log_addresses++;
557 break;
John Naylon1542e432001-12-19 12:38:20 +0000558
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000559 case 'A':
Robert Story69b77f42005-11-20 19:39:13 +0000560 netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID,
561 NETSNMP_DS_LIB_APPEND_LOGFILES, 1);
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000562 break;
John Naylon1542e432001-12-19 12:38:20 +0000563
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000564 case 'c':
565 if (optarg != NULL) {
John Naylonbb401c12002-05-15 12:53:01 +0000566 netsnmp_ds_set_string(NETSNMP_DS_LIBRARY_ID,
567 NETSNMP_DS_LIB_OPTIONALCONFIG, optarg);
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000568 } else {
569 usage(argv[0]);
570 }
571 break;
John Naylon1542e432001-12-19 12:38:20 +0000572
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000573 case 'C':
John Naylonbb401c12002-05-15 12:53:01 +0000574 netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID,
575 NETSNMP_DS_LIB_DONT_READ_CONFIGS, 1);
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000576 break;
John Naylon1542e432001-12-19 12:38:20 +0000577
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000578 case 'd':
Jan Safranek0ef34e62012-03-08 10:11:30 +0100579 netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID,
580 NETSNMP_DS_LIB_DUMP_PACKET,
Wes Hardaker3b14c592011-02-23 00:01:18 +0000581 ++snmp_dump_packet);
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000582 break;
John Naylon1542e432001-12-19 12:38:20 +0000583
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000584 case 'D':
Niels Baggesen131c93b2014-05-06 06:20:28 +0200585#ifdef NETSNMP_DISABLE_DEBUGGING
586 fprintf(stderr, "Debugging not configured\n");
587 exit(1);
588#else
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000589 debug_register_tokens(optarg);
590 snmp_set_do_debugging(1);
Niels Baggesen131c93b2014-05-06 06:20:28 +0200591#endif
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000592 break;
John Naylon1542e432001-12-19 12:38:20 +0000593
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000594 case 'f':
595 dont_fork = 1;
596 break;
John Naylon1542e432001-12-19 12:38:20 +0000597
598#if HAVE_UNISTD_H
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000599 case 'g':
600 if (optarg != NULL) {
Dave Shield968c0dd2008-08-17 20:50:55 +0000601 char *ecp;
602 int gid;
603
604 gid = strtoul(optarg, &ecp, 10);
Bart Van Assche396e1222011-12-01 09:32:16 +0100605#if HAVE_GETGRNAM && HAVE_PWD_H
Dave Shield968c0dd2008-08-17 20:50:55 +0000606 if (*ecp) {
Dave Shield968c0dd2008-08-17 20:50:55 +0000607 struct group *info;
Bart Van Assche396e1222011-12-01 09:32:16 +0100608
Dave Shield968c0dd2008-08-17 20:50:55 +0000609 info = getgrnam(optarg);
Bart Van Assche396e1222011-12-01 09:32:16 +0100610 gid = info ? info->gr_gid : -1;
611 endgrent();
612 }
Dave Shield968c0dd2008-08-17 20:50:55 +0000613#endif
Bart Van Assche396e1222011-12-01 09:32:16 +0100614 if (gid < 0) {
615 fprintf(stderr, "Bad group id: %s\n", optarg);
616 exit(1);
Dave Shield968c0dd2008-08-17 20:50:55 +0000617 }
John Naylonbb401c12002-05-15 12:53:01 +0000618 netsnmp_ds_set_int(NETSNMP_DS_APPLICATION_ID,
Dave Shield968c0dd2008-08-17 20:50:55 +0000619 NETSNMP_DS_AGENT_GROUPID, gid);
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000620 } else {
621 usage(argv[0]);
622 }
623 break;
Wes Hardakerabf3f572000-10-26 00:44:22 +0000624#endif
Dave Shieldc76ef932000-03-21 16:47:40 +0000625
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000626 case 'h':
627 usage(argv[0]);
628 break;
Wes Hardaker45aeab11999-11-17 19:55:41 +0000629
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000630 case 'H':
Robert Storyd004d1b2005-09-27 15:34:40 +0000631 do_help = 1;
632 break;
Wes Hardaker87bed831999-04-06 22:13:53 +0000633
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000634 case 'I':
635 if (optarg != NULL) {
636 add_to_init_list(optarg);
637 } else {
638 usage(argv[0]);
639 }
640 break;
Wes Hardaker87bed831999-04-06 22:13:53 +0000641
Bart Van Assche2e9466d2011-07-26 13:10:04 +0200642#ifndef NETSNMP_FEATURE_REMOVE_LOGGING_FILE
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000643 case 'l':
Dave Shielde7551852003-10-24 12:49:22 +0000644 printf("Warning: -l option is deprecated, use -Lf <file> instead\n");
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000645 if (optarg != NULL) {
646 if (strlen(optarg) > PATH_MAX) {
647 fprintf(stderr,
648 "%s: logfile path too long (limit %d chars)\n",
649 argv[0], PATH_MAX);
650 exit(1);
651 }
Robert Story69b77f42005-11-20 19:39:13 +0000652 snmp_enable_filelog(optarg,
653 netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID,
654 NETSNMP_DS_LIB_APPEND_LOGFILES));
Robert Story60371e82005-06-20 20:38:41 +0000655 log_set = 1;
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000656 } else {
657 usage(argv[0]);
658 }
659 break;
Bart Van Assche2e9466d2011-07-26 13:10:04 +0200660#endif /* NETSNMP_FEATURE_REMOVE_LOGGING_FILE */
Wes Hardaker45aeab11999-11-17 19:55:41 +0000661
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000662 case 'L':
Dave Shield93d8c232003-06-17 12:54:27 +0000663 if (snmp_log_options( optarg, argc, argv ) < 0 ) {
664 usage(argv[0]);
665 }
Robert Story60371e82005-06-20 20:38:41 +0000666 log_set = 1;
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000667 break;
Wes Hardaker45aeab11999-11-17 19:55:41 +0000668
Dave Shielde7551852003-10-24 12:49:22 +0000669 case 'm':
670 if (optarg != NULL) {
671 setenv("MIBS", optarg, 1);
672 } else {
673 usage(argv[0]);
674 }
675 break;
676
677 case 'M':
678 if (optarg != NULL) {
679 setenv("MIBDIRS", optarg, 1);
680 } else {
681 usage(argv[0]);
682 }
683 break;
684
Robert Storyd004d1b2005-09-27 15:34:40 +0000685 case 'n':
686 if (optarg != NULL) {
687 app_name = optarg;
688 netsnmp_ds_set_string(NETSNMP_DS_LIBRARY_ID,
689 NETSNMP_DS_LIB_APPTYPE, app_name);
690 } else {
691 usage(argv[0]);
692 }
693 break;
694
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000695 case 'P':
Dave Shielde7551852003-10-24 12:49:22 +0000696 printf("Warning: -P option is deprecated, use -p instead\n");
697 case 'p':
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000698 if (optarg != NULL) {
699 pid_file = optarg;
700 } else {
701 usage(argv[0]);
702 }
703 break;
Wes Hardakerdc4b3f62000-05-31 22:53:11 +0000704
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000705 case 'q':
Wes Hardaker3b14c592011-02-23 00:01:18 +0000706 netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID,
707 NETSNMP_DS_LIB_QUICK_PRINT, 1);
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000708 break;
709
710 case 'r':
John Naylonbb401c12002-05-15 12:53:01 +0000711 netsnmp_ds_toggle_boolean(NETSNMP_DS_APPLICATION_ID,
712 NETSNMP_DS_AGENT_NO_ROOT_ACCESS);
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000713 break;
714
Wes Hardaker3b14c592011-02-23 00:01:18 +0000715#ifndef NETSNMP_FEATURE_REMOVE_LOGGING_SYSLOG
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000716 case 's':
Dave Shielde7551852003-10-24 12:49:22 +0000717 printf("Warning: -s option is deprecated, use -Lsd instead\n");
Robert Story68f98092005-06-20 21:38:40 +0000718 snmp_enable_syslog();
719 log_set = 1;
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000720 break;
Wes Hardakerdc4b3f62000-05-31 22:53:11 +0000721
Dave Shieldf542e4a2002-12-12 16:37:26 +0000722 case 'S':
Dave Shielde7551852003-10-24 12:49:22 +0000723 printf("Warning: -S option is deprecated, use -Ls <facility> instead\n");
Dave Shieldf542e4a2002-12-12 16:37:26 +0000724 if (optarg != NULL) {
725 switch (*optarg) {
726 case 'd':
727 case 'D':
728 Facility = LOG_DAEMON;
729 break;
730 case 'i':
731 case 'I':
732 Facility = LOG_INFO;
733 break;
734 case '0':
735 Facility = LOG_LOCAL0;
736 break;
737 case '1':
738 Facility = LOG_LOCAL1;
739 break;
740 case '2':
741 Facility = LOG_LOCAL2;
742 break;
743 case '3':
744 Facility = LOG_LOCAL3;
745 break;
746 case '4':
747 Facility = LOG_LOCAL4;
748 break;
749 case '5':
750 Facility = LOG_LOCAL5;
751 break;
752 case '6':
753 Facility = LOG_LOCAL6;
754 break;
755 case '7':
756 Facility = LOG_LOCAL7;
757 break;
758 default:
John Naylon32ae1772002-12-18 13:19:07 +0000759 fprintf(stderr, "invalid syslog facility: -S%c\n",*optarg);
Dave Shieldf542e4a2002-12-12 16:37:26 +0000760 usage(argv[0]);
761 }
Robert Story68f98092005-06-20 21:38:40 +0000762 snmp_enable_syslog_ident(snmp_log_syslogname(NULL), Facility);
763 log_set = 1;
Dave Shieldf542e4a2002-12-12 16:37:26 +0000764 } else {
765 fprintf(stderr, "no syslog facility specified\n");
766 usage(argv[0]);
767 }
768 break;
Wes Hardaker3b14c592011-02-23 00:01:18 +0000769#endif /* NETSNMP_FEATURE_REMOVE_LOGGING_SYSLOG */
Dave Shieldf542e4a2002-12-12 16:37:26 +0000770
Dave Shield1dff19c2002-12-11 09:30:55 +0000771 case 'U':
772 netsnmp_ds_toggle_boolean(NETSNMP_DS_APPLICATION_ID,
773 NETSNMP_DS_AGENT_LEAVE_PIDFILE);
774 break;
775
Wes Hardaker553f1cc1999-07-08 23:02:58 +0000776#if HAVE_UNISTD_H
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000777 case 'u':
778 if (optarg != NULL) {
779 char *ecp;
780 int uid;
John Naylon1542e432001-12-19 12:38:20 +0000781
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000782 uid = strtoul(optarg, &ecp, 10);
Bart Van Assche396e1222011-12-01 09:32:16 +0100783#if HAVE_GETPWNAM && HAVE_PWD_H
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000784 if (*ecp) {
Bart Van Assche396e1222011-12-01 09:32:16 +0100785 struct passwd *info;
786
John Naylon0cd236a2002-05-21 12:32:04 +0000787 info = getpwnam(optarg);
Bart Van Assche396e1222011-12-01 09:32:16 +0100788 uid = info ? info->pw_uid : -1;
789 endpwent();
790 }
Wes Hardaker9743e9f2001-10-11 21:01:50 +0000791#endif
Bart Van Assche396e1222011-12-01 09:32:16 +0100792 if (uid < 0) {
793 fprintf(stderr, "Bad user id: %s\n", optarg);
794 exit(1);
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000795 }
John Naylonbb401c12002-05-15 12:53:01 +0000796 netsnmp_ds_set_int(NETSNMP_DS_APPLICATION_ID,
797 NETSNMP_DS_AGENT_USERID, uid);
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000798 } else {
799 usage(argv[0]);
800 }
801 break;
Michael Slifcakfd869ca1999-07-30 11:00:28 +0000802#endif
John Naylon1542e432001-12-19 12:38:20 +0000803
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000804 case 'v':
805 version();
John Naylon1542e432001-12-19 12:38:20 +0000806
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000807 case 'V':
John Naylonbb401c12002-05-15 12:53:01 +0000808 netsnmp_ds_set_boolean(NETSNMP_DS_APPLICATION_ID,
809 NETSNMP_DS_AGENT_VERBOSE, 1);
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000810 break;
John Naylon1542e432001-12-19 12:38:20 +0000811
812#if defined(USING_AGENTX_SUBAGENT_MODULE)|| defined(USING_AGENTX_MASTER_MODULE)
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000813 case 'x':
814 if (optarg != NULL) {
John Naylonbb401c12002-05-15 12:53:01 +0000815 netsnmp_ds_set_string(NETSNMP_DS_APPLICATION_ID,
816 NETSNMP_DS_AGENT_X_SOCKET, optarg);
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000817 } else {
818 usage(argv[0]);
819 }
John Naylonbb401c12002-05-15 12:53:01 +0000820 netsnmp_ds_set_boolean(NETSNMP_DS_APPLICATION_ID,
821 NETSNMP_DS_AGENT_AGENTX_MASTER, 1);
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000822 break;
John Naylon1542e432001-12-19 12:38:20 +0000823#endif
824
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000825 case 'X':
John Naylon1542e432001-12-19 12:38:20 +0000826#if defined(USING_AGENTX_SUBAGENT_MODULE)
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000827 agent_mode = SUB_AGENT;
John Naylon1542e432001-12-19 12:38:20 +0000828#else
John Naylon32ae1772002-12-18 13:19:07 +0000829 fprintf(stderr, "%s: Illegal argument -X:"
830 "AgentX support not compiled in.\n", argv[0]);
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000831 usage(argv[0]);
832 exit(1);
John Naylon1542e432001-12-19 12:38:20 +0000833#endif
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000834 break;
John Naylon1542e432001-12-19 12:38:20 +0000835
Wes Hardaker8b14d732006-09-01 23:17:28 +0000836 case 'Y':
837 netsnmp_config_remember(optarg);
838 break;
839
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000840 default:
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000841 usage(argv[0]);
842 break;
843 }
John Naylon1542e432001-12-19 12:38:20 +0000844 }
845
Robert Storyd004d1b2005-09-27 15:34:40 +0000846 if (do_help) {
847 netsnmp_ds_set_boolean(NETSNMP_DS_APPLICATION_ID,
848 NETSNMP_DS_AGENT_NO_ROOT_ACCESS, 1);
849 init_agent(app_name); /* register our .conf handlers */
850 init_mib_modules();
851 init_snmp(app_name);
852 fprintf(stderr, "Configuration directives understood:\n");
853 read_config_print_usage(" ");
854 exit(0);
855 }
856
John Naylon1542e432001-12-19 12:38:20 +0000857 if (optind < argc) {
Wes Hardaker6c4cdaf2011-04-27 21:31:39 +0000858#ifndef NETSNMP_NO_LISTEN_SUPPORT
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000859 /*
860 * There are optional transport addresses on the command line.
861 */
862 DEBUGMSGTL(("snmpd/main", "optind %d, argc %d\n", optind, argc));
863 for (i = optind; i < argc; i++) {
John Naylon32ae1772002-12-18 13:19:07 +0000864 char *c, *astring;
John Naylonbb401c12002-05-15 12:53:01 +0000865 if ((c = netsnmp_ds_get_string(NETSNMP_DS_APPLICATION_ID,
866 NETSNMP_DS_AGENT_PORTS))) {
Dave Shield679c4302010-06-16 14:22:27 +0000867 astring = (char*)malloc(strlen(c) + 2 + strlen(argv[i]));
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000868 if (astring == NULL) {
John Naylon32ae1772002-12-18 13:19:07 +0000869 fprintf(stderr, "malloc failure processing argv[%d]\n", i);
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000870 exit(1);
871 }
872 sprintf(astring, "%s,%s", c, argv[i]);
John Naylonbb401c12002-05-15 12:53:01 +0000873 netsnmp_ds_set_string(NETSNMP_DS_APPLICATION_ID,
874 NETSNMP_DS_AGENT_PORTS, astring);
Wes Hardaker91a011e2003-07-02 20:29:47 +0000875 SNMP_FREE(astring);
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000876 } else {
John Naylonbb401c12002-05-15 12:53:01 +0000877 netsnmp_ds_set_string(NETSNMP_DS_APPLICATION_ID,
878 NETSNMP_DS_AGENT_PORTS, argv[i]);
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000879 }
880 }
881 DEBUGMSGTL(("snmpd/main", "port spec: %s\n",
John Naylonbb401c12002-05-15 12:53:01 +0000882 netsnmp_ds_get_string(NETSNMP_DS_APPLICATION_ID,
883 NETSNMP_DS_AGENT_PORTS)));
Wes Hardaker6c4cdaf2011-04-27 21:31:39 +0000884#else /* NETSNMP_NO_LISTEN_SUPPORT */
885 fprintf(stderr, "You specified ports to open; this agent was built to only send notifications\n");
886 exit(1);
887#endif /* NETSNMP_NO_LISTEN_SUPPORT */
John Naylon1542e432001-12-19 12:38:20 +0000888 }
889
Thomas Anderse7563bf2006-09-15 00:48:50 +0000890#ifdef NETSNMP_LOGFILE
Bart Van Assche2e9466d2011-07-26 13:10:04 +0200891#ifndef NETSNMP_FEATURE_REMOVE_LOGGING_FILE
Robert Story60371e82005-06-20 20:38:41 +0000892 if (0 == log_set)
Thomas Anderse7563bf2006-09-15 00:48:50 +0000893 snmp_enable_filelog(NETSNMP_LOGFILE,
Robert Story69b77f42005-11-20 19:39:13 +0000894 netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID,
895 NETSNMP_DS_LIB_APPEND_LOGFILES));
Bart Van Assche2e9466d2011-07-26 13:10:04 +0200896#endif /* NETSNMP_FEATURE_REMOVE_LOGGING_FILE */
Robert Story60371e82005-06-20 20:38:41 +0000897#endif
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000898
Magnus Fromreideadff1602010-10-16 22:32:38 +0000899#ifdef USING_UTIL_FUNCS_RESTART_MODULE
900 {
901 /*
902 * Initialize a argv set to the current for restarting the agent.
903 */
904 char *cptr, **argvptr;
905
906 argvrestartp = (char **)malloc((argc + 2) * sizeof(char *));
907 argvptr = argvrestartp;
908 for (i = 0, ret = 1; i < argc; i++) {
909 ret += strlen(argv[i]) + 1;
910 }
911 argvrestart = (char *) malloc(ret);
912 argvrestartname = (char *) malloc(strlen(argv[0]) + 1);
913 if (!argvrestartp || !argvrestart || !argvrestartname) {
914 fprintf(stderr, "malloc failure processing argvrestart\n");
915 exit(1);
916 }
917 strcpy(argvrestartname, argv[0]);
918
919 for (cptr = argvrestart, i = 0; i < argc; i++) {
920 strcpy(cptr, argv[i]);
921 *(argvptr++) = cptr;
922 cptr += strlen(argv[i]) + 1;
923 }
John Naylon1542e432001-12-19 12:38:20 +0000924 }
Magnus Fromreideadff1602010-10-16 22:32:38 +0000925#endif /* USING_UTIL_FUNCS_RESTART_MODULE */
926
John Naylon1542e432001-12-19 12:38:20 +0000927 if (agent_mode == -1) {
Magnus Fromreideadff1602010-10-16 22:32:38 +0000928 if (strstr(argv[0], "agentxd") != NULL) {
929 netsnmp_ds_set_boolean(NETSNMP_DS_APPLICATION_ID,
John Naylonbb401c12002-05-15 12:53:01 +0000930 NETSNMP_DS_AGENT_ROLE, SUB_AGENT);
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000931 } else {
Magnus Fromreideadff1602010-10-16 22:32:38 +0000932 netsnmp_ds_set_boolean(NETSNMP_DS_APPLICATION_ID,
John Naylonbb401c12002-05-15 12:53:01 +0000933 NETSNMP_DS_AGENT_ROLE, MASTER_AGENT);
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000934 }
John Naylon1542e432001-12-19 12:38:20 +0000935 } else {
Magnus Fromreideadff1602010-10-16 22:32:38 +0000936 netsnmp_ds_set_boolean(NETSNMP_DS_APPLICATION_ID,
John Naylonbb401c12002-05-15 12:53:01 +0000937 NETSNMP_DS_AGENT_ROLE, agent_mode);
John Naylon1542e432001-12-19 12:38:20 +0000938 }
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000939
Michael Slifcak1b78b761999-11-19 01:58:47 +0000940 SOCK_STARTUP;
Bart Van Asschee1b910b2012-02-23 10:27:54 +0100941 if (init_agent(app_name) != 0) {
942 snmp_log(LOG_ERR, "Agent initialization failed\n");
943 exit(1);
944 }
Wes Hardakerf2dc63b1999-09-28 23:52:04 +0000945 init_mib_modules();
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000946
947 /*
948 * start library
949 */
Robert Storyd004d1b2005-09-27 15:34:40 +0000950 init_snmp(app_name);
Wes Hardaker87bed831999-04-06 22:13:53 +0000951
Wes Hardakerb469d772001-02-09 18:12:51 +0000952 if ((ret = init_master_agent()) != 0) {
Wes Hardaker6d1f6022002-04-20 07:08:20 +0000953 /*
954 * Some error opening one of the specified agent transports.
955 */
Magnus Fromreide7cf3cf92010-10-17 01:41:29 +0000956 snmp_log(LOG_ERR, "Server Exiting with code 1\n");
957 exit(1);
Wes Hardakerb469d772001-02-09 18:12:51 +0000958 }
Niels Baggesen448c1c31999-08-19 21:09:25 +0000959
Jan Safranekdb21e012008-12-04 12:33:33 +0000960 /*
961 * Initialize the world. Detach from the shell. Create initial user.
962 */
963 if(!dont_fork) {
964 int quit = ! netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID,
965 NETSNMP_DS_AGENT_QUIT_IMMEDIATELY);
Wes Hardaker3b14c592011-02-23 00:01:18 +0000966 ret = netsnmp_daemonize(quit,
Bart Van Assche2e9466d2011-07-26 13:10:04 +0200967#ifndef NETSNMP_FEATURE_REMOVE_LOGGING_STDIO
Wes Hardaker3b14c592011-02-23 00:01:18 +0000968 snmp_stderrlog_status()
Bart Van Assche2e9466d2011-07-26 13:10:04 +0200969#else /* NETSNMP_FEATURE_REMOVE_LOGGING_STDIO */
Wes Hardaker3b14c592011-02-23 00:01:18 +0000970 0
Bart Van Assche2e9466d2011-07-26 13:10:04 +0200971#endif /* NETSNMP_FEATURE_REMOVE_LOGGING_STDIO */
Wes Hardaker3b14c592011-02-23 00:01:18 +0000972 );
Jan Safranekdb21e012008-12-04 12:33:33 +0000973 /*
974 * xxx-rks: do we care if fork fails? I think we should...
975 */
Magnus Fromreide7cf3cf92010-10-17 01:41:29 +0000976 if(ret != 0) {
977 snmp_log(LOG_ERR, "Server Exiting with code 1\n");
978 exit(1);
979 }
Jan Safranekdb21e012008-12-04 12:33:33 +0000980 }
981
John Naylon8ec2cfd2002-04-30 15:16:06 +0000982#if HAVE_GETPID
983 if (pid_file != NULL) {
Wes Hardakera36188e2003-07-02 17:58:55 +0000984 /*
985 * unlink the pid_file, if it exists, prior to open. Without
986 * doing this the open will fail if the user specified pid_file
987 * already exists.
988 */
989 unlink(pid_file);
990 fd = open(pid_file, O_CREAT | O_EXCL | O_WRONLY, 0600);
991 if (fd == -1) {
992 snmp_log_perror(pid_file);
John Naylonbb401c12002-05-15 12:53:01 +0000993 if (!netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID,
Wes Hardakera36188e2003-07-02 17:58:55 +0000994 NETSNMP_DS_AGENT_NO_ROOT_ACCESS)) {
John Naylon8ec2cfd2002-04-30 15:16:06 +0000995 exit(1);
996 }
997 } else {
Wes Hardakera36188e2003-07-02 17:58:55 +0000998 if ((PID = fdopen(fd, "w")) == NULL) {
999 snmp_log_perror(pid_file);
1000 exit(1);
1001 } else {
1002 fprintf(PID, "%d\n", (int) getpid());
1003 fclose(PID);
1004 }
Bart Van Assche2f8d4f52011-08-05 12:02:51 +02001005#ifndef _MSC_VER
Wes Hardaker6ec32d82011-08-08 15:12:55 -07001006 /* The sequence open()/fdopen()/fclose()/close() makes MSVC crash,
1007 hence skip the close() call when using the MSVC runtime. */
Wes Hardakera36188e2003-07-02 17:58:55 +00001008 close(fd);
Bart Van Assche2f8d4f52011-08-05 12:02:51 +02001009#endif
John Naylon8ec2cfd2002-04-30 15:16:06 +00001010 }
1011 }
1012#endif
1013
Bart Van Assche275d23e2011-10-26 09:33:09 +02001014#if defined(HAVE_UNISTD_H) && (defined(HAVE_CHOWN) || defined(HAVE_SETGID) || defined(HAVE_SETUID))
1015 {
1016 const char *persistent_dir;
1017 int uid, gid;
1018
Niels Baggesena2a73162009-05-06 21:59:20 +00001019 persistent_dir = get_persistent_directory();
1020 mkdirhier( persistent_dir, NETSNMP_AGENT_DIRECTORY_MODE, 0 );
Dave Shield1183b672006-01-30 09:52:54 +00001021
1022 uid = netsnmp_ds_get_int(NETSNMP_DS_APPLICATION_ID,
1023 NETSNMP_DS_AGENT_USERID);
1024 gid = netsnmp_ds_get_int(NETSNMP_DS_APPLICATION_ID,
1025 NETSNMP_DS_AGENT_GROUPID);
1026
Bart Van Asscheb9637b42010-01-22 13:32:10 +00001027#ifdef HAVE_CHOWN
Dave Shield1183b672006-01-30 09:52:54 +00001028 if ( uid != 0 || gid != 0 )
Niels Baggesena2a73162009-05-06 21:59:20 +00001029 chown( persistent_dir, uid, gid );
Bart Van Asscheb9637b42010-01-22 13:32:10 +00001030#endif
Dave Shield1183b672006-01-30 09:52:54 +00001031
Wes Hardakerfc336c62000-11-07 23:15:37 +00001032#ifdef HAVE_SETGID
John Naylonbb401c12002-05-15 12:53:01 +00001033 if ((gid = netsnmp_ds_get_int(NETSNMP_DS_APPLICATION_ID,
Jan Safranek2b6c1192012-01-03 15:51:10 +01001034 NETSNMP_DS_AGENT_GROUPID)) > 0) {
Wes Hardaker6d1f6022002-04-20 07:08:20 +00001035 DEBUGMSGTL(("snmpd/main", "Changing gid to %d.\n", gid));
1036 if (setgid(gid) == -1
Wes Hardaker9743e9f2001-10-11 21:01:50 +00001037#ifdef HAVE_SETGROUPS
John Naylonc6a2d0c2002-07-09 10:14:13 +00001038 || setgroups(1, (gid_t *)&gid) == -1
Wes Hardaker9743e9f2001-10-11 21:01:50 +00001039#endif
Wes Hardaker6d1f6022002-04-20 07:08:20 +00001040 ) {
1041 snmp_log_perror("setgid failed");
John Naylonbb401c12002-05-15 12:53:01 +00001042 if (!netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID,
1043 NETSNMP_DS_AGENT_NO_ROOT_ACCESS)) {
Wes Hardaker6d1f6022002-04-20 07:08:20 +00001044 exit(1);
1045 }
1046 }
John Naylon1542e432001-12-19 12:38:20 +00001047 }
Wes Hardakerfc336c62000-11-07 23:15:37 +00001048#endif
1049#ifdef HAVE_SETUID
John Naylonbb401c12002-05-15 12:53:01 +00001050 if ((uid = netsnmp_ds_get_int(NETSNMP_DS_APPLICATION_ID,
Jan Safranek2b6c1192012-01-03 15:51:10 +01001051 NETSNMP_DS_AGENT_USERID)) > 0) {
Dave Shielda1cb2fc2007-05-27 19:06:25 +00001052#if HAVE_GETPWNAM && HAVE_PWD_H && HAVE_INITGROUPS
Bart Van Assche396e1222011-12-01 09:32:16 +01001053 struct passwd *info;
1054
Dave Shieldf81731d2010-04-06 10:06:13 +00001055 /*
1056 * Set supplementary groups before changing UID
1057 * (which probably involves giving up privileges)
1058 */
Dave Shielda1cb2fc2007-05-27 19:06:25 +00001059 info = getpwuid(uid);
1060 if (info) {
1061 DEBUGMSGTL(("snmpd/main", "Supplementary groups for %s.\n", info->pw_name));
Dave Shieldd979c392010-06-16 14:37:14 +00001062 if (initgroups(info->pw_name, (gid != 0 ? (gid_t)gid : info->pw_gid)) == -1) {
Dave Shielda1cb2fc2007-05-27 19:06:25 +00001063 snmp_log_perror("initgroups failed");
1064 if (!netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID,
1065 NETSNMP_DS_AGENT_NO_ROOT_ACCESS)) {
1066 exit(1);
1067 }
1068 }
1069 }
Bart Van Assche396e1222011-12-01 09:32:16 +01001070 endpwent();
Dave Shielda1cb2fc2007-05-27 19:06:25 +00001071#endif
Dave Shieldf81731d2010-04-06 10:06:13 +00001072 DEBUGMSGTL(("snmpd/main", "Changing uid to %d.\n", uid));
1073 if (setuid(uid) == -1) {
1074 snmp_log_perror("setuid failed");
1075 if (!netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID,
1076 NETSNMP_DS_AGENT_NO_ROOT_ACCESS)) {
1077 exit(1);
1078 }
1079 }
John Naylon1542e432001-12-19 12:38:20 +00001080 }
Wes Hardaker553f1cc1999-07-08 23:02:58 +00001081#endif
Bart Van Assche275d23e2011-10-26 09:33:09 +02001082 }
Wes Hardakerfc336c62000-11-07 23:15:37 +00001083#endif
Wes Hardaker553f1cc1999-07-08 23:02:58 +00001084
Wes Hardaker6d1f6022002-04-20 07:08:20 +00001085 /*
Dave Shield1183b672006-01-30 09:52:54 +00001086 * Store persistent data immediately in case we crash later.
1087 */
1088 snmp_store(app_name);
1089
Wes Hardaker5e8c26a2007-05-17 13:53:50 +00001090#ifdef SIGHUP
1091 DEBUGMSGTL(("signal", "registering SIGHUP signal handler\n"));
1092 signal(SIGHUP, SnmpdReconfig);
1093#endif
1094
Dave Shield1183b672006-01-30 09:52:54 +00001095 /*
1096 * Send coldstart trap if possible.
1097 */
1098 send_easy_trap(0, 0);
1099
1100 /*
Wes Hardaker6d1f6022002-04-20 07:08:20 +00001101 * We're up, log our version number.
1102 */
Wes Hardaker11ef61b2002-01-30 01:53:05 +00001103 snmp_log(LOG_INFO, "NET-SNMP version %s\n", netsnmp_get_version());
Michael Slifcak0ef01642004-02-09 12:06:24 +00001104#ifdef WIN32SERVICE
Dave Shield8b2adde2002-02-15 15:28:29 +00001105 agent_status = AGENT_RUNNING;
1106#endif
Wes Hardaker6fb202d2002-03-07 21:42:31 +00001107 netsnmp_addrcache_initialise();
John Naylon1542e432001-12-19 12:38:20 +00001108
Wes Hardaker6d1f6022002-04-20 07:08:20 +00001109 /*
1110 * Forever monitor the dest_port for incoming PDUs.
1111 */
John Naylon1542e432001-12-19 12:38:20 +00001112 DEBUGMSGTL(("snmpd/main", "We're up. Starting to process data.\n"));
John Naylonbb401c12002-05-15 12:53:01 +00001113 if (!netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID,
1114 NETSNMP_DS_AGENT_QUIT_IMMEDIATELY))
Wes Hardaker90e79b42002-02-20 21:42:25 +00001115 receive();
John Naylon1542e432001-12-19 12:38:20 +00001116 DEBUGMSGTL(("snmpd/main", "sending shutdown trap\n"));
1117 SnmpTrapNodeDown();
1118 DEBUGMSGTL(("snmpd/main", "Bye...\n"));
Robert Storyd004d1b2005-09-27 15:34:40 +00001119 snmp_shutdown(app_name);
Wes Hardaker9dfe0f92003-07-02 20:20:57 +00001120 shutdown_master_agent();
1121 shutdown_agent();
1122
John Naylon32ae1772002-12-18 13:19:07 +00001123 if (!netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID,
1124 NETSNMP_DS_AGENT_LEAVE_PIDFILE) &&
1125 (pid_file != NULL)) {
Dave Shield594fade2002-12-09 14:32:02 +00001126 unlink(pid_file);
1127 }
Michael Slifcak0ef01642004-02-09 12:06:24 +00001128#ifdef WIN32SERVICE
Dave Shieldb02d7fd2002-02-22 16:55:42 +00001129 agent_status = AGENT_STOPPED;
1130#endif
Wes Hardaker9dfe0f92003-07-02 20:20:57 +00001131
Magnus Fromreideadff1602010-10-16 22:32:38 +00001132#ifdef USING_UTIL_FUNCS_RESTART_MODULE
Wes Hardaker9dfe0f92003-07-02 20:20:57 +00001133 SNMP_FREE(argvrestartname);
1134 SNMP_FREE(argvrestart);
1135 SNMP_FREE(argvrestartp);
Magnus Fromreideadff1602010-10-16 22:32:38 +00001136#endif /* USING_UTIL_FUNCS_RESTART_MODULE */
1137
Robert Story23c7b842005-02-25 22:45:10 +00001138 SOCK_CLEANUP;
John Naylon1542e432001-12-19 12:38:20 +00001139 return 0;
Wes Hardaker6d1f6022002-04-20 07:08:20 +00001140} /* End main() -- snmpd */
Wes Hardaker8c6ab571998-06-10 15:53:53 +00001141
Bart Van Assche3937cf12010-10-18 08:52:00 +00001142#if defined(WIN32)
1143
Bart Van Assche3937cf12010-10-18 08:52:00 +00001144#include <process.h>
Wes Hardakerbf934d92010-12-01 01:06:05 +00001145#include <net-snmp/library/snmp_assert.h>
Bart Van Assche3937cf12010-10-18 08:52:00 +00001146
1147static unsigned s_threadid;
1148HANDLE s_thread_handle;
1149
1150static unsigned __stdcall wait_for_stdin(void* arg)
1151{
1152 if (getc(stdin) != EOF)
1153 netsnmp_running = 0;
1154 return 0;
1155}
1156
Bart Van Assche509e9782011-02-14 14:38:00 +00001157static void create_stdin_waiter_thread(void)
Bart Van Assche3937cf12010-10-18 08:52:00 +00001158{
Wes Hardakerbf934d92010-12-01 01:06:05 +00001159 netsnmp_assert(s_thread_handle == 0);
Bart Van Assche3937cf12010-10-18 08:52:00 +00001160 s_thread_handle = (HANDLE)_beginthreadex(0, 0, wait_for_stdin, 0, 0, &s_threadid);
Wes Hardakerbf934d92010-12-01 01:06:05 +00001161 netsnmp_assert(s_thread_handle != 0);
Bart Van Assche3937cf12010-10-18 08:52:00 +00001162}
1163
Bart Van Assche509e9782011-02-14 14:38:00 +00001164static void join_stdin_waiter_thread(void)
Bart Van Assche3937cf12010-10-18 08:52:00 +00001165{
1166 int result;
1167
Wes Hardakerbf934d92010-12-01 01:06:05 +00001168 netsnmp_assert(s_thread_handle != 0);
Bart Van Assche3937cf12010-10-18 08:52:00 +00001169 result = WaitForSingleObject(s_thread_handle, 1000);
Wes Hardakerbf934d92010-12-01 01:06:05 +00001170 netsnmp_assert(result != WAIT_TIMEOUT);
Bart Van Assche3937cf12010-10-18 08:52:00 +00001171 CloseHandle(s_thread_handle);
1172 s_thread_handle = 0;
1173}
1174#endif
1175
Wes Hardaker87bed831999-04-06 22:13:53 +00001176/*******************************************************************-o-******
1177 * receive
1178 *
1179 * Parameters:
1180 *
1181 * Returns:
1182 * 0 On success.
1183 * -1 System error.
1184 *
Bart Van Assche675bc132011-03-10 20:01:33 +00001185 * Infinite while-loop which monitors incoming messages for the agent.
Wes Hardaker87bed831999-04-06 22:13:53 +00001186 * Invoke the established message handlers for incoming messages on a per
1187 * port basis. Handle timeouts.
1188 */
John Naylon1542e432001-12-19 12:38:20 +00001189static int
Wes Hardaker87bed831999-04-06 22:13:53 +00001190receive(void)
Wes Hardakerc0485231994-10-13 20:55:30 +00001191{
Wes Hardaker6d1f6022002-04-20 07:08:20 +00001192 int numfds;
Dave Shield23d9c652009-04-21 15:33:42 +00001193 netsnmp_large_fd_set readfds, writefds, exceptfds;
Wes Hardaker6d1f6022002-04-20 07:08:20 +00001194 struct timeval timeout, *tvp = &timeout;
Wes Hardaker6d1f6022002-04-20 07:08:20 +00001195 int count, block, i;
Dave Shieldbe290001999-12-13 12:43:14 +00001196#ifdef USING_SMUX_MODULE
Wes Hardaker6d1f6022002-04-20 07:08:20 +00001197 int sd;
1198#endif /* USING_SMUX_MODULE */
Wes Hardakerc0485231994-10-13 20:55:30 +00001199
Dave Shield23d9c652009-04-21 15:33:42 +00001200 netsnmp_large_fd_set_init(&readfds, FD_SETSIZE);
1201 netsnmp_large_fd_set_init(&writefds, FD_SETSIZE);
1202 netsnmp_large_fd_set_init(&exceptfds, FD_SETSIZE);
1203
Wes Hardakera6495ce1999-08-30 16:33:41 +00001204 /*
Robert Story6fbb1052005-06-22 20:04:36 +00001205 * ignore early sighup during startup
1206 */
1207 reconfig = 0;
1208
Bart Van Assche3937cf12010-10-18 08:52:00 +00001209#if defined(WIN32)
1210 create_stdin_waiter_thread();
1211#endif
1212
Robert Story6fbb1052005-06-22 20:04:36 +00001213 /*
Robert Story9c1e5f12004-04-19 14:53:52 +00001214 * Loop-forever: execute message handlers for sockets with data
Wes Hardakera6495ce1999-08-30 16:33:41 +00001215 */
Robert Story7627ec72005-11-15 16:37:58 +00001216 while (netsnmp_running) {
Wes Hardakera6495ce1999-08-30 16:33:41 +00001217 if (reconfig) {
Wes Hardakerc66bc252006-10-18 01:46:27 +00001218#if HAVE_SIGHOLD
1219 sighold(SIGHUP);
1220#endif
Wes Hardaker6d1f6022002-04-20 07:08:20 +00001221 reconfig = 0;
1222 snmp_log(LOG_INFO, "Reconfiguring daemon\n");
John Naylon7c5170c2002-08-22 11:11:57 +00001223 /* Stop and restart logging. This allows logfiles to be
1224 rotated etc. */
Robert Story60371e82005-06-20 20:38:41 +00001225 netsnmp_logging_restart();
John Naylon7c5170c2002-08-22 11:11:57 +00001226 snmp_log(LOG_INFO, "NET-SNMP version %s restarted\n",
1227 netsnmp_get_version());
Wes Hardaker6d1f6022002-04-20 07:08:20 +00001228 update_config();
1229 send_easy_trap(SNMP_TRAP_ENTERPRISESPECIFIC, 3);
Wes Hardakerc66bc252006-10-18 01:46:27 +00001230#if HAVE_SIGHOLD
1231 sigrelse(SIGHUP);
1232#endif
Wes Hardakera6495ce1999-08-30 16:33:41 +00001233 }
John Naylon01e01882001-04-04 13:46:57 +00001234
Robert Story9c1e5f12004-04-19 14:53:52 +00001235 /*
Robert Story24fd55f2005-11-07 18:41:49 +00001236 * default to sleeping for a really long time. INT_MAX
1237 * should be sufficient (eg we don't care if time_t is
1238 * a long that's bigger than an int).
Robert Story9c1e5f12004-04-19 14:53:52 +00001239 */
Robert Storyd0ee22c2004-10-06 21:11:23 +00001240 tvp = &timeout;
Robert Story24fd55f2005-11-07 18:41:49 +00001241 tvp->tv_sec = INT_MAX;
Robert Story9c1e5f12004-04-19 14:53:52 +00001242 tvp->tv_usec = 0;
Wes Hardaker87bed831999-04-06 22:13:53 +00001243
Wes Hardaker6d1f6022002-04-20 07:08:20 +00001244 numfds = 0;
Dave Shield23d9c652009-04-21 15:33:42 +00001245 NETSNMP_LARGE_FD_ZERO(&readfds);
1246 NETSNMP_LARGE_FD_ZERO(&writefds);
1247 NETSNMP_LARGE_FD_ZERO(&exceptfds);
Wes Hardakera6495ce1999-08-30 16:33:41 +00001248 block = 0;
Dave Shield23d9c652009-04-21 15:33:42 +00001249 snmp_select_info2(&numfds, &readfds, tvp, &block);
John Naylon32ae1772002-12-18 13:19:07 +00001250 if (block == 1) {
Wes Hardaker6d1f6022002-04-20 07:08:20 +00001251 tvp = NULL; /* block without timeout */
John Naylon32ae1772002-12-18 13:19:07 +00001252 }
Dave Shieldbe290001999-12-13 12:43:14 +00001253
1254#ifdef USING_SMUX_MODULE
Wes Hardaker6d1f6022002-04-20 07:08:20 +00001255 if (smux_listen_sd >= 0) {
Magnus Fromreide018c1e92009-04-22 21:03:21 +00001256 NETSNMP_LARGE_FD_SET(smux_listen_sd, &readfds);
Wes Hardaker6d1f6022002-04-20 07:08:20 +00001257 numfds =
1258 smux_listen_sd >= numfds ? smux_listen_sd + 1 : numfds;
Dave Shieldc3fb5c82007-03-22 19:12:22 +00001259
1260 for (i = 0; i < smux_snmp_select_list_get_length(); i++) {
1261 sd = smux_snmp_select_list_get_SD_from_List(i);
1262 if (sd != 0)
1263 {
Magnus Fromreide018c1e92009-04-22 21:03:21 +00001264 NETSNMP_LARGE_FD_SET(sd, &readfds);
Dave Shieldc3fb5c82007-03-22 19:12:22 +00001265 numfds = sd >= numfds ? sd + 1 : numfds;
1266 }
Wes Hardaker6d1f6022002-04-20 07:08:20 +00001267 }
1268 }
1269#endif /* USING_SMUX_MODULE */
Dave Shieldbe290001999-12-13 12:43:14 +00001270
Wes Hardaker3b14c592011-02-23 00:01:18 +00001271#ifndef NETSNMP_FEATURE_REMOVE_FD_EVENT_MANAGER
Dave Shield23d9c652009-04-21 15:33:42 +00001272 netsnmp_external_event_info2(&numfds, &readfds, &writefds, &exceptfds);
Wes Hardaker3b14c592011-02-23 00:01:18 +00001273#endif /* NETSNMP_FEATURE_REMOVE_FD_EVENT_MANAGER */
Wes Hardakerb469d772001-02-09 18:12:51 +00001274
John Naylon98382152002-10-11 13:40:10 +00001275 reselect:
Magnus Fromreided8d60782013-11-19 08:06:34 +01001276#ifndef NETSNMP_FEATURE_REMOVE_REGISTER_SIGNAL
Jan Safranek11bd6e02011-07-27 14:45:09 +02001277 for (i = 0; i < NUM_EXTERNAL_SIGS; i++) {
1278 if (external_signal_scheduled[i]) {
1279 external_signal_scheduled[i]--;
1280 external_signal_handler[i](i);
1281 }
1282 }
Magnus Fromreided8d60782013-11-19 08:06:34 +01001283#endif /* NETSNMP_FEATURE_REMOVE_REGISTER_SIGNAL */
Jan Safranek11bd6e02011-07-27 14:45:09 +02001284
Robert Story6bac8a02002-10-16 21:00:57 +00001285 DEBUGMSGTL(("snmpd/select", "select( numfds=%d, ..., tvp=%p)\n",
1286 numfds, tvp));
Bart Van Asscheb4a19502012-02-20 16:58:47 +01001287 if (tvp)
Bart Van Assche9484d832012-02-24 14:00:32 +01001288 DEBUGMSGTL(("timer", "tvp %ld.%ld\n", (long) tvp->tv_sec,
Bart Van Asscheb4a19502012-02-20 16:58:47 +01001289 (long) tvp->tv_usec));
Dave Shieldfe3d4602011-03-14 09:31:57 +00001290 count = netsnmp_large_fd_set_select(numfds, &readfds, &writefds, &exceptfds,
Bart Van Asschea6967c92011-03-10 19:48:43 +00001291 tvp);
Wes Hardaker6d1f6022002-04-20 07:08:20 +00001292 DEBUGMSGTL(("snmpd/select", "returned, count = %d\n", count));
Wes Hardaker5415a522000-03-10 18:52:37 +00001293
Wes Hardaker6d1f6022002-04-20 07:08:20 +00001294 if (count > 0) {
Wes Hardaker5415a522000-03-10 18:52:37 +00001295
Wes Hardaker6d1f6022002-04-20 07:08:20 +00001296#ifdef USING_SMUX_MODULE
1297 /*
1298 * handle the SMUX sd's
1299 */
1300 if (smux_listen_sd >= 0) {
Dave Shieldc3fb5c82007-03-22 19:12:22 +00001301 for (i = 0; i < smux_snmp_select_list_get_length(); i++) {
1302 sd = smux_snmp_select_list_get_SD_from_List(i);
Magnus Fromreide018c1e92009-04-22 21:03:21 +00001303 if (NETSNMP_LARGE_FD_ISSET(sd, &readfds)) {
Dave Shieldc3fb5c82007-03-22 19:12:22 +00001304 if (smux_process(sd) < 0) {
1305 smux_snmp_select_list_del(sd);
Wes Hardaker6d1f6022002-04-20 07:08:20 +00001306 }
1307 }
1308 }
1309 /*
1310 * new connection
1311 */
Magnus Fromreide018c1e92009-04-22 21:03:21 +00001312 if (NETSNMP_LARGE_FD_ISSET(smux_listen_sd, &readfds)) {
Wes Hardaker6d1f6022002-04-20 07:08:20 +00001313 if ((sd = smux_accept(smux_listen_sd)) >= 0) {
Dave Shieldc3fb5c82007-03-22 19:12:22 +00001314 smux_snmp_select_list_add(sd);
Wes Hardaker6d1f6022002-04-20 07:08:20 +00001315 }
1316 }
1317 }
Dave Shieldc3fb5c82007-03-22 19:12:22 +00001318
Wes Hardaker6d1f6022002-04-20 07:08:20 +00001319#endif /* USING_SMUX_MODULE */
Wes Hardaker3b14c592011-02-23 00:01:18 +00001320
1321#ifndef NETSNMP_FEATURE_REMOVE_FD_EVENT_MANAGER
Dave Shield23d9c652009-04-21 15:33:42 +00001322 netsnmp_dispatch_external_events2(&count, &readfds,
1323 &writefds, &exceptfds);
Wes Hardaker3b14c592011-02-23 00:01:18 +00001324#endif /* NETSNMP_FEATURE_REMOVE_FD_EVENT_MANAGER */
1325
Dave Shield2decc2f2005-04-29 16:12:33 +00001326 /* If there are still events leftover, process them */
1327 if (count > 0) {
Dave Shield23d9c652009-04-21 15:33:42 +00001328 snmp_read2(&readfds);
Wes Hardaker6d1f6022002-04-20 07:08:20 +00001329 }
Wes Hardaker6d1f6022002-04-20 07:08:20 +00001330 } else
1331 switch (count) {
1332 case 0:
Wes Hardakera6495ce1999-08-30 16:33:41 +00001333 snmp_timeout();
1334 break;
Wes Hardaker6d1f6022002-04-20 07:08:20 +00001335 case -1:
Robert Story19617e32004-03-02 04:52:45 +00001336 DEBUGMSGTL(("snmpd/select", " errno = %d\n", errno));
Wes Hardaker6d1f6022002-04-20 07:08:20 +00001337 if (errno == EINTR) {
Robert Story6bac8a02002-10-16 21:00:57 +00001338 /*
1339 * likely that we got a signal. Check our special signal
1340 * flags before retrying select.
1341 */
Robert Story7627ec72005-11-15 16:37:58 +00001342 if (netsnmp_running && !reconfig) {
Robert Story6bac8a02002-10-16 21:00:57 +00001343 goto reselect;
John Naylon32ae1772002-12-18 13:19:07 +00001344 }
Robert Storyecf9c8e2002-10-16 21:19:52 +00001345 continue;
Wes Hardaker6d1f6022002-04-20 07:08:20 +00001346 } else {
Wes Hardakera6495ce1999-08-30 16:33:41 +00001347 snmp_log_perror("select");
Wes Hardaker6d1f6022002-04-20 07:08:20 +00001348 }
1349 return -1;
1350 default:
1351 snmp_log(LOG_ERR, "select returned %d\n", count);
1352 return -1;
1353 } /* endif -- count>0 */
Niels Baggesen2ddc7eb1999-08-18 09:40:48 +00001354
Wes Hardaker6d1f6022002-04-20 07:08:20 +00001355 /*
Robert Storyacb59522010-08-17 18:31:31 +00001356 * see if persistent store needs to be saved
1357 */
1358 snmp_store_if_needed();
1359
1360 /*
Wes Hardaker6d1f6022002-04-20 07:08:20 +00001361 * run requested alarms
1362 */
Wes Hardaker6d05cac1999-11-19 22:20:25 +00001363 run_alarms();
Wes Hardaker6d1f6022002-04-20 07:08:20 +00001364
Wes Hardakera7482e52002-03-27 07:23:12 +00001365 netsnmp_check_outstanding_agent_requests();
Wes Hardaker36e94be2001-11-22 02:56:52 +00001366
Wes Hardaker6d1f6022002-04-20 07:08:20 +00001367 } /* endwhile */
Niels Baggesen448c1c31999-08-19 21:09:25 +00001368
Dave Shield23d9c652009-04-21 15:33:42 +00001369 netsnmp_large_fd_set_cleanup(&readfds);
1370 netsnmp_large_fd_set_cleanup(&writefds);
1371 netsnmp_large_fd_set_cleanup(&exceptfds);
1372
Bart Van Assche3937cf12010-10-18 08:52:00 +00001373#if defined(WIN32)
1374 join_stdin_waiter_thread();
1375#endif
1376
John Naylon32ae1772002-12-18 13:19:07 +00001377 snmp_log(LOG_INFO, "Received TERM or STOP signal... shutting down...\n");
Wes Hardakera6495ce1999-08-30 16:33:41 +00001378 return 0;
Wes Hardaker87bed831999-04-06 22:13:53 +00001379
Wes Hardaker6d1f6022002-04-20 07:08:20 +00001380} /* end receive() */
Wes Hardaker87bed831999-04-06 22:13:53 +00001381
1382
1383
Wes Hardaker87bed831999-04-06 22:13:53 +00001384/*******************************************************************-o-******
1385 * snmp_input
1386 *
1387 * Parameters:
1388 * op
1389 * *session
1390 * requid
1391 * *pdu
1392 * *magic
1393 *
1394 * Returns:
1395 * 1 On success -OR-
1396 * Passes through Return from alarmGetResponse() when
1397 * USING_V2PARTY_ALARM_MODULE is defined.
1398 *
1399 * Call-back function to manage responses to traps (informs) and alarms.
1400 * Not used by the agent to process other Response PDUs.
1401 */
Wes Hardakerc0485231994-10-13 20:55:30 +00001402int
Wes Hardaker40b2bf11999-01-18 18:47:36 +00001403snmp_input(int op,
Wes Hardaker6d1f6022002-04-20 07:08:20 +00001404 netsnmp_session * session,
1405 int reqid, netsnmp_pdu *pdu, void *magic)
Wes Hardakerc0485231994-10-13 20:55:30 +00001406{
Wes Hardaker6d1f6022002-04-20 07:08:20 +00001407 struct get_req_state *state = (struct get_req_state *) magic;
1408
Wes Hardaker73925b82002-03-09 00:28:07 +00001409 if (op == NETSNMP_CALLBACK_OP_RECEIVED_MESSAGE) {
Wes Hardaker6d1f6022002-04-20 07:08:20 +00001410 if (pdu->command == SNMP_MSG_GET) {
1411 if (state->type == EVENT_GET_REQ) {
1412 /*
1413 * this is just the ack to our inform pdu
1414 */
1415 return 1;
1416 }
1417 }
1418 } else if (op == NETSNMP_CALLBACK_OP_TIMED_OUT) {
1419 if (state->type == ALARM_GET_REQ) {
1420 /*
1421 * Need a mechanism to replace obsolete SNMPv2p alarm
1422 */
1423 }
Wes Hardakerc0485231994-10-13 20:55:30 +00001424 }
1425 return 1;
Wes Hardaker87bed831999-04-06 22:13:53 +00001426
Wes Hardaker6d1f6022002-04-20 07:08:20 +00001427} /* end snmp_input() */
Dave Shield8b2adde2002-02-15 15:28:29 +00001428
1429
1430
Wes Hardaker6d1f6022002-04-20 07:08:20 +00001431/*
1432 * Windows Service Related functions
1433 */
Michael Slifcak0ef01642004-02-09 12:06:24 +00001434#ifdef WIN32SERVICE
Dave Shield8b2adde2002-02-15 15:28:29 +00001435/************************************************************
1436* main function for Windows
1437* Parse command line arguments for startup options,
1438* to start as service or console mode application in windows.
Michael Slifcak8784cbc2004-09-09 10:22:23 +00001439* Invokes appropriate startup functions depending on the
1440* parameters passed
Dave Shield8b2adde2002-02-15 15:28:29 +00001441*************************************************************/
1442int
Wes Hardaker6d1f6022002-04-20 07:08:20 +00001443 __cdecl
1444_tmain(int argc, TCHAR * argv[])
Dave Shield8b2adde2002-02-15 15:28:29 +00001445{
Wes Hardaker6d1f6022002-04-20 07:08:20 +00001446 /*
1447 * Define Service Name and Description, which appears in windows SCM
1448 */
Alex Burger327de412005-11-10 02:33:17 +00001449 LPCTSTR lpszServiceName = app_name_long; /* Service Registry Name */
Michael Slifcakb9cc54c2004-01-29 20:32:27 +00001450 LPCTSTR lpszServiceDisplayName = _T("Net-SNMP Agent"); /* Display Name */
Wes Hardaker6d1f6022002-04-20 07:08:20 +00001451 LPCTSTR lpszServiceDescription =
Michael Slifcak78d401c2004-02-27 12:50:16 +00001452#ifdef IFDESCR
Michael Slifcakb9cc54c2004-01-29 20:32:27 +00001453 _T("SNMPv2c / SNMPv3 command responder from Net-SNMP. Supports MIB objects for IP,ICMP,TCP,UDP, and network interface sub-layers.");
1454#else
1455 _T("SNMPv2c / SNMPv3 command responder from Net-SNMP");
1456#endif
Wes Hardaker6d1f6022002-04-20 07:08:20 +00001457 InputParams InputOptions;
Dave Shield8b2adde2002-02-15 15:28:29 +00001458
1459
Wes Hardaker6d1f6022002-04-20 07:08:20 +00001460 int nRunType = RUN_AS_CONSOLE;
Michael Slifcak73c0c442004-09-22 18:20:16 +00001461 int quiet = 0;
1462
Bart Van Assche13b46d02010-10-28 08:44:08 +00001463#if 0
1464 _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF /*| _CRTDBG_CHECK_ALWAYS_DF*/);
1465#endif
1466
Michael Slifcak73c0c442004-09-22 18:20:16 +00001467 nRunType = ParseCmdLineForServiceOption(argc, argv, &quiet);
Dave Shield8b2adde2002-02-15 15:28:29 +00001468
Wes Hardaker6d1f6022002-04-20 07:08:20 +00001469 switch (nRunType) {
1470 case REGISTER_SERVICE:
1471 /*
1472 * Register As service
1473 */
1474 InputOptions.Argc = argc;
1475 InputOptions.Argv = argv;
Michael Slifcak73c0c442004-09-22 18:20:16 +00001476 exit (RegisterService(lpszServiceName,
Wes Hardaker6d1f6022002-04-20 07:08:20 +00001477 lpszServiceDisplayName,
Michael Slifcak73c0c442004-09-22 18:20:16 +00001478 lpszServiceDescription, &InputOptions, quiet));
Wes Hardaker6d1f6022002-04-20 07:08:20 +00001479 break;
1480 case UN_REGISTER_SERVICE:
1481 /*
1482 * Unregister service
1483 */
Michael Slifcak73c0c442004-09-22 18:20:16 +00001484 exit (UnregisterService(lpszServiceName, quiet));
Wes Hardaker6d1f6022002-04-20 07:08:20 +00001485 break;
1486 case RUN_AS_SERVICE:
1487 /*
1488 * Run as service
1489 */
1490 /*
1491 * Register Stop Function
1492 */
1493 RegisterStopFunction(StopSnmpAgent);
1494 return RunAsService(SnmpDaemonMain);
1495 break;
1496 default:
1497 /*
Michael Slifcak8784cbc2004-09-09 10:22:23 +00001498 * Run in console mode
Wes Hardaker6d1f6022002-04-20 07:08:20 +00001499 */
1500 return SnmpDaemonMain(argc, argv);
1501 break;
1502 }
Dave Shield8b2adde2002-02-15 15:28:29 +00001503}
1504
1505/*
Michael Slifcak8784cbc2004-09-09 10:22:23 +00001506 * To stop Snmp Agent daemon
Dave Shield8b2adde2002-02-15 15:28:29 +00001507 * This portion is still not working
1508 */
Wes Hardaker6d1f6022002-04-20 07:08:20 +00001509void
1510StopSnmpAgent(void)
Dave Shield8b2adde2002-02-15 15:28:29 +00001511{
Wes Hardaker6d1f6022002-04-20 07:08:20 +00001512 /*
1513 * Shut Down Agent
1514 */
1515 SnmpdShutDown(1);
Dave Shield8b2adde2002-02-15 15:28:29 +00001516
Wes Hardaker6d1f6022002-04-20 07:08:20 +00001517 /*
1518 * Wait till agent is completely stopped
1519 */
Dave Shield8b2adde2002-02-15 15:28:29 +00001520
Wes Hardaker6d1f6022002-04-20 07:08:20 +00001521 while (agent_status != AGENT_STOPPED) {
1522 Sleep(100);
1523 }
Dave Shield8b2adde2002-02-15 15:28:29 +00001524}
1525
Michael Slifcak8784cbc2004-09-09 10:22:23 +00001526#endif /*WIN32SERVICE*/