File: agent_swinst_dpkg_date

package info (click to toggle)
net-snmp 5.9.3%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 37,540 kB
  • sloc: ansic: 282,201; perl: 17,710; sh: 12,006; makefile: 2,712; python: 735; xml: 663; pascal: 62; sql: 47
file content (89 lines) | stat: -rw-r--r-- 3,750 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
Description: swinst_apt: Use dpkg for modified date
 Instead of trawling through dpkg's files, let dpkg-query tell us
 when the package was last modified. It means if the directory
 changes or any other internal changes happen, this will still
 keep working.
 .
 db-sys:Last-Modified field has been in since dpkg-query 1.19.3
 which means it works for Debian stable (Buster) onwards.
Author: Craig Small <csmall@debian.org>
Origin: Debian
Bug: https://github.com/net-snmp/net-snmp/pull/91
Bug-Debian: https://bugs.debian.org/905668
Reviewed-by: Craig Small <csmall@debian.org>
Last-Update: 2020-04-04
--- a/agent/mibgroup/host/data_access/swinst_apt.c
+++ b/agent/mibgroup/host/data_access/swinst_apt.c
@@ -33,17 +33,14 @@
 
 config_require(date_n_time)
 
-char pkg_directory[SNMP_MAXBUF];
 static char apt_fmt[SNMP_MAXBUF];
-static char file[SNMP_MAXBUF];
 
 /* ---------------------------------------------------------------------
  */
 void
 netsnmp_swinst_arch_init(void)
 {
-    strlcpy(pkg_directory, "/var/lib/dpkg/info", sizeof(pkg_directory));
-    snprintf(apt_fmt, SNMP_MAXBUF, "%%%d[^#]#%%%d[^#]#%%%d[^#]#%%%d[^#]#%%%d[^#]#%%%d[^#]#%%%ds",
+    snprintf(apt_fmt, SNMP_MAXBUF, "%%%d[^#]#%%%d[^#]#%%%d[^#]#%%%d[^#]#%%%d[^#]#%%%d[^#]#%%%d[^#]#%%lld",
 	SNMP_MAXBUF-1, SNMP_MAXBUF-1, SNMP_MAXBUF-1, SNMP_MAXBUF-1,
 	SNMP_MAXBUF-1, SNMP_MAXBUF-1, SNMP_MAXBUF-1);
 }
@@ -60,7 +57,7 @@
 int
 netsnmp_swinst_arch_load( netsnmp_container *container, u_int flags)
 {
-    FILE *p = popen("dpkg-query --show --showformat '${Package}#${Version}#${Section}#${Priority}#${Essential}#${Architecture}#${Status}\n'", "r");
+    FILE *p = popen("dpkg-query --show --showformat '${Package}#${Version}#${Section}#${Priority}#${Essential}#${Architecture}#${Status}#${db-fsys:Last-Modified}\n'", "r");
     char package[SNMP_MAXBUF];
     char version[SNMP_MAXBUF];
     char section[SNMP_MAXBUF];
@@ -68,8 +65,8 @@
     char essential[SNMP_MAXBUF];
     char arch[SNMP_MAXBUF];
     char status[SNMP_MAXBUF];
+    long long last_modified;
     char buf[BUFSIZ];
-    struct stat stat_buf;
     netsnmp_swinst_entry *entry;
     u_char *date_buf;
     size_t date_len;
@@ -87,7 +84,7 @@
             continue;   /* error already logged by function */
         CONTAINER_INSERT(container, entry);
 
-	sscanf(buf, apt_fmt, package, version, section, priority, essential, arch, status);
+	sscanf(buf, apt_fmt, package, version, section, priority, essential, arch, status, &last_modified);
 	if (strstr(status, "not-installed"))
 	    continue;
 
@@ -99,21 +96,10 @@
                         ? 2      /* operatingSystem */
                         : 4;     /*  application    */
 
-        /* get the last mod date */
-        snprintf(file, sizeof(file), "%s/%s.list", pkg_directory, package);
-        if(stat(file, &stat_buf) != -1) {
-            date_buf = date_n_time(&stat_buf.st_mtime, &date_len);
-            entry->swDate_len = date_len;
-            memcpy(entry->swDate, date_buf, entry->swDate_len);
-        } else {
-            /* somewhy some files include :arch in .list name */
-            snprintf(file, sizeof(file), "%s/%s:%s.list", pkg_directory, package, arch);
-            if(stat(file, &stat_buf) != -1) {
-                date_buf = date_n_time(&stat_buf.st_mtime, &date_len);
-                entry->swDate_len = date_len;
-                memcpy(entry->swDate, date_buf, entry->swDate_len);
-            }
-        }
+	date_buf = date_n_time((time_t*)(&last_modified), &date_len);
+        entry->swDate_len = date_len;
+        memcpy(entry->swDate, date_buf, entry->swDate_len);
+
         /* FIXME, or fallback to whatever nonsesnse was here before, or leave it uninitialied?
              else {
         entry->swDate_len = 8;