@@ -64,6 +64,21 @@ public enum SigactionDelayer_Unsig {
6464 }
6565 }
6666
67+ /**
68+ Change the original sigaction of the given signal if it was registered for an
69+ unsigaction. This is useful if you want to change the sigaction handler after
70+ having registered an unsigaction.
71+
72+ Returns the previous sigaction if there was an unsigaction registered for the
73+ given signal, `nil` otherwise. */
74+ public static func updateOriginalSigaction( for signal: Signal , to sigaction: Sigaction ) -> Sigaction ? {
75+ return signalProcessingQueue. sync {
76+ let previous = unsigactionedSignals [ signal] ? . originalSigaction
77+ unsigactionedSignals [ signal] ? . originalSigaction = sigaction
78+ return previous
79+ }
80+ }
81+
6782 /* ***************
6883 MARK: - Private
6984 *************** */
@@ -150,7 +165,7 @@ public enum SigactionDelayer_Unsig {
150165 private static func registerDelayedSigactionOnQueue( _ signal: Signal , handler: @escaping DelayedSigactionHandler ) throws -> DelayedSigaction {
151166 /* Whether the signal was retained before or not, we re-install the ignore
152167 * handler on the given signal. */
153- let oldSigaction = try Sigaction . ignoreAction. install ( on: signal, revertIfIgnored: false )
168+ let oldSigaction = try Sigaction . ignoreAction. install ( on: signal, revertIfIgnored: false , updateUnsigRegistrations : false )
154169
155170 let delayedSigaction = DelayedSigaction ( signal: signal)
156171
@@ -212,7 +227,7 @@ public enum SigactionDelayer_Unsig {
212227
213228 /* Now we have removed **all** unsigactions on the given signal. Let’s
214229 * restore the signal to the state before unsigactions. */
215- try unsigactionedSignal. originalSigaction. install ( on: id. signal, revertIfIgnored: false )
230+ try unsigactionedSignal. originalSigaction. install ( on: id. signal, revertIfIgnored: false , updateUnsigRegistrations : false )
216231 unsigactionedSignal. dispatchSource. cancel ( )
217232
218233 /* Finally, once the sigaction has been restored to the original value, we
@@ -315,7 +330,7 @@ public enum SigactionDelayer_Unsig {
315330 /* Install the original sigaction temporarily. In case of
316331 * failure we do not even send the signal to ourselves, it’d
317332 * be useless. */
318- let previousSigaction = try sigaction. install ( on: signal, revertIfIgnored: false )
333+ let previousSigaction = try sigaction. install ( on: signal, revertIfIgnored: false , updateUnsigRegistrations : false )
319334
320335 /* We send the signal to the thread directly. libdispatch uses
321336 * kqueue (on BSD, signalfd on Linux) and thus signals sent to
@@ -352,7 +367,7 @@ public enum SigactionDelayer_Unsig {
352367 * This is the only way I can think of. */
353368// sleep(3)
354369 if let previousSigaction = previousSigaction {
355- do { try previousSigaction. install ( on: signal, revertIfIgnored: false ) }
370+ do { try previousSigaction. install ( on: signal, revertIfIgnored: false , updateUnsigRegistrations : false ) }
356371 catch let error as SignalHandlingError {
357372 throw error. upgradeToDestructive ( )
358373 }
0 commit comments