Debian Codegraph @

debian/patches/agent_swinst_dpkg_date

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