Commit af3c514e authored by Levin Zimmermann's avatar Levin Zimmermann

.

parent b15ad994
...@@ -492,6 +492,7 @@ import ( ...@@ -492,6 +492,7 @@ import (
stdlog "log" stdlog "log"
"math" "math"
"os" "os"
"os/user"
"runtime" "runtime"
"sort" "sort"
"strings" "strings"
...@@ -516,7 +517,6 @@ import ( ...@@ -516,7 +517,6 @@ import (
"github.com/johncgriffin/overflow" "github.com/johncgriffin/overflow"
"github.com/hanwen/go-fuse/v2/fuse" "github.com/hanwen/go-fuse/v2/fuse"
"github.com/hanwen/go-fuse/v2/fuse/nodefs" "github.com/hanwen/go-fuse/v2/fuse/nodefs"
"github.com/elastic/go-sysinfo"
"github.com/elastic/go-sysinfo/types" "github.com/elastic/go-sysinfo/types"
"github.com/pkg/errors" "github.com/pkg/errors"
...@@ -748,11 +748,11 @@ type Stats struct { ...@@ -748,11 +748,11 @@ type Stats struct {
type Client struct { type Client struct {
proc *os.Process // Holds process to send signals to proc *os.Process // Holds process to send signals to
info types.ProcessInfo // Provides information about process info types.ProcessInfo // Provides information about process
user types.UserInfo // Provides information about the user that owns the client process user *user.User // Provides information about the user that owns the client process
} }
func (client Client) Format () string { func (client Client) Format () string {
return fmt.Sprintf("process(PID=%v;exe=%v;UID=%v)", client.info.PID, client.info.Exe, client.user.UID) return fmt.Sprintf("process(PID=%v;name=%v;user=%v)", client.info.PID, client.info.Name, client.user.Username)
} }
func debugClient(client Client, format string, argv ...interface{}) { func debugClient(client Client, format string, argv ...interface{}) {
...@@ -2018,6 +2018,45 @@ func (wnode *WatchNode) Open(flags uint32, fctx *fuse.Context) (nodefs.File, fus ...@@ -2018,6 +2018,45 @@ func (wnode *WatchNode) Open(flags uint32, fctx *fuse.Context) (nodefs.File, fus
return node, err2LogStatus(err) return node, err2LogStatus(err)
} }
func getProcessInfo(p *os.Process) (types.ProcessInfo, error) {
pid := p.Pid
bpath := fmt.Sprintf("/proc/%v/", pid)
comm, err := os.ReadFile(bpath + "comm")
if err != nil {
return types.ProcessInfo{}, err
}
pinfo := &types.ProcessInfo{
Name: strings.TrimSuffix(string(comm[:]), "\n"),
PID: pid,
}
return *pinfo, nil
}
func getProcessUserInfo(p *os.Process) (*user.User, error) {
pid := p.Pid
bpath := fmt.Sprintf("/proc/%v/", pid)
status, err := os.ReadFile(bpath + "status")
if err != nil {
return nil, err
}
lines := strings.SplitN(string(status[:]), "\n", -1)
var uid string
for _, line := range lines {
words := strings.SplitN(line, "\t", -1)
if words[0] == "Uid:" {
uid = words[1]
}
}
u, err := user.LookupId(uid)
if err != nil {
u = &user.User{Uid:uid,Username:"???" + uid}
}
return u, nil
}
func (wnode *WatchNode) open(flags uint32, fctx *fuse.Context) (_ nodefs.File, err error) { func (wnode *WatchNode) open(flags uint32, fctx *fuse.Context) (_ nodefs.File, err error) {
defer xerr.Contextf(&err, "/head/watch: open") defer xerr.Contextf(&err, "/head/watch: open")
...@@ -2032,15 +2071,21 @@ func (wnode *WatchNode) open(flags uint32, fctx *fuse.Context) (_ nodefs.File, e ...@@ -2032,15 +2071,21 @@ func (wnode *WatchNode) open(flags uint32, fctx *fuse.Context) (_ nodefs.File, e
} }
// fetch info of client process for logging // fetch info of client process for logging
syslog_proc, err := sysinfo.Process(proc.Pid) // XXX Can't use because /proc/PID/exe is only available to user
if err != nil { // (setting capacities doesn't help).
return nil, err // syslog_proc, err := sysinfo.Process(proc.Pid)
} // if err != nil {
proc_info, err := syslog_proc.Info() // return nil, err
// }
// proc_info, err := syslog_proc.Info()
// if err != nil {
// return nil, err
// }
proc_info, err := getProcessInfo(proc)
if err != nil { if err != nil {
return nil, err return nil, err
} }
user_info, err := syslog_proc.User() user_info, err := getProcessUserInfo(proc)
if err != nil { if err != nil {
return nil, err return nil, err
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment