-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
100 lines (94 loc) · 2.33 KB
/
main.go
File metadata and controls
100 lines (94 loc) · 2.33 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
92
93
94
95
96
97
98
99
100
package main
import (
"docker-systemd/common"
"docker-systemd/journalctl"
"docker-systemd/systemctl"
"docker-systemd/systemd"
"fmt"
"log"
"os"
"path"
"path/filepath"
"strings"
"time"
_ "embed"
"github.com/bestmethod/inslice"
)
//go:embed VERSION
var version string
func main() {
_, name := path.Split(os.Args[0])
if os.Getpid() == 1 {
name = "init"
}
switch name {
case "journalctl":
journalctl.Main()
case "systemctl":
if len(os.Args) == 2 && os.Args[1] == "version" {
fmt.Println(strings.Trim(version, "\r\n\t "))
return
}
systemctl.Main(os.Args)
case "poweroff":
systemctl.Main([]string{os.Args[0], "poweroff"})
case "shutdown":
systemctl.Main([]string{os.Args[0], "poweroff"})
case "service":
args := os.Args
if len(args) > 2 {
prev := args[1]
args[1] = os.Args[2]
args[2] = prev
}
systemctl.Main(args)
default:
startTime := time.Now().Format(time.RFC3339)
log.Printf("INIT: Booting <%s>", startTime)
install()
os.WriteFile(common.GetBootFile(), []byte(startTime), 0644)
systemd.Main()
}
}
func install() {
me, err := os.Executable()
if err != nil {
log.Fatalf("Could not get excutable name of self: %s", err)
}
fp := filepath.SplitList(os.Getenv("PATH"))
suitablePaths := []string{"/usr/local/sbin", "/usr/local/bin", "/usr/bin", "/bin", "/usr/sbin", "/sbin"}
basePath := "/usr/sbin"
for _, item := range fp {
if inslice.HasString(suitablePaths, item) {
basePath = strings.TrimRight(item, "/")
break
}
}
for _, f := range []string{"/journalctl", "/systemctl", "/systemd", "/init", "/poweroff", "/shutdown", "/service"} {
f = basePath + f
if me == f {
continue
}
if nstat, err := os.Lstat(f); err != nil || nstat.Mode()&os.ModeSymlink == 0 {
if err == nil {
log.Printf("%v", nstat.Mode())
}
log.Printf("File not found, or not a symlink; Linking %s => %s", me, f)
os.Rename(f, f+".old")
err = os.Symlink(me, f)
if err != nil {
log.Fatalf("Could not link %s to %s: %s", me, f, err)
}
} else if nstat.Mode()&os.ModeSymlink != 0 {
dest, err := os.Readlink(f)
if err != nil || dest != me {
log.Printf("Can not read symlink or symlink points to the wrong file; Linking %s => %s", me, f)
os.Rename(f, f+".old")
err = os.Symlink(me, f)
if err != nil {
log.Fatalf("Could not link %s to %s: %s", me, f, err)
}
}
}
}
}