Logo
Home page Net-SNMP

Archive Search:

Require all words?

Site Search:
Google
snmpd.c File Reference
Main Page   Data Structures   File List   Data Fields   Globals  

snmpd.c File Reference

#include <config.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/time.h>
#include <time.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <sys/file.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <signal.h>
#include <sys/param.h>
#include <dmalloc.h>
#include "asn1.h"
#include "snmp_api.h"
#include "snmp_impl.h"
#include "system.h"
#include "read_config.h"
#include "snmp.h"
#include "mib.h"
#include "m2m.h"
#include "snmp_vars.h"
#include "agent_read_config.h"
#include "snmpv3.h"
#include "callback.h"
#include "snmp_alarm.h"
#include "default_store.h"
#include "mib_module_config.h"
#include "snmp_client.h"
#include "snmpd.h"
#include "agent_handler.h"
#include "var_struct.h"
#include "mibgroup/struct.h"
#include "snmp_debug.h"
#include "mib_modules.h"
#include "snmp_secmod.h"
#include "snmpusm.h"
#include "tools.h"
#include "lcd_time.h"
#include "mibgroup/util_funcs.h"
#include "snmp_agent.h"
#include "agent_trap.h"
#include "ds_agent.h"
#include "snmp_logging.h"
#include "snmp_transport.h"
#include "version.h"
#include <helpers/table.h>
#include <helpers/table_iterator.h>
#include "mib_module_includes.h"
#include "mib_module_shutdown.h"

Go to the source code of this file.


Defines

#define NFDBITS   (sizeof(fd_mask) * NBBY)
#define FD_SET(n, p)   ((p)->fds_bits[(n)/NFDBITS] |= (1 << ((n) % NFDBITS)))
#define FD_CLR(n, p)   ((p)->fds_bits[(n)/NFDBITS] &= ~(1 << ((n) % NFDBITS)))
#define FD_ISSET(n, p)   ((p)->fds_bits[(n)/NFDBITS] & (1 << ((n) % NFDBITS)))
#define FD_ZERO(p)   memset((p), 0, sizeof(*(p)))
#define TIMETICK   500000L
#define ONE_SEC   1000000L
#define NUM_SOCKETS   32

Typedefs

typedef long fd_mask

Functions

int snmp_read_packet (int)
int snmp_input (int, struct snmp_session *, int, struct snmp_pdu *, void *)
int main (int, char **)
RETSIGTYPE SnmpdShutDown (int a)
int main (int argc, char *argv[])

Variables

int snmp_dump_packet
int running = 1
int reconfig = 0
char ** argvrestartp
char * argvrestart
char * argvrestartname

Define Documentation

#define FD_CLR n,
     ((p)->fds_bits[(n)/NFDBITS] &= ~(1 << ((n) % NFDBITS)))
 

Definition at line 100 of file snmpd.c.

Referenced by main().

#define FD_ISSET n,
     ((p)->fds_bits[(n)/NFDBITS] & (1 << ((n) % NFDBITS)))
 

Definition at line 101 of file snmpd.c.

Referenced by main().

#define FD_SET n,
     ((p)->fds_bits[(n)/NFDBITS] |= (1 << ((n) % NFDBITS)))
 

Definition at line 99 of file snmpd.c.

Referenced by main().

#define FD_ZERO      memset((p), 0, sizeof(*(p)))
 

Definition at line 102 of file snmpd.c.

Referenced by agent_check_and_process(), and main().

#define NFDBITS   (sizeof(fd_mask) * NBBY)
 

Definition at line 98 of file snmpd.c.

#define NUM_SOCKETS   32
 

Definition at line 174 of file snmpd.c.

#define ONE_SEC   1000000L
 

Definition at line 163 of file snmpd.c.

#define TIMETICK   500000L
 

Definition at line 162 of file snmpd.c.


Typedef Documentation

typedef long fd_mask
 

Definition at line 97 of file snmpd.c.


Function Documentation

RETSIGTYPE SnmpdShutDown int    a
 

Definition at line 243 of file snmpd.c.

00244 {
00245         running = 0;
00246 }

int main int    argc,
char *    argv[]
 

