Debian Codegraph @

sdk/resource/os_release_unix.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 //go:build aix || dragonfly || freebsd || linux || netbsd || openbsd || solaris || zos
5
6 package resource // import "go.opentelemetry.io/otel/sdk/resource"
7
8 import (
9 "bufio"
10 "fmt"
11 "io"
12 "os"
13 "strings"
14 )
15
16 // osRelease builds a string describing the operating system release based on the
17 // properties of the os-release file. If no os-release file is found, or if the
18 // required properties to build the release description string are missing, an empty
19 // string is returned instead. For more information about os-release files, see:
20 // https://www.freedesktop.org/software/systemd/man/os-release.html
21 func osRelease() string {
22 file, err := getOSReleaseFile()
23 if err != nil {
24 return ""
25 }
26
27 defer file.Close()
28
29 values := parseOSReleaseFile(file)
30
31 return buildOSRelease(values)
32 }
33
34 // getOSReleaseFile returns a *os.File pointing to one of the well-known os-release
35 // files, according to their order of preference. If no file can be opened, it
36 // returns an error.
37 func getOSReleaseFile() (*os.File, error) {
38 return getFirstAvailableFile([]string{"/etc/os-release", "/usr/lib/os-release"})
39 }
40
41 // parseOSReleaseFile process the file pointed by `file` as an os-release file and
42 // returns a map with the key-values contained in it. Empty lines or lines starting
43 // with a '#' character are ignored, as well as lines with the missing key=value
44 // separator. Values are unquoted and unescaped.
45 func parseOSReleaseFile(file io.Reader) map[string]string {
46 values := make(map[string]string)
47 scanner := bufio.NewScanner(file)
48
49 for scanner.Scan() {
50 line := scanner.Text()
51
52 if skip(line) {
53 continue
54 }
55
56 key, value, ok := parse(line)
57 if ok {
58 values[key] = value
59 }
60 }
61
62 return values
63 }
64
65 // skip reports whether the line is blank or starts with a '#' character, and
66 // therefore should be skipped from processing.
67 func skip(line string) bool {
68 line = strings.TrimSpace(line)
69
70 return line == "" || strings.HasPrefix(line, "#")
71 }
72
73 // parse attempts to split the provided line on the first '=' character, and then
74 // sanitize each side of the split before returning them as a key-value pair.
75 func parse(line string) (string, string, bool) {
76 k, v, found := strings.Cut(line, "=")
77
78 if !found || k == "" {
79 return "", "", false
80 }
81
82 key := strings.TrimSpace(k)
83 value := unescape(unquote(strings.TrimSpace(v)))
84
85 return key, value, true
86 }
87
88 // unquote checks whether the string `s` is quoted with double or single quotes
89 // and, if so, returns a version of the string without them. Otherwise it returns
90 // the provided string unchanged.
91 func unquote(s string) string {
92 if len(s) < 2 {
93 return s
94 }
95
96 if (s[0] == '"' || s[0] == '\'') && s[0] == s[len(s)-1] {
97 return s[1 : len(s)-1]
98 }
99
100 return s
101 }
102
103 // unescape removes the `\` prefix from some characters that are expected
104 // to have it added in front of them for escaping purposes.
105 func unescape(s string) string {
106 return strings.NewReplacer(
107 `\$`, `$`,
108 `\"`, `"`,
109 `\'`, `'`,
110 `\\`, `\`,
111 "\\`", "`",
112 ).Replace(s)
113 }
114
115 // buildOSRelease builds a string describing the OS release based on the properties
116 // available on the provided map. It favors a combination of the `NAME` and `VERSION`
117 // properties as first option (falling back to `VERSION_ID` if `VERSION` isn't
118 // found), and using `PRETTY_NAME` alone if some of the previous are not present. If
119 // none of these properties are found, it returns an empty string.
120 //
121 // The rationale behind not using `PRETTY_NAME` as first choice was that, for some
122 // Linux distributions, it doesn't include the same detail that can be found on the
123 // individual `NAME` and `VERSION` properties, and combining `PRETTY_NAME` with
124 // other properties can produce "pretty" redundant strings in some cases.
125 func buildOSRelease(values map[string]string) string {
126 var osRelease string
127
128 name := values["NAME"]
129 version := values["VERSION"]
130
131 if version == "" {
132 version = values["VERSION_ID"]
133 }
134
135 if name != "" && version != "" {
136 osRelease = fmt.Sprintf("%s %s", name, version)
137 } else {
138 osRelease = values["PRETTY_NAME"]
139 }
140
141 return osRelease
142 }