2018-11-30 03:15:08 +00:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2019-02-13 19:43:59 +00:00
|
|
|
"path/filepath"
|
2018-11-30 03:15:08 +00:00
|
|
|
|
|
|
|
psutil "github.com/shirou/gopsutil/process"
|
|
|
|
)
|
|
|
|
|
2019-02-13 19:43:59 +00:00
|
|
|
func FileProcessUsers(path string) (map[int32]*psutil.Process, error) {
|
|
|
|
fusers := make(map[int32]*psutil.Process)
|
2018-11-30 03:15:08 +00:00
|
|
|
|
|
|
|
processes, err := psutil.Processes()
|
|
|
|
if err != nil &&
|
|
|
|
err != os.ErrPermission {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-02-13 19:43:59 +00:00
|
|
|
// Eval symlinks
|
|
|
|
relPath, err := filepath.EvalSymlinks(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
//log.Debugf("checking against path: %s", relPath)
|
2018-11-30 03:15:08 +00:00
|
|
|
for _, p := range processes {
|
|
|
|
|
|
|
|
files, err := p.OpenFiles()
|
2018-12-04 17:06:30 +00:00
|
|
|
|
2019-02-26 10:23:00 +00:00
|
|
|
//TEST: use os.IsNotExist to test the path error
|
2019-02-20 17:39:45 +00:00
|
|
|
if err != nil && os.IsNotExist(err) {
|
2019-02-13 19:43:59 +00:00
|
|
|
continue
|
2018-11-30 03:15:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check if path in files
|
2019-02-13 19:43:59 +00:00
|
|
|
|
2018-11-30 03:15:08 +00:00
|
|
|
for _, f := range files {
|
2019-02-13 19:43:59 +00:00
|
|
|
//log.Debug(f)
|
|
|
|
if f.Path == relPath {
|
|
|
|
fusers[p.Pid] = p
|
2018-11-30 03:15:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return fusers, nil
|
|
|
|
}
|