SNMP Subagent Programmer's Guide
Programming Tutorials
SNMP Subagent Programmer’s Guide—119728 2-31
Fault-Tolerant Hello World Program
 /*  Set up each agent environment */
 living_agent_count = 0;
 for (i=1; i<argc; i++) {                        
<-- 4
 fprintf(stderr, "agent #%i is process %s\n", i, argv[i]);
 /*  Set up the management environment,  registering ourselves as
  *   1.3.6.1.4.1.442.2.1 with no password. */
 if ((agent_info[i].mgmt_env = mgmt_init_env(argv[i],            
<-- 5
            (ObjId_t *) &GI_hello,
            "Fault Tolerant Hello World Example",
            (Octets_t *) NULL,
            (void_function) NULL)) == NULL)
  {
   (void) fprintf(stderr, "%s: mgmt_init_env failure opening %s.\n",
         argv[0], argv[i]);
   return(2);
  }
 /* Let the agent know information what we're responsible for. */
 if ((agent_info[i].mgmt_handle = w(agent_info[i].mgmt_env,          
<-- 6
                  &SMI_GROUP_hello,
                  (void *)NULL)) == NULL)
  {
   (void) fprintf(stderr, "%s: mgmt_new_instance failure on #%i (%s)\n",
         argv[0], i, argv[i]);
   return(2);
  }
 /* allocate an IPC buffer: */
  agent_info[i].mgmt_env->sess->agent_msg_buffer =
   (char *) malloc (SNMP_MSG_MAX);                   
<-- 7
 if (agent_info[i].mgmt_env->sess->agent_msg_buffer == NULL) {
  (void) fprintf(stderr,
    "%s: failure to malloc IPC buffer for agent #%\n", argv[0], i);
  return(2);
 }
  /* start a READ on each agent */
 agent_info[i].agent_filenumber = mgmt_read_nowait(             
<-- 8
          (struct mgmt_env *) agent_info[i].mgmt_env, NULL);
 if (agent_info[i].agent_filenumber == -1) {
  (void) fprintf(stderr, "%s: error from mgmt_read_nowait\n", argv[0]);
  return(2);
 }
 living_agent_count++;
 } /* end for */
/* main loop */
 for(;;) {                              
<-- 9
wait:
 if (living_agent_count < 1) {
  (void) fprintf (stderr, "all agent processes died; I'm quitting too!\n");
  exit(1);
  } /* end if no agents */
  event_fileno = -1;
 status = AWAITIOX(&event_fileno,,&count_xferd,,100*helloPrintFreq); <-- 
10
 if (status != CCE) {
 FILE_GETINFO_(event_fileno, &error);            <-- 
11
  if (error == 40) { /* timeout! */
   (void) fprintf (stderr,"%i second time out\n", helloPrintFreq);
   (void) fputs((char *) helloText.val, stderr);
   (void) fputs("\n", stderr);
   helloPrintCnt++;
   goto wait;
  } else {
   /* handle other errors here */
   (void) fprintf(stderr, "AWAITIO error %i on file %i\n", error,
       event_fileno);
   for (i=1; i<argc; i++) {
  if (event_fileno == agent_info[i].agent_filenumber) {     <-- 
12
Example 2-4. Fault-Tolerant Hello World Program (page 2 of 3)










