<div dir="ltr">Not sure if anyone has ran into this use case before; thought I'd ask.<div><br></div><div>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).</div><div><br></div><div>Example:</div><div><br></div><div><div>import Glibc</div><div><br></div><div>enum Signal: Int32 {</div><div> case HUP = 1</div><div> case INT = 2</div><div> case QUIT = 3</div><div> case ABRT = 6</div><div> case KILL = 9</div><div> case ALRM = 14</div><div> case TERM = 15</div><div>}</div><div><br></div><div>typealias SignalHandler = (Int) -> Void</div><div><br></div><div>func trap(signal:Signal, action:SignalHandler) {</div><div> var act:sigaction = sigaction() // would expect this to be struct</div><div> act.sa_handler = &action // but woe, it is not</div><div> sigaction(signal.rawValue, &act, nil)</div><div>}</div><div><br></div><div>trap(.INT) { signal in</div><div> print("Intercepted signal \(signal)")</div><div>}</div><div><br></div><div>print("going to sleep...")</div><div>sigsuspend(nil)</div><div><br></div><div>This code gives:</div><div><br></div><div><div>sighandle.swift:17:3: error: value of type 'sigaction' has no member 'sa_handler'</div><div> act.sa_handler = &action // but woe, it is not</div></div><div><br></div><div>Any ideas of working around this or alternative approach to trapping signals on Linux with Swift?</div><div><br></div><div>Joe</div><div><br></div><div>Credit for original to Adam Sharp's gist at <a href="https://gist.github.com/sharplet/d640eea5b6c99605ac79">https://gist.github.com/sharplet/d640eea5b6c99605ac79</a> </div><div><br></div>-- <br><div class="gmail_signature"><div dir="ltr">---<div><a href="http://dev.iachieved.it/iachievedit/" target="_blank">http://dev.iachieved.it/iachievedit/</a></div><div>@iachievedit</div></div></div>
</div></div>