<div dir="ltr">Using signal rather than sigaction does work:<div><br></div><div><div>typealias SignalHandler = __sighandler_t</div><div><br></div><div>func trap(signum:Signal, action:SignalHandler) {</div><div> signal(signum.rawValue, action)</div><div>}</div></div><div><br></div><div>Not ideal but serviceable. I'd still be curious to understand whether sigaction can be used.</div><div>Joe</div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Sun, Jan 10, 2016 at 11:01 PM, Joseph Bell <span dir="ltr"><<a href="mailto:joe@iachieved.it" target="_blank">joe@iachieved.it</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><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" target="_blank">https://gist.github.com/sharplet/d640eea5b6c99605ac79</a> </div><span class="HOEnZb"><font color="#888888"><div><br></div>-- <br><div><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>
</font></span></div></div>
</blockquote></div><br><br clear="all"><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>