Glibc struct sigaction and sigaction

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

···

--
---

@iachievedit

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@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
Simple signal handling in Swift · GitHub

--
---
iAchieved.it – Thoughts from a Technology Leader
@iachievedit

--
---
http://dev.iachieved.it/iachievedit/
@iachievedit