|
| 1 | +import Foundation |
| 2 | + |
| 3 | +import SystemPackage |
| 4 | + |
| 5 | + |
| 6 | + |
| 7 | +public struct Sigaction : Equatable, RawRepresentable { |
| 8 | + |
| 9 | + public static let ignoreAction = Sigaction(handler: .ignoreHandler) |
| 10 | + public static let defaultAction = Sigaction(handler: .defaultHandler) |
| 11 | + |
| 12 | + /** |
| 13 | + Check if the given signal is ignored using `sigaction`. */ |
| 14 | + public static func isSignalIgnored(_ signal: Signal) throws -> Bool { |
| 15 | + return try Sigaction(signal: signal).handler == .ignoreHandler |
| 16 | + } |
| 17 | + |
| 18 | + /** |
| 19 | + Check if the given signal is handled with default action using `sigaction`. */ |
| 20 | + public static func isSignalDefaultAction(_ signal: Signal) throws -> Bool { |
| 21 | + return try Sigaction(signal: signal).handler == .defaultHandler |
| 22 | + } |
| 23 | + |
| 24 | + public var mask: Set<Signal> = [] |
| 25 | + public var flags: SigactionFlags = [] |
| 26 | + |
| 27 | + public var handler: SigactionHandler |
| 28 | + |
| 29 | + public init(handler: SigactionHandler) { |
| 30 | + self.mask = [] |
| 31 | + switch handler { |
| 32 | + case .posix: self.flags = [.siginfo] |
| 33 | + case .ignoreHandler, .defaultHandler, .ansiC: self.flags = [] |
| 34 | + } |
| 35 | + self.handler = handler |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + Create a `Sigaction` from a `sigaction`. |
| 40 | + |
| 41 | + If the handler of the sigaction is `SIG_IGN` or `SIG_DFL`, we check the |
| 42 | + `sa_flags` not to contains the `SA_SIGINFO` bit. If they do, we log an error, |
| 43 | + as this is invalid. */ |
| 44 | + public init(rawValue: sigaction) { |
| 45 | + self.mask = Signal.set(from: rawValue.sa_mask) |
| 46 | + self.flags = SigactionFlags(rawValue: rawValue.sa_flags) |
| 47 | + |
| 48 | + switch OpaquePointer(bitPattern: unsafeBitCast(rawValue.__sigaction_u.__sa_handler, to: Int.self)) { |
| 49 | + case OpaquePointer(bitPattern: unsafeBitCast(SIG_IGN, to: Int.self)): self.handler = .ignoreHandler |
| 50 | + case OpaquePointer(bitPattern: unsafeBitCast(SIG_DFL, to: Int.self)): self.handler = .defaultHandler |
| 51 | + default: |
| 52 | + if flags.contains(.siginfo) {self.handler = .posix(rawValue.__sigaction_u.__sa_sigaction)} |
| 53 | + else {self.handler = .ansiC(rawValue.__sigaction_u.__sa_handler)} |
| 54 | + } |
| 55 | + |
| 56 | + if !isValid { |
| 57 | + SignalHandlingConfig.logger?.warning("Initialized an invalid Sigaction.") |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + public init(signal: Signal) throws { |
| 62 | + var action = sigaction() |
| 63 | + guard sigaction(signal.rawValue, nil, &action) == 0 else { |
| 64 | + throw SignalHandlingError.nonDestructiveSystemError(Errno(rawValue: errno)) |
| 65 | + } |
| 66 | + self.init(rawValue: action) |
| 67 | + } |
| 68 | + |
| 69 | + public var rawValue: sigaction { |
| 70 | + if !isValid { |
| 71 | + SignalHandlingConfig.logger?.warning("Getting sigaction from an invalid Sigaction.") |
| 72 | + } |
| 73 | + |
| 74 | + var ret = sigaction() |
| 75 | + ret.sa_mask = Signal.sigset(from: mask) |
| 76 | + ret.sa_flags = flags.rawValue |
| 77 | + |
| 78 | + switch handler { |
| 79 | + case .ignoreHandler: ret.__sigaction_u.__sa_handler = SIG_IGN |
| 80 | + case .defaultHandler: ret.__sigaction_u.__sa_handler = SIG_DFL |
| 81 | + case .ansiC(let h): ret.__sigaction_u.__sa_handler = h |
| 82 | + case .posix(let h): ret.__sigaction_u.__sa_sigaction = h |
| 83 | + } |
| 84 | + |
| 85 | + return ret |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + Only one check: do the flags **not** contain `siginfo` if handler is either |
| 90 | + `.ignoreHandler` or `.defaultHandler`. */ |
| 91 | + public var isValid: Bool { |
| 92 | + return !flags.contains(.siginfo) || (handler != .ignoreHandler && handler != .defaultHandler) |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + Installs the sigaction and returns the old one if different. |
| 97 | + |
| 98 | + It is impossible for a sigaction handler to be `nil`. If the method returns |
| 99 | + `nil`, the previous handler was exactly the same as the one you installed. |
| 100 | + Note however the sigaction function is always called in this method. */ |
| 101 | + @discardableResult |
| 102 | + public func install(on signal: Signal, revertIfIgnored: Bool = true) throws -> Sigaction? { |
| 103 | + var oldCAction = sigaction() |
| 104 | + var newCAction = self.rawValue |
| 105 | + guard sigaction(signal.rawValue, &newCAction, &oldCAction) == 0 else { |
| 106 | + throw SignalHandlingError.nonDestructiveSystemError(Errno(rawValue: errno)) |
| 107 | + } |
| 108 | + let oldSigaction = Sigaction(rawValue: oldCAction) |
| 109 | + if revertIfIgnored && oldSigaction == .ignoreAction { |
| 110 | + guard sigaction(signal.rawValue, &newCAction, &oldCAction) == 0 else { |
| 111 | + throw SignalHandlingError.destructiveSystemError(Errno(rawValue: errno)) |
| 112 | + } |
| 113 | + } |
| 114 | + if oldSigaction != self {return oldSigaction} |
| 115 | + else {return nil} |
| 116 | + } |
| 117 | + |
| 118 | +} |
0 commit comments