[swift-dev] Glibc struct sigaction and sigaction
Joseph Bell
joe at iachieved.it
Sun Jan 10 23:17:13 CST 2016
Using signal rather than sigaction does work:
typealias SignalHandler = __sighandler_t
func trap(signum:Signal, action:SignalHandler) {
signal(signum.rawValue, action)
}
Not ideal but serviceable. I'd still be curious to understand whether
sigaction can be used.
Joe
On Sun, Jan 10, 2016 at 11:01 PM, Joseph Bell <joe at iachieved.it> wrote:
> Not sure if anyone has ran into this use case before; thought I'd ask.
>
> On many Unices the sigaction function is used to trap receipt of a signal
> and run a signal handler action. I want to use this to trap SIGINT and I
> think Swift's presentation of Glibc is leaving the struct sigaction
> definition masked by the function definition (see man 2 sigaction).
>
> Example:
>
> import Glibc
>
> enum Signal: Int32 {
> case HUP = 1
> case INT = 2
> case QUIT = 3
> case ABRT = 6
> case KILL = 9
> case ALRM = 14
> case TERM = 15
> }
>
> typealias SignalHandler = (Int) -> Void
>
> func trap(signal:Signal, action:SignalHandler) {
> var act:sigaction = sigaction() // would expect this to be struct
> act.sa_handler = &action // but woe, it is not
> sigaction(signal.rawValue, &act, nil)
> }
>
> trap(.INT) { signal in
> print("Intercepted signal \(signal)")
> }
>
> print("going to sleep...")
> sigsuspend(nil)
>
> This code gives:
>
> sighandle.swift:17:3: error: value of type 'sigaction' has no member
> 'sa_handler'
> act.sa_handler = &action // but woe, it is not
>
> Any ideas of working around this or alternative approach to trapping
> signals on Linux with Swift?
>
> Joe
>
> Credit for original to Adam Sharp's gist at
> https://gist.github.com/sharplet/d640eea5b6c99605ac79
>
> --
> ---
> http://dev.iachieved.it/iachievedit/
> @iachievedit
>
--
---
http://dev.iachieved.it/iachievedit/
@iachievedit
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-dev/attachments/20160110/9e97f015/attachment.html>
More information about the swift-dev
mailing list