mirror of
https://github.com/safing/portmaster
synced 2025-09-01 18:19:12 +00:00
69 lines
1.6 KiB
Go
69 lines
1.6 KiB
Go
package process
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/safing/portbase/log"
|
|
"github.com/safing/portmaster/profile"
|
|
)
|
|
|
|
// GetProfile finds and assigns a profile set to the process.
|
|
func (p *Process) GetProfile(ctx context.Context) (changed bool, err error) {
|
|
// Update profile metadata outside of *Process lock.
|
|
defer p.UpdateProfileMetadata()
|
|
|
|
p.Lock()
|
|
defer p.Unlock()
|
|
|
|
// Check if profile is already loaded.
|
|
if p.profile != nil {
|
|
log.Tracer(ctx).Trace("process: profile already loaded")
|
|
return
|
|
}
|
|
|
|
// If not, continue with loading the profile.
|
|
log.Tracer(ctx).Trace("process: loading profile")
|
|
|
|
// Check if we need a special profile.
|
|
profileID := ""
|
|
switch p.Pid {
|
|
case UnidentifiedProcessID:
|
|
profileID = profile.UnidentifiedProfileID
|
|
case SystemProcessID:
|
|
profileID = profile.SystemProfileID
|
|
}
|
|
|
|
// Get the (linked) local profile.
|
|
localProfile, err := profile.GetProfile(profile.SourceLocal, profileID, p.Path)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
|
|
// Assign profile to process.
|
|
p.LocalProfileKey = localProfile.Key()
|
|
p.profile = localProfile.LayeredProfile()
|
|
|
|
return true, nil
|
|
}
|
|
|
|
func (p *Process) UpdateProfileMetadata() {
|
|
// Check if there is a profile to work with.
|
|
localProfile := p.Profile().LocalProfile()
|
|
if localProfile == nil {
|
|
return
|
|
}
|
|
|
|
// Update metadata of profile.
|
|
metadataUpdated := localProfile.UpdateMetadata(p.Name)
|
|
|
|
// Mark profile as used.
|
|
profileChanged := localProfile.MarkUsed()
|
|
|
|
// Save the profile if we changed something.
|
|
if metadataUpdated || profileChanged {
|
|
err := localProfile.Save()
|
|
if err != nil {
|
|
log.Warningf("process: failed to save profile %s: %s", localProfile.ScopedID(), err)
|
|
}
|
|
}
|
|
}
|