Fix special process loading

This commit is contained in:
Daniel 2023-04-06 14:37:44 +02:00
parent 4802de61fa
commit 64b721dcc8
2 changed files with 26 additions and 32 deletions

View file

@ -170,19 +170,18 @@ func GetOrFindProcess(ctx context.Context, pid int) (*Process, error) {
return GetSystemProcess(ctx), nil
}
// Get pid and created time for identification.
// Get pid and creation time for identification.
pInfo, err := processInfo.NewProcessWithContext(ctx, int32(pid))
if err != nil {
return nil, err
}
createdTime, err := pInfo.CreateTimeWithContext(ctx)
createdAt, err := pInfo.CreateTimeWithContext(ctx)
if err != nil {
return nil, err
}
key := getProcessKey(int32(pid), createdAt)
key := getProcessKey(int32(pid), createdTime)
// Load process and make sure it is only loaded once.
p, err, _ := getProcessSingleInflight.Do(key, func() (interface{}, error) {
return loadProcess(ctx, key, pInfo)
})
@ -197,9 +196,7 @@ func GetOrFindProcess(ctx context.Context, pid int) (*Process, error) {
}
func loadProcess(ctx context.Context, key string, pInfo *processInfo.Process) (*Process, error) {
// Get created time of process. The value should be cached.
createdAt, _ := pInfo.CreateTimeWithContext(ctx)
// Check if we already have the process.
process, ok := GetProcessFromStorage(key)
if ok {
return process, nil
@ -208,13 +205,13 @@ func loadProcess(ctx context.Context, key string, pInfo *processInfo.Process) (*
// Create new a process object.
process = &Process{
Pid: int(pInfo.Pid),
CreatedAt: createdAt,
FirstSeen: time.Now().Unix(),
processKey: key,
}
// Get process information from the system.
pInfo, err := processInfo.NewProcessWithContext(ctx, pInfo.Pid)
// Get creation time of process. (The value should be cached by the library.)
var err error
process.CreatedAt, err = pInfo.CreateTimeWithContext(ctx)
if err != nil {
return nil, err
}

View file

@ -38,35 +38,32 @@ func init() {
var (
// unidentifiedProcess is used for non-attributed outgoing connections.
unidentifiedProcess = &Process{
UserID: UnidentifiedProcessID,
UserName: "Unknown",
Pid: UnidentifiedProcessID,
CreatedAt: 1,
ParentPid: UnidentifiedProcessID,
ParentCreatedAt: 1,
Name: profile.UnidentifiedProfileName,
UserID: UnidentifiedProcessID,
UserName: "Unknown",
Pid: UnidentifiedProcessID,
ParentPid: UnidentifiedProcessID,
Name: profile.UnidentifiedProfileName,
processKey: getProcessKey(UnidentifiedProcessID, 0),
}
// unsolicitedProcess is used for non-attributed incoming connections.
unsolicitedProcess = &Process{
UserID: UnsolicitedProcessID,
UserName: "Unknown",
Pid: UnsolicitedProcessID,
CreatedAt: 1,
ParentPid: UnsolicitedProcessID,
ParentCreatedAt: 1,
Name: profile.UnsolicitedProfileName,
UserID: UnsolicitedProcessID,
UserName: "Unknown",
Pid: UnsolicitedProcessID,
ParentPid: UnsolicitedProcessID,
Name: profile.UnsolicitedProfileName,
processKey: getProcessKey(UnsolicitedProcessID, 0),
}
// systemProcess is used to represent the Kernel.
systemProcess = &Process{
UserID: SystemProcessID,
UserName: "Kernel",
Pid: SystemProcessID,
CreatedAt: 1,
ParentPid: SystemProcessID,
ParentCreatedAt: 1,
Name: profile.SystemProfileName,
UserID: SystemProcessID,
UserName: "Kernel",
Pid: SystemProcessID,
ParentPid: SystemProcessID,
Name: profile.SystemProfileName,
processKey: getProcessKey(SystemProcessID, 0),
}
getSpecialProcessSingleInflight singleflight.Group