Linux C: upon receiving a signal, is it possible to know the PID of the sender?

11,729

Solution 1

Yes. Register your signal handler using sigaction with the SA_SIGINFO flag, filling in the sa_sigaction field. Now your handler function takes a siginfo_t* parameter, which includes a field si_pid.

Note that si_pid is only set under some circumstances. In your case, you'll want to check that check that si_code is set to SI_USER or SI_QUEUE. Read man 2 sigaction for more.

Solution 2

Another option is using signalfd(). If you use signals to send information between processes, then a more structured signal handling than signal handlers is most likely what you want. struct signalfd_siginfo::ssi_pid is the sender.

Example from the man page:

#include <sys/signalfd.h>
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>

#define handle_error(msg) \
    do { perror(msg); exit(EXIT_FAILURE); } while (0)

int main(int argc, char *argv[]) {
    sigset_t mask;
    int sfd;
    struct signalfd_siginfo fdsi;
    ssize_t s;

    sigemptyset(&mask);
    sigaddset(&mask, SIGINT);
    sigaddset(&mask, SIGQUIT);

    /* Block signals so that they aren't handled
       according to their default dispositions */

    if (sigprocmask(SIG_BLOCK, &mask, NULL) == -1)
        handle_error("sigprocmask");

    sfd = signalfd(-1, &mask, 0);
    if (sfd == -1)
        handle_error("signalfd");

    for (;;) {
        s = read(sfd, &fdsi, sizeof(struct signalfd_siginfo));
        if (s != sizeof(struct signalfd_siginfo))
            handle_error("read");

        if (fdsi.ssi_signo == SIGINT) {
            printf("Got SIGINT\n");
        } else if (fdsi.ssi_signo == SIGQUIT) {
            printf("Got SIGQUIT\n");
            exit(EXIT_SUCCESS);
        } else {
            printf("Read unexpected signal\n");
        }
    }
}

See also: sigqueue(). Like kill(), but you can pass an integer or pointer in the same call.

Solution 3

Here's a complete example of the POSIX-standard sigaction() API:

#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

void sigusr1(int signo, siginfo_t *si, void *data) {
  (void)signo;
  (void)data;
  printf("Signal %d from pid %lu\n", (int)si->si_signo,
         (unsigned long)si->si_pid);
  exit(0);
}

int main(void) {
  struct sigaction sa;
  memset(&sa, 0, sizeof(sa));
  sa.sa_flags = SA_SIGINFO;
  sa.sa_sigaction = sigusr1;
  if (sigaction(SIGUSR1, &sa, 0) == -1) {
    fprintf(stderr, "%s: %s\n", "sigaction", strerror(errno));
  }
  printf("Pid %lu waiting for SIGUSR1\n", (unsigned long)getpid());
  for (;;) {
    sleep(10);
  }
  return 0;
}

Try to run it and then send it SIGUSR1 (e.g. kill -SIGUSR1 that-pid from a shell)

sigaction function: http://pubs.opengroup.org/onlinepubs/009604499/functions/sigaction.html

siginfo structure: http://pubs.opengroup.org/onlinepubs/009604499/basedefs/signal.h.html

Share:
11,729
daisy
Author by

daisy

Updated on June 05, 2022

Comments

  • daisy
    daisy almost 2 years

    Suppose my C program handles SIGUSR1.

    When it receives this signal, is it possible to know who sent it? I.e., How to get the pid of sender process?