-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
rpm_linux.go
394 lines (319 loc) · 10.3 KB
/
rpm_linux.go
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.
// +build linux,cgo
package pkg
import (
"errors"
"fmt"
"runtime"
"time"
"unsafe"
"github.com/coreos/pkg/dlopen"
)
/*
#include <stdio.h>
#include <stdlib.h>
#include <rpm/rpmlib.h>
#include <rpm/header.h>
#include <rpm/rpmts.h>
#include <rpm/rpmdb.h>
rpmts
my_rpmtsCreate(void *f) {
rpmts (*rpmtsCreate)();
rpmtsCreate = (rpmts (*)())f;
return rpmtsCreate();
}
void
my_rpmFreeMacros(void *f) {
void (*rpmFreeMacros)(void *);
rpmFreeMacros = (void(*)(void*))f;
rpmFreeMacros(NULL);
}
void
my_rpmFreeRpmrc(void *f) {
void (*rpmFreeRpmrc)(void);
rpmFreeRpmrc = (void (*)(void))f;
rpmFreeRpmrc();
}
int
my_rpmReadConfigFiles(void *f) {
int (*rpmReadConfigFiles)(const char*, const char*);
rpmReadConfigFiles = (int (*)(const char*, const char*))f;
return rpmReadConfigFiles(NULL, NULL);
}
rpmdbMatchIterator
my_rpmtsInitIterator(void *f, rpmts ts) {
rpmdbMatchIterator (*rpmtsInitIterator)(const rpmts, rpmTag, const void*, size_t);
rpmtsInitIterator = (rpmdbMatchIterator (*)(const rpmts, rpmTag, const void*, size_t))f;
return rpmtsInitIterator(ts, RPMDBI_PACKAGES, NULL, 0);
}
Header
my_rpmdbNextIterator(void *f, rpmdbMatchIterator mi) {
Header (*rpmdbNextIterator)(rpmdbMatchIterator);
rpmdbNextIterator = (Header (*)(rpmdbMatchIterator))f;
return rpmdbNextIterator(mi);
}
Header
my_headerLink(void *f, Header h) {
Header (*headerLink)(Header);
headerLink = (Header (*)(Header))f;
return headerLink(h);
}
// Note: Using int32_t instead of rpmTag/rpmTagVal in definitions
// to make it work on CentOS 6.x, 7.x, and Fedora 29.
const char *
my_headerGetString(void *f, Header h, int32_t tag) {
const char * (*headerGetString)(Header, int32_t);
headerGetString = (const char * (*)(Header, int32_t))f;
return headerGetString(h, tag);
}
// Note: Using int32_t instead of rpmTag/rpmTagVal in definitions
// to make it work on CentOS 6.x, 7.x, and Fedora 29.
uint64_t
my_headerGetNumber(void *f, Header h, int32_t tag) {
uint64_t (*headerGetNumber)(Header, int32_t);
headerGetNumber = (uint64_t (*)(Header, int32_t))f;
return headerGetNumber(h, tag);
}
void
my_headerFree(void *f, Header h) {
Header (*headerFree)(Header);
headerFree = (Header (*)(Header))f;
headerFree(h);
}
void
my_rpmdbFreeIterator(void *f, rpmdbMatchIterator mi) {
rpmdbMatchIterator (*rpmdbFreeIterator)(rpmdbMatchIterator);
rpmdbFreeIterator = (rpmdbMatchIterator (*)(rpmdbMatchIterator))f;
rpmdbFreeIterator(mi);
}
void
my_rpmtsFree(void *f, rpmts ts) {
rpmts (*rpmtsFree)(rpmts);
rpmtsFree = (rpmts (*)(rpmts))f;
rpmtsFree(ts);
}
// By default, librpm is going to trap various UNIX signals including SIGINT and SIGTERM
// which will prevent Beats from shutting down correctly.
//
// This disables that behavior by nullifying rpmsqEnable. We should be very dilligent in
// cleaning up in our use of librpm.
//
// More recent versions of librpm have a new function rpmsqSetInterruptSafety()
// to do this, see below.
//
// See also:
// - librpm traps signals and calls exit(1) to terminate the whole process incl. our Go code: https://github.com/rpm-software-management/rpm/blob/rpm-4.11.3-release/lib/rpmdb.c#L640
// - has caused problems for gdb before, they also nullify rpmsqEnable: https://bugzilla.redhat.com/show_bug.cgi?id=643031
// - the new rpmsqSetInterruptSafety(), unfortunately only available in librpm>=4.14.0 (CentOS 7 has 4.11.3): https://github.com/rpm-software-management/rpm/commit/56f49d7f5af7c1c8a3eb478431356195adbfdd25
extern int rpmsqEnable (int signum, void *handler);
int
rpmsqEnable (int signum, void *handler)
{
return 0;
}
void
my_rpmsqSetInterruptSafety(void *f, int on) {
void (*rpmsqSetInterruptSafety)(int);
rpmsqSetInterruptSafety = (void (*)(int))f;
rpmsqSetInterruptSafety(on);
}
*/
import "C"
// Constants in sync with /usr/include/rpm/rpmtag.h
const (
RPMTAG_NAME = 1000
RPMTAG_VERSION = 1001
RPMTAG_RELEASE = 1002
RPMTAG_SUMMARY = 1004
RPMTAG_LICENSE = 1014
RPMTAG_URL = 1020
RPMTAG_ARCH = 1022
RPMTAG_SIZE = 1009
RPMTAG_INSTALLTIME = 1008
)
var openedLibrpm *librpm
// closeDataset performs cleanup when the dataset is closed.
func closeDataset() error {
if openedLibrpm != nil {
err := openedLibrpm.close()
openedLibrpm = nil
return err
}
return nil
}
type librpm struct {
handle *dlopen.LibHandle
rpmtsCreate unsafe.Pointer
rpmReadConfigFiles unsafe.Pointer
rpmtsInitIterator unsafe.Pointer
rpmdbNextIterator unsafe.Pointer
headerLink unsafe.Pointer
headerGetString unsafe.Pointer
headerGetNumber unsafe.Pointer
headerFree unsafe.Pointer
rpmdbFreeIterator unsafe.Pointer
rpmtsFree unsafe.Pointer
rpmsqSetInterruptSafety unsafe.Pointer
rpmFreeRpmrc unsafe.Pointer
rpmFreeMacros unsafe.Pointer
}
func (lib *librpm) close() error {
if lib.handle != nil {
return lib.handle.Close()
}
return nil
}
func openLibrpm() (*librpm, error) {
var librpmNames = []string{
"librpm.so", // with rpm-devel installed
"librpm.so.9", // Fedora 31/32
"librpm.so.8", // Fedora 29/30
"librpm.so.3", // CentOS 7
"librpm.so.1", // CentOS 6
// Following for completeness, but not explicitly tested
"librpm.so.10",
"librpm.so.7",
"librpm.so.6",
"librpm.so.5",
"librpm.so.4",
"librpm.so.2",
}
var librpm librpm
var err error
librpm.handle, err = dlopen.GetHandle(librpmNames)
if err != nil {
return nil, err
}
librpm.rpmtsCreate, err = librpm.handle.GetSymbolPointer("rpmtsCreate")
if err != nil {
return nil, err
}
librpm.rpmReadConfigFiles, err = librpm.handle.GetSymbolPointer("rpmReadConfigFiles")
if err != nil {
return nil, err
}
librpm.rpmtsInitIterator, err = librpm.handle.GetSymbolPointer("rpmtsInitIterator")
if err != nil {
return nil, err
}
librpm.rpmdbNextIterator, err = librpm.handle.GetSymbolPointer("rpmdbNextIterator")
if err != nil {
return nil, err
}
librpm.headerLink, err = librpm.handle.GetSymbolPointer("headerLink")
if err != nil {
return nil, err
}
librpm.headerGetString, err = librpm.handle.GetSymbolPointer("headerGetString")
if err != nil {
return nil, err
}
librpm.headerGetNumber, err = librpm.handle.GetSymbolPointer("headerGetNumber")
if err != nil {
return nil, err
}
librpm.headerFree, err = librpm.handle.GetSymbolPointer("headerFree")
if err != nil {
return nil, err
}
librpm.rpmdbFreeIterator, err = librpm.handle.GetSymbolPointer("rpmdbFreeIterator")
if err != nil {
return nil, err
}
librpm.rpmtsFree, err = librpm.handle.GetSymbolPointer("rpmtsFree")
if err != nil {
return nil, err
}
librpm.rpmFreeRpmrc, err = librpm.handle.GetSymbolPointer("rpmFreeRpmrc")
if err != nil {
return nil, err
}
// Only available in librpm>=4.13.0
librpm.rpmsqSetInterruptSafety, err = librpm.handle.GetSymbolPointer("rpmsqSetInterruptSafety")
// no error check
// Only available in librpm>=4.6.0
librpm.rpmFreeMacros, err = librpm.handle.GetSymbolPointer("rpmFreeMacros")
// no error check
return &librpm, nil
}
func listRPMPackages() ([]*Package, error) {
// In newer versions, librpm is using the thread-local variable
// `disableInterruptSafety` in rpmio/rpmsq.c to disable signal
// traps. To make sure our settings remain in effect throughout
// our function calls we have to lock the OS thread here, since
// Golang can otherwise use any thread it likes for each C.* call.
runtime.LockOSThread()
defer runtime.UnlockOSThread()
if openedLibrpm == nil {
var err error
openedLibrpm, err = openLibrpm()
if err != nil {
return nil, err
}
}
if openedLibrpm.rpmsqSetInterruptSafety != nil {
C.my_rpmsqSetInterruptSafety(openedLibrpm.rpmsqSetInterruptSafety, 0)
}
rpmts := C.my_rpmtsCreate(openedLibrpm.rpmtsCreate)
if rpmts == nil {
return nil, fmt.Errorf("Failed to get rpmts")
}
defer C.my_rpmtsFree(openedLibrpm.rpmtsFree, rpmts)
res := C.my_rpmReadConfigFiles(openedLibrpm.rpmReadConfigFiles)
if int(res) != 0 {
return nil, fmt.Errorf("Error: %d", int(res))
}
defer C.my_rpmFreeRpmrc(openedLibrpm.rpmFreeRpmrc)
if openedLibrpm.rpmFreeMacros != nil {
defer C.my_rpmFreeMacros(openedLibrpm.rpmFreeMacros)
}
mi := C.my_rpmtsInitIterator(openedLibrpm.rpmtsInitIterator, rpmts)
if mi == nil {
return nil, fmt.Errorf("Failed to get match iterator")
}
defer C.my_rpmdbFreeIterator(openedLibrpm.rpmdbFreeIterator, mi)
var packages []*Package
for header := C.my_rpmdbNextIterator(openedLibrpm.rpmdbNextIterator, mi); header != nil; header = C.my_rpmdbNextIterator(openedLibrpm.rpmdbNextIterator, mi) {
pkg, err := packageFromHeader(header, openedLibrpm)
if err != nil {
return nil, err
}
packages = append(packages, pkg)
}
return packages, nil
}
func packageFromHeader(header C.Header, openedLibrpm *librpm) (*Package, error) {
header = C.my_headerLink(openedLibrpm.headerLink, header)
if header == nil {
return nil, fmt.Errorf("Error calling headerLink")
}
defer C.my_headerFree(openedLibrpm.headerFree, header)
pkg := Package{
Type: "rpm",
}
name := C.my_headerGetString(openedLibrpm.headerGetString, header, RPMTAG_NAME)
if name != nil {
pkg.Name = C.GoString(name)
} else {
return nil, errors.New("Failed to get package name")
}
version := C.my_headerGetString(openedLibrpm.headerGetString, header, RPMTAG_VERSION)
if version != nil {
pkg.Version = C.GoString(version)
} else {
pkg.error = errors.New("failed to get package version")
}
pkg.Release = C.GoString(C.my_headerGetString(openedLibrpm.headerGetString, header, RPMTAG_RELEASE))
pkg.License = C.GoString(C.my_headerGetString(openedLibrpm.headerGetString, header, RPMTAG_LICENSE))
pkg.Arch = C.GoString(C.my_headerGetString(openedLibrpm.headerGetString, header, RPMTAG_ARCH))
pkg.URL = C.GoString(C.my_headerGetString(openedLibrpm.headerGetString, header, RPMTAG_URL))
pkg.Summary = C.GoString(C.my_headerGetString(openedLibrpm.headerGetString, header, RPMTAG_SUMMARY))
pkg.Size = uint64(C.my_headerGetNumber(openedLibrpm.headerGetNumber, header, RPMTAG_SIZE))
installTime := C.my_headerGetNumber(openedLibrpm.headerGetNumber, header, RPMTAG_INSTALLTIME)
if installTime != 0 {
pkg.InstallTime = time.Unix(int64(installTime), 0)
}
return &pkg, nil
}