@@ -10,9 +10,12 @@ import (
1010 "errors"
1111 "fmt"
1212 "runtime"
13+ "strings"
1314 "time"
1415 "unsafe"
1516
17+ "debug/elf"
18+
1619 "github.com/coreos/pkg/dlopen"
1720)
1821
@@ -204,29 +207,57 @@ func (lib *librpm) close() error {
204207 return nil
205208}
206209
207- func openLibrpm () (* librpm , error ) {
208- var librpmNames = []string {
209- "librpm.so" , // with rpm-devel installed
210- "librpm.so.9" , // Fedora 31/32
211- "librpm.so.8" , // Fedora 29/30
212- "librpm.so.3" , // CentOS 7
213- "librpm.so.1" , // CentOS 6
214-
215- // Following for completeness, but not explicitly tested
216- "librpm.so.10" ,
217- "librpm.so.7" ,
218- "librpm.so.6" ,
219- "librpm.so.5" ,
220- "librpm.so.4" ,
221- "librpm.so.2" ,
210+ // getLibrpmNames determines the versions of librpm.so that are
211+ // installed on a system. rpm-devel rpm installs the librpm.so
212+ // symbolic link to the correct version of librpm, but that isn't a
213+ // required package. rpm will install librpm.so.X, where X is the
214+ // version number. getLibrpmNames looks at the elf header for the rpm
215+ // binary to determine what version of librpm.so it is linked against.
216+ func getLibrpmNames () []string {
217+ var rpmPaths = []string {
218+ "/usr/bin/rpm" ,
219+ "/bin/rpm" ,
220+ }
221+ var libNames = []string {
222+ "librpm.so" ,
222223 }
224+ var rpmElf * elf.File
225+ var err error
226+
227+ for _ , path := range rpmPaths {
228+ rpmElf , err = elf .Open (path )
229+ if err == nil {
230+ break
231+ }
232+ }
233+ if err != nil {
234+ return libNames
235+ }
236+
237+ impLibs , err := rpmElf .ImportedLibraries ()
238+ if err != nil {
239+ return libNames
240+ }
241+
242+ for _ , lib := range impLibs {
243+ if strings .Contains (lib , "librpm.so" ) {
244+ libNames = append (libNames , lib )
245+ }
246+ }
247+
248+ return libNames
249+ }
250+
251+ func openLibrpm () (* librpm , error ) {
223252
224253 var librpm librpm
225254 var err error
226255
256+ librpmNames := getLibrpmNames ()
257+
227258 librpm .handle , err = dlopen .GetHandle (librpmNames )
228259 if err != nil {
229- return nil , err
260+ return nil , fmt . Errorf ( "Couldn't open %v" , librpmNames )
230261 }
231262
232263 librpm .rpmtsCreate , err = librpm .handle .GetSymbolPointer ("rpmtsCreate" )
0 commit comments