/
tmux_linux.go
57 lines (41 loc) · 1.51 KB
/
tmux_linux.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
//go:build linux
// +build linux
package tty
// ////////////////////////////////////////////////////////////////////////////////// //
// //
// Copyright (c) 2024 ESSENTIAL KAOS //
// Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0> //
// //
// ////////////////////////////////////////////////////////////////////////////////// //
import (
"errors"
"os"
"strconv"
"strings"
"github.com/essentialkaos/ek/v12/strutil"
)
// ////////////////////////////////////////////////////////////////////////////////// //
var procFS = "/proc"
// ////////////////////////////////////////////////////////////////////////////////// //
// isTmuxAncestor returns true if the current process is an ancestor of tmux
func isTmuxAncestor() (bool, error) {
pid := strconv.Itoa(os.Getppid())
for {
statFile := procFS + "/" + pid + "/stat"
statData, err := os.ReadFile(statFile)
if err != nil {
return false, errors.New("Can't check process tree for tmux server")
}
statString := string(statData)
processName := strutil.ReadField(statString, 1, false, ' ')
if strings.HasPrefix(processName, "(tmux:") {
return true, nil
}
parentPID := strutil.ReadField(statString, 3, false, ' ')
if parentPID == "1" || parentPID == "0" {
break
}
pid = parentPID
}
return false, nil
}