-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathruntime.go
executable file
·93 lines (76 loc) · 2.51 KB
/
runtime.go
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
// Copyright © 2022-2023 Christian R. Vozar
// Licensed under the MIT License. All rights reserved.
package criprof
import (
"io/ioutil"
"os"
"strings"
)
// Detectable container runtimes.
const (
runtimeDocker = "docker" // Docker
runtimeRkt = "rkt" // CoreOS rkt
runtimeRunC = "runc" // Open Container Initiative runc
runtimeContainerD = "containerd" // containerd
runtimeLXC = "lxc" // LXC (Linux Containers)
runtimeLXD = "lxd" // LXD (containerd + LXC)
runtimeOpenVZ = "openvz" // OpenVZ
runtimeWASM = "wasm" // Web Assembly
runtimeUndetermined = "undetermined" // Undetermined
)
// getRuntime returns the name of the container runtime that is currently running.
func getRuntime() string {
// Check if the /.dockerinit file exists to detect a Docker runtime.
if _, err := os.Stat("/.dockerinit"); err == nil {
return runtimeDocker
}
// Check if the /.dockerenv file exists to detect a Docker runtime.
if _, err := os.Stat("/.dockerenv"); err == nil {
return runtimeDocker
}
// Check if /run/.containerenv file exists to detect a CRI-O or containerd
// runtime.
if _, err := os.Stat("/run/.containerenv"); err == nil {
return runtimeContainerD
}
// Check the cgroup to detect a Docker runtime.
cgroup, _ := ioutil.ReadFile("/proc/self/cgroup")
if strings.Contains(string(cgroup), "docker") {
return runtimeDocker
}
// Check if the AC_METADATA_URL environment variable is set to detect an rkt runtime.
if _, ok := EnvironmentVariables["AC_METADATA_URL"]; ok {
return runtimeRkt
}
// Check if the AC_APP_NAME environment variable is set to detect an rkt runtime.
if _, ok := EnvironmentVariables["AC_APP_NAME"]; ok {
return runtimeRkt
}
// Check if the /dev/lxd/sock file exists to detect an LXD runtime.
if _, err := os.Stat("/dev/lxd/sock"); err == nil {
return runtimeLXD
}
if isOpenVZ() {
return runtimeOpenVZ
}
if isWASM() {
return runtimeWASM
}
// If none of the above checks pass, return an undetermined runtime.
return runtimeUndetermined
}
// isOpenVZ returns true if the program is running inside an OpenVZ container.
func isOpenVZ() bool {
// Check if the /proc/vz directory exists.
if _, err := os.Stat("/proc/vz"); err == nil {
return true
}
return false
}
// isWasm returns true if the program is running inside a WebAssembly environment
func isWASM() bool {
if (os.Getenv("GOOS") == "js") && (os.Getenv("GOARCH") == "wasm") {
return true
}
return false
}