PERFORCE change 80858 for review
Victor Cruceru
soc-victor at FreeBSD.org
Sat Jul 23 12:11:15 GMT 2005
http://perforce.freebsd.org/chv.cgi?CH=80858
Change 80858 by soc-victor at soc-victor_82.76.158.176 on 2005/07/23 12:11:12
Various fixes (mainly related to memset, strcpy & friends) + some coding style improvements.
Affected files ...
.. //depot/projects/soc2005/bsnmp/usr.sbin/bsnmpd/modules/snmp_hostres/hostres_scalars.c#8 edit
.. //depot/projects/soc2005/bsnmp/usr.sbin/bsnmpd/modules/snmp_hostres/hostres_snmp.h#13 edit
.. //depot/projects/soc2005/bsnmp/usr.sbin/bsnmpd/modules/snmp_hostres/hostres_storage_tbl.c#7 edit
Differences ...
==== //depot/projects/soc2005/bsnmp/usr.sbin/bsnmpd/modules/snmp_hostres/hostres_scalars.c#8 (text+ko) ====
@@ -447,7 +447,7 @@
return (NULL); /*error*/
}
- (void)strncpy(hrState_g.k_boot_line, buf, 127);
+ (void)strncpy(hrState_g.k_boot_line, buf, sizeof(hrState_g.k_boot_line) - 1);
HR_DPRINTF((stderr, "Got kernel boot file: %s\n", hrState_g.k_boot_line));
free(buf);
}
==== //depot/projects/soc2005/bsnmp/usr.sbin/bsnmpd/modules/snmp_hostres/hostres_snmp.h#13 (text+ko) ====
@@ -253,7 +253,7 @@
FILE *utmp_fp; /*file pointer to keep an open instance of utmp*/
kvm_t *kd; /*kernel descriptor*/
uint32_t kernel_boot; /*boot timestamp in centi-seconds*/
- char k_boot_line[128]; /*kernel boot line*/
+ char k_boot_line[128 + 1]; /*kernel boot line*/
int max_proc; /*maximum number of processes */
uint32_t phys_mem_size; /*physical memory size in Kb*/
==== //depot/projects/soc2005/bsnmp/usr.sbin/bsnmpd/modules/snmp_hostres/hostres_storage_tbl.c#7 (text+ko) ====
@@ -57,7 +57,7 @@
return (NULL);
}
memset(entry, 0, sizeof(*entry));
- strcpy(entry->descr, name);
+ strncpy(entry->descr, name, sizeof(entry->descr) - 1);
STAILQ_FOREACH(map, &hrState_g.storage_name_map, link)
if (strcmp(map->a_name, entry->descr) == 0) {
@@ -74,17 +74,17 @@
}
if ((map = malloc(sizeof(*map))) == NULL) {
- syslog(LOG_ERR, "%s: %m", __func__ );
+ syslog(LOG_ERR, "hrStorageTable: %s: %m", __func__ );
free(entry);
return (NULL);
}
map->hrIndex = hrState_g.next_hrStorage_index ++;
- strncpy(map->a_name, entry->descr, 255);
+ strncpy(map->a_name, entry->descr, sizeof(map->a_name) - 1);
map->entry_p = entry;
STAILQ_INSERT_TAIL(&hrState_g.storage_name_map, map, link);
- HR_DPRINTF((stderr, "%s added into hrStorageMap at index=%d\n ",name, map->hrIndex));
+ HR_DPRINTF((stderr, "%s added into hrStorageMap at index=%d\n ", name, map->hrIndex));
} else {
- HR_DPRINTF((stderr, "%s exists in hrStorageMap index=%d\n ",name, map->hrIndex));
+ HR_DPRINTF((stderr, "%s exists in hrStorageMap index=%d\n ", name, map->hrIndex));
}
entry->index = map->hrIndex;
@@ -118,7 +118,7 @@
struct hrStorageTblEntry *entry = NULL;
TAILQ_FOREACH(entry, &hrState_g.hr_storage_tbl, link)
- if (strncmp(entry->descr, name, 255) == 0)
+ if (strncmp(entry->descr, name, sizeof(entry->descr) - 1) == 0)
return (entry);
return (NULL);
}
@@ -149,8 +149,8 @@
int len = sizeof(hrState_g.mem_stats);
int page_size_bytes;
struct hrStorageTblEntry *entry = NULL;
- if( sysctl(mib, 2, &hrState_g.mem_stats, &len, NULL, 0) < 0 ) {
- syslog(LOG_ERR, "%s: sysctl( { CTL_VM, VM_METER } ) failed: %m ", __func__ );
+ if ( sysctl(mib, 2, &hrState_g.mem_stats, &len, NULL, 0) < 0 ) {
+ syslog(LOG_ERR, "hrStoragetable: %s: sysctl( { CTL_VM, VM_METER } ) failed: %m ", __func__ );
assert(0);
return;
}
@@ -161,11 +161,11 @@
/*Real Memory Metrics*/
entry = hrStorageTblEntry_find_by_name("Real Memory Metrics");
- if( entry == NULL ) {
+ if ( entry == NULL ) {
entry = hrStorageTblEntry_create("Real Memory Metrics");
}
assert(entry != NULL);
- if( entry == NULL) return; /*I'm out of luck now, maybe next time*/
+ if ( entry == NULL) return; /*I'm out of luck now, maybe next time*/
entry->flags |= HR_STORAGE_FOUND;
entry->type = (struct asn_oid)OIDX_hrStorageRam;
@@ -178,11 +178,11 @@
/*Shared Real Memory Metrics*/
entry = hrStorageTblEntry_find_by_name("Shared Real Memory Metrics");
- if( entry == NULL ) {
+ if ( entry == NULL ) {
entry = hrStorageTblEntry_create("Shared Real Memory Metrics");
}
assert(entry != NULL);
- if( entry == NULL) return; /*I'm out of luck now, maybe next time*/
+ if ( entry == NULL) return; /*I'm out of luck now, maybe next time*/
entry->flags |= HR_STORAGE_FOUND;
entry->type = (struct asn_oid)OIDX_hrStorageRam;
@@ -202,16 +202,16 @@
char swap_w_prefix[255+1];
if ( sysctlbyname("vm.nswapdev", &nswapdev,&len, NULL,0 ) < 0 ) {
- syslog(LOG_ERR,"sysctlbyname(\"vm.nswapdev\") failed. %m \n");
+ syslog(LOG_ERR, "hrStorageTable: sysctlbyname(\"vm.nswapdev\") failed. %m \n");
assert(0);
return;
}
- if( nswapdev <= 0 ) {
+ if ( nswapdev <= 0 ) {
HR_DPRINTF((stderr, "vm.nswapdev is %d\n ",nswapdev));
return;
}
- if( nswapdev + 1 != (int)hrState_g.swap_devs_len || hrState_g.swap_devs == NULL ) {
+ if ( nswapdev + 1 != (int)hrState_g.swap_devs_len || hrState_g.swap_devs == NULL ) {
hrState_g.swap_devs_len = nswapdev + 1;
hrState_g.swap_devs =
@@ -227,19 +227,20 @@
nswapdev = kvm_getswapinfo( hrState_g.kd, hrState_g.swap_devs,hrState_g.swap_devs_len, 0);
- if( nswapdev < 0 ) {
- syslog(LOG_ERR,"kvm_getswapinfo failed. %m \n");
+ if ( nswapdev < 0 ) {
+ syslog(LOG_ERR,"hrStorageTable: kvm_getswapinfo failed. %m \n");
assert(0);
return;
}
- for(len = 0; len < nswapdev; len++ ) {
+ for (len = 0; len < nswapdev; len++ ) {
memset(&swap_w_prefix[0], '\0', sizeof(swap_w_prefix) );
- snprintf(swap_w_prefix,255,"Swap:/dev/%s",hrState_g.swap_devs[len].ksw_devname);
+ snprintf(swap_w_prefix, sizeof(swap_w_prefix) - 1,
+ "Swap:/dev/%s", hrState_g.swap_devs[len].ksw_devname);
entry = hrStorageTblEntry_find_by_name(swap_w_prefix);
- if(entry != NULL) {
+ if (entry != NULL) {
entry->flags |= HR_STORAGE_FOUND;
entry->type = (struct asn_oid)OIDX_hrStorageVirtualMemory;
entry->allocationUnits = getpagesize();
@@ -272,33 +273,33 @@
void hrStrorage_getType_v(const struct statfs *fs_p, struct asn_oid *out_type_p) {
assert(fs_p != NULL);
assert(out_type_p != NULL);
- if( !(fs_p->f_flags & MNT_LOCAL) ) {
+ if ( !(fs_p->f_flags & MNT_LOCAL) ) {
*out_type_p = (struct asn_oid)OIDX_hrStorageNetworkDisk;
return;
}
- if( strncmp (fs_p->f_fstypename, "procfs", MFSNAMELEN ) == 0 ||
- strncmp (fs_p->f_fstypename, "devfs", MFSNAMELEN ) == 0 ) {
+ if ( strncmp (fs_p->f_fstypename, "procfs", MFSNAMELEN ) == 0 ||
+ strncmp (fs_p->f_fstypename, "devfs", MFSNAMELEN ) == 0 ) {
*out_type_p = (struct asn_oid)OIDX_hrStorageOther;
return;
}
- if( strncmp (fs_p->f_mntfromname, "/dev/fd", strlen("/dev/fd") ) == 0 ||
- strncmp (fs_p->f_mntfromname, "/dev/afd", strlen("/dev/afd") ) == 0) {
+ if ( strncmp (fs_p->f_mntfromname, "/dev/fd", strlen("/dev/fd") ) == 0 ||
+ strncmp (fs_p->f_mntfromname, "/dev/afd", strlen("/dev/afd") ) == 0) {
*out_type_p = (struct asn_oid)OIDX_hrStorageFloppyDisk;
return;
}
- if( strncmp (fs_p->f_mntfromname, "/dev/acd", strlen("/dev/acd") ) == 0 ||
- strncmp (fs_p->f_mntfromname, "/dev/cd", strlen("/dev/cd") ) == 0) {
+ if ( strncmp (fs_p->f_mntfromname, "/dev/acd", strlen("/dev/acd") ) == 0 ||
+ strncmp (fs_p->f_mntfromname, "/dev/cd", strlen("/dev/cd") ) == 0) {
*out_type_p = (struct asn_oid)OIDX_hrStorageCompactDisc;
return;
}
- if( strncmp (fs_p->f_mntfromname, "/dev/ad", strlen("/dev/ad") ) == 0 ||
- strncmp (fs_p->f_mntfromname, "/dev/ar", strlen("/dev/ar") ) == 0 ||
- strncmp (fs_p->f_mntfromname, "/dev/da", strlen("/dev/da") ) == 0 ) {
+ if ( strncmp (fs_p->f_mntfromname, "/dev/ad", strlen("/dev/ad") ) == 0 ||
+ strncmp (fs_p->f_mntfromname, "/dev/ar", strlen("/dev/ar") ) == 0 ||
+ strncmp (fs_p->f_mntfromname, "/dev/da", strlen("/dev/da") ) == 0 ) {
*out_type_p = (struct asn_oid)OIDX_hrStorageFixedDisk;
return;
}
@@ -321,26 +322,26 @@
char fs_string[255+1];
uint64_t used_blocks_count = 0;
- if( (mounted_fs_count = getfsstat(NULL, 0, MNT_NOWAIT)) < 0 ) {
- syslog(LOG_ERR, "getfsstat(NULL, 0, MNT_NOWAIT) failed: %m\n");
+ if ( (mounted_fs_count = getfsstat(NULL, 0, MNT_NOWAIT)) < 0 ) {
+ syslog(LOG_ERR, "hrStorageTable: getfsstat(NULL, 0, MNT_NOWAIT) failed: %m\n");
return; /*out of luck this time*/
}
- if( mounted_fs_count != (int)hrState_g.fs_buf_count || hrState_g.fs_buf == NULL ) {
+ if ( mounted_fs_count != (int)hrState_g.fs_buf_count || hrState_g.fs_buf == NULL ) {
hrState_g.fs_buf_count = mounted_fs_count;
hrState_g.fs_buf = (struct statfs *)reallocf(hrState_g.fs_buf,
hrState_g.fs_buf_count * sizeof(struct statfs));
- if(hrState_g.fs_buf == NULL) {
+ if (hrState_g.fs_buf == NULL) {
hrState_g.fs_buf_count = 0;
assert(0);
return;
}
}
- if( (mounted_fs_count = getfsstat(hrState_g.fs_buf,
+ if ( (mounted_fs_count = getfsstat(hrState_g.fs_buf,
hrState_g.fs_buf_count * sizeof(struct statfs),
MNT_NOWAIT)) < 0 ) {
- syslog(LOG_ERR, "getfsstat(, , MNT_NOWAIT) failed: %m \n");
+ syslog(LOG_ERR, "hrStorageTable: getfsstat(, , MNT_NOWAIT) failed: %m \n");
return; /*out of luck this time*/
}
@@ -348,16 +349,16 @@
FS_tbl_pre_refresh_v();
- for(i = 0; i < mounted_fs_count; i++ ) {
+ for (i = 0; i < mounted_fs_count; i++ ) {
memset(&fs_string[0], '\0', sizeof(fs_string) );
- snprintf(fs_string, 255, "%s, type: %s, dev: %s", hrState_g.fs_buf[i].f_mntonname,
+ snprintf(fs_string, sizeof(fs_string) - 1, "%s, type: %s, dev: %s", hrState_g.fs_buf[i].f_mntonname,
hrState_g.fs_buf[i].f_fstypename,
hrState_g.fs_buf[i].f_mntfromname);
entry = hrStorageTblEntry_find_by_name(fs_string);
- if(entry != NULL) {
+ if (entry != NULL) {
entry->flags |= HR_STORAGE_FOUND;
hrStrorage_getType_v( &hrState_g.fs_buf[i], &entry->type);
@@ -449,7 +450,7 @@
n1 = STAILQ_FIRST(&hrState_g.storage_name_map);
while (n1 != NULL) {
n2 = STAILQ_NEXT(n1, link);
- if(n1->entry_p != NULL){
+ if (n1->entry_p != NULL) {
TAILQ_REMOVE(&hrState_g.hr_storage_tbl, n1->entry_p, link);
free( n1->entry_p );
n1->entry_p = NULL;
More information about the p4-projects
mailing list