-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.swift
53 lines (44 loc) · 1.49 KB
/
main.swift
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
import Foundation
import Dispatch
guard CommandLine.arguments.count > 1 else {
fputs("Usage: \(CommandLine.arguments[0]) <event_stream_name> [program] [args...]\n", stderr)
exit(1)
}
// Add signal handling
signal(SIGINT) { _ in
fputs("\nReceived interrupt signal. Exiting...\n", stderr)
exit(0)
}
signal(SIGTERM) { _ in
fputs("\nReceived termination signal. Exiting...\n", stderr)
exit(0)
}
autoreleasepool {
let eventStreamName = CommandLine.arguments[1]
guard eventStreamName == "com.apple.iokit.matching" ||
eventStreamName == "com.apple.notifyd.matching" ||
eventStreamName == "me.mdawaffe.xpc.test" else {
fputs("Error: Event stream must be either 'com.apple.iokit.matching' or 'com.apple.notifyd.matching'\n", stderr)
exit(1)
}
let programArgs = Array(CommandLine.arguments.dropFirst(2))
let semaphore = DispatchSemaphore(value: 0)
xpc_set_event_stream_handler(eventStreamName, nil) { _ in
semaphore.signal()
}
// Wait for single event
semaphore.wait()
// Run program if arguments were provided
if !programArgs.isEmpty {
let process = Process()
process.executableURL = URL(fileURLWithPath: programArgs[0])
process.arguments = Array(programArgs.dropFirst())
do {
try process.run()
process.waitUntilExit()
} catch {
fputs("Error launching program: \(error)\n", stderr)
exit(1)
}
}
}