The signal handlers I register don't seem to be invoked when their associated signals are delivered to my process on iOS.
Is there something I specifically need to do on iOS to enable user handling of signals? Can't find any documentation on this.
For example, the following won't work, when ran from an iOS app, but will work when compiled and run on OS X.
#include <signal.h>
volatile int fooC = 0;
volatile int fooPosix = 0;
void posixHandler(int sig, siginfo_t *info, void *uap)
{
fooPosix = 1;
}
void cHandler(int sig)
{
fooC = 1;
}
int fn()
{
struct sigaction sigAction = {0};
sigAction.sa_sigaction = &posixHandler;
sigAction.sa_flags = SA_SIGINFO;
if(sigaction(SIGUSR1, &sigAction, NULL) == -1)
{
return -1;
}
if(signal(SIGUSR2, &cHandler) == SIG_ERR)
{
return -1;
}
raise(SIGUSR1);
raise(SIGUSR2);
while(fooC == 0 && fooPosix == 0) ;
return 0;
}
Questions about signal handling almost always raise the What are you really trying to do? response. I’ll come to that later, but first let’s look at your issue as stated.
You wrote:
For example, the following won't work, when ran from an iOS app, but will work when compiled and run on OS X.
That code works for me. I put your code into a new test project (based on the Single View Application template), tweaked it to log the result:
NSLog(@">wait");
while(fooC == 0 && fooPosix == 0) ;
NSLog(@"<wait %d %d", fooC, fooPosix);
called
fn
in
-viewDidLoad
, then ran it on my device (iPod touch (5th generation), iOS 9.3.2). It worked as you’d expected:
Jul 13 05:59:22 VPhone SigTest[574] <Warning>: >wait
Jul 13 05:59:22 VPhone SigTest[574] <Warning>: <wait 1 1
The only gotcha is that I have to run the app from the home screen, rather than Xcode, because Xcode’s debugger catches signals.
Notwithstanding all of the above, I really have to ask about your high-level goals here. While there are very rare cases where writing a signal handler is the right thing to do on iOS, they are few and far between. Why are you doing this?
Share and Enjoy
—
Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware
let myEmail = "eskimo" + "1" + "@apple.com"