SNMP Subagent Programmer's Guide
Programming Tutorials
SNMP Subagent Programmer’s Guide—119728 2-17
Hello World Program
 if ((mgmt_env = mgmt_init_env(argv[1],            <-- 
9
               (ObjId_t *) &GI_hello,
               "Hello World Example",
               (Octets_t *) NULL,
               (void_function) NULL)) == NULL)
   {
       (void) fputs(argv[0], stderr);
       (void) fputs(": mgmt_init_env failure\n", stderr);
       return(1);
   }
   /*
    *    Let the agent know information what we're responsible for.
    */
  if ((mgmt_hello_handle = mgmt_new_instance(mgmt_env,     <-- 
10
                    &SMI_GROUP_hello,
                    (void *)NULL)) == NULL)
   {
       (void) fputs(argv[0], stderr);
       (void) fputs(": mgmt_new_instance failure\n", stderr);
       mgmt_term_env(mgmt_env);
       mgmt_env = NULL;
       return(2);
 while (!time_to_die)                    <-- 
11
   {
       /*  Wait for something to happen, either alarm or
       *    some kind of management operation*/
   if (mgmt_poll(mgmt_env, (struct timeval *) NULL,&dummy) < 0) { <-- 
12
          (void) fputs(argv[0], stderr);
          (void) fputs(": agent unavailable\n", stderr);
          break;
       }
       /*    If the print frequency has been modified output the
       *    helloFreqChange trap.
       */
     if (last_freq != helloPrintFreq)          <-- 
13
       {
             last_freq = helloPrintFreq;
         (void) mgmt_trap(mgmt_env,        <-- 
14
                       &TRAP_helloFreqChange);
       }
       /*
     *  print whatever the message currently is,
       *    keeping a count, of course.
       */
     helloText.val[helloText.len] = '\0';         <-- 
15
       (void) fputs((char *) helloText.val, stderr);
       (void) fputs("\n", stderr);
     helloPrintCnt++;                <-- 
16
   }
   /*
    *    We encountered a unrecoverable condition. Clean
   *  up and go away.
    */
  if (mgmt_env)                    <-- 
17
   {
       if (mgmt_hello_handle)
       {
      mgmt_del_instance(mgmt_env, mgmt_hello_handle);     <-- 
18
          mgmt_hello_handle = NULL;
       }
     mgmt_term_env(mgmt_env);             <-- 
19
       mgmt_env = NULL;
}
Example 2-2. Hello World Source Code (page 2 of 2)










