Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
151 lines (131 loc) · 4.61 KB
/
Copy pathProgram.cs
File metadata and controls
151 lines (131 loc) · 4.61 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
147
148
149
150
151
using Lsof.Cli;
using Lsof.Collectors;
using Lsof.Models;
using Lsof.Native;
using Lsof.Output;
using Lsof.Processes;
namespace Lsof;
internal static class Program
{
private const int InvalidArgumentsExitCode = 1;
private const int IncompleteCollectionExitCode = 2;
private const int CancelledExitCode = 130;
private static int Main(string[] args)
{
using CancellationTokenSource cancellation = new();
// Console.CancelKeyPress fires on Ctrl+C; setting eventArgs.Cancel keeps the process alive so
// the collectors can stop cooperatively through the token and return the cancelled exit code.
ConsoleCancelEventHandler cancelHandler = (_, eventArgs) =>
{
eventArgs.Cancel = true;
cancellation.Cancel();
};
Console.CancelKeyPress += cancelHandler;
try
{
return Run(args, cancellation.Token);
}
finally
{
Console.CancelKeyPress -= cancelHandler;
}
}
internal static int Run(string[] args, CancellationToken cancellationToken)
{
try
{
return RunCore(args, cancellationToken);
}
catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested)
{
Console.Error.WriteLine("Cancelled.");
return CancelledExitCode;
}
}
private static int RunCore(string[] args, CancellationToken cancellationToken)
{
if (!CommandLineOptions.TryParse(args, out CommandLineOptions options, out bool showHelp))
{
Console.Error.WriteLine("Invalid arguments. Use --help for usage.");
return InvalidArgumentsExitCode;
}
if (showHelp)
{
HelpPrinter.Print();
return 0;
}
ApplyDefaultCollectors(options);
CollectionReport report = new();
using ProcessCatalog catalog = ProcessCatalog.Create(report, cancellationToken);
ProcessFilter filter = new(options.ProcessFilters);
ProcessSelection selection = filter.ResolveSelection(catalog, report, cancellationToken);
List<LsofEntry> entries = new();
if (options.Network)
{
NetworkCollector.Collect(catalog, selection, options.PortFilters, entries, report, cancellationToken);
}
if (options.Files)
{
if (!selection.IsEmpty)
{
Console.Error.WriteLine("Enumerating open file handles...");
}
FileHandleCollector.Collect(
catalog,
selection,
entries,
report,
progress => Console.Error.WriteLine(
$"Enumerated {progress.ProcessedHandles:N0}/{progress.TotalHandles:N0} handles; found {progress.FilesFound:N0} disk files."),
cancellationToken);
}
if (options.Modules)
{
ModuleCollector.Collect(catalog, selection, entries, report, cancellationToken);
}
cancellationToken.ThrowIfCancellationRequested();
if (options.Unique)
{
entries = LsofEntryDeduplicator.RemoveDuplicateEntries(entries);
}
entries.Sort(LsofEntryComparer.Instance);
if (options.Json)
{
JsonOutputWriter.Write(entries);
WriteDiagnostics(report);
return GetExitCode(report);
}
if (entries.Count == 0)
{
Console.WriteLine("No matching open files, connections, or modules.");
WriteDiagnostics(report);
return GetExitCode(report);
}
TableOutputWriter.Write(entries);
LsofSummary summary = LsofSummary.FromEntries(entries);
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine($"{summary.Connections} connection(s), {summary.Files} file handle(s), {summary.Modules} module(s)");
Console.ResetColor();
WriteDiagnostics(report);
return GetExitCode(report);
}
internal static int GetExitCode(CollectionReport report)
{
return report.HasWarnings ? IncompleteCollectionExitCode : 0;
}
internal static void ApplyDefaultCollectors(CommandLineOptions options)
{
if (!options.Network && !options.Files && !options.Modules)
{
options.Network = true;
options.Files = true;
}
}
private static void WriteDiagnostics(CollectionReport report)
{
foreach (CollectionDiagnostic diagnostic in report.Diagnostics)
{
Console.Error.WriteLine(diagnostic.Message);
}
}
}