Files
.bencher
.github
appc
atomicfile
chirp
client
clientupdate
cmd
control
derp
disco
docs
doctor
drive
envknob
feature
gokrazy
health
hostinfo
internal
ipn
jsondb
k8s-operator
kube
licenses
log
logpolicy
logtail
maths
metrics
net
omit
packages
paths
portlist
posture
prober
proxymap
release
safesocket
safeweb
scripts
sessionrecording
smallzstd
ssh
syncs
tailcfg
tempfork
tka
tool
tsconsensus
tsconst
tsd
tsnet
tstest
tstime
tsweb
types
util
cache
cibuild
clientmetric
cloudenv
cmpver
codegen
cstruct
ctxkey
deephash
dirwalk
dnsname
eventbus
execqueue
expvarx
goroutines
groupmember
hashx
httphdr
httpm
jsonutil
limiter
lineiter
lineread
linuxfw
lru
mak
multierr
must
nocasemaps
osdiag
osshare
osuser
pidowner
pidowner.go
pidowner_linux.go
pidowner_noimpl.go
pidowner_test.go
pidowner_windows.go
pool
precompress
progresstracking
quarantine
race
racebuild
rands
reload
ringbuffer
set
singleflight
slicesx
stringsx
syspolicy
sysresources
systemd
testenv
topk
truncate
usermetric
vizerror
winutil
zstdframe
version
wf
wgengine
words
.gitattributes
.gitignore
.golangci.yml
ALPINE.txt
AUTHORS
CODEOWNERS
CODE_OF_CONDUCT.md
Dockerfile
Dockerfile.base
LICENSE
Makefile
PATENTS
README.md
SECURITY.md
VERSION.txt
api.md
assert_ts_toolchain_match.go
build_dist.sh
build_docker.sh
flake.lock
flake.nix
go.mod
go.mod.sri
go.sum
go.toolchain.branch
go.toolchain.rev
gomod_test.go
header.txt
pkgdoc_test.go
pull-toolchain.sh
shell.nix
staticcheck.conf
update-flake.sh
version-embed.go
version_tailscale_test.go
version_test.go
tailscale/util/pidowner/pidowner_linux.go
Brad Fitzpatrick 01185e436f types/result, util/lineiter: add package for a result type, use it
This adds a new generic result type (motivated by golang/go#70084) to
try it out, and uses it in the new lineutil package (replacing the old
lineread package), changing that package to return iterators:
sometimes over []byte (when the input is all in memory), but sometimes
iterators over results of []byte, if errors might happen at runtime.

Updates 
Updates 

Change-Id: Iacdc1070e661b5fb163907b1e8b07ac7d51d3f83
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2024-11-05 10:27:52 -08:00

37 lines
700 B
Go

// Copyright (c) Tailscale Inc & AUTHORS
// SPDX-License-Identifier: BSD-3-Clause
package pidowner
import (
"fmt"
"os"
"strings"
"tailscale.com/util/lineiter"
)
func ownerOfPID(pid int) (userID string, err error) {
file := fmt.Sprintf("/proc/%d/status", pid)
for lr := range lineiter.File(file) {
line, err := lr.Value()
if err != nil {
if os.IsNotExist(err) {
return "", ErrProcessNotFound
}
return "", err
}
if len(line) < 4 || string(line[:4]) != "Uid:" {
continue
}
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
}