Skip to content

Commit 3344f03

Browse files
committed
Move dlopen to internal package
Signed-off-by: Sascha Grunert <sgrunert@suse.com>
1 parent fd7a80b commit 3344f03

File tree

7 files changed

+147
-8
lines changed

7 files changed

+147
-8
lines changed

Gopkg.toml

-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
[[constraint]]
2-
name = "github.com/coreos/pkg"
3-
version = "4.0.0"
4-
51
[[constraint]]
62
name = "github.com/godbus/dbus"
73
version = "5.0"

internal/dlopen/dlopen.go

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// Copyright 2016 CoreOS, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
// Package dlopen provides some convenience functions to dlopen a library and
16+
// get its symbols.
17+
package dlopen
18+
19+
// #cgo LDFLAGS: -ldl
20+
// #include <stdlib.h>
21+
// #include <dlfcn.h>
22+
import "C"
23+
import (
24+
"errors"
25+
"fmt"
26+
"unsafe"
27+
)
28+
29+
var ErrSoNotFound = errors.New("unable to open a handle to the library")
30+
31+
// LibHandle represents an open handle to a library (.so)
32+
type LibHandle struct {
33+
Handle unsafe.Pointer
34+
Libname string
35+
}
36+
37+
// GetHandle tries to get a handle to a library (.so), attempting to access it
38+
// by the names specified in libs and returning the first that is successfully
39+
// opened. Callers are responsible for closing the handler. If no library can
40+
// be successfully opened, an error is returned.
41+
func GetHandle(libs []string) (*LibHandle, error) {
42+
for _, name := range libs {
43+
libname := C.CString(name)
44+
defer C.free(unsafe.Pointer(libname))
45+
handle := C.dlopen(libname, C.RTLD_LAZY)
46+
if handle != nil {
47+
h := &LibHandle{
48+
Handle: handle,
49+
Libname: name,
50+
}
51+
return h, nil
52+
}
53+
}
54+
return nil, ErrSoNotFound
55+
}
56+
57+
// GetSymbolPointer takes a symbol name and returns a pointer to the symbol.
58+
func (l *LibHandle) GetSymbolPointer(symbol string) (unsafe.Pointer, error) {
59+
sym := C.CString(symbol)
60+
defer C.free(unsafe.Pointer(sym))
61+
62+
C.dlerror()
63+
p := C.dlsym(l.Handle, sym)
64+
e := C.dlerror()
65+
if e != nil {
66+
return nil, fmt.Errorf("error resolving symbol %q: %v", symbol, errors.New(C.GoString(e)))
67+
}
68+
69+
return p, nil
70+
}
71+
72+
// Close closes a LibHandle.
73+
func (l *LibHandle) Close() error {
74+
C.dlerror()
75+
C.dlclose(l.Handle)
76+
e := C.dlerror()
77+
if e != nil {
78+
return fmt.Errorf("error closing %v: %v", l.Libname, errors.New(C.GoString(e)))
79+
}
80+
81+
return nil
82+
}

internal/dlopen/dlopen_test.go

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Copyright 2015 CoreOS, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
package dlopen
15+
16+
import (
17+
"fmt"
18+
"testing"
19+
)
20+
21+
func checkFailure(shouldSucceed bool, err error) (rErr error) {
22+
switch {
23+
case err != nil && shouldSucceed:
24+
rErr = fmt.Errorf("expected test to succeed, failed unexpectedly: %v", err)
25+
case err == nil && !shouldSucceed:
26+
rErr = fmt.Errorf("expected test to fail, succeeded unexpectedly")
27+
}
28+
29+
return
30+
}
31+
32+
func TestDlopen(t *testing.T) {
33+
tests := []struct {
34+
libs []string
35+
shouldSucceed bool
36+
}{
37+
{
38+
libs: []string{
39+
"libc.so.6",
40+
"libc.so",
41+
},
42+
shouldSucceed: true,
43+
},
44+
{
45+
libs: []string{
46+
"libstrange.so",
47+
},
48+
shouldSucceed: false,
49+
},
50+
}
51+
52+
for i, tt := range tests {
53+
expLen := 4
54+
len, err := strlen(tt.libs, "test")
55+
if checkFailure(tt.shouldSucceed, err) != nil {
56+
t.Errorf("case %d: %v", i, err)
57+
}
58+
59+
if tt.shouldSucceed && len != expLen {
60+
t.Errorf("case %d: expected length %d, got %d", i, expLen, len)
61+
}
62+
}
63+
}

scripts/jenkins/periodic-go-systemd-builder.sh

-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,5 @@ if [ ! -h gopath/src/github.com/coreos/go-systemd ]; then
1919
fi
2020
export GOPATH=${PWD}/gopath
2121
go get -u github.com/godbus/dbus
22-
go get github.com/coreos/pkg/dlopen
2322

2423
sudo -E ./test

sdjournal/functions.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
package sdjournal
1717

1818
import (
19-
"github.com/coreos/pkg/dlopen"
19+
"github.com/coreos/go-systemd/internal/dlopen"
2020
"sync"
2121
"unsafe"
2222
)

test

-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ if [ -z "$GOPATH" ]; then
2424
fi
2525
export GOPATH=${PWD}/gopath
2626
go get -u github.com/godbus/dbus
27-
go get -u github.com/coreos/pkg/dlopen
2827
fi
2928

3029
TESTABLE="activation daemon journal login1 unit"

util/util_cgo.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ import (
5858
"syscall"
5959
"unsafe"
6060

61-
"github.com/coreos/pkg/dlopen"
61+
"github.com/coreos/go-systemd/internal/dlopen"
6262
)
6363

6464
var libsystemdNames = []string{

0 commit comments

Comments
 (0)