18
18
// than linking against them.
19
19
package util
20
20
21
- // #include <stdlib.h>
22
- // #include <sys/types.h>
23
- // #include <unistd.h>
24
- //
25
- // int
26
- // my_sd_pid_get_owner_uid(void *f, pid_t pid, uid_t *uid)
27
- // {
28
- // int (*sd_pid_get_owner_uid)(pid_t, uid_t *);
29
- //
30
- // sd_pid_get_owner_uid = (int (*)(pid_t, uid_t *))f;
31
- // return sd_pid_get_owner_uid(pid, uid);
32
- // }
33
- //
34
- // int
35
- // my_sd_pid_get_unit(void *f, pid_t pid, char **unit)
36
- // {
37
- // int (*sd_pid_get_unit)(pid_t, char **);
38
- //
39
- // sd_pid_get_unit = (int (*)(pid_t, char **))f;
40
- // return sd_pid_get_unit(pid, unit);
41
- // }
42
- //
43
- // int
44
- // my_sd_pid_get_slice(void *f, pid_t pid, char **slice)
45
- // {
46
- // int (*sd_pid_get_slice)(pid_t, char **);
47
- //
48
- // sd_pid_get_slice = (int (*)(pid_t, char **))f;
49
- // return sd_pid_get_slice(pid, slice);
50
- // }
51
- //
52
- // int
53
- // am_session_leader()
54
- // {
55
- // return (getsid(0) == getpid());
56
- // }
57
- import "C"
58
21
import (
59
22
"fmt"
60
23
"io/ioutil"
61
24
"os"
62
25
"strings"
63
- "syscall"
64
- "unsafe"
65
-
66
- "github.com/coreos/pkg/dlopen"
67
26
)
68
27
69
- var libsystemdNames = []string {
70
- // systemd < 209
71
- "libsystemd-login.so.0" ,
72
- "libsystemd-login.so" ,
73
-
74
- // systemd >= 209 merged libsystemd-login into libsystemd proper
75
- "libsystemd.so.0" ,
76
- "libsystemd.so" ,
77
- }
78
-
79
28
// GetRunningSlice attempts to retrieve the name of the systemd slice in which
80
29
// the current process is running.
81
30
// This function is a wrapper around the libsystemd C library; if it cannot be
82
31
// opened, an error is returned.
83
- func GetRunningSlice () (slice string , err error ) {
84
- var h * dlopen.LibHandle
85
- h , err = dlopen .GetHandle (libsystemdNames )
86
- if err != nil {
87
- return
88
- }
89
- defer func () {
90
- if err1 := h .Close (); err1 != nil {
91
- err = err1
92
- }
93
- }()
94
-
95
- sd_pid_get_slice , err := h .GetSymbolPointer ("sd_pid_get_slice" )
96
- if err != nil {
97
- return
98
- }
99
-
100
- var s string
101
- sl := C .CString (s )
102
- defer C .free (unsafe .Pointer (sl ))
103
-
104
- ret := C .my_sd_pid_get_slice (sd_pid_get_slice , 0 , & sl )
105
- if ret < 0 {
106
- err = fmt .Errorf ("error calling sd_pid_get_slice: %v" , syscall .Errno (- ret ))
107
- return
108
- }
109
-
110
- return C .GoString (sl ), nil
32
+ func GetRunningSlice () (string , error ) {
33
+ return getRunningSlice ()
111
34
}
112
35
113
36
// RunningFromSystemService tries to detect whether the current process has
@@ -125,81 +48,17 @@ func GetRunningSlice() (slice string, err error) {
125
48
//
126
49
// This function is a wrapper around the libsystemd C library; if this is
127
50
// unable to successfully open a handle to the library for any reason (e.g. it
128
- // cannot be found), an errr will be returned
129
- func RunningFromSystemService () (ret bool , err error ) {
130
- var h * dlopen.LibHandle
131
- h , err = dlopen .GetHandle (libsystemdNames )
132
- if err != nil {
133
- return
134
- }
135
- defer func () {
136
- if err1 := h .Close (); err1 != nil {
137
- err = err1
138
- }
139
- }()
140
-
141
- sd_pid_get_owner_uid , err := h .GetSymbolPointer ("sd_pid_get_owner_uid" )
142
- if err != nil {
143
- return
144
- }
145
-
146
- var uid C.uid_t
147
- errno := C .my_sd_pid_get_owner_uid (sd_pid_get_owner_uid , 0 , & uid )
148
- serrno := syscall .Errno (- errno )
149
- // when we're running from a unit file, sd_pid_get_owner_uid returns
150
- // ENOENT (systemd <220) or ENXIO (systemd >=220)
151
- switch {
152
- case errno >= 0 :
153
- ret = false
154
- case serrno == syscall .ENOENT , serrno == syscall .ENXIO :
155
- // Since the implementation of sessions in systemd relies on
156
- // the `pam_systemd` module, using the sd_pid_get_owner_uid
157
- // heuristic alone can result in false positives if that module
158
- // (or PAM itself) is not present or properly configured on the
159
- // system. As such, we also check if we're the session leader,
160
- // which should be the case if we're invoked from a unit file,
161
- // but not if e.g. we're invoked from the command line from a
162
- // user's login session
163
- ret = C .am_session_leader () == 1
164
- default :
165
- err = fmt .Errorf ("error calling sd_pid_get_owner_uid: %v" , syscall .Errno (- errno ))
166
- }
167
- return
51
+ // cannot be found), an error will be returned.
52
+ func RunningFromSystemService () (bool , error ) {
53
+ return runningFromSystemService ()
168
54
}
169
55
170
56
// CurrentUnitName attempts to retrieve the name of the systemd system unit
171
57
// from which the calling process has been invoked. It wraps the systemd
172
58
// `sd_pid_get_unit` call, with the same caveat: for processes not part of a
173
59
// systemd system unit, this function will return an error.
174
- func CurrentUnitName () (unit string , err error ) {
175
- var h * dlopen.LibHandle
176
- h , err = dlopen .GetHandle (libsystemdNames )
177
- if err != nil {
178
- return
179
- }
180
- defer func () {
181
- if err1 := h .Close (); err1 != nil {
182
- err = err1
183
- }
184
- }()
185
-
186
- sd_pid_get_unit , err := h .GetSymbolPointer ("sd_pid_get_unit" )
187
- if err != nil {
188
- return
189
- }
190
-
191
- var s string
192
- u := C .CString (s )
193
- defer C .free (unsafe .Pointer (u ))
194
-
195
- ret := C .my_sd_pid_get_unit (sd_pid_get_unit , 0 , & u )
196
- if ret < 0 {
197
- err = fmt .Errorf ("error calling sd_pid_get_unit: %v" , syscall .Errno (- ret ))
198
- return
199
- }
200
-
201
- unit = C .GoString (u )
202
- return
60
+ func CurrentUnitName () (string , error ) {
61
+ return currentUnitName ()
203
62
}
204
63
205
64
// IsRunningSystemd checks whether the host was booted with systemd as its init
0 commit comments