Skip to content

Commit b62dc6f

Browse files
authored
Add CmdLine function to parse /proc/cmdline (#393)
Our company internal Prometheus exporter uses the procfs library and needs to read `/proc/cmdline`. Since `fs.proc.Path` cannot be accessed from outside, add `CmdLine` to procfs to read `/proc/cmdline`. Signed-off-by: Benjamin Drung <[email protected]>
1 parent 99f3c37 commit b62dc6f

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-0
lines changed

cmdline.go

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright 2021 The Prometheus Authors
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
package procfs
15+
16+
import (
17+
"strings"
18+
19+
"github.com/prometheus/procfs/internal/util"
20+
)
21+
22+
// CmdLine returns the command line of the kernel.
23+
func (fs FS) CmdLine() ([]string, error) {
24+
data, err := util.ReadFileNoStat(fs.proc.Path("cmdline"))
25+
if err != nil {
26+
return nil, err
27+
}
28+
29+
return strings.Fields(string(data)), nil
30+
}

cmdline_test.go

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright 2021 The Prometheus Authors
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
// +build linux
15+
16+
package procfs
17+
18+
import (
19+
"testing"
20+
21+
"github.com/google/go-cmp/cmp"
22+
)
23+
24+
func TestCmdline(t *testing.T) {
25+
fs, err := NewFS(procTestFixtures)
26+
if err != nil {
27+
t.Fatal(err)
28+
}
29+
30+
got, err := fs.CmdLine()
31+
if err != nil {
32+
t.Fatal(err)
33+
}
34+
35+
want := []string{
36+
"BOOT_IMAGE=/vmlinuz-5.11.0-22-generic",
37+
"root=UUID=456a0345-450d-4f7b-b7c9-43e3241d99ad",
38+
"ro",
39+
"quiet",
40+
"splash",
41+
"vt.handoff=7",
42+
}
43+
44+
if diff := cmp.Diff(want, got); diff != "" {
45+
t.Fatalf("unexpected CmdLine (-want +got):\n%s", diff)
46+
}
47+
}

fixtures.ttar

+5
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,11 @@ Node 0, zone DMA32 759 572 791 475 194 45 12 0
644644
Node 0, zone Normal 4381 1093 185 1530 567 102 4 0 0 0 0
645645
Mode: 644
646646
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
647+
Path: fixtures/proc/cmdline
648+
Lines: 1
649+
BOOT_IMAGE=/vmlinuz-5.11.0-22-generic root=UUID=456a0345-450d-4f7b-b7c9-43e3241d99ad ro quiet splash vt.handoff=7
650+
Mode: 444
651+
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
647652
Path: fixtures/proc/cpuinfo
648653
Lines: 216
649654
processor : 0

0 commit comments

Comments
 (0)