FreeBSD/usr.sbin/bsnmpd/modules/snmp_hostres/hostres_device_tbl.c
/**
 * Create a new entry out of thin air.
 */
Create a new entry out of thin air.
Create a new entry out of thin air .
struct device_entry *
device_entry_create(const char *name, const char *location, const char *descr)
{
	struct device_entry *entry = NULL;
	struct device_map_entry *map = NULL;
	size_t name_len;
	size_t location_len;
	assert((name[0] != 0) || (location[0] != 0));
	if (name[0] == 0 && location[0] == 0)
		return (NULL);
	STAILQ_FOREACH(map, &device_map, link) {
		assert(map->name_key != NULL);
		assert(map->location_key != NULL);
		if (strcmp(map->name_key, name) == 0 &&
		    strcmp(map->location_key, location) == 0) {
			break;
		}
	}
	if (map == NULL) {
		if (next_device_index > INT_MAX) {
		        syslog(LOG_ERR,
			    "%s: hrDeviceTable index wrap", __func__);
			errx(EX_SOFTWARE, "hrDeviceTable index wrap");
		}
		if ((map = malloc(sizeof(*map))) == NULL) {
			syslog(LOG_ERR, "hrDeviceTable: %s: %m", __func__ );
			return (NULL);
		}
		map->entry_p = NULL;
		name_len = strlen(name) + 1;
		if (name_len > DEV_NAME_MLEN)
			name_len = DEV_NAME_MLEN;
		if ((map->name_key = malloc(name_len)) == NULL) {
			syslog(LOG_ERR, "hrDeviceTable: %s: %m", __func__ );
			free(map);
			return (NULL);
		}
		location_len = strlen(location) + 1;
		if (location_len > DEV_LOC_MLEN)
			location_len = DEV_LOC_MLEN;
		if ((map->location_key = malloc(location_len )) == NULL) {
			syslog(LOG_ERR, "hrDeviceTable: %s: %m", __func__ );
			free(map->name_key);
			free(map);
			return (NULL);
		}
		map->hrIndex = next_device_index++;
		strlcpy(map->name_key, name, name_len);
		strlcpy(map->location_key, location, location_len);
		STAILQ_INSERT_TAIL(&device_map, map, link);
		HRDBG("%s at %s added into hrDeviceMap at index=%d",
		    name, location, map->hrIndex);
	} else {
		HRDBG("%s at %s exists in hrDeviceMap index=%d",
		    name, location, map->hrIndex);
	}
	if ((entry = malloc(sizeof(*entry))) == NULL) {
		syslog(LOG_WARNING, "hrDeviceTable: %s: %m", __func__);
		return (NULL);
	}
	memset(entry, 0, sizeof(*entry));
	entry->index = map->hrIndex;
	map->entry_p = entry;
	if ((entry->name = strdup(map->name_key)) == NULL) {
		syslog(LOG_ERR, "hrDeviceTable: %s: %m", __func__ );
		free(entry);
		return (NULL);
	}
	if ((entry->location = strdup(map->location_key)) == NULL) {
		syslog(LOG_ERR, "hrDeviceTable: %s: %m", __func__ );
		free(entry->name);
		free(entry);
		return (NULL);
	}
	if (name[0] != '\0')
		name_len = strlen(name) + strlen(descr) +
		    strlen(": ") + 1;
	else
		name_len = strlen(location) + strlen(descr) +
		    strlen("unknown at : ") + 1;
	if (name_len > DEV_DESCR_MLEN)
		name_len = DEV_DESCR_MLEN;
	if ((entry->descr = malloc(name_len )) == NULL) {
		syslog(LOG_ERR, "hrDeviceTable: %s: %m", __func__ );
		free(entry->name);
		free(entry->location);
		free(entry);
		return (NULL);
	}
	memset(&entry->descr[0], '\0', name_len);
	if (name[0] != '\0')
		snprintf(entry->descr, name_len,
		    "%s: %s", name, descr);
	else
		snprintf(entry->descr, name_len,
		    "unknown at %s: %s", location, descr);
	entry->id = &oid_zeroDotZero;	
	entry->status = (u_int)DS_UNKNOWN;
	entry->errors = 0;
	entry->type = &OIDX_hrDeviceOther_c;
	INSERT_OBJECT_INT(entry, &device_tbl);
	return (entry);
}
struct device_entry * device_entry_create ( const char * name , const char * location , const char * descr ) { struct device_entry * entry = NULL ; struct device_map_entry * map = NULL ; size_t name_len ; size_t location_len ; assert ( ( name [ 0 ] != 0 ) || ( location [ 0 ] != 0 ) ) ; if ( name [ 0 ] == 0 && location [ 0 ] == 0 ) return ( NULL ) ; STAILQ_FOREACH ( map , & device_map , link ) { assert ( map -> name_key != NULL ) ; assert ( map -> location_key != NULL ) ; if ( strcmp ( map -> name_key , name ) == 0 && strcmp ( map -> location_key , location ) == 0 ) { break ; } } if ( map == NULL ) { if ( next_device_index > INT_MAX ) { syslog ( LOG_ERR , " " , __func__ ) ; errx ( EX_SOFTWARE , " " ) ; } if ( ( map = malloc ( sizeof ( * map ) ) ) == NULL ) { syslog ( LOG_ERR , " " , __func__ ) ; return ( NULL ) ; } map -> entry_p = NULL ; name_len = strlen ( name ) + 1 ; if ( name_len > DEV_NAME_MLEN ) name_len = DEV_NAME_MLEN ; if ( ( map -> name_key = malloc ( name_len ) ) == NULL ) { syslog ( LOG_ERR , " " , __func__ ) ; free ( map ) ; return ( NULL ) ; } location_len = strlen ( location ) + 1 ; if ( location_len > DEV_LOC_MLEN ) location_len = DEV_LOC_MLEN ; if ( ( map -> location_key = malloc ( location_len ) ) == NULL ) { syslog ( LOG_ERR , " " , __func__ ) ; free ( map -> name_key ) ; free ( map ) ; return ( NULL ) ; } map -> hrIndex = next_device_index ++ ; strlcpy ( map -> name_key , name , name_len ) ; strlcpy ( map -> location_key , location , location_len ) ; STAILQ_INSERT_TAIL ( & device_map , map , link ) ; HRDBG ( " " , name , location , map -> hrIndex ) ; } else { HRDBG ( " " , name , location , map -> hrIndex ) ; } if ( ( entry = malloc ( sizeof ( * entry ) ) ) == NULL ) { syslog ( LOG_WARNING , " " , __func__ ) ; return ( NULL ) ; } memset ( entry , 0 , sizeof ( * entry ) ) ; entry -> index = map -> hrIndex ; map -> entry_p = entry ; if ( ( entry -> name = strdup ( map -> name_key ) ) == NULL ) { syslog ( LOG_ERR , " " , __func__ ) ; free ( entry ) ; return ( NULL ) ; } if ( ( entry -> location = strdup ( map -> location_key ) ) == NULL ) { syslog ( LOG_ERR , " " , __func__ ) ; free ( entry -> name ) ; free ( entry ) ; return ( NULL ) ; } if ( name [ 0 ] != ' \0 ' ) name_len = strlen ( name ) + strlen ( descr ) + strlen ( " " ) + 1 ; else name_len = strlen ( location ) + strlen ( descr ) + strlen ( " " ) + 1 ; if ( name_len > DEV_DESCR_MLEN ) name_len = DEV_DESCR_MLEN ; if ( ( entry -> descr = malloc ( name_len ) ) == NULL ) { syslog ( LOG_ERR , " " , __func__ ) ; free ( entry -> name ) ; free ( entry -> location ) ; free ( entry ) ; return ( NULL ) ; } memset ( & entry -> descr [ 0 ] , ' \0 ' , name_len ) ; if ( name [ 0 ] != ' \0 ' ) snprintf ( entry -> descr , name_len , " " , name , descr ) ; else snprintf ( entry -> descr , name_len , " " , location , descr ) ; entry -> id = & oid_zeroDotZero ; entry -> status = ( u_int ) DS_UNKNOWN ; entry -> errors = 0 ; entry -> type = & OIDX_hrDeviceOther_c ; INSERT_OBJECT_INT ( entry , & device_tbl ) ; return ( entry ) ; }
Create a new entry out of thin air.
Create a new entry out of thin air .
/* new object - get a new index */ /* There isn't much we can do here.
			 * If the next_swins_index is consumed
			 * then we can't add entries to this table
			 * So it is better to exit - if the table is sparsed
			 * at the next agent run we can fill it fully.
			 */ /* not reachable */ /*
	 * From here till the end of this function we reuse name_len
	 * for a different purpose - for device_entry::descr
	 */ /* unknown id - FIXME */