Skip to content

Add signal handlers #2

@eonist

Description

@eonist
import Foundation

// Define a function type for signal handlers
typealias SignalHandler = (Int32) -> Void

// Global signal handlers
let sigAbortHandler = SignalHandler { _ in
    print("Received SIGABRT")
    // Add more detailed logging or crash reporting here
}
let sigIllHandler = SignalHandler { _ in
    print("Received SIGILL")
    // Add more detailed logging or crash reporting here
}
let sigSegvHandler = SignalHandler { _ in
    print("Received SIGSEGV")
    // Add more detailed logging or crash reporting here
}
let sigFpeHandler = SignalHandler { _ in
    print("Received SIGFPE")
    // Add more detailed logging or crash reporting here
}
let sigBusHandler = SignalHandler { _ in
    print("Received SIGBUS")
    // Add more detailed logging or crash reporting here
}
let sigPipeHandler = SignalHandler { _ in
    print("Received SIGPIPE")
    // Add more detailed logging or crash reporting here
}

// Function to install signal handlers using sigaction
func installSignalHandlers() {
    let sa: sigaction = {
        var sa = sigaction()
        sa.sa_handler = { sig in
            print("Caught signal \(sig)")
            // Call the corresponding handler function
            sigHandlers[sig]?()
        }
        return sa
    }()
    
    let sigHandlers = [
        SIGABRT: sigAbortHandler,
        SIGILL: sigIllHandler,
        SIGSEGV: sigSegvHandler,
        SIGFPE: sigFpeHandler,
        SIGBUS: sigBusHandler,
        SIGPIPE: sigPipeHandler
    ]
    
    for (sig, handler) in sigHandlers {
        sigaction(sig, &sa, nil)
    }
}

// Function to uninstall signal handlers
func uninstallSignalHandlers() {
    let sa = sigaction()
    for (sig, _) in sigHandlers {
        sigaction(sig, &sa, nil)
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions