components/net-snmp-57/sun/agent/modules/entityMib/entPhysicalTable.c
changeset 5867 445e2cf1c845
parent 252 ee0fb1eabcbf
equal deleted inserted replaced
5866:683c5c035a79 5867:445e2cf1c845
       
     1 /*
       
     2  * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
       
     3  *
       
     4  * U.S. Government Rights - Commercial software. Government users are subject
       
     5  * to the Sun Microsystems, Inc. standard license agreement and applicable
       
     6  * provisions of the FAR and its supplements.
       
     7  *
       
     8  *
       
     9  * This distribution may include materials developed by third parties. Sun,
       
    10  * Sun Microsystems, the Sun logo and Solaris are trademarks or registered
       
    11  * trademarks of Sun Microsystems, Inc. in the U.S. and other countries.
       
    12  *
       
    13  */
       
    14 /*
       
    15  * Note: this file originally auto-generated by mib2c using
       
    16  *        : mib2c.iterate.conf,v 5.5 2002/12/16 22:50:18 hardaker Exp $
       
    17  */
       
    18 
       
    19 #include <net-snmp/net-snmp-config.h>
       
    20 #include <net-snmp/net-snmp-includes.h>
       
    21 #include <net-snmp/agent/net-snmp-agent-includes.h>
       
    22 #include "stdhdr.h"
       
    23 #include "entPhysicalTable.h"
       
    24 #include "entPhysicalContainsTable.h"
       
    25 #include "entLastChangeTime.h"
       
    26 #include "entAliasMappingTable.h"
       
    27 #include "entLPMappingTable.h"
       
    28 
       
    29 entPhysicalEntry_t *gPhysicalTableTail, *gPhysicalTableHead;
       
    30 int gPhysicalTableSize;
       
    31 #define ENTPHYSICALSERIALNUM_CACHE "entphyserialnum_cache"
       
    32 /* Fix for 4915824 */
       
    33 #define ENTPHYSICALALIAS_CACHE "entphyalias_cache"
       
    34 #define ENTPHYSICALASSETID_CACHE "entphyassetid_cache"
       
    35 /* End of Fix for 4915824 */
       
    36 
       
    37 static int AddToPhysicalTable(entPhysicalEntry_t *);
       
    38 static int FreePhysicalEntry(entPhysicalEntry_t *);
       
    39 
       
    40 /** Initialize the entPhysicalTable table by defining its contents and how it's structured */
       
    41 void
       
    42 initialize_table_entPhysicalTable(void)
       
    43 {
       
    44     static oid entPhysicalTable_oid[] = {1,3,6,1,2,1,47,1,1,1};
       
    45     netsnmp_table_registration_info *table_info;
       
    46     netsnmp_handler_registration *my_handler;
       
    47     netsnmp_iterator_info *iinfo;
       
    48 
       
    49     /* create the table structure itself */
       
    50     table_info = SNMP_MALLOC_TYPEDEF(netsnmp_table_registration_info);
       
    51     iinfo = SNMP_MALLOC_TYPEDEF(netsnmp_iterator_info);
       
    52 
       
    53     /* if your table is read only, it's easiest to change the
       
    54        HANDLER_CAN_RWRITE definition below to HANDLER_CAN_RONLY */
       
    55     /* Fix for 4921485: Temporarily disabling write access, until we */
       
    56     /*                  actually need to re-enable it */
       
    57     my_handler = netsnmp_create_handler_registration("entPhysicalTable",
       
    58                                                      entPhysicalTable_handler,
       
    59                                                      entPhysicalTable_oid,
       
    60                                                      OID_LENGTH(entPhysicalTable_oid),
       
    61                                                      HANDLER_CAN_RONLY);
       
    62     /* End of Fix for 4921485 */
       
    63 
       
    64     /* Future Fix for 4922782: When we re-enable write access, we will also */
       
    65     /*                         look into 4914919 - persistency.  Of course */
       
    66     /*                         we need to remove 4921485 and enable the */
       
    67     /*                         CAN_RWRITE.   Treat the write handling code */
       
    68     /*                         in entPhysicalTable_handlerNeeds as hostile: */
       
    69     /*                         everything should be tested and properly */
       
    70     /*                         reviewed.  Pay special attention to when */
       
    71     /*                         to return error codes */
       
    72 /*
       
    73     my_handler = netsnmp_create_handler_registration("entPhysicalTable",
       
    74                                                      entPhysicalTable_handler,
       
    75                                                      entPhysicalTable_oid,
       
    76                                                      OID_LENGTH(entPhysicalTable_oid),
       
    77                                                      HANDLER_CAN_RWRITE);
       
    78 */
       
    79     /* End of Future Fix for 4922782 */
       
    80             
       
    81     if (!my_handler || !table_info || !iinfo)
       
    82         return; /* mallocs failed */
       
    83 
       
    84     /***************************************************
       
    85      * Setting up the table's definition
       
    86      */
       
    87     netsnmp_table_helper_add_indexes(table_info,
       
    88                                      ASN_INTEGER, /* index: entPhysicalIndex */
       
    89                                      0);
       
    90 
       
    91     table_info->min_column = 2;
       
    92     table_info->max_column = 16;
       
    93 
       
    94     /* iterator access routines */
       
    95     iinfo->get_first_data_point = entPhysicalTable_get_first_data_point;
       
    96     iinfo->get_next_data_point = entPhysicalTable_get_next_data_point;
       
    97 
       
    98     iinfo->table_reginfo = table_info;
       
    99 
       
   100     /***************************************************
       
   101      * registering the table with the master agent
       
   102      */
       
   103     DEBUGMSGTL(("initialize_table_entPhysicalTable",
       
   104                 "Registering table entPhysicalTable as a table iterator\n"));		 
       
   105     netsnmp_register_table_iterator(my_handler, iinfo);
       
   106 }
       
   107 
       
   108 /** Initializes the entPhysicalTable module */
       
   109 void
       
   110 init_entPhysicalTable(void)
       
   111 {
       
   112 
       
   113     /* here we initialize all the tables we're planning on supporting */
       
   114     initialize_table_entPhysicalTable();
       
   115     gPhysicalTableTail = gPhysicalTableHead = NULL;
       
   116     gPhysicalTableSize = 0;
       
   117 }
       
   118 
       
   119 /** returns the first data point within the entPhysicalTable table data.
       
   120 
       
   121     Set the my_loop_context variable to the first data point structure
       
   122     of your choice (from which you can find the next one).  This could
       
   123     be anything from the first node in a linked list, to an integer
       
   124     pointer containing the beginning of an array variable.
       
   125 
       
   126     Set the my_data_context variable to something to be returned to
       
   127     you later that will provide you with the data to return in a given
       
   128     row.  This could be the same pointer as what my_loop_context is
       
   129     set to, or something different.
       
   130 
       
   131     The put_index_data variable contains a list of snmp variable
       
   132     bindings, one for each index in your table.  Set the values of
       
   133     each appropriately according to the data matching the first row
       
   134     and return the put_index_data variable at the end of the function.
       
   135 */
       
   136 netsnmp_variable_list *
       
   137 entPhysicalTable_get_first_data_point(void **my_loop_context, void **my_data_context,
       
   138                                       netsnmp_variable_list *put_index_data,
       
   139                                       netsnmp_iterator_info *mydata)
       
   140 {
       
   141 
       
   142     netsnmp_variable_list *vptr;
       
   143     entPhysicalEntry_t *zRunner;
       
   144     zRunner = gPhysicalTableHead;
       
   145 
       
   146     while (zRunner) {
       
   147         if (zRunner->entPhysicalIndex > 0)
       
   148             break;
       
   149         zRunner = zRunner->pNextEntry;
       
   150     }
       
   151 
       
   152     if (zRunner == NULL) return NULL;
       
   153 
       
   154     *my_loop_context = (void *)zRunner;
       
   155     *my_data_context = (void *)zRunner;
       
   156 
       
   157     vptr = put_index_data;
       
   158     
       
   159     snmp_set_var_value(vptr, (u_char *)&zRunner->entPhysicalIndex, sizeof(zRunner->entPhysicalIndex));
       
   160     vptr = vptr->next_variable;
       
   161 
       
   162     return put_index_data;
       
   163 }
       
   164 
       
   165 /** functionally the same as entPhysicalTable_get_first_data_point, but
       
   166    my_loop_context has already been set to a previous value and should
       
   167    be updated to the next in the list.  For example, if it was a
       
   168    linked list, you might want to cast it and the return
       
   169    my_loop_context->next.  The my_data_context pointer should be set
       
   170    to something you need later and the indexes in put_index_data
       
   171    updated again. */
       
   172 
       
   173 netsnmp_variable_list *
       
   174 entPhysicalTable_get_next_data_point(void **my_loop_context, void **my_data_context,
       
   175                                      netsnmp_variable_list *put_index_data,
       
   176                                      netsnmp_iterator_info *mydata)
       
   177 {
       
   178 
       
   179     netsnmp_variable_list *vptr;
       
   180     entPhysicalEntry_t *zRunner;
       
   181 
       
   182     zRunner = (entPhysicalEntry_t *) *my_loop_context;
       
   183 
       
   184     while (zRunner) {
       
   185         zRunner = zRunner->pNextEntry;
       
   186         if (zRunner && zRunner->entPhysicalIndex > 0)
       
   187             break;
       
   188     }
       
   189 
       
   190     if (zRunner == NULL) return NULL;
       
   191 
       
   192     *my_loop_context = (void *)zRunner;
       
   193     *my_data_context = (void *)zRunner;
       
   194 
       
   195     vptr = put_index_data;
       
   196     
       
   197     snmp_set_var_value(vptr, (u_char *)&zRunner->entPhysicalIndex, sizeof(zRunner->entPhysicalIndex));
       
   198     vptr = vptr->next_variable;
       
   199 
       
   200     return put_index_data;
       
   201 }
       
   202 
       
   203 /** handles requests for the entPhysicalTable table, if anything else needs to be done */
       
   204 int
       
   205 entPhysicalTable_handler(
       
   206     netsnmp_mib_handler               *handler,
       
   207     netsnmp_handler_registration      *reginfo,
       
   208     netsnmp_agent_request_info        *reqinfo,
       
   209     netsnmp_request_info              *requests) {
       
   210 
       
   211     netsnmp_request_info *request;
       
   212     netsnmp_table_request_info *table_info;
       
   213     netsnmp_variable_list *var;
       
   214     entPhysicalEntry_t *zPhysicalEntry;
       
   215     static char *zentPhySerialNum, *zentPhyAlias, *zentPhyAssetId;
       
   216     static char *zOldentPhySerialNum, *zOldentPhyAlias, *zOldentPhyAssetId;
       
   217     
       
   218     for(request = requests; request; request = request->next) {
       
   219         var = request->requestvb;
       
   220         if (request->processed != 0)
       
   221             continue;
       
   222 
       
   223         /* perform anything here that you need to do before each
       
   224            request is processed. */
       
   225 
       
   226         /* the following extracts the my_data_context pointer set in
       
   227            the loop functions above.  You can then use the results to
       
   228            help return data for the columns of the entPhysicalTable table in question */
       
   229         zPhysicalEntry = (entPhysicalEntry_t *) netsnmp_extract_iterator_context(request);
       
   230         if ((zPhysicalEntry==NULL)||(zPhysicalEntry->entPhysicalIndex <= 0) ) {
       
   231             if (reqinfo->mode == MODE_GET) {
       
   232                 netsnmp_set_request_error(reqinfo, request, SNMP_NOSUCHINSTANCE);
       
   233                 continue;
       
   234             }
       
   235             /* XXX: no row existed, if you support creation and this is a
       
   236                set, start dealing with it here, else continue */
       
   237         }
       
   238 
       
   239         /* extracts the information about the table from the request */
       
   240         table_info = netsnmp_extract_table_info(request);
       
   241         /* table_info->colnum contains the column number requested */
       
   242         /* table_info->indexes contains a linked list of snmp variable
       
   243            bindings for the indexes of the table.  Values in the list
       
   244            have been set corresponding to the indexes of the
       
   245            request */
       
   246         if (table_info==NULL) {
       
   247             continue;
       
   248         }
       
   249 
       
   250         switch(reqinfo->mode) {
       
   251             /* the table_iterator helper should change all GETNEXTs
       
   252                into GETs for you automatically, so you don't have to
       
   253                worry about the GETNEXT case.  Only GETs and SETs need
       
   254                to be dealt with here */
       
   255         case MODE_GET:
       
   256             switch(table_info->colnum) {
       
   257             case COLUMN_ENTPHYSICALDESCR:
       
   258                 snmp_set_var_typed_value(var, ASN_OCTET_STR, (u_char *) zPhysicalEntry->entPhysicalDescr, strlen(zPhysicalEntry->entPhysicalDescr));
       
   259                 break;
       
   260 
       
   261             case COLUMN_ENTPHYSICALVENDORTYPE:
       
   262                 snmp_set_var_typed_value(var, ASN_OBJECT_ID, (u_char *) zPhysicalEntry->entPhysicalVendorType, zPhysicalEntry->entPhysicalVendorTypeSize);
       
   263                 break;
       
   264 
       
   265             case COLUMN_ENTPHYSICALCONTAINEDIN:
       
   266                 snmp_set_var_typed_value(var, ASN_INTEGER, (u_char *)&zPhysicalEntry->entPhysicalContainedIn, sizeof(zPhysicalEntry->entPhysicalContainedIn));
       
   267                 break;
       
   268 
       
   269             case COLUMN_ENTPHYSICALCLASS:
       
   270                 snmp_set_var_typed_value(var, ASN_INTEGER, (u_char *)&zPhysicalEntry->entPhysicalClass, sizeof(zPhysicalEntry->entPhysicalClass));
       
   271                 break;
       
   272 
       
   273             case COLUMN_ENTPHYSICALPARENTRELPOS:
       
   274                 snmp_set_var_typed_value(var, ASN_INTEGER, (u_char *)&zPhysicalEntry->entPhysicalParentRelPos, sizeof(zPhysicalEntry->entPhysicalParentRelPos));
       
   275                 break;
       
   276 
       
   277             case COLUMN_ENTPHYSICALNAME:
       
   278                 snmp_set_var_typed_value(var, ASN_OCTET_STR, (u_char *) zPhysicalEntry->entPhysicalName, strlen(zPhysicalEntry->entPhysicalName));
       
   279                 break;
       
   280 
       
   281             case COLUMN_ENTPHYSICALHARDWAREREV:
       
   282                 snmp_set_var_typed_value(var, ASN_OCTET_STR, (u_char *) zPhysicalEntry->entPhysicalHardwareRev, strlen(zPhysicalEntry->entPhysicalHardwareRev));
       
   283                 break;
       
   284 
       
   285             case COLUMN_ENTPHYSICALFIRMWAREREV:
       
   286                 snmp_set_var_typed_value(var, ASN_OCTET_STR, (u_char *) zPhysicalEntry->entPhysicalFirmwareRev, strlen(zPhysicalEntry->entPhysicalFirmwareRev));
       
   287                 break;
       
   288 
       
   289             case COLUMN_ENTPHYSICALSOFTWAREREV:
       
   290                 snmp_set_var_typed_value(var, ASN_OCTET_STR, (u_char *) zPhysicalEntry->entPhysicalSoftwareRev, strlen(zPhysicalEntry->entPhysicalSoftwareRev));
       
   291                 break;
       
   292 
       
   293             case COLUMN_ENTPHYSICALSERIALNUM:
       
   294                 snmp_set_var_typed_value(var, ASN_OCTET_STR, (u_char *) zPhysicalEntry->entPhysicalSerialNum, strlen(zPhysicalEntry->entPhysicalSerialNum));
       
   295                 break;
       
   296 
       
   297             case COLUMN_ENTPHYSICALMFGNAME:
       
   298                 snmp_set_var_typed_value(var, ASN_OCTET_STR, (u_char *) zPhysicalEntry->entPhysicalMfgName, strlen(zPhysicalEntry->entPhysicalMfgName));
       
   299                 break;
       
   300 
       
   301             case COLUMN_ENTPHYSICALMODELNAME:
       
   302                 snmp_set_var_typed_value(var, ASN_OCTET_STR, (u_char *) zPhysicalEntry->entPhysicalModelName, strlen(zPhysicalEntry->entPhysicalModelName));
       
   303                 break;
       
   304 
       
   305             case COLUMN_ENTPHYSICALALIAS:
       
   306                 snmp_set_var_typed_value(var, ASN_OCTET_STR, (u_char *) zPhysicalEntry->entPhysicalAlias, strlen(zPhysicalEntry->entPhysicalAlias));
       
   307                 break;
       
   308 
       
   309             case COLUMN_ENTPHYSICALASSETID:
       
   310                 snmp_set_var_typed_value(var, ASN_OCTET_STR, (u_char *) zPhysicalEntry->entPhysicalAssetID, strlen(zPhysicalEntry->entPhysicalAssetID));
       
   311                 break;
       
   312 
       
   313             case COLUMN_ENTPHYSICALISFRU:
       
   314                 snmp_set_var_typed_value(var, ASN_INTEGER, (u_char *) &zPhysicalEntry->entPhysicalIsFRU, sizeof(zPhysicalEntry->entPhysicalIsFRU));
       
   315                 break;
       
   316 
       
   317             default:
       
   318                 /* We shouldn't get here */
       
   319                 snmp_log(LOG_ERR, "problem encountered in entPhysicalTable_handler: unknown column\n");
       
   320             }
       
   321             break;
       
   322 
       
   323         /* Fix for 4915824: We have rewritten much of the section here, and */
       
   324         /*                  we have also improved the way the boundary */
       
   325         /*                  checking and return code is handled */
       
   326         case MODE_SET_RESERVE1:
       
   327             switch(table_info->colnum) {
       
   328             case COLUMN_ENTPHYSICALSERIALNUM:
       
   329                 if (var->type != ASN_OCTET_STR) {
       
   330                     netsnmp_set_request_error(reqinfo, request, SNMP_ERR_WRONGTYPE);
       
   331                     return SNMP_ERR_WRONGTYPE;
       
   332                 }
       
   333                 /* Fix for 4911817: Check for size and boundary limits */
       
   334                 else if ((const char*)var->val.string != NULL) {
       
   335                     if (var->val_len > 32) {
       
   336                         netsnmp_set_request_error(reqinfo, request, SNMP_ERR_WRONGLENGTH);
       
   337                         return SNMP_ERR_WRONGLENGTH;
       
   338                     }
       
   339                 }
       
   340                 /* End of Fix for 4911817 */
       
   341                 break;
       
   342             case COLUMN_ENTPHYSICALALIAS:
       
   343                 if (var->type != ASN_OCTET_STR) {
       
   344                     netsnmp_set_request_error(reqinfo, request, SNMP_ERR_WRONGTYPE);
       
   345                     return SNMP_ERR_WRONGTYPE;
       
   346                 }
       
   347                 /* Fix for 4911817: Check for size and boundary limits */
       
   348                 else if ((const char*)var->val.string != NULL) {
       
   349                     if (var->val_len > 32) {
       
   350                         netsnmp_set_request_error(reqinfo, request, SNMP_ERR_WRONGLENGTH);
       
   351                         return SNMP_ERR_WRONGLENGTH;
       
   352                     }
       
   353                 }
       
   354                 /* End of Fix for 4911817 */
       
   355                 break;
       
   356             case COLUMN_ENTPHYSICALASSETID:
       
   357                 if (var->type != ASN_OCTET_STR) {
       
   358                     netsnmp_set_request_error(reqinfo, request, SNMP_ERR_WRONGTYPE);
       
   359                     return SNMP_ERR_WRONGTYPE;
       
   360                 }
       
   361                 /* Fix for 4911817: Check for size and boundary limits */
       
   362                 else if ((const char*)var->val.string != NULL) {
       
   363                     if (var->val_len > 32) {
       
   364                         netsnmp_set_request_error(reqinfo, request, SNMP_ERR_WRONGLENGTH);
       
   365                         return SNMP_ERR_WRONGLENGTH;
       
   366                     }
       
   367                 }
       
   368                 /* End of Fix for 4911817 */
       
   369                 break;
       
   370             default:
       
   371                 netsnmp_set_request_error(reqinfo, request, SNMP_ERR_NOTWRITABLE);
       
   372             }
       
   373             break;
       
   374         case MODE_SET_RESERVE2:
       
   375             switch(table_info->colnum) {
       
   376             case COLUMN_ENTPHYSICALSERIALNUM:
       
   377                 zOldentPhySerialNum = strdup(zPhysicalEntry->entPhysicalSerialNum);
       
   378                 if (zOldentPhySerialNum == NULL)
       
   379                     netsnmp_set_request_error(reqinfo, request, SNMP_ERR_RESOURCEUNAVAILABLE);
       
   380                 netsnmp_request_add_list_data (request, 
       
   381                                                netsnmp_create_data_list(ENTPHYSICALSERIALNUM_CACHE,
       
   382                                                                         zOldentPhySerialNum,
       
   383                                                                         free));
       
   384                 break;
       
   385             case COLUMN_ENTPHYSICALALIAS:
       
   386                 zOldentPhyAlias = strdup(zPhysicalEntry->entPhysicalAlias);
       
   387                 if (zOldentPhyAlias == NULL)
       
   388                     netsnmp_set_request_error(reqinfo, request, SNMP_ERR_RESOURCEUNAVAILABLE);
       
   389                 netsnmp_request_add_list_data (request,
       
   390                                                netsnmp_create_data_list(ENTPHYSICALALIAS_CACHE,
       
   391                                                                         zOldentPhyAlias,
       
   392                                                                         free));
       
   393                 break;
       
   394             case COLUMN_ENTPHYSICALASSETID:
       
   395                 zOldentPhyAssetId = strdup(zPhysicalEntry->entPhysicalAssetID);
       
   396                 if (zOldentPhyAssetId == NULL)
       
   397                     netsnmp_set_request_error(reqinfo, request, SNMP_ERR_RESOURCEUNAVAILABLE);
       
   398                 netsnmp_request_add_list_data (request,
       
   399                                                netsnmp_create_data_list(ENTPHYSICALASSETID_CACHE,
       
   400                                                                         zOldentPhyAssetId,
       
   401                                                                         free));
       
   402                 break;
       
   403             default:
       
   404                 netsnmp_set_request_error(reqinfo, request, SNMP_ERR_NOTWRITABLE);
       
   405             }
       
   406             break;
       
   407         case MODE_SET_FREE:
       
   408             switch(table_info->colnum) {
       
   409             /* We do not need to free anything, because the zOld* will be */
       
   410             /* freed as part of the request */
       
   411             case COLUMN_ENTPHYSICALSERIALNUM:
       
   412                 break;
       
   413             case COLUMN_ENTPHYSICALALIAS:
       
   414                 break;
       
   415             case COLUMN_ENTPHYSICALASSETID:
       
   416                 break;
       
   417 
       
   418             default:
       
   419                 netsnmp_set_request_error(reqinfo, request, SNMP_ERR_NOTWRITABLE);
       
   420             }
       
   421             break;
       
   422         case MODE_SET_ACTION:
       
   423             switch(table_info->colnum) {
       
   424             case COLUMN_ENTPHYSICALSERIALNUM:
       
   425                 /* As part of the 4915824 fix, we have improved */
       
   426                 /* the way we are doing the string copying. Using */
       
   427                 /* var->val_len will guarantee that we are not copying junk, */
       
   428                 /* since var->val.string is not NULL terminated*/
       
   429                 free (zPhysicalEntry->entPhysicalSerialNum);
       
   430                 zPhysicalEntry->entPhysicalSerialNum = (char *)malloc(sizeof(char) * (var->val_len + 1));
       
   431                 if (zPhysicalEntry->entPhysicalSerialNum == NULL)
       
   432                     netsnmp_set_request_error(reqinfo, request, SNMP_ERR_RESOURCEUNAVAILABLE);
       
   433                 else {
       
   434                     strncpy(zPhysicalEntry->entPhysicalSerialNum, (const char *)var->val.string, var->val_len);
       
   435                     zPhysicalEntry->entPhysicalSerialNum[var->val_len] = 0;
       
   436                 }
       
   437                 break;		
       
   438             case COLUMN_ENTPHYSICALALIAS:
       
   439                 free (zPhysicalEntry->entPhysicalAlias);
       
   440                 zPhysicalEntry->entPhysicalAlias = (char *)malloc(sizeof(char) * (var->val_len + 1));
       
   441                 if (zPhysicalEntry->entPhysicalAlias == NULL)
       
   442                     netsnmp_set_request_error(reqinfo, request, SNMP_ERR_RESOURCEUNAVAILABLE);
       
   443                 else {
       
   444                     strncpy(zPhysicalEntry->entPhysicalAlias, (const char *)var->val.string, var->val_len);
       
   445                     zPhysicalEntry->entPhysicalAlias[var->val_len] = 0;
       
   446                 }
       
   447                 break;
       
   448             case COLUMN_ENTPHYSICALASSETID:
       
   449                 free (zPhysicalEntry->entPhysicalAssetID);
       
   450                 zPhysicalEntry->entPhysicalAssetID = (char *)malloc(sizeof(char) * (var->val_len + 1));
       
   451                 if (zPhysicalEntry->entPhysicalAssetID == NULL)
       
   452                     netsnmp_set_request_error(reqinfo, request, SNMP_ERR_RESOURCEUNAVAILABLE);
       
   453                 else {
       
   454                     strncpy(zPhysicalEntry->entPhysicalAssetID, (const char *)var->val.string, var->val_len);
       
   455                     zPhysicalEntry->entPhysicalAssetID[var->val_len] = 0;
       
   456                 }
       
   457                 break;
       
   458             default:
       
   459                 netsnmp_set_request_error(reqinfo, request, SNMP_ERR_NOTWRITABLE);
       
   460             }
       
   461             break;
       
   462         case MODE_SET_COMMIT:
       
   463             switch(table_info->colnum) {
       
   464             case COLUMN_ENTPHYSICALSERIALNUM:
       
   465                 /* Fix for 4910657: There is no need to free the data set, */
       
   466                 /*                  as it is handled by the agent */
       
   467                 /*                  infrastructure.  However, Purify should */
       
   468                 /*                  be run to make sure that there is no */
       
   469                 /*                  memory leak */
       
   470                 /* End of Fix 4910657 */
       
   471                 break;
       
   472             case COLUMN_ENTPHYSICALALIAS:
       
   473                 break;
       
   474             case COLUMN_ENTPHYSICALASSETID:
       
   475                 break;
       
   476             default:
       
   477                 netsnmp_set_request_error(reqinfo, request, SNMP_ERR_NOTWRITABLE);
       
   478             }
       
   479             break;
       
   480         case MODE_SET_UNDO:
       
   481             switch(table_info->colnum) {
       
   482             case COLUMN_ENTPHYSICALSERIALNUM:
       
   483                 free(zPhysicalEntry->entPhysicalSerialNum);
       
   484                 zentPhySerialNum = strdup((char *)netsnmp_request_get_list_data(request, ENTPHYSICALSERIALNUM_CACHE));
       
   485                 zPhysicalEntry->entPhysicalSerialNum = zentPhySerialNum;
       
   486                 break;
       
   487             case COLUMN_ENTPHYSICALALIAS:
       
   488                 free(zPhysicalEntry->entPhysicalAlias);
       
   489                 zentPhyAlias = strdup((char *)netsnmp_request_get_list_data(request, ENTPHYSICALALIAS_CACHE));
       
   490                 zPhysicalEntry->entPhysicalAlias = zentPhyAlias;
       
   491                 break;
       
   492             case COLUMN_ENTPHYSICALASSETID:
       
   493                 free(zPhysicalEntry->entPhysicalAssetID);
       
   494                 zentPhyAssetId = strdup((char *)netsnmp_request_get_list_data(request, ENTPHYSICALASSETID_CACHE));
       
   495                 zPhysicalEntry->entPhysicalAssetID = zentPhyAssetId;
       
   496                 break;
       
   497             default:
       
   498                 netsnmp_set_request_error(reqinfo, request, SNMP_ERR_NOTWRITABLE);
       
   499             }
       
   500             break;
       
   501         /* End of Fix for 4915824 */           
       
   502         default:
       
   503             snmp_log(LOG_ERR, "problem encountered in entPhysicalTable_handler: unsupported mode\n");
       
   504         }
       
   505     }
       
   506     return SNMP_ERR_NOERROR;
       
   507 }
       
   508 
       
   509 
       
   510 
       
   511 /*
       
   512  * Allocates a physical entry. if physidx >0 attempts to reuse an
       
   513  * existing entry
       
   514  */
       
   515 int
       
   516 allocPhysicalEntry(int physidx, entPhysicalEntry_t * newPhysEntry)
       
   517 {
       
   518     entPhysicalEntry_t *physent;
       
   519     int index;
       
   520     /* Fix for 4927388 */
       
   521     oid zerooid[] = { 0, 0 };
       
   522     /* End of Fix for 4927388 */
       
   523     /* Fix for 4927412 */
       
   524     char emptystring[1] = "";
       
   525     /* End of Fix for 4927412 */
       
   526 
       
   527     /* Fix for 4927412: according to RFC 2737, entPhysicalDescr has to be */
       
   528     /*                  unique - NULL should be rejected. */
       
   529     /*                  entPhysicalVendorType will default to { 0, 0 } for */
       
   530     /*                  NULL.  For the rest, we will force NULL = "" */
       
   531     /*                  (zero-length string) */
       
   532     if (newPhysEntry->entPhysicalDescr == NULL)
       
   533         return -1;
       
   534     /* Fix for 4927388 */
       
   535     if (newPhysEntry->entPhysicalVendorType == NULL) {
       
   536         newPhysEntry->entPhysicalVendorType = zerooid;
       
   537         newPhysEntry->entPhysicalVendorTypeSize = sizeof(zerooid);
       
   538     }
       
   539     /* End of Fix for 4927388 */
       
   540     if (newPhysEntry->entPhysicalName == NULL)
       
   541         newPhysEntry->entPhysicalName = emptystring;
       
   542     if (newPhysEntry->entPhysicalHardwareRev == NULL)
       
   543         newPhysEntry->entPhysicalHardwareRev = emptystring;
       
   544     if (newPhysEntry->entPhysicalFirmwareRev == NULL)
       
   545         newPhysEntry->entPhysicalFirmwareRev = emptystring;
       
   546     if (newPhysEntry->entPhysicalSoftwareRev == NULL)
       
   547         newPhysEntry->entPhysicalSoftwareRev = emptystring;
       
   548     if (newPhysEntry->entPhysicalSerialNum == NULL)
       
   549         newPhysEntry->entPhysicalSerialNum = emptystring;
       
   550     if (newPhysEntry->entPhysicalMfgName == NULL)
       
   551         newPhysEntry->entPhysicalMfgName = emptystring;
       
   552     if (newPhysEntry->entPhysicalModelName == NULL)
       
   553         newPhysEntry->entPhysicalModelName = emptystring;
       
   554     if (newPhysEntry->entPhysicalAlias == NULL)
       
   555         newPhysEntry->entPhysicalAlias = emptystring;
       
   556     if (newPhysEntry->entPhysicalAssetID == NULL)
       
   557         newPhysEntry->entPhysicalAssetID = emptystring;
       
   558     /* End of Fix for 4927412 */
       
   559 
       
   560     /* Fix for 4921309 */
       
   561     if (physidx < 0 || physidx > MAX_ENTITY_INDEX)
       
   562         return NULL;
       
   563     /* End of Fix for 4921309 */
       
   564 
       
   565     /* Fix for 4884681: Check entPhysicalClass and make sure that it
       
   566        is between 1 and 11 - read the Entity MIB RFC
       
   567        for details */
       
   568     if (newPhysEntry->entPhysicalClass < ENTPHYSICAL_CLASS_OTHER || newPhysEntry->entPhysicalClass > ENTPHYSICAL_CLASS_STACK) {
       
   569         return (-1);
       
   570     }
       
   571     /* End of Fix for 4884681 */
       
   572 
       
   573     /* Fix for 4884702: */
       
   574     /* Make sure when entPhysicalContainedIn is 0 that */
       
   575     /* entPhysicalParentRelPos is -1.  Also check for boundary */
       
   576     if (newPhysEntry->entPhysicalParentRelPos < -1 || newPhysEntry->entPhysicalParentRelPos > MAX_ENTITY_INDEX)
       
   577         return -1;
       
   578     if (newPhysEntry->entPhysicalContainedIn == 0 && newPhysEntry->entPhysicalParentRelPos != -1)
       
   579         return -1;
       
   580     /* End of Fix for 4884702 */
       
   581 
       
   582     /* Fix for 4929711 */
       
   583     /* Make sure when entPhysicalRelPos is -1 that */
       
   584     /* entPhysicalParentContainedIn is 0.  */
       
   585     if (newPhysEntry->entPhysicalParentRelPos == -1
       
   586         && newPhysEntry->entPhysicalContainedIn != 0)
       
   587         return -1;
       
   588     /* End of Fix for 4929711 */
       
   589 
       
   590     /* Fix for 4893121: */
       
   591     /* check for invalid entries: negative number or greater than allowed */
       
   592     /* range, stale, or non-existing entry. */
       
   593     if ((newPhysEntry->entPhysicalContainedIn < 0 || newPhysEntry->entPhysicalContainedIn > MAX_ENTITY_INDEX)) {
       
   594         /* reject because it is beyond the valid physical index range */
       
   595         return (-1);
       
   596     } else if (newPhysEntry->entPhysicalContainedIn != 0 && getPhysicalTableEntry(newPhysEntry->entPhysicalContainedIn) == NULL) {
       
   597         /* If the intended parent (newPhysEntry->entPhysicalContainedIn) is */
       
   598         /* a possible index (0 - 2^31-1), but it is stale or does not exist, */
       
   599         /* then we will reject the entry */
       
   600         return (-1);
       
   601     }
       
   602 
       
   603     /* End of Fix for 4893121 */
       
   604 
       
   605     /* Fix for 4911817: Check for size and boundary limits */
       
   606     /* entPhysicalDescr 0..255 */
       
   607     if (strlen(newPhysEntry->entPhysicalDescr) > 255)
       
   608         return -1;
       
   609 
       
   610     /* entPhysicalName 0..255 */
       
   611     if (strlen(newPhysEntry->entPhysicalName) > 255)
       
   612         return -1;
       
   613 
       
   614     /* entPhysicalHardwareRev 0..255 */
       
   615     if (strlen(newPhysEntry->entPhysicalHardwareRev) > 255)
       
   616         return -1;
       
   617 
       
   618     /* entPhysicalFirmwareRev 0..255 */
       
   619     if (strlen(newPhysEntry->entPhysicalFirmwareRev) > 255)
       
   620         return -1;
       
   621 
       
   622     /* entPhysicalSoftwareRev 0..255 */
       
   623     if (strlen(newPhysEntry->entPhysicalSoftwareRev) > 255)
       
   624         return -1;
       
   625 
       
   626     /* entPhysicalSerialNum 0..32 */
       
   627     if (strlen(newPhysEntry->entPhysicalSerialNum) > 32)
       
   628         return -1;
       
   629 
       
   630     /* entPhysicalMfgName 0..255 */
       
   631     if (strlen(newPhysEntry->entPhysicalMfgName) > 255)
       
   632         return -1;
       
   633 
       
   634     /* entPhysicalModelName 0..255 */
       
   635     if (strlen(newPhysEntry->entPhysicalModelName) > 255)
       
   636         return -1;
       
   637 
       
   638     /* entPhysicalAlias 0..32 */
       
   639     if (strlen(newPhysEntry->entPhysicalAlias) > 32)
       
   640         return -1;
       
   641 
       
   642     /* entPhysicalAssetID 0..32 */
       
   643     if (strlen(newPhysEntry->entPhysicalAssetID) > 32)
       
   644         return -1;
       
   645 
       
   646     /* entPhysicalIsFRU only MIB_TRUE and MIB_FALSE */
       
   647     if (newPhysEntry->entPhysicalIsFRU < MIB_TRUE || newPhysEntry->entPhysicalIsFRU > MIB_FALSE)
       
   648         return (-1);
       
   649     /* End of Fix for 4911817 */
       
   650 
       
   651     physent = malloc(sizeof (entPhysicalEntry_t));
       
   652     if (physent == NULL) {
       
   653         /* sent a message to log file */
       
   654         return (-1);
       
   655     }
       
   656 
       
   657     physent->entPhysicalIndex = physidx;
       
   658 
       
   659     physent->entPhysicalDescr = strdup(newPhysEntry->entPhysicalDescr);
       
   660     /* Fix for 4884526 */
       
   661     if (physent->entPhysicalDescr == NULL) {
       
   662         free(physent);
       
   663         return (-1);
       
   664     }
       
   665     /* End of Fix for 4884526 */
       
   666 
       
   667     physent->entPhysicalVendorType =
       
   668         malloc(newPhysEntry->entPhysicalVendorTypeSize);
       
   669 
       
   670     /* Fix for 4884526 */
       
   671     if (physent->entPhysicalVendorType == NULL) {
       
   672         free(physent->entPhysicalDescr);
       
   673         free(physent);
       
   674         return (-1);
       
   675     }
       
   676     /* End of Fix for 4884526 */
       
   677 
       
   678     memcpy(physent->entPhysicalVendorType,
       
   679            newPhysEntry->entPhysicalVendorType,
       
   680            newPhysEntry->entPhysicalVendorTypeSize);
       
   681     physent->entPhysicalVendorTypeSize =
       
   682         newPhysEntry->entPhysicalVendorTypeSize;
       
   683 
       
   684     physent->entPhysicalContainedIn = newPhysEntry->entPhysicalContainedIn;
       
   685     physent->entPhysicalClass = newPhysEntry->entPhysicalClass;
       
   686     physent->entPhysicalParentRelPos =
       
   687         newPhysEntry->entPhysicalParentRelPos;
       
   688     physent->entPhysicalName = strdup(newPhysEntry->entPhysicalName);
       
   689     /* Fix for 4884526 */
       
   690     if (physent->entPhysicalName == NULL) {
       
   691         free(physent->entPhysicalVendorType);
       
   692         free(physent->entPhysicalDescr);
       
   693         free(physent);
       
   694         return (-1);
       
   695     }
       
   696     /* End of Fix for 4884526 */
       
   697     physent->entPhysicalHardwareRev = 
       
   698         strdup(newPhysEntry->entPhysicalHardwareRev);
       
   699     /* Fix for 4884526 */
       
   700     if (physent->entPhysicalHardwareRev == NULL) {
       
   701         free(physent->entPhysicalName);
       
   702         free(physent->entPhysicalVendorType);
       
   703         free(physent->entPhysicalDescr);
       
   704         free(physent);
       
   705         return (-1);
       
   706     }
       
   707     /* End of Fix for 4884526 */
       
   708     physent->entPhysicalFirmwareRev =
       
   709         strdup(newPhysEntry->entPhysicalFirmwareRev);
       
   710     /* Fix for 4884526 */
       
   711     if (physent->entPhysicalFirmwareRev == NULL) {
       
   712         free(physent->entPhysicalHardwareRev);
       
   713         free(physent->entPhysicalName);
       
   714         free(physent->entPhysicalVendorType);
       
   715         free(physent->entPhysicalDescr);
       
   716         free(physent);
       
   717         return (-1);
       
   718     }
       
   719     /* End of Fix for 4884526 */
       
   720     physent->entPhysicalSoftwareRev =
       
   721         strdup(newPhysEntry->entPhysicalSoftwareRev);
       
   722     /* Fix for 4884526 */
       
   723     if (physent->entPhysicalSoftwareRev == NULL) {
       
   724         free(physent->entPhysicalFirmwareRev);
       
   725         free(physent->entPhysicalHardwareRev);
       
   726         free(physent->entPhysicalName);
       
   727         free(physent->entPhysicalVendorType);
       
   728         free(physent->entPhysicalDescr);
       
   729         free(physent);
       
   730         return (-1);
       
   731     }
       
   732     /* End of Fix for 4884526 */
       
   733     physent->entPhysicalSerialNum =
       
   734         strdup(newPhysEntry->entPhysicalSerialNum);
       
   735     /* Fix for 4884526 */
       
   736     if (physent->entPhysicalSerialNum == NULL) {
       
   737         free(physent->entPhysicalSoftwareRev);
       
   738         free(physent->entPhysicalFirmwareRev);
       
   739         free(physent->entPhysicalHardwareRev);
       
   740         free(physent->entPhysicalName);
       
   741         free(physent->entPhysicalVendorType);
       
   742         free(physent->entPhysicalDescr);
       
   743         free(physent);
       
   744         return (-1);
       
   745     }
       
   746     /* End of Fix for 4884526 */
       
   747     physent->entPhysicalMfgName =
       
   748         strdup(newPhysEntry->entPhysicalMfgName);
       
   749     /* Fix for 4884526 */
       
   750     if (physent->entPhysicalMfgName == NULL) {
       
   751         free(physent->entPhysicalSerialNum);
       
   752         free(physent->entPhysicalSoftwareRev);
       
   753         free(physent->entPhysicalFirmwareRev);
       
   754         free(physent->entPhysicalHardwareRev);
       
   755         free(physent->entPhysicalName);
       
   756         free(physent->entPhysicalVendorType);
       
   757         free(physent->entPhysicalDescr);
       
   758         free(physent);
       
   759         return (-1);
       
   760     }
       
   761     /* End of Fix for 4884526 */
       
   762     physent->entPhysicalModelName =
       
   763         strdup(newPhysEntry->entPhysicalModelName);
       
   764     /* Fix for 4884526 */
       
   765     if (physent->entPhysicalModelName == NULL) {
       
   766         free(physent->entPhysicalMfgName);
       
   767         free(physent->entPhysicalSerialNum);
       
   768         free(physent->entPhysicalSoftwareRev);
       
   769         free(physent->entPhysicalFirmwareRev);
       
   770         free(physent->entPhysicalHardwareRev);
       
   771         free(physent->entPhysicalName);
       
   772         free(physent->entPhysicalVendorType);
       
   773         free(physent->entPhysicalDescr);
       
   774         free(physent);
       
   775         return (-1);
       
   776     }
       
   777     /* End of Fix for 4884526 */
       
   778     physent->entPhysicalAlias =
       
   779         strdup(newPhysEntry->entPhysicalAlias);
       
   780     /* Fix for 4884526 */
       
   781     if (physent->entPhysicalAlias == NULL) {
       
   782         free(physent->entPhysicalModelName);
       
   783         free(physent->entPhysicalMfgName);
       
   784         free(physent->entPhysicalSerialNum);
       
   785         free(physent->entPhysicalSoftwareRev);
       
   786         free(physent->entPhysicalFirmwareRev);
       
   787         free(physent->entPhysicalHardwareRev);
       
   788         free(physent->entPhysicalName);
       
   789         free(physent->entPhysicalVendorType);
       
   790         free(physent->entPhysicalDescr);
       
   791         free(physent);
       
   792         return (-1);
       
   793     }
       
   794     /* End of Fix for 4884526 */
       
   795     physent->entPhysicalAssetID =
       
   796         strdup(newPhysEntry->entPhysicalAssetID);
       
   797     /* Fix for 4884526 */
       
   798     if (physent->entPhysicalAssetID == NULL) /* Memory allocation failed */
       
   799     {
       
   800         free(physent->entPhysicalAlias);
       
   801         free(physent->entPhysicalModelName);
       
   802         free(physent->entPhysicalMfgName);
       
   803         free(physent->entPhysicalSerialNum);
       
   804         free(physent->entPhysicalSoftwareRev);
       
   805         free(physent->entPhysicalFirmwareRev);
       
   806         free(physent->entPhysicalHardwareRev);
       
   807         free(physent->entPhysicalName);
       
   808         free(physent->entPhysicalVendorType);
       
   809         free(physent->entPhysicalDescr);
       
   810         free(physent);
       
   811         return (-1);
       
   812     }
       
   813     /* End of Fix for 4884526 */
       
   814 
       
   815     physent->entPhysicalIsFRU = newPhysEntry->entPhysicalIsFRU;
       
   816 
       
   817     index = AddToPhysicalTable(physent);
       
   818     return (index);
       
   819 }
       
   820 
       
   821 static int
       
   822 AddToPhysicalTable(entPhysicalEntry_t * xnewPhysicalEntry)
       
   823 {
       
   824     entPhysicalEntry_t *zRunner, *temp;
       
   825     int placeFound; /* Fix for 4921309 */
       
   826     int zIndex;
       
   827     zRunner = gPhysicalTableHead;
       
   828 
       
   829     if (xnewPhysicalEntry == NULL) return (-1);
       
   830     xnewPhysicalEntry->pNextEntry = NULL;
       
   831 
       
   832     /* Fix for 4921309 */
       
   833     /* If index > 0, attempt to insert in appropriate place. */
       
   834     if (xnewPhysicalEntry->entPhysicalIndex > 0) {
       
   835 	placeFound = 0;
       
   836 	temp = zRunner;
       
   837 	while (zRunner != NULL) {
       
   838 	    if (xnewPhysicalEntry->entPhysicalIndex >
       
   839 		abs(zRunner->entPhysicalIndex)) {
       
   840 		temp = zRunner;
       
   841 		zRunner = zRunner->pNextEntry;
       
   842 	    } else {
       
   843 		break;
       
   844 	    }
       
   845 	}
       
   846 
       
   847 	/* If the indexes don't match, we can use the specified index */
       
   848 	if (temp == NULL) {
       
   849             /* List is empty, make this the first/last entry */
       
   850 	    gPhysicalTableHead = xnewPhysicalEntry;
       
   851             gPhysicalTableTail = xnewPhysicalEntry;
       
   852 	    placeFound = 1;
       
   853 	} else if (zRunner == NULL) {
       
   854             /* Index > last value, make this the last entry */
       
   855 	    temp->pNextEntry = xnewPhysicalEntry;
       
   856             gPhysicalTableTail = xnewPhysicalEntry;
       
   857 	    placeFound = 1;
       
   858 	} else if (xnewPhysicalEntry->entPhysicalIndex != 
       
   859 		   abs(zRunner->entPhysicalIndex)) {
       
   860             /* Index < zRunner, insert entry before it */
       
   861 	    xnewPhysicalEntry->pNextEntry = zRunner;
       
   862             if (zRunner == gPhysicalTableHead) {
       
   863                 /* Index fits before list head, insert entry */
       
   864                 gPhysicalTableHead = xnewPhysicalEntry;
       
   865             } else {
       
   866                 /* Index fits between two entries, insert entry */
       
   867 	        temp->pNextEntry = xnewPhysicalEntry;
       
   868             }
       
   869 	    placeFound = 1;
       
   870 	}
       
   871 
       
   872 	if (placeFound) {
       
   873 	    gPhysicalTableSize++;
       
   874 	    configChanged();
       
   875 	    return (xnewPhysicalEntry->entPhysicalIndex);
       
   876 	} else {
       
   877             /* Re-initialize for code that follows */
       
   878             zRunner = gPhysicalTableHead;
       
   879 	}
       
   880     }
       
   881 
       
   882     /* Either index was zero or specified index is already taken */
       
   883     /* End of Fix for 4921309 */
       
   884 
       
   885     if (gPhysicalTableSize > LARGE_TABLE) {
       
   886         gPhysicalTableTail->pNextEntry = xnewPhysicalEntry;
       
   887         zIndex = abs(gPhysicalTableTail->entPhysicalIndex) + 1;
       
   888         xnewPhysicalEntry->entPhysicalIndex = zIndex;
       
   889         gPhysicalTableTail = xnewPhysicalEntry;
       
   890         gPhysicalTableSize++;
       
   891         configChanged();
       
   892         return (zIndex);
       
   893     }
       
   894 
       
   895     if (gPhysicalTableHead) { /* A slightly slower way to add into the list */
       
   896         while (zRunner->pNextEntry != NULL) {
       
   897             if ((abs(zRunner->pNextEntry->entPhysicalIndex)
       
   898                  - abs(zRunner->entPhysicalIndex)) > 1) {
       
   899                 temp = zRunner->pNextEntry;
       
   900                 zRunner->pNextEntry = xnewPhysicalEntry;
       
   901                 zIndex = abs(zRunner->entPhysicalIndex) + 1;
       
   902                 xnewPhysicalEntry->entPhysicalIndex = zIndex;
       
   903                 xnewPhysicalEntry->pNextEntry = temp;
       
   904                 gPhysicalTableSize++;
       
   905                 configChanged();
       
   906                 return (zIndex);
       
   907             }
       
   908             zRunner = zRunner->pNextEntry;
       
   909         }
       
   910         zIndex = abs(zRunner->entPhysicalIndex) + 1;
       
   911         xnewPhysicalEntry->entPhysicalIndex = zIndex;
       
   912         zRunner->pNextEntry = xnewPhysicalEntry;
       
   913     }  else {
       
   914         zIndex = xnewPhysicalEntry->entPhysicalIndex = 1;
       
   915         gPhysicalTableHead = xnewPhysicalEntry;
       
   916     }
       
   917     gPhysicalTableTail = xnewPhysicalEntry;
       
   918     gPhysicalTableSize++;
       
   919     configChanged();
       
   920     return (zIndex);
       
   921 }
       
   922 
       
   923 entPhysicalEntry_t*
       
   924 getPhysicalTableEntry(int xPhysicalIndex)
       
   925 {
       
   926     entPhysicalEntry_t *zRunner;
       
   927     int zPhysicalIndex;
       
   928 
       
   929     /* Fix for 4888088 */
       
   930     if (xPhysicalIndex <= 0 || xPhysicalIndex > MAX_ENTITY_INDEX)
       
   931         return NULL;
       
   932     /* End of Fix for 4888088 */
       
   933 
       
   934     zRunner = gPhysicalTableHead;
       
   935     while (zRunner) {
       
   936         zPhysicalIndex = zRunner->entPhysicalIndex;
       
   937         if (zPhysicalIndex > 0) {
       
   938             if (zPhysicalIndex == xPhysicalIndex)
       
   939                 return zRunner;
       
   940         }
       
   941         if (zPhysicalIndex == -(xPhysicalIndex)) {
       
   942             return NULL; /* The stale entry exist, we can stop the search*/
       
   943         }
       
   944         zRunner = zRunner->pNextEntry;
       
   945     }
       
   946     return NULL;
       
   947 }
       
   948 
       
   949 entPhysicalEntry_t*
       
   950 getPhysicalTableStaleEntry(int xPhysicalIndex)
       
   951 {
       
   952     entPhysicalEntry_t *zRunner;
       
   953     int zPhysicalIndex;
       
   954 
       
   955     /* Fix for 4888088 */
       
   956     if (xPhysicalIndex <= 0 || xPhysicalIndex > MAX_ENTITY_INDEX)
       
   957         return NULL;
       
   958     /* End of Fix for 4888088 */
       
   959 
       
   960     zRunner = gPhysicalTableHead;
       
   961     while (zRunner) {
       
   962         zPhysicalIndex = zRunner->entPhysicalIndex;
       
   963         if (zPhysicalIndex < 0) {
       
   964             if (zPhysicalIndex == -(xPhysicalIndex))
       
   965                 return zRunner;
       
   966         }
       
   967         if (zPhysicalIndex == xPhysicalIndex) {
       
   968             return NULL; /* The live entry exist, we can stop the search*/
       
   969         }
       
   970         zRunner = zRunner->pNextEntry;
       
   971     }
       
   972     return NULL;
       
   973 }
       
   974 
       
   975 
       
   976 void populate_entPhysicalEntry(entPhysicalEntry_t *phyStatic,
       
   977                                char *entPhysicalDescr,
       
   978                                oid *entPhysicalVendorType,
       
   979                                int entPhysicalVendorTypeSize,
       
   980                                int entPhysicalContainedIn,
       
   981                                int entPhysicalClass,
       
   982                                int entPhysicalParentRelPos,
       
   983                                char *entPhysicalName,
       
   984                                char *entPhysicalHardwareRev,
       
   985                                char *entPhysicalFirmwareRev,
       
   986                                char *entPhysicalSoftwareRev,
       
   987                                char *entPhysicalSerialNum,
       
   988                                char *entPhysicalMfgName,
       
   989                                char *entPhysicalModelName,
       
   990                                char *entPhysicalAlias,
       
   991                                char *entPhysicalAssetID,
       
   992                                int entPhysicalIsFRU)
       
   993 {
       
   994     phyStatic->entPhysicalDescr = entPhysicalDescr;
       
   995     phyStatic->entPhysicalVendorType = entPhysicalVendorType;
       
   996     phyStatic->entPhysicalVendorTypeSize = entPhysicalVendorTypeSize;
       
   997     phyStatic->entPhysicalContainedIn = entPhysicalContainedIn;
       
   998     phyStatic->entPhysicalClass = entPhysicalClass;
       
   999     phyStatic->entPhysicalParentRelPos = entPhysicalParentRelPos;
       
  1000     phyStatic->entPhysicalName = entPhysicalName;
       
  1001     phyStatic->entPhysicalHardwareRev = entPhysicalHardwareRev;
       
  1002     phyStatic->entPhysicalFirmwareRev = entPhysicalFirmwareRev;
       
  1003     phyStatic->entPhysicalSoftwareRev = entPhysicalSoftwareRev;
       
  1004     phyStatic->entPhysicalSerialNum = entPhysicalSerialNum;
       
  1005     phyStatic->entPhysicalMfgName = entPhysicalMfgName;
       
  1006     phyStatic->entPhysicalModelName = entPhysicalModelName;
       
  1007     phyStatic->entPhysicalAlias = entPhysicalAlias;
       
  1008     phyStatic->entPhysicalAssetID = entPhysicalAssetID;
       
  1009     phyStatic->entPhysicalIsFRU = entPhysicalIsFRU;
       
  1010 }
       
  1011 
       
  1012 int
       
  1013 deletePhysicalTableEntry(int xPhysicalIndex)
       
  1014 {
       
  1015     entPhysicalEntry_t *zRunner, *temp, *prevEntry;
       
  1016     int zPhysicalIndex;
       
  1017 
       
  1018     /* Fix for 4888088 */
       
  1019     if (xPhysicalIndex <= 0 || xPhysicalIndex > MAX_ENTITY_INDEX)
       
  1020         return (-1);
       
  1021     /* End of Fix for 4888088 */
       
  1022 
       
  1023     zRunner = gPhysicalTableHead;
       
  1024     prevEntry = NULL;
       
  1025     while (zRunner) {
       
  1026         zPhysicalIndex = zRunner->entPhysicalIndex;
       
  1027         if (zPhysicalIndex > 0) {
       
  1028             if (zPhysicalIndex == xPhysicalIndex) {
       
  1029                 /* Fix for 4918876: We need to delete the related entries */
       
  1030                 /* first */
       
  1031 /*
       
  1032    Delete all instances of this physical index in all other
       
  1033    tables to maintain table integrity. Should we roll-back if a
       
  1034    deletion fails, perhaps not ?
       
  1035 */
       
  1036                 deleteAliasMappingPhysicalIndex(xPhysicalIndex); /*Alias mapping*/
       
  1037                 deleteLPMappingPhysicalIndex(xPhysicalIndex); /* LPTable */
       
  1038                 /* Fix for 4891869 */
       
  1039                 deletePhysicalContainsParentIndex(xPhysicalIndex);
       
  1040                 deletePhysicalContainsChildIndex(xPhysicalIndex);
       
  1041                 /* End of Fix for 4891869 */
       
  1042                 /* End of Fix for 4918876 */
       
  1043 
       
  1044                 temp = zRunner->pNextEntry;
       
  1045                 zRunner->pNextEntry = NULL;
       
  1046                 if (prevEntry)
       
  1047                     prevEntry->pNextEntry = temp;
       
  1048                 else
       
  1049                     gPhysicalTableHead =  temp;
       
  1050                 FreePhysicalEntry(zRunner);
       
  1051                 gPhysicalTableSize--;
       
  1052 
       
  1053                 configChanged();
       
  1054                 return (0);
       
  1055             }
       
  1056         }
       
  1057         if (zPhysicalIndex == -(xPhysicalIndex)) {
       
  1058             return (-2); /* The stale entry exist, we can stop the search*/
       
  1059         }
       
  1060         prevEntry = zRunner;
       
  1061         zRunner = zRunner->pNextEntry;
       
  1062     }
       
  1063     return (-1);
       
  1064 }
       
  1065 
       
  1066 static int
       
  1067 FreePhysicalEntry(entPhysicalEntry_t *xEntry)
       
  1068 {
       
  1069     if (xEntry == NULL) return (-1);
       
  1070     free(xEntry->entPhysicalDescr);
       
  1071     free(xEntry->entPhysicalVendorType);
       
  1072     free(xEntry->entPhysicalName);
       
  1073     free(xEntry->entPhysicalHardwareRev);
       
  1074     free(xEntry->entPhysicalFirmwareRev);
       
  1075     free(xEntry->entPhysicalSoftwareRev);
       
  1076     free(xEntry->entPhysicalSerialNum);
       
  1077     free(xEntry->entPhysicalMfgName);
       
  1078     free(xEntry->entPhysicalModelName);
       
  1079     free(xEntry->entPhysicalAlias);
       
  1080     free(xEntry->entPhysicalAssetID);
       
  1081     free(xEntry);
       
  1082     xEntry = NULL;
       
  1083     return (1);
       
  1084 }
       
  1085 
       
  1086 int
       
  1087 makePhysicalTableEntryStale(int xPhysicalIndex)
       
  1088 {
       
  1089 
       
  1090     entPhysicalEntry_t *zRunner;
       
  1091     int zPhysicalIndex;
       
  1092 
       
  1093     /* Fix for 4888088 */
       
  1094     if (xPhysicalIndex <= 0 || xPhysicalIndex > MAX_ENTITY_INDEX)
       
  1095         return (-1);
       
  1096     /* End of Fix for 4888088 */
       
  1097 
       
  1098     zRunner = gPhysicalTableHead;
       
  1099 
       
  1100     while (zRunner) {
       
  1101         zPhysicalIndex = zRunner->entPhysicalIndex;
       
  1102         if (zPhysicalIndex < 0) {
       
  1103             if (zPhysicalIndex == -(xPhysicalIndex))
       
  1104                 return (-2); /* Entry is already stale */
       
  1105         }
       
  1106         if (zPhysicalIndex == xPhysicalIndex) {
       
  1107             /* Fix for 4918876: We need to delete the related entries first */
       
  1108 /*
       
  1109   Delete all instances of this physical index in all other
       
  1110   tables to maintain table integrity. Should we roll-back if a
       
  1111   deletion fails, perhaps not ?
       
  1112 */
       
  1113             deleteAliasMappingPhysicalIndex(xPhysicalIndex); /*Alias mapping*/
       
  1114             deleteLPMappingPhysicalIndex(xPhysicalIndex); /* LPTable */
       
  1115             /* Fix for 4891869 */
       
  1116             deletePhysicalContainsParentIndex(xPhysicalIndex);
       
  1117             deletePhysicalContainsChildIndex(xPhysicalIndex);
       
  1118             /* End of Fix for 4891869 */
       
  1119             /* End of Fix for 4918876 */
       
  1120 
       
  1121             zRunner->entPhysicalIndex = -zPhysicalIndex;
       
  1122 
       
  1123             configChanged();
       
  1124 
       
  1125             return (0);
       
  1126         }
       
  1127         zRunner = zRunner->pNextEntry;
       
  1128     }
       
  1129     return (-1);
       
  1130 }
       
  1131 
       
  1132 int
       
  1133 makePhysicalTableEntryLive(int xPhysicalIndex)
       
  1134 {
       
  1135     entPhysicalEntry_t *zRunner;
       
  1136     int zPhysicalIndex;
       
  1137 
       
  1138     /* Fix for 4888088 */
       
  1139     if (xPhysicalIndex <= 0 || xPhysicalIndex > MAX_ENTITY_INDEX)
       
  1140         return (-1);
       
  1141     /* End of Fix for 4888088 */
       
  1142 
       
  1143     zRunner = gPhysicalTableHead;
       
  1144 
       
  1145     while (zRunner) {
       
  1146         zPhysicalIndex = zRunner->entPhysicalIndex;
       
  1147         if (zPhysicalIndex > 0) {
       
  1148             if (zPhysicalIndex == xPhysicalIndex)
       
  1149                 return (-2); /* Entry is already live */
       
  1150         }
       
  1151         if (zPhysicalIndex == -(xPhysicalIndex)) {
       
  1152             zRunner->entPhysicalIndex = xPhysicalIndex;
       
  1153             configChanged();
       
  1154             return (0);
       
  1155         }
       
  1156         zRunner = zRunner->pNextEntry;
       
  1157     }
       
  1158     return (-1);
       
  1159 }
       
  1160 
       
  1161 /* Fix for 4915080 */
       
  1162 /* Given a parent, find all its children based on the relationship defined */
       
  1163 /* in entPhysicalContainedIn.  Needs to free the memory after */
       
  1164 int *getAllChildrenFromPhysicalContainedIn(int parentIdx)
       
  1165 {
       
  1166     int *child_list, *temp, i;
       
  1167     entPhysicalEntry_t *zRunner;
       
  1168 
       
  1169     child_list = NULL;
       
  1170     i = 0;
       
  1171     /* Fix for 4888088 */
       
  1172     if (parentIdx <= 0 || parentIdx > MAX_ENTITY_INDEX)
       
  1173         return NULL;
       
  1174     /* End of Fix for 4888088 */
       
  1175 
       
  1176     zRunner = gPhysicalTableHead;
       
  1177     while (zRunner) {
       
  1178         if (zRunner->entPhysicalContainedIn == parentIdx) {
       
  1179             if (child_list == NULL) {
       
  1180                 child_list = (int *)malloc(2 * sizeof(int));
       
  1181                 if (child_list == NULL)
       
  1182                     return NULL;
       
  1183                 child_list[0] = zRunner->entPhysicalIndex;
       
  1184                 child_list[1] = 0;
       
  1185                 i++;
       
  1186             } else {
       
  1187                 temp = child_list;
       
  1188                 child_list = (int *)realloc(child_list, (i + 2)*sizeof(int));
       
  1189                 if (child_list == NULL) {
       
  1190                     free(temp);
       
  1191                     return NULL;
       
  1192                 }
       
  1193                 child_list[i] = zRunner->entPhysicalIndex;
       
  1194                 child_list[i+1] = 0;
       
  1195             }
       
  1196         }
       
  1197         zRunner = zRunner->pNextEntry;
       
  1198     }
       
  1199     return child_list;
       
  1200 }
       
  1201 /* End of Fix for 4915080 */