-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathservice_linux.go
More file actions
91 lines (78 loc) · 2.28 KB
/
service_linux.go
File metadata and controls
91 lines (78 loc) · 2.28 KB
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
// +build linux
package service
import (
"bytes"
"io/ioutil"
"path/filepath"
"strings"
logging "github.com/codemodify/systemkit-logging"
spec "github.com/codemodify/systemkit-service-spec"
)
var logTag = "LINUX-SERVICE"
func newServiceFromSERVICE(serviceSpec spec.SERVICE) Service {
switch getInitType() {
case spec.InitSystemV:
return newServiceFromSERVICE_SystemV(serviceSpec)
case spec.InitSystemd:
return newServiceFromSERVICE_SystemD(serviceSpec)
case spec.InitUpstart:
return newServiceFromSERVICE_Upstart(serviceSpec)
default:
}
return nil
}
func newServiceFromName(name string) (Service, error) {
switch getInitType() {
case spec.InitSystemV:
return newServiceFromName_SystemV(name)
case spec.InitSystemd:
return newServiceFromName_SystemD(name)
case spec.InitUpstart:
return newServiceFromName_Upstart(name)
default:
}
return nil, nil
}
func newServiceFromPlatformTemplate(name string, template string) (Service, error) {
switch getInitType() {
case spec.InitSystemV:
return newServiceFromPlatformTemplate_SystemV(name, template)
case spec.InitSystemd:
return newServiceFromPlatformTemplate_SystemD(name, template)
case spec.InitUpstart:
return newServiceFromPlatformTemplate_Upstart(name, template)
default:
}
return nil, nil
}
func getInitType() spec.InitType {
initBinary, err := ioutil.ReadFile("/proc/1/cmdline")
if err != nil {
logging.Errorf("%s: can't find underlying system service framework, error: %s", logTag, err.Error())
return spec.InitUknown
}
// trim any nul bytes, this is present with some kernels
init := string(bytes.TrimRight(initBinary, "\x00"))
if strings.Contains(init, "init [") {
return spec.InitSystemV
}
if strings.Contains(init, "systemd") {
return spec.InitSystemd
}
if strings.Contains(init, "init") {
// not so fast! you may think this is upstart, but it may be
// a symlink to systemd... yeah, debian does that... ( x )
var target string
if len(init) > 9 && init[0:10] == "/sbin/init" {
target, err = filepath.EvalSymlinks("/sbin/init")
} else {
target, err = filepath.EvalSymlinks(init)
}
if err == nil && strings.Contains(target, "systemd") {
return spec.InitSystemd
}
return spec.InitUpstart
}
// failed to detect init system, falling back to sysvinit
return spec.InitSystemV
}