📄 hr_swinst.c
字号:
oid * name, size_t * length, int exact, size_t * var_len, WriteMethod ** write_method){ SWI_t *swi = &_myswi; /* XXX static for now */ int sw_idx = 0; static char string[SNMP_MAXPATH]; u_char *ret = NULL; struct stat stat_buf; if (vp->magic < HRSWINST_INDEX) { if (header_hrswinst(vp, name, length, exact, var_len, write_method) == MATCH_FAILED) return NULL; } else { sw_idx = header_hrswInstEntry(vp, name, length, exact, var_len, write_method); if (sw_idx == MATCH_FAILED) return NULL; } switch (vp->magic) { case HRSWINST_CHANGE: case HRSWINST_UPDATE: string[0] = '\0'; if (swi->swi_directory != NULL) { strncpy(string, swi->swi_directory, sizeof(string)); string[ sizeof(string)-1 ] = 0; } if (*string && (stat(string, &stat_buf) != -1)) { if (stat_buf.st_mtime > starttime.tv_sec) /* * changed 'recently' - i.e. since this agent started */ long_return = (stat_buf.st_mtime - starttime.tv_sec) * 100; else long_return = 0; /* predates this agent */ } else#if NETSNMP_NO_DUMMY_VALUES return NULL;#else long_return = 363136200;#endif ret = (u_char *) & long_return; break; case HRSWINST_INDEX: long_return = sw_idx; ret = (u_char *) & long_return; break; case HRSWINST_NAME: { strncpy(string, swi->swi_name, sizeof(string) - 1); /* * This will be unchanged from the initial "null" * value, if swi->swi_name is not defined */ string[sizeof(string) - 1] = '\0'; *var_len = strlen(string); ret = (u_char *) string; } break; case HRSWINST_ID: *var_len = nullOidLen; ret = (u_char *) nullOid; break; case HRSWINST_TYPE: {#ifdef HAVE_PKGINFO /* * at least on solaris2 this works */ char *catg = pkgparam(swi->swi_name, "CATEGORY"); if (catg == NULL) { long_return = 1; /* unknown */ } else { if (strstr(catg, "system") != NULL) { long_return = 2; /* operatingSystem */ } else if (strstr(catg, "application") != NULL) { long_return = 4; /* applcation */ } else { long_return = 1; /* unknown */ } free(catg); }#else# ifdef HAVE_LIBRPM char *rpm_groups; if ( headerGetEntry(swi->swi_h, RPMTAG_GROUP, NULL, (void **) &rpm_groups, NULL) ) { if ( strstr(rpm_groups, "System Environment") != NULL ) long_return = 2; /* operatingSystem */ else long_return = 4; /* applcation */ } else { long_return = 1; /* unknown */ }# else long_return = 1; /* unknown */# endif#endif ret = (u_char *) & long_return; } break; case HRSWINST_DATE: {#ifdef HAVE_LIBRPM int_32 *rpm_data; if ( headerGetEntry(swi->swi_h, RPMTAG_INSTALLTIME, NULL, (void **) &rpm_data, NULL) ) { time_t installTime = *rpm_data; ret = date_n_time(&installTime, var_len); } else { ret = date_n_time(0, var_len); }#else if (swi->swi_directory != NULL) { snprintf(string, sizeof(string), "%s/%s", swi->swi_directory, swi->swi_name); string[ sizeof(string)-1 ] = 0; stat(string, &stat_buf); ret = date_n_time(&stat_buf.st_mtime, var_len); } else {#if NETSNMP_NO_DUMMY_VALUES return NULL;#endif sprintf(string, "back in the mists of time"); *var_len = strlen(string); ret = (u_char *) string; }#endif } break; default: DEBUGMSGTL(("snmpd", "unknown sub-id %d in var_hrswinst\n", vp->magic)); ret = NULL; break; } Release_HRSW_token(); return ret;} /********************* * * Internal implementation functions * *********************/#ifdef HAVE_LIBRPMstatic voidCheck_HRSW_cache(void *xxx){ SWI_t *swi = (SWI_t *) xxx; /* * Make sure cache is up-to-date */ if (swi->swi_recs != NULL) { struct stat sb; lstat(swi->swi_directory, &sb); if (swi->swi_timestamp == sb.st_mtime) return; swi->swi_timestamp = sb.st_mtime; swi->swi_maxrec = 0; } /* * Get header offsets */ { int ix = 0; int offset;#if defined(RPMDBI_PACKAGES) rpmdbMatchIterator mi = NULL; Header h; mi = rpmdbInitIterator(swi->swi_rpmdb, RPMDBI_PACKAGES, NULL, 0); while ((h = rpmdbNextIterator(mi)) != NULL) { offset = rpmdbGetIteratorOffset(mi);#else for (offset = rpmdbFirstRecNum(swi->swi_rpmdb); offset != 0; offset = rpmdbNextRecNum(swi->swi_rpmdb, offset)) {#endif if (ix >= swi->swi_maxrec) { swi->swi_maxrec += 256; swi->swi_recs = (swi->swi_recs == NULL) ? (int *) malloc(swi->swi_maxrec * sizeof(int)) : (int *) realloc(swi->swi_recs, swi->swi_maxrec * sizeof(int)); } swi->swi_recs[ix++] = offset;#if !defined(RPMDBI_PACKAGES) }#else } rpmdbFreeIterator(mi);#endif swi->swi_nrec = ix; }}#endif /* HAVE_LIBRPM */voidInit_HR_SWInst(void){ SWI_t *swi = &_myswi; /* XXX static for now */ swi->swi_index = 0;#ifdef HAVE_LIBRPM if (swi->swi_rpmdb != NULL) return; if (rpmdbOpen("", &swi->swi_rpmdb, O_RDONLY, 0644) != 0) swi->swi_index = -1; Check_HRSW_cache(swi);#else if (swi->swi_directory != NULL) { if (swi->swi_dp != NULL) { closedir(swi->swi_dp); swi->swi_dp = NULL; } if ((swi->swi_dp = opendir(swi->swi_directory)) == NULL) swi->swi_index = -1; } else swi->swi_index = -1;#endif}intGet_Next_HR_SWInst(void){ SWI_t *swi = &_myswi; /* XXX static for now */ if (swi->swi_index == -1) return -1;#ifdef HAVE_LIBRPM /* * XXX Watchout: index starts with 1 */ if (0 <= swi->swi_index && swi->swi_index < swi->swi_nrec) return ++swi->swi_index;#else if (swi->swi_directory != NULL) { while ((swi->swi_dep = readdir(swi->swi_dp)) != NULL) { if (swi->swi_dep->d_name[0] == '.') continue; /* * Ought to check for "properly-formed" entry */ return ++swi->swi_index; } }#endif return -1;}voidSave_HR_SW_info(int ix){ SWI_t *swi = &_myswi; /* XXX static for now */#ifdef HAVE_LIBRPM /* * XXX Watchout: ix starts with 1 */ if (1 <= ix && ix <= swi->swi_nrec && ix != swi->swi_prevx) { int offset; Header h; char *n, *v, *r; offset = swi->swi_recs[ix - 1];#if defined(RPMDBI_PACKAGES) { rpmdbMatchIterator mi; mi = rpmdbInitIterator(swi->swi_rpmdb, RPMDBI_PACKAGES, &offset, sizeof(offset)); if ((h = rpmdbNextIterator(mi)) != NULL) h = headerLink(h); rpmdbFreeIterator(mi); }#else h = rpmdbGetRecord(swi->swi_rpmdb, offset);#endif if (h == NULL) return; if (swi->swi_h != NULL) headerFree(swi->swi_h); swi->swi_h = h; swi->swi_prevx = ix; headerGetEntry(swi->swi_h, RPMTAG_NAME, NULL, (void **) &n, NULL); headerGetEntry(swi->swi_h, RPMTAG_VERSION, NULL, (void **) &v, NULL); headerGetEntry(swi->swi_h, RPMTAG_RELEASE, NULL, (void **) &r, NULL); snprintf(swi->swi_name, sizeof(swi->swi_name), "%s-%s-%s", n, v, r); swi->swi_name[ sizeof(swi->swi_name)-1 ] = 0; }#else snprintf(swi->swi_name, sizeof(swi->swi_name), swi->swi_dep->d_name); swi->swi_name[ sizeof(swi->swi_name)-1 ] = 0;#endif}#ifdef HAVE_LIBRPMvoidMark_HRSW_token(void){}voidRelease_HRSW_token(void){ SWI_t *swi = &_myswi; /* XXX static for now */ if (swi != NULL && swi->swi_h) { headerFree(swi->swi_h); swi->swi_h = NULL; swi->swi_prevx = -1; }}#endif /* HAVE_LIBRPM */voidEnd_HR_SWInst(void){ SWI_t *swi = &_myswi; /* XXX static for now */#ifdef HAVE_LIBRPM rpmdbClose(swi->swi_rpmdb); /* or only on finishing ? */ swi->swi_rpmdb = NULL;#else if (swi->swi_dp != NULL) closedir(swi->swi_dp); swi->swi_dp = NULL;#endif}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -