Commit af3c514e authored by Levin Zimmermann's avatar Levin Zimmermann

.

parent b15ad994
......@@ -492,6 +492,7 @@ import (
stdlog "log"
"math"
"os"
"os/user"
"runtime"
"sort"
"strings"
......@@ -516,7 +517,6 @@ import (
"github.com/johncgriffin/overflow"
"github.com/hanwen/go-fuse/v2/fuse"
"github.com/hanwen/go-fuse/v2/fuse/nodefs"
"github.com/elastic/go-sysinfo"
"github.com/elastic/go-sysinfo/types"
"github.com/pkg/errors"
......@@ -748,11 +748,11 @@ type Stats struct {
type Client struct {
proc *os.Process // Holds process to send signals to
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 {
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{}) {
......@@ -2018,6 +2018,45 @@ func (wnode *WatchNode) Open(flags uint32, fctx *fuse.Context) (nodefs.File, fus
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) {
defer xerr.Contextf(&err, "/head/watch: open")
......@@ -2032,15 +2071,21 @@ func (wnode *WatchNode) open(flags uint32, fctx *fuse.Context) (_ nodefs.File, e
}
// fetch info of client process for logging
syslog_proc, err := sysinfo.Process(proc.Pid)
if err != nil {
return nil, err
}
proc_info, err := syslog_proc.Info()
// XXX Can't use because /proc/PID/exe is only available to user
// (setting capacities doesn't help).
// syslog_proc, err := sysinfo.Process(proc.Pid)
// if err != nil {
// return nil, err
// }
// proc_info, err := syslog_proc.Info()
// if err != nil {
// return nil, err
// }
proc_info, err := getProcessInfo(proc)
if err != nil {
return nil, err
}
user_info, err := syslog_proc.User()
user_info, err := getProcessUserInfo(proc)
if err != nil {
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