Definition at line 291 of file snmpd.c.

00292 {
00293         int             arg, i;
00294         int             ret;
00295         int             dont_fork = 0;
00296         char            logfile[SNMP_MAXBUF_SMALL];
00297         char           *cptr, **argvptr;
00298         char           *pid_file = NULL;
00299         char            buf[SPRINT_MAX_LEN];
00300 #if HAVE_GETPID
00301         FILE           *PID;
00302 #endif
00303         int             dont_zero_log = 0;
00304         int             stderr_log=0, syslog_log=0;
00305         int             uid=0, gid=0;
00306         int             agent_mode=-1;
00307 
00308         logfile[0]              = 0;
00309 
00310 #ifdef LOGFILE
00311         strcpy(logfile, LOGFILE);
00312 #endif
00313 
00314 #ifdef NO_ROOT_ACCESS
00315         /* default to no */
00316         ds_set_boolean(DS_APPLICATION_ID, DS_AGENT_NO_ROOT_ACCESS, 1);
00317 #endif
00318                         /* Default to NOT running an AgentX master */
00319         ds_set_boolean(DS_APPLICATION_ID, DS_AGENT_AGENTX_MASTER, 0);
00320 
00321         /*
00322          * usage: snmpd
00323          */
00324         for (arg = 1; arg < argc; arg++)
00325           {
00326             if (argv[arg][0] == '-') {
00327               switch (argv[arg][1]) {
00328 
00329                 case 'c':
00330                   if (++arg == argc)
00331                     usage(argv[0]);
00332                   ds_set_string(DS_LIBRARY_ID, DS_LIB_OPTIONALCONFIG,
00333                                  argv[arg]);
00334                   break;
00335 
00336                 case 'C':
00337                     ds_set_boolean(DS_LIBRARY_ID, DS_LIB_DONT_READ_CONFIGS, 1);
00338                     break;
00339 
00340                 case 'd':
00341                     snmp_set_dump_packet(++snmp_dump_packet);
00342                     ds_set_boolean(DS_APPLICATION_ID, DS_AGENT_VERBOSE, 1);
00343                     break;
00344 
00345                 case 'q':
00346                     snmp_set_quick_print(1);
00347                     break;
00348 
00349                 case 'T':
00350                     if (argv[arg][2] != '\0') 
00351                         cptr = &argv[arg][2];
00352                     else if (++arg>argc) {
00353                         fprintf(stderr,"%s: Need UDP or TCP after -T flag.\n", argv[0]);
00354                         usage(argv[0]);
00355                         exit(1);
00356                     } else {
00357                         cptr = argv[arg];
00358                     }
00359                     if (strcasecmp(cptr,"TCP") == 0) {
00360                         ds_set_int(DS_APPLICATION_ID, DS_AGENT_FLAGS,
00361                                    ds_get_int(DS_APPLICATION_ID, DS_AGENT_FLAGS)
00362                                    | SNMP_FLAGS_STREAM_SOCKET);
00363                     } else if (strcasecmp(cptr,"UDP") == 0) {
00364                         /* default, do nothing */
00365                     } else {
00366                         fprintf(stderr,
00367                                 "%s: Unknown transport \"%s\" after -T flag.\n",
00368                                 argv[0], cptr);
00369                         usage(argv[0]);
00370                         exit(1);
00371                     }
00372                     break;
00373 
00374                 case 'D':
00375                     debug_register_tokens(&argv[arg][2]);
00376                     snmp_set_do_debugging(1);
00377                     break;
00378 
00379                 case 'p':
00380                   if (++arg == argc)
00381                     usage(argv[0]);
00382 
00383                   /* has something been specified before? */
00384                   cptr = ds_get_string(DS_APPLICATION_ID, DS_AGENT_PORTS);
00385                       
00386                   /* set the specification string up */
00387                   if (cptr) {
00388                       /*  Append to the older specification string.  */
00389                     sprintf(buf,"%s,%s", cptr, argv[arg]);
00390                   } else {
00391                     strcpy(buf,argv[arg]);
00392                   }
00393 
00394                   DEBUGMSGTL(("snmpd_ports","port spec: %s\n", buf));
00395                   ds_set_string(DS_APPLICATION_ID, DS_AGENT_PORTS, buf);
00396                   break;
00397 
00398 #if defined(USING_AGENTX_SUBAGENT_MODULE) || defined(USING_AGENTX_MASTER_MODULE)
00399                 case 'x':
00400                   if (++arg == argc)
00401                     usage(argv[0]);
00402                   ds_set_string(DS_APPLICATION_ID, DS_AGENT_X_SOCKET, argv[arg]);
00403                   ds_set_boolean(DS_APPLICATION_ID, DS_AGENT_AGENTX_MASTER, 1 );
00404                   break;
00405 #endif
00406 
00407                 case 'X':
00408 #if defined(USING_AGENTX_SUBAGENT_MODULE)
00409                   agent_mode = SUB_AGENT;
00410 #else
00411                   fprintf(stderr,"%s: Illegal argument -X: AgentX support not compiled in.\n", argv[0]);
00412                   usage(argv[0]);
00413                   exit(1);
00414 #endif
00415                   break;
00416 
00417                 case 'r':
00418                     ds_toggle_boolean(DS_APPLICATION_ID,
00419                                       DS_AGENT_NO_ROOT_ACCESS);
00420                     break;
00421 
00422                 case 'P':
00423                   if (++arg == argc)
00424                     usage(argv[0]);
00425                   pid_file = argv[arg];
00426 
00427                 case 'a':
00428                   log_addresses++;
00429                   break;
00430 
00431                 case 'V':
00432                   ds_set_boolean(DS_APPLICATION_ID, DS_AGENT_VERBOSE, 1);
00433                   break;
00434 
00435                 case 'f':
00436                   dont_fork = 1;
00437                   break;
00438 
00439                 case 'l':
00440                   if (++arg == argc)
00441                     usage(argv[0]);
00442                   strcpy(logfile, argv[arg]);
00443                   break;
00444 
00445                 case 'L':
00446                     stderr_log=1;
00447                     break;
00448 
00449                 case 's':
00450                     syslog_log=1;
00451                     break;
00452 
00453                 case 'A':
00454                     dont_zero_log = 1;
00455                     break;
00456 
00457                 case 'I':
00458                   if (++arg == argc) usage(argv[0]);
00459                   add_to_init_list(argv[arg]);
00460                   break;
00461 
00462 #if HAVE_UNISTD_H
00463                 case 'u':
00464                   if (++arg == argc) usage(argv[0]);
00465                   ds_set_int(DS_APPLICATION_ID, DS_AGENT_USERID,atoi(argv[arg]));
00466                   break;
00467                 case 'g':
00468                   if (++arg == argc) usage(argv[0]);
00469                   ds_set_int(DS_APPLICATION_ID, DS_AGENT_GROUPID, atoi(argv[arg]));
00470                   break;
00471 #endif
00472                 case 'h':
00473                   usage(argv[0]);
00474                   break;
00475                 case 'H':
00476                   ds_set_boolean(DS_APPLICATION_ID, DS_AGENT_NO_ROOT_ACCESS, 1);
00477                   init_agent("snmpd");   /* register our .conf handlers */
00478                   init_mib_modules();
00479                   init_snmp("snmpd");
00480                   fprintf(stderr, "Configuration directives understood:\n");
00481                   read_config_print_usage("  ");
00482                   exit(0);
00483                 case 'v':
00484                   printf("\nUCD-snmp version:  %s\n",VersionInfo);
00485                   printf("Web:               http://www.net-snmp.org/\n");
00486                   printf("Email:             net-snmp-coders@lists.sourceforge.net\n\n");
00487                   exit (0);
00488                 case '-':
00489                   switch(argv[arg][2]){
00490                     case 'v': 
00491                       printf("\nUCD-snmp version:  %s\n",VersionInfo);
00492                       printf("Web:               http://www.net-snmp.org/\n");
00493                       printf("Email:             net-snmp-coders@lists.sourceforge.net\n\n");
00494                       exit (0);
00495                     case 'h':
00496                       usage(argv[0]);
00497                       exit(0);
00498                   }
00499 
00500                 default:
00501                   fprintf(stderr, "%s: Invalid option: %s\n", argv[0], argv[arg]);
00502                   usage(argv[0]);
00503                   break;
00504               }
00505               continue;
00506             }
00507             else {
00508               fprintf(stderr, "%s: Bad argument: %s\n", argv[0], argv[arg]);
00509               exit(1);
00510             }
00511         }  /* end-for */
00512 
00513         /* honor selection of standard error output */
00514         if (!stderr_log)
00515                 snmp_disable_stderrlog();
00516 
00517         /* 
00518          * Initialize a argv set to the current for restarting the agent.
00519          */
00520         argvrestartp = (char **) malloc((argc + 2) * sizeof(char *));
00521         argvptr = argvrestartp;
00522         for (i = 0, ret = 1; i < argc; i++) {
00523                 ret += strlen(argv[i]) + 1;
00524         }
00525         argvrestart = (char *) malloc(ret);
00526         argvrestartname = (char *) malloc(strlen(argv[0]) + 1);
00527         strcpy(argvrestartname, argv[0]);
00528         if (agent_mode == -1) {
00529             if ( strstr(argvrestartname, "agentxd") != NULL )
00530                 ds_set_boolean(DS_APPLICATION_ID, DS_AGENT_ROLE, SUB_AGENT);
00531             else
00532                 ds_set_boolean(DS_APPLICATION_ID, DS_AGENT_ROLE, MASTER_AGENT);
00533         } else {
00534             ds_set_boolean(DS_APPLICATION_ID, DS_AGENT_ROLE, agent_mode);
00535         }
00536         
00537         for (cptr = argvrestart, i = 0; i < argc; i++) {
00538                 strcpy(cptr, argv[i]);
00539                 *(argvptr++) = cptr;
00540                 cptr += strlen(argv[i]) + 1;
00541         }
00542         *cptr = 0;
00543         *argvptr = NULL;
00544 
00545         /* 
00546          * Open the logfile if necessary.
00547          */
00548 
00549         /* Should open logfile and/or syslog based on arguments */
00550         if (logfile[0])
00551                 snmp_enable_filelog(logfile, dont_zero_log);
00552         if (syslog_log)
00553                 snmp_enable_syslog(); 
00554 #ifdef BUFSIZ
00555         setvbuf(stdout, NULL, _IOLBF, BUFSIZ);
00556 #endif
00557     /* 
00558      * Initialize the world.  Detach from the shell.
00559      * Create initial user.
00560      */
00561 #if HAVE_FORK
00562     if (!dont_fork && fork() != 0) {
00563       exit(0);
00564     }
00565 #endif
00566 
00567 #if HAVE_GETPID
00568     if (pid_file != NULL) {
00569       if ((PID = fopen(pid_file, "w")) == NULL) {
00570         snmp_log_perror("fopen");
00571         if (!ds_get_boolean(DS_APPLICATION_ID, DS_AGENT_NO_ROOT_ACCESS))
00572           exit(1);
00573       }
00574       else {
00575         fprintf(PID, "%d\n", (int)getpid());
00576         fclose(PID);
00577       }
00578     }
00579 #endif
00580 
00581     SOCK_STARTUP;
00582     init_agent("snmpd");                /* do what we need to do first. */
00583     init_mib_modules();
00584     
00585 
00586     /* start library */
00587     init_snmp("snmpd");
00588 
00589     if ((ret = init_master_agent()) != 0) {
00590       /*  Some error opening one of the specified agent transports.  */
00591       Exit(1); /*  Exit logs exit val for us  */
00592     }
00593 
00594 #ifdef SIGTERM
00595     signal(SIGTERM, SnmpdShutDown);
00596 #endif
00597 #ifdef SIGINT
00598     signal(SIGINT, SnmpdShutDown);
00599 #endif
00600 #ifdef SIGHUP
00601     signal(SIGHUP, SnmpdReconfig);
00602 #endif
00603 #ifdef SIGUSR1
00604     signal(SIGUSR1, SnmpdDump);
00605 #endif
00606 #ifdef SIGPIPE
00607     signal(SIGPIPE, SIG_IGN);   /* 'Inline' failure of wayward readers */
00608 #endif
00609 
00610     /* store persistent data immediately in case we crash later */
00611     snmp_store("snmpd");
00612 
00613     /* send coldstart trap via snmptrap(1) if possible */
00614     send_easy_trap (0, 0);
00615         
00616 #if HAVE_UNISTD_H
00617 #ifdef HAVE_SETGID
00618         if ((gid = ds_get_int(DS_APPLICATION_ID, DS_AGENT_GROUPID)) != 0) {
00619                 DEBUGMSGTL(("snmpd", "Changing gid to %d.\n", gid));
00620                 if (setgid(gid)==-1) {
00621                         snmp_log_perror("setgid failed");
00622                         if (!ds_get_boolean(DS_APPLICATION_ID, DS_AGENT_NO_ROOT_ACCESS))
00623                             exit(1);
00624                 }
00625         }
00626 #endif
00627 #ifdef HAVE_SETUID
00628         if ((uid = ds_get_int(DS_APPLICATION_ID, DS_AGENT_USERID)) != 0) {
00629                 DEBUGMSGTL(("snmpd", "Changing uid to %d.\n", uid));
00630                 if(setuid(uid)==-1) {
00631                         snmp_log_perror("setuid failed");
00632                         if (!ds_get_boolean(DS_APPLICATION_ID, DS_AGENT_NO_ROOT_ACCESS))
00633                             exit(1);
00634                 }
00635         }
00636 #endif
00637 #endif
00638 
00639         /* we're up, log our version number */
00640         snmp_log(LOG_INFO, "UCD-SNMP version %s\n", VersionInfo);
00641         snmp_addrcache_initialise();
00642         /* 
00643          * Forever monitor the dest_port for incoming PDUs.
00644          */
00645         DEBUGMSGTL(("snmpd", "We're up.  Starting to process data.\n"));
00646         receive();
00647 #include "mib_module_shutdown.h"
00648         DEBUGMSGTL(("snmpd", "sending shutdown trap\n"));
00649         SnmpTrapNodeDown();
00650         DEBUGMSGTL(("snmpd", "Bye...\n"));
00651         snmp_shutdown("snmpd");
00652         return 0;
00653 
00654 }  /* End main() -- snmpd */

int main int   ,
char **   
 

int snmp_input int    op,
struct snmp_session *    session,
int    reqid,
struct snmp_pdu *    pdu,
void *    magic
 

Definition at line 883 of file snmpd.c.

00888 {
00889     struct get_req_state *state = (struct get_req_state *)magic;
00890     
00891     if (op == SNMP_CALLBACK_OP_RECEIVED_MESSAGE) {
00892         if (pdu->command == SNMP_MSG_GET) {
00893             if (state->type == EVENT_GET_REQ) {
00894                 /* this is just the ack to our inform pdu */
00895                 return 1;
00896             }
00897         }
00898     }
00899     else if (op == SNMP_CALLBACK_OP_TIMED_OUT) {
00900         if (state->type == ALARM_GET_REQ) {
00901                 /* Need a mechanism to replace obsolete SNMPv2p alarm */
00902         }
00903     }
00904     return 1;
00905 
00906 } /* end snmp_input() */

int snmp_read_packet int   
 


Variable Documentation

char* argvrestart
 

Definition at line 171 of file snmpd.c.

char* argvrestartname
 

Definition at line 172 of file snmpd.c.

char** argvrestartp
 

Definition at line 170 of file snmpd.c.

int reconfig = 0
 

Definition at line 167 of file snmpd.c.

int running = 1
 

Definition at line 166 of file snmpd.c.

int snmp_dump_packet
 

Definition at line 165 of file snmpd.c.


Generated on Sat Nov 10 14:09:56 2001 for net-snmp by doxygen1.2.11 written by Dimitri van Heesch, © 1997-2001
[an error occurred while processing this directive] [an error occurred while processing this directive]

Valid CSS!


Last modified: Wednesday, 01-Aug-2018 04:41:28 UTC
For questions regarding web content and site functionality, please write to the net-snmp-users mail list.