Fix parent process key an refactoring
This commit is contained in:
@@ -9,5 +9,5 @@ import (
|
|||||||
var Module *modules.Module
|
var Module *modules.Module
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
Module = modules.Register("intel", nil, nil, nil, "geoip", "filterlists")
|
Module = modules.Register("intel", nil, nil, nil, "geoip", "filterlists", "customlists")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -59,7 +59,7 @@ func (p *Process) Save() {
|
|||||||
|
|
||||||
// save
|
// save
|
||||||
processesLock.Lock()
|
processesLock.Lock()
|
||||||
processes[p.key] = p
|
processes[p.processKey] = p
|
||||||
processesLock.Unlock()
|
processesLock.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -75,7 +75,7 @@ func (p *Process) Delete() {
|
|||||||
|
|
||||||
// delete from internal storage
|
// delete from internal storage
|
||||||
processesLock.Lock()
|
processesLock.Lock()
|
||||||
delete(processes, p.key)
|
delete(processes, p.processKey)
|
||||||
processesLock.Unlock()
|
processesLock.Unlock()
|
||||||
|
|
||||||
// propagate delete
|
// propagate delete
|
||||||
|
|||||||
@@ -46,7 +46,7 @@ type Process struct {
|
|||||||
Env map[string]string
|
Env map[string]string
|
||||||
|
|
||||||
// unique process identifier ("Pid-CreatedAt")
|
// unique process identifier ("Pid-CreatedAt")
|
||||||
key string
|
processKey string
|
||||||
|
|
||||||
// Profile attributes.
|
// Profile attributes.
|
||||||
// Once set, these don't change; safe for concurrent access.
|
// Once set, these don't change; safe for concurrent access.
|
||||||
@@ -160,6 +160,16 @@ func (p *Process) String() string {
|
|||||||
func GetOrFindProcess(ctx context.Context, pid int) (*Process, error) {
|
func GetOrFindProcess(ctx context.Context, pid int) (*Process, error) {
|
||||||
log.Tracer(ctx).Tracef("process: getting process for PID %d", pid)
|
log.Tracer(ctx).Tracef("process: getting process for PID %d", pid)
|
||||||
|
|
||||||
|
// Check for special processes
|
||||||
|
switch pid {
|
||||||
|
case UnidentifiedProcessID:
|
||||||
|
return GetUnidentifiedProcess(ctx), nil
|
||||||
|
case UnsolicitedProcessID:
|
||||||
|
return GetUnsolicitedProcess(ctx), nil
|
||||||
|
case SystemProcessID:
|
||||||
|
return GetSystemProcess(ctx), nil
|
||||||
|
}
|
||||||
|
|
||||||
// Get pid and created time for identification.
|
// Get pid and created time for identification.
|
||||||
pInfo, err := processInfo.NewProcessWithContext(ctx, int32(pid))
|
pInfo, err := processInfo.NewProcessWithContext(ctx, int32(pid))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -187,29 +197,20 @@ func GetOrFindProcess(ctx context.Context, pid int) (*Process, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func loadProcess(ctx context.Context, key string, pInfo *processInfo.Process) (*Process, error) {
|
func loadProcess(ctx context.Context, key string, pInfo *processInfo.Process) (*Process, error) {
|
||||||
switch pInfo.Pid {
|
|
||||||
case UnidentifiedProcessID:
|
|
||||||
return GetUnidentifiedProcess(ctx), nil
|
|
||||||
case UnsolicitedProcessID:
|
|
||||||
return GetUnsolicitedProcess(ctx), nil
|
|
||||||
case SystemProcessID:
|
|
||||||
return GetSystemProcess(ctx), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get created time of process. The value should be cached.
|
// Get created time of process. The value should be cached.
|
||||||
createdAt, _ := pInfo.CreateTimeWithContext(ctx)
|
createdAt, _ := pInfo.CreateTimeWithContext(ctx)
|
||||||
|
|
||||||
process, ok := GetProcessFromStorage(getProcessKey(pInfo.Pid, createdAt))
|
process, ok := GetProcessFromStorage(key)
|
||||||
if ok {
|
if ok {
|
||||||
return process, nil
|
return process, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create new a process object.
|
// Create new a process object.
|
||||||
process = &Process{
|
process = &Process{
|
||||||
Pid: int(pInfo.Pid),
|
Pid: int(pInfo.Pid),
|
||||||
CreatedAt: createdAt,
|
CreatedAt: createdAt,
|
||||||
FirstSeen: time.Now().Unix(),
|
FirstSeen: time.Now().Unix(),
|
||||||
key: key,
|
processKey: key,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get process information from the system.
|
// Get process information from the system.
|
||||||
@@ -246,7 +247,12 @@ func loadProcess(ctx context.Context, key string, pInfo *processInfo.Process) (*
|
|||||||
process.ParentPid = int(ppid)
|
process.ParentPid = int(ppid)
|
||||||
|
|
||||||
// Parent created at time
|
// Parent created at time
|
||||||
parentCreatedAt, err := pInfo.CreateTimeWithContext(ctx)
|
parentPInfo, err := processInfo.NewProcessWithContext(ctx, ppid)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
parentCreatedAt, err := parentPInfo.CreateTimeWithContext(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -88,9 +88,9 @@ func GetSystemProcess(ctx context.Context) *Process {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func getSpecialProcess(ctx context.Context, template *Process) *Process {
|
func getSpecialProcess(ctx context.Context, template *Process) *Process {
|
||||||
p, _, _ := getSpecialProcessSingleInflight.Do(template.key, func() (interface{}, error) {
|
p, _, _ := getSpecialProcessSingleInflight.Do(template.processKey, func() (interface{}, error) {
|
||||||
// Check if we have already loaded the special process.
|
// Check if we have already loaded the special process.
|
||||||
process, ok := GetProcessFromStorage(template.key)
|
process, ok := GetProcessFromStorage(template.processKey)
|
||||||
if ok {
|
if ok {
|
||||||
return process, nil
|
return process, nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user