-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcessPolicyService.cs
More file actions
146 lines (128 loc) · 3.81 KB
/
Copy pathProcessPolicyService.cs
File metadata and controls
146 lines (128 loc) · 3.81 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
using System.Diagnostics;
namespace CodexProcessKeeper;
internal sealed record ProcessPolicyResult(
int ProcessId,
string ProcessName,
string ProcessPath,
string Priority,
bool EfficiencyModeOff,
bool Success,
int ErrorCode,
string Message);
internal sealed record ProcessPolicySnapshot(
DateTime CheckedAt,
IReadOnlyList<ProcessPolicyResult> Processes,
string? ServiceError);
internal sealed class ProcessPolicyService : IDisposable
{
private static readonly HashSet<string> TargetProcessNames = new(
StringComparer.OrdinalIgnoreCase)
{
"ChatGPT",
"Codex",
"codex",
"codex-code-mode-host",
"codex-command-runner",
"codex-windows-sandbox-setup"
};
private readonly TimeSpan scanInterval;
private readonly System.Threading.Timer scanTimer;
private int scanRunning;
private bool disposed;
internal ProcessPolicyService(TimeSpan scanInterval)
{
this.scanInterval = scanInterval;
scanTimer = new System.Threading.Timer(ScanProcesses);
}
internal event EventHandler<ProcessPolicySnapshot>? SnapshotUpdated;
internal void Start()
{
scanTimer.Change(TimeSpan.Zero, scanInterval);
}
internal void ApplyNow()
{
ThreadPool.QueueUserWorkItem(ScanProcesses);
}
private void ScanProcesses(object? state)
{
if (disposed || Interlocked.Exchange(ref scanRunning, 1) != 0)
{
return;
}
try
{
List<ProcessPolicyResult> results = [];
foreach (Process process in Process.GetProcesses())
{
using (process)
{
string processName;
try
{
processName = process.ProcessName;
}
catch
{
continue;
}
if (!TargetProcessNames.Contains(processName))
{
continue;
}
NativePolicyResult nativeResult = NativeProcessPolicy.Apply(process.Id);
if (nativeResult.ErrorCode is 6 or 87)
{
continue;
}
results.Add(new ProcessPolicyResult(
process.Id,
processName,
TryGetProcessPath(process),
nativeResult.PriorityLabel,
nativeResult.EfficiencyModeOff,
nativeResult.Success,
nativeResult.ErrorCode,
nativeResult.Message));
}
}
ProcessPolicySnapshot snapshot = new(
DateTime.Now,
results
.OrderBy(result => result.ProcessName, StringComparer.OrdinalIgnoreCase)
.ThenBy(result => result.ProcessId)
.ToArray(),
null);
SnapshotUpdated?.Invoke(this, snapshot);
}
catch (Exception exception)
{
SnapshotUpdated?.Invoke(
this,
new ProcessPolicySnapshot(DateTime.Now, [], exception.Message));
}
finally
{
Interlocked.Exchange(ref scanRunning, 0);
}
}
private static string TryGetProcessPath(Process process)
{
try
{
return process.MainModule?.FileName ?? string.Empty;
}
catch
{
return string.Empty;
}
}
public void Dispose()
{
if (disposed)
{
return;
}
disposed = true;
scanTimer.Dispose();
}
}