This repository has been archived by the owner on Apr 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprocesses.py
74 lines (62 loc) · 2.33 KB
/
processes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
"""
Functions for obtaining information about running processes.
These use procfs so will only function on linux or BSD.
"""
import collections
import os
import re
NUMBER = re.compile('^[0-9]+$')
def process_ids():
return [
int(pid) for pid in os.listdir('/proc')
if NUMBER.match(pid)
]
def running_processes_by_name(names):
"""
Get the pids of all running processes with the supplied names.
Returns a dict mapping process name to list of pids
"""
names = set(names)
name_pid_mapping = collections.defaultdict(list)
for pid in process_ids():
comm_file = os.path.join('/proc', str(pid), 'comm')
try:
with open(comm_file, 'r') as f:
process_name = f.read().strip()
except (IOError, OSError) as exc:
# Processes may end while we are examining their files
if exc.errno != 2:
raise
if process_name in names:
name_pid_mapping[process_name].append(int(pid))
return name_pid_mapping
def check_for_open_files(filenames):
"""
Determines which files there are open handles to on the system.
filenames is an iterable for full file paths. The function returns
a pair of sets closed_handles, open_handles where closed_handles is
the names of all the files which do not have handles open to them, and
open_handles is the filenames where there is one or more handle open to them
"""
closed_handles = set(filenames)
open_handles = set()
for pid in process_ids():
fd_directory = os.path.join('/proc', str(pid), 'fd')
try:
for fd in os.listdir(fd_directory):
fd = os.path.join(fd_directory, fd)
try:
fd_target = os.readlink(fd)
except (IOError, OSError) as exc:
# Files may be closed while we are trying to read them
if exc.errno != 2:
raise
else:
if fd_target in closed_handles:
open_handles.add(fd_target)
closed_handles.remove(fd_target)
except (IOError, OSError) as exc:
# Processes may end while we are examining their files
if exc.errno != 2:
raise
return closed_handles, open_handles