Date: Mon, 11 Jul 2005 20:54:24 GMT From: soc-victor <soc-victor@FreeBSD.org> To: Perforce Change Reviews <perforce@freebsd.org> Subject: PERFORCE change 80025 for review Message-ID: <200507112054.j6BKsOWq086160@repoman.freebsd.org>
next in thread | raw e-mail | index | archive | help
http://perforce.freebsd.org/chv.cgi?CH=80025 Change 80025 by soc-victor@soc-victor_82.76.158.176 on 2005/07/11 20:53:56 Added partial SNMP SET support for hrSystemDate SNMP scalar. (Timezone change still not implemented). Also fixed a bunch of coding style issues as Harti advised me. More coding style cleanup in the next submits. Affected files ... .. //depot/projects/soc2005/bsnmp/usr.sbin/bsnmpd/modules/snmp_hostres/Makefile#4 edit .. //depot/projects/soc2005/bsnmp/usr.sbin/bsnmpd/modules/snmp_hostres/hostres_scalars.c#3 edit .. //depot/projects/soc2005/bsnmp/usr.sbin/bsnmpd/modules/snmp_hostres/hostres_snmp.c#3 edit .. //depot/projects/soc2005/bsnmp/usr.sbin/bsnmpd/modules/snmp_hostres/hostres_snmp.h#3 edit Differences ... ==== //depot/projects/soc2005/bsnmp/usr.sbin/bsnmpd/modules/snmp_hostres/Makefile#4 (text+ko) ==== @@ -8,6 +8,7 @@ MOD= hostres SRCS= hostres_snmp.c hostres_scalars.c WARNS?= 6 +#Not having NDEBUG defined will enable assertions and a lot of output on stderr CFLAGS+= -DNDEBUG XSYM= host hrStorageOther hrStorageRam hrStorageVirtualMemory \ hrStorageFixedDisk hrStorageRemovableDisk hrStorageFloppyDisk \ ==== //depot/projects/soc2005/bsnmp/usr.sbin/bsnmpd/modules/snmp_hostres/hostres_scalars.c#3 (text+ko) ==== @@ -112,31 +112,33 @@ */ static uint32_t OS_getMemorySize(void); + +/* + * Try to use the s_date_time parameter as a DateAndTime TC to fill in + * the second paramter. + * Returns 0 on succes an < 0 for an error. + * Bug: time zone info is not used + */ +static +int OS_checkSystemDateInput(u_char s_date_time[11], struct timeval* out); /* + * Set system date and time. Timezone is not changed + * Return 0 for success, < 0 for an error + */ +static +int OS_setSystemDate(struct timeval* timeval_to_set); +/* * prototype of this function was genrated by gensnmptree tool in header file * hostres_tree.h * Returns SNMP_ERR_NOERROR on success */ -int op_hrSystem(struct snmp_context *ctx __unused, +int op_hrSystem(struct snmp_context *ctx, struct snmp_value *value, u_int sub, u_int iidx __unused, enum snmp_op curr_op) { - switch (curr_op) { - case SNMP_OP_SET: - case SNMP_OP_GET: - break; - - case SNMP_OP_ROLLBACK: - case SNMP_OP_COMMIT: - case SNMP_OP_GETNEXT: - default:{ - assert(0); - return (SNMP_ERR_GENERR); - } - }/*end switch*/ if(curr_op == SNMP_OP_GET){ switch (value->var.subs[sub - 1]) { @@ -144,7 +146,7 @@ value->v.uint32 = OS_getSystemUptime(); break; - case LEAF_hrSystemDate:{ + case LEAF_hrSystemDate: { u_char s_date_time[11]; int s_date_time_len = -1; if((s_date_time_len = OS_getSystemDate(s_date_time)) < 0){ @@ -157,17 +159,17 @@ value->v.uint32 = 0; /*FIX ME*/ break; - case LEAF_hrSystemInitialLoadParameters:{ + case LEAF_hrSystemInitialLoadParameters: { u_char *sys_load_param = NULL; - if((sys_load_param = OS_getSystemInitialLoadParameters()) != NULL){ + if((sys_load_param = OS_getSystemInitialLoadParameters()) != NULL) { return (string_get(value, sys_load_param, -1)); }else{ return (SNMP_ERR_GENERR); } } - case LEAF_hrSystemNumUsers:{ + case LEAF_hrSystemNumUsers: { int num_users = OS_getSystemNumUsers(); - if( num_users >= 0 ){ + if( num_users >= 0 ) { value->v.uint32 = num_users; }else{ return (SNMP_ERR_GENERR); @@ -175,9 +177,9 @@ } break; - case LEAF_hrSystemProcesses:{ + case LEAF_hrSystemProcesses: { int num_proc = OS_getSystemProcesses(); - if( num_proc >= 0 ){ + if( num_proc >= 0 ) { value->v.uint32 = num_proc; }else{ return (SNMP_ERR_GENERR); @@ -185,7 +187,7 @@ } break; - case LEAF_hrSystemMaxProcesses:{ + case LEAF_hrSystemMaxProcesses: { int max_proc = OS_getSystemMaxProcesses(); if( max_proc >= 0 ){ value->v.uint32 = max_proc; @@ -201,13 +203,31 @@ } }/*end switch*/ - }else{ - /*SNMP SET*/ - switch (value->var.subs[sub - 1]){ - case LEAF_hrSystemDate: + } else if(curr_op == SNMP_OP_SET ) { + + switch (value->var.subs[sub - 1]) { + case LEAF_hrSystemDate: { + if ( string_save(value, ctx, 8, + &hostres_ctx_g.time_to_set ) == SNMP_ERR_NOERROR + || + string_save(value, ctx, 11, + &hostres_ctx_g.time_to_set ) == SNMP_ERR_NOERROR + ) { + + if( OS_checkSystemDateInput( hostres_ctx_g.time_to_set, + &hostres_ctx_g.timeval_to_set) != 0 ) { + HR_DPRINTF((stderr, + "Parse for '%s' failed as DateAndTime \n", + hostres_ctx_g.time_to_set)); + return (SNMP_ERR_GENERR); + } + } + return (SNMP_ERR_NOERROR); + + } case LEAF_hrSystemInitialLoadDevice: case LEAF_hrSystemInitialLoadParameters: - default:{ + default: { /*not implmeted yet*/ assert(0); return (SNMP_ERR_GENERR); @@ -215,7 +235,55 @@ } } - } + } else if (curr_op == SNMP_OP_ROLLBACK ) { + switch (value->var.subs[sub - 1]) { + case LEAF_hrSystemDate: { + HR_DPRINTF((stderr, "SNMP_OP_ROLLBACK for LEAF_hrSystemDate\n")); + string_rollback(ctx, &hostres_ctx_g.time_to_set); + hostres_ctx_g.timeval_to_set.tv_sec = 0; + hostres_ctx_g.timeval_to_set.tv_usec = 0; + return (SNMP_ERR_NOERROR); + } + + case LEAF_hrSystemInitialLoadDevice: + case LEAF_hrSystemInitialLoadParameters: + default: { + /*not implmeted yet*/ + assert(0); + return (SNMP_ERR_GENERR); + + } + } + + } else if (curr_op == SNMP_OP_COMMIT ) { + switch (value->var.subs[sub - 1]) { + case LEAF_hrSystemDate: { + HR_DPRINTF((stderr, + "SNMP_OP_COMMIT for LEAF_hrSystemDate\n")); + string_commit(ctx); + if( OS_setSystemDate(&hostres_ctx_g.timeval_to_set) != 0 ) { + HR_DPRINTF((stderr, + "SNMP_OP_COMMIT FAILEDfor LEAF_hrSystemDate\n")); + return (SNMP_ERR_GENERR); + } + return (SNMP_ERR_NOERROR); + } + + case LEAF_hrSystemInitialLoadDevice: + case LEAF_hrSystemInitialLoadParameters: + default: { + /*not implmeted yet*/ + assert(0); + return (SNMP_ERR_GENERR); + + } + } + + } else{ + /*get next*/ + HR_DPRINTF((stderr, "SNMP_OP_GETNEXT meaningless for scalars \n")); + return (SNMP_ERR_GENERR); + } return (SNMP_ERR_NOERROR); } @@ -273,12 +341,12 @@ * FreeBSD specific functions zone */ -uint32_t OS_getSystemUptime(void){ +uint32_t OS_getSystemUptime(void) { struct timeval right_now = {0,0}; - if( hostres_ctx_g.kernel_boot == 0 ){ + if( hostres_ctx_g.kernel_boot == 0 ) { /*first time, do the sysctl*/ @@ -286,12 +354,12 @@ int mib[2]={CTL_KERN,KERN_BOOTTIME}; size_t len = sizeof(kernel_boot_timestamp); - if(sysctl(mib, 2, &kernel_boot_timestamp, &len, NULL, 0) == -1){ - syslog(LOG_ERR,"sysctl KERN_BOOTTIME failed: %m "); - return 0; /*error*/ + if(sysctl(mib, 2, &kernel_boot_timestamp, &len, NULL, 0) == -1) { + syslog(LOG_ERR, "sysctl KERN_BOOTTIME failed: %m "); + return (0); /*error*/ } - HR_DPRINTF((stderr,"Got boot timestamp from kernel:{%ld,%ld}\n", + HR_DPRINTF((stderr, "Got boot timestamp from kernel:{%ld,%ld}\n", kernel_boot_timestamp.tv_sec, kernel_boot_timestamp.tv_usec)); @@ -299,9 +367,9 @@ (kernel_boot_timestamp.tv_usec / 10000); } - if( gettimeofday(&right_now, NULL) < 0 ){ - syslog(LOG_ERR,"gettimeofday failed: %m "); - return 0; /*error*/ + if( gettimeofday(&right_now, NULL) < 0 ) { + syslog(LOG_ERR, "gettimeofday failed: %m "); + return (0); /*error*/ } return ( (right_now.tv_sec * 100) + @@ -314,21 +382,21 @@ -int OS_getSystemDate(u_char s_date_time[11]){ +int OS_getSystemDate(u_char s_date_time[11]) { struct tm tloc_tm; time_t tloc_time_t; struct timeval right_now = {0,0}; int string_len = -1; - if( gettimeofday(&right_now, NULL) < 0 ){ - syslog(LOG_ERR,"gettimeofday failed: %m "); + if( gettimeofday(&right_now, NULL) < 0 ) { + syslog(LOG_ERR, "gettimeofday failed: %m "); return (-1); /*error*/ } tloc_time_t = right_now.tv_sec; - if(localtime_r(&tloc_time_t, &tloc_tm) == NULL ){ - syslog(LOG_ERR,"localtime_r() failed: %m "); + if(localtime_r(&tloc_time_t, &tloc_tm) == NULL ) { + syslog(LOG_ERR, "localtime_r() failed: %m "); return (-1); /*error*/ } @@ -359,28 +427,28 @@ OS_getSystemInitialLoadParameters(void){ - if( strlen(hostres_ctx_g.k_boot_line) == 0){ - int mib[2]={CTL_KERN,KERN_BOOTFILE}; + if( strlen(hostres_ctx_g.k_boot_line) == 0) { + int mib[2]={ CTL_KERN, KERN_BOOTFILE }; char *buf = NULL; size_t buf_len = 0; - sysctl(mib, 2, NULL, &buf_len, NULL, 0);/*got the needed buffer len */ + sysctl(mib, 2, NULL, &buf_len, NULL, 0); /*got the needed buffer len */ buf = (u_char*)malloc(buf_len); if(buf == NULL){ - syslog(LOG_ERR,"malloc failed "); + syslog(LOG_ERR, "malloc failed "); return (NULL); /*error*/ } if ( sysctl(mib, 2, buf, &buf_len, NULL, 0) ) { - syslog(LOG_ERR,"sysctl({CTL_KERN,KERN_BOOTFILE}) failed: %m "); + syslog(LOG_ERR, "sysctl({CTL_KERN,KERN_BOOTFILE}) failed: %m "); free(buf); return (NULL); /*error*/ } (void)strncpy(hostres_ctx_g.k_boot_line, buf, 127); - HR_DPRINTF((stderr,"Got kernel boot file: %s\n",hostres_ctx_g.k_boot_line)); + HR_DPRINTF((stderr, "Got kernel boot file: %s\n", hostres_ctx_g.k_boot_line)); free(buf); } return ((u_char*)&hostres_ctx_g.k_boot_line[0]); @@ -388,73 +456,116 @@ -int OS_getSystemNumUsers(void){ +int OS_getSystemNumUsers(void) { int user_counter = 0; struct utmp utmp_entry; struct passwd *pw; - if( hostres_ctx_g.utmp_fp == (FILE*)NULL ){ + if( hostres_ctx_g.utmp_fp == (FILE*)NULL ) { assert(0); - return -1; /*error*/ + return (-1); /*error*/ } /*start with the begining of the utmp file*/ rewind(hostres_ctx_g.utmp_fp); - while (fread(&utmp_entry, sizeof(utmp_entry), 1, hostres_ctx_g.utmp_fp) == 1){ - if (utmp_entry.ut_name[0] && utmp_entry.ut_line[0]){ - if( (pw = getpwnam(utmp_entry.ut_name)) == NULL){ + while (fread(&utmp_entry, sizeof(utmp_entry), 1, hostres_ctx_g.utmp_fp) == 1 ) { + if (utmp_entry.ut_name[0] && utmp_entry.ut_line[0]) { + if( (pw = getpwnam(utmp_entry.ut_name)) == NULL ) { continue; } user_counter++; } } - return user_counter; + return (user_counter); } -int OS_getSystemProcesses(void){ +int OS_getSystemProcesses(void) { int proc_count = 0; - if( hostres_ctx_g.kd == (kvm_t*)NULL ){ + if( hostres_ctx_g.kd == (kvm_t*)NULL ) { assert(0); - return -1; /*error*/ + return (-1); /*error*/ } - if( kvm_getprocs(hostres_ctx_g.kd, KERN_PROC_PROC, 0, &proc_count) == NULL ){ - syslog(LOG_ERR,"kvm_getprocs failed: %m "); - return -1;/*error*/ + if( kvm_getprocs(hostres_ctx_g.kd, KERN_PROC_PROC, 0, &proc_count) == NULL ) { + syslog(LOG_ERR, "kvm_getprocs failed: %m "); + return (-1);/*error*/ } - return proc_count; + return (proc_count); } -int OS_getSystemMaxProcesses(void){ - if( hostres_ctx_g.max_proc == 0 ){ +int OS_getSystemMaxProcesses(void) { + if( hostres_ctx_g.max_proc == 0 ) { int mib[2] = {CTL_KERN, KERN_MAXPROC}; size_t len = 0; len = sizeof(hostres_ctx_g.max_proc); - if(sysctl(mib, 2, &hostres_ctx_g.max_proc, &len, NULL, 0) == -1){ - syslog(LOG_ERR,"sysctl KERN_MAXPROC failed: %m "); - return -1; /*error*/ + if(sysctl(mib, 2, &hostres_ctx_g.max_proc, &len, NULL, 0) == -1) { + syslog(LOG_ERR, "sysctl KERN_MAXPROC failed: %m "); + return (-1); /*error*/ } HR_DPRINTF((stderr, "Got kernel maxporc: %d\n",hostres_ctx_g.max_proc)); } - return hostres_ctx_g.max_proc; + return (hostres_ctx_g.max_proc); } -uint32_t OS_getMemorySize(void){ +uint32_t OS_getMemorySize(void) { - if(hostres_ctx_g.phys_mem_size == 0){ + if(hostres_ctx_g.phys_mem_size == 0) { int mib[2] = { CTL_HW, HW_PHYSMEM }; size_t len = sizeof(hostres_ctx_g.phys_mem_size); - if (sysctl(mib, 2, &hostres_ctx_g.phys_mem_size, &len, NULL, 0) == -1){ - syslog(LOG_ERR,"sysctl({ CTL_HW, HW_PHYSMEM }) failed: %m "); - return 0;/*error*/ + if (sysctl(mib, 2, &hostres_ctx_g.phys_mem_size, &len, NULL, 0) == -1) { + syslog(LOG_ERR, "sysctl({ CTL_HW, HW_PHYSMEM }) failed: %m "); + return 0; /*error*/ } - hostres_ctx_g.phys_mem_size = hostres_ctx_g.phys_mem_size/1024; + hostres_ctx_g.phys_mem_size = hostres_ctx_g.phys_mem_size / 1024; + } + return (hostres_ctx_g.phys_mem_size); + +} + + +int OS_checkSystemDateInput(u_char s_date_time[11], struct timeval* out) { + struct tm tm_to_set; + time_t time_t_to_set; + + + if( out == NULL ) { + assert(0); + return (-1); /*error*/ + } + + tm_to_set.tm_year = (( (int32_t)s_date_time[0] )<<8) + + ((int32_t)s_date_time[1]-1900); + tm_to_set.tm_mon = (int32_t)s_date_time[2]-1; + tm_to_set.tm_mday = (int32_t)s_date_time[3]; + tm_to_set.tm_hour = (int32_t)s_date_time[4]; + tm_to_set.tm_min = (int32_t)s_date_time[5]; + tm_to_set.tm_sec = (int32_t)s_date_time[6]; + + /*the input is interpreted as local time, not UTC, not GMT*/ + + if( (time_t_to_set = mktime(&tm_to_set) ) == (time_t)-1) { + return (-1); /*error*/ } - return hostres_ctx_g.phys_mem_size; + out->tv_sec = time_t_to_set; /*seconds*/ + out->tv_usec = (int32_t)s_date_time[7] * 100000; /*deci-seconds to micro-seconds*/ + + return ( 0 ); + +} +int OS_setSystemDate(struct timeval* timeval_to_set) { + if(timeval_to_set != NULL) { + if( settimeofday( timeval_to_set, (struct timezone *)NULL) < 0 ) { + HR_DPRINTF(( stderr, "settimeofday done\n" )); + syslog(LOG_ERR, "settimeofday failed: %m "); + return (-1); /*error*/ + } + return (0); + } + return (-1); /*error*/ } ==== //depot/projects/soc2005/bsnmp/usr.sbin/bsnmpd/modules/snmp_hostres/hostres_snmp.c#3 (text+ko) ==== @@ -56,7 +56,7 @@ Returns 0 on success, < 0 on error */ static -int hostres_init(struct lmodule * mod, int argc __unused, char *argv[] __unused){ +int hostres_init(struct lmodule * mod, int argc __unused, char *argv[] __unused) { hostres_module = mod; @@ -72,20 +72,20 @@ hostres_ctx_g.phys_mem_size = 0; - if( ( hostres_ctx_g.utmp_fp = fopen(_PATH_UTMP, "r") ) == (FILE*)NULL){ - syslog(LOG_ERR,"fopen(%s) failed: %m ",(const char*)_PATH_UTMP); + if( ( hostres_ctx_g.utmp_fp = fopen(_PATH_UTMP, "r") ) == (FILE*)NULL) { + syslog(LOG_ERR, "fopen(%s) failed: %m ", (const char*)_PATH_UTMP); return (-1); /*error*/ } if( ( hostres_ctx_g.kd = kvm_open(NULL, _PATH_DEVNULL, NULL, O_RDONLY, "kvm_open")) == (kvm_t*)NULL ) { - syslog(LOG_ERR,"kvm_open failed: %m "); + syslog(LOG_ERR, "kvm_open failed: %m "); return (-1); /*error*/ } - HR_DPRINTF((stderr,"[%s] done.\n", __FUNCTION__)); + HR_DPRINTF((stderr, "[%s] done.\n", __func__)); return (0); } @@ -98,17 +98,17 @@ int hostres_fini(void) { /* here I free the resources used by this module*/ - if( hostres_ctx_g.utmp_fp != (FILE*)NULL ){ - if( fclose(hostres_ctx_g.utmp_fp) != 0 ){ - syslog(LOG_ERR,"fclose failed: %m "); + if( hostres_ctx_g.utmp_fp != (FILE*)NULL ) { + if( fclose(hostres_ctx_g.utmp_fp) != 0 ) { + syslog(LOG_ERR, "fclose failed: %m "); return (-1); /*error*/ } hostres_ctx_g.utmp_fp = (FILE*)NULL; } - if( hostres_ctx_g.kd != (kvm_t*)NULL ){ - if( kvm_close(hostres_ctx_g.kd) != 0 ){ - syslog(LOG_ERR,"kvm_close failed: %m "); + if( hostres_ctx_g.kd != (kvm_t*)NULL ) { + if( kvm_close(hostres_ctx_g.kd) != 0 ) { + syslog(LOG_ERR, "kvm_close failed: %m "); return (-1); /*error*/ } hostres_ctx_g.kd = (kvm_t*)NULL; @@ -127,7 +127,7 @@ if( host_registration_id > 0){ or_unregister(host_registration_id); } - HR_DPRINTF((stderr,"[%s] done.\n", __FUNCTION__)); + HR_DPRINTF((stderr, "[%s] done.\n", __func__)); return (0); } @@ -136,9 +136,9 @@ returns nothing */ static -void hostres_idle_v(void){ +void hostres_idle_v(void) { /*nothing iteresting here for the time being*/ - HR_DPRINTF((stderr,"[%s] done.\n ", __FUNCTION__)); + HR_DPRINTF((stderr, "[%s] done.\n ", __func__)); } /* @@ -146,9 +146,9 @@ returns nothing */ static -void hostres_dump_v(void){ +void hostres_dump_v(void) { /*nothing iteresting here for the time being*/ - HR_DPRINTF((stderr, "[%s] done.\n ", __FUNCTION__)); + HR_DPRINTF((stderr, "[%s] done.\n ", __func__)); } @@ -157,9 +157,9 @@ returns nothing */ static -void hostres_config_v(void){ +void hostres_config_v(void) { /*nothing iteresting here for the time being*/ - HR_DPRINTF((stderr, "[%s] done.\n ", __FUNCTION__)); + HR_DPRINTF((stderr, "[%s] done.\n ", __func__)); } @@ -168,11 +168,11 @@ void hostres_loading_v(const struct lmodule *mod __unused, int loaded) { /*nothing iteresting here for the time being*/ - if (loaded == 1){ + if ( loaded == 1 ) { goto LABEL_RETURN; } LABEL_RETURN:; - HR_DPRINTF((stderr,"[%s] done.\n ", __FUNCTION__)); + HR_DPRINTF((stderr,"[%s] done.\n ", __func__)); } /* @@ -180,11 +180,11 @@ returns nothing */ static -void hostres_start_v(void){ - host_registration_id = or_register(&oid_host, - "The MIB module for host resource mib (rfc 2790).", +void hostres_start_v(void) { + host_registration_id = or_register(&oid_host, + "The MIB module for host resource mib (rfc 2790).", hostres_module); - HR_DPRINTF((stderr, "[%s] done.\n ", __FUNCTION__)); + HR_DPRINTF((stderr, "[%s] done.\n ", __func__)); } /*this identifies the HOST RESOURCES mib module*/ ==== //depot/projects/soc2005/bsnmp/usr.sbin/bsnmpd/modules/snmp_hostres/hostres_snmp.h#3 (text+ko) ==== @@ -34,20 +34,20 @@ #include "asn1.h" #include "snmp.h" #include "snmpmod.h" -#include <stdio.h> /*for FILE */ -#include <fcntl.h> /*for kvm_open & friends*/ +#include <stdio.h> /*for FILE */ +#include <fcntl.h> /*for kvm_open & friends*/ #include <kvm.h> +#include <sys/time.h> /*for struct timeval*/ /*a debug macro*/ #ifndef NDEBUG - -#define HR_DPRINTF(args) (fprintf(stderr,"HR DEBUG: "), fprintf args) - +#define HR_DPRINTF(ARGS) do { \ + fprintf(stderr, "HRDEBUG: "); \ + fprintf ARGS; \ + } while (0) #else - #define HR_DPRINTF(args) - #endif /*NDEBUG*/ typedef struct hostres_ctx_struct{ @@ -58,6 +58,10 @@ int max_proc; /*maximum number of processes */ uint32_t phys_mem_size; /*physical memory size in Kb*/ + u_char *time_to_set; /*for setting hrSystemDate*/ + struct timeval timeval_to_set; /*for setting hrSystemDate*/ + + }hostres_ctx_type; extern hostres_ctx_type hostres_ctx_g;
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200507112054.j6BKsOWq086160>