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

void sigint_handler(int sig) {
  write(0, "nice try\n", 10);
}
int main(void) {
  struct sigaction sa; // configure sigaction
  sa.sa_handler=sigint_handler;
  sa.sa_flags=0;
  sa.sa_mask=0;
  if (sigaction(SIGINT, & sa, NULL) == -1) { // set handler
    perror("sigaction");
    exit(1);
  }
  printf("You shall not exit!\n");
  while (1) {
    sleep(1);
  }
}

The above code when compiled with g++ gives the error :

sigaction.c:13:14: error: no match for ‘operator=’ (operand types are ‘__sigset_t’ and
 ‘int’)
   13 |   sa.sa_mask=0;
      |              ^
In file included from /usr/include/x86_64-linux-gnu/bits/types/sigset_t.h:4,
                 from /usr/include/x86_64-linux-gnu/sys/select.h:33,
                 from /usr/include/x86_64-linux-gnu/sys/types.h:179,
                 from /usr/include/stdlib.h:394,
                 from /usr/include/c++/9/cstdlib:75,
                 from /usr/include/c++/9/stdlib.h:36,
                 from sigaction.c:2:

In the man pages for sigaction its defined as sigset_t sa_mask; I am not sure what this sigset_t is exactly. What could be possible values for it.

asha rani
  • 11
  • 1
  • 1
    The `sa_mask` field defines a set of signals that you do not want to have interrupt your signal handler. You need to use the `sigaddset`, `sigdelset`, `sigemptyset`, and `sigfillset` functions to manipulate the field. See the duplicates and the [`sigsetops` manpage](https://linux.die.net/man/3/sigsetops). – zwol Dec 09 '20 at 19:59
  • The error message (especially the part about `operator=`) indicates that you're programming in C++ and not C. As does the use of `g++` instead of `gcc` to build. Please pay attention to the tags you select when writing your questions. On the other hand, there's nothing C++-specific in the shown code, which perhaps means you have create the wrong kind of project in your IDE or have otherwise added flags to explicitly build as C++, because even `g++` would build a `.c` file as C. – Some programmer dude Dec 09 '20 at 20:05

0 Answers0