Skip to content

Commit

Permalink
Do not cache usernames
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardobranco777 committed Dec 30, 2024
1 parent 517d98b commit 29f582c
Showing 1 changed file with 18 additions and 31 deletions.
49 changes: 18 additions & 31 deletions restartable.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ type ProcPidFS struct {
pid int
}

var usernames map[int]string

var opts struct {
short int
user bool
Expand All @@ -59,15 +57,6 @@ var (
regexOpenRC = regexp.MustCompile(`\d+:name=openrc:/(.*)$`)
)

// Quote special characters
func quoteString(str string) string {
if len(str) > 0 {
str = strconv.Quote(str)
return str[1 : len(str)-1]
}
return ""
}

// OpenProc opens a /proc/<pid> directory and returns a ProcPidFS instance
func OpenProcPid(pid int) (*ProcPidFS, error) {
path := filepath.Join("/proc", strconv.Itoa(pid))
Expand Down Expand Up @@ -124,24 +113,6 @@ func (p *ProcPidFS) ReadLink(path string) (string, error) {
}
}

// Get username from UID
func getUser(uid int) string {
var username string

if _, ok := usernames[uid]; ok {
username = usernames[uid]
} else {
if info, err := user.LookupId(strconv.Itoa(uid)); err != nil {
username = "-"
} else {
username = info.Username
}
usernames[uid] = username
}

return username
}

// GetDeleted retrieves deleted file mappings for a process
func (p *ProcPidFS) GetDeleted() ([]string, error) {
maps, err := p.ReadFile("maps")
Expand Down Expand Up @@ -261,6 +232,24 @@ func getInfo(pid int) (*Info, error) {
}, nil
}

// Quote special characters
func quoteString(str string) string {
if len(str) > 0 {
str = strconv.Quote(str)
return str[1 : len(str)-1]
}
return ""
}

// Get username from UID
func getUser(uid int) string {
if info, err := user.LookupId(strconv.Itoa(uid)); err != nil {
return "-"
} else {
return info.Username
}
}

func init() {
log.SetPrefix("ERROR: ")
log.SetFlags(0)
Expand All @@ -283,8 +272,6 @@ func init() {
}

func main() {
usernames = make(map[int]string)

if os.Geteuid() != 0 {
fmt.Fprintln(os.Stderr, "WARN: Run this program as root")
}
Expand Down

0 comments on commit 29f582c

Please sign in to comment.