2023-01-27 21:37:20 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2020-09-11 15:25:23 +00:00
|
|
|
|
2020-09-11 15:09:51 +00:00
|
|
|
package pidowner
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
2024-11-05 04:49:40 +00:00
|
|
|
"tailscale.com/util/lineiter"
|
2020-09-11 15:09:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func ownerOfPID(pid int) (userID string, err error) {
|
|
|
|
file := fmt.Sprintf("/proc/%d/status", pid)
|
2024-11-05 04:49:40 +00:00
|
|
|
for lr := range lineiter.File(file) {
|
|
|
|
line, err := lr.Value()
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return "", ErrProcessNotFound
|
|
|
|
}
|
|
|
|
return "", err
|
|
|
|
}
|
2020-09-11 15:09:51 +00:00
|
|
|
if len(line) < 4 || string(line[:4]) != "Uid:" {
|
2024-11-05 04:49:40 +00:00
|
|
|
continue
|
2020-09-11 15:09:51 +00:00
|
|
|
}
|
|
|
|
f := strings.Fields(string(line))
|
|
|
|
if len(f) >= 2 {
|
|
|
|
userID = f[1] // real userid
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if userID == "" {
|
|
|
|
return "", fmt.Errorf("missing Uid line in %s", file)
|
|
|
|
}
|
|
|
|
return userID, nil
|
|
|
|
}
|