si_pid and si_uid not set in sigaction handler

My understanding of sigaction is that my signal handler function should get a siginfo_t argument with the pid and uid of the process sending the signal.

#include <stdatomic.h>
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

static atomic_int gTermParentPid = -1;

static void SigTermHandler(int signalValue, siginfo_t *info, void *uap) {
  gTermParentPid = info->si_pid;
}

int main(int argc, const char * argv[]) {
  struct sigaction handlerAction;

  handlerAction.sa_sigaction = SigTermHandler;
  handlerAction.sa_flags = SA_SIGINFO;
  sigemptyset(&handlerAction.sa_mask);
  if (sigaction(SIGTERM, &handlerAction, NULL) != 0) {
    perror(NULL);
    abort();
  }

  printf("pid: %d\n", getpid());
  while (gTermParentPid == -1) {
    sleep(1);
  }
  printf("ParentPid: %d\n", gTermParentPid);
}

so when I run the above program and then send it a kill from another shell I would expect it to log the pid of the shell I sent the kill from. In all cases though I am getting a pid and uid of 0. Is this expected? This seems to work fine on linux.

I tested this with a variety of other signals (SIGALRM, SIGINT) with the same results.

Not sure if Exception Handling was the right tag, but it was the closest I could find.

Filed as FB11850436 as well.

Not sure if Exception Handling was the right tag

It’s not, but it’s hard to find a better one. I eventually settled on Kernel.

I wasn’t able to reproduce your result. Here’s what I did:

  1. I took the code you posted and put it into a new command-line tool target.

  2. I build it using Xcode 14.1.

  3. On macOS 13.0.1, I ran it in one Terminal window:

    % ./Test721195
    pid: 675
    
  4. In another Terminal window I sent it a SIGTERM:

    % kill -TERM `pgrep Test721195`
    
  5. Back in the first Terminal window, I saw this:

    % ./Test721195
    pid: 675
    ParentPid: 281
    

    where 281 is the pid of the second Terminal window’s shell.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Sigh... ok so it turns out I forgot to mention that I was running my sample code from within Xcode, so the debugger was in play. Apparently having lldb in the mix messes with the signal handling (not a huge surprise, but still annoying).

You are correct that it works fine without the debugger in the mix. I'll update the feedback accordingly.

Thanks (somewhat embarrassed :) )

Dave

si_pid and si_uid not set in sigaction handler
 
 
Q