From 2c9274a5f7e73359848adc84fd932af313f143b9 Mon Sep 17 00:00:00 2001 From: Mitja Felicijan Date: Mon, 17 Jun 2024 16:52:00 +0200 Subject: Note: Sending signals to C programs --- .../2024-06-17-sending-signals-to-c-programs.md | 81 ++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 content/notes/2024-06-17-sending-signals-to-c-programs.md (limited to 'content') diff --git a/content/notes/2024-06-17-sending-signals-to-c-programs.md b/content/notes/2024-06-17-sending-signals-to-c-programs.md new file mode 100644 index 0000000..187beeb --- /dev/null +++ b/content/notes/2024-06-17-sending-signals-to-c-programs.md @@ -0,0 +1,81 @@ +--- +title: "Sending signals to C programs" +url: sending-signals-to-c-program +date: 2024-06-17T16:13:13+02:00 +type: note +draft: false +--- + +For simple and easy IPC to the C program we can use signals. +https://www.man7.org/linux/man-pages/man7/signal.7.html + +Only two user defined signals are available but you can hijack other ones even +though this is really not advisable. + +```c +// signal.c +#include +#include +#include +#include + +void handle_signal(int signal) { + printf("Signal received %d\n", signal); +} + +int main() { + signal(SIGUSR1, handle_signal); // is equal to 10 + signal(SIGUSR2, handle_signal); // is equal to 12 + + while(1) { + sleep(1); + } + + return 0; +} +``` + +Compile with `gcc -o signal signal.c` and run the program with `./signal` and +then from another terminal send the signal to the program with `kill -10 +$(pidof signal)` which should print out `Signal received 10`. + +| Signal | x86/ARM | Alpha/ | MIPS | PARISC | Notes | +|-----------|---------|--------|------|--------|---------------| +| SIGHUP | 1 | 1 | 1 | 1 | | +| SIGINT | 2 | 2 | 2 | 2 | | +| SIGQUIT | 3 | 3 | 3 | 3 | | +| SIGILL | 4 | 4 | 4 | 4 | | +| SIGTRAP | 5 | 5 | 5 | 5 | | +| SIGABRT | 6 | 6 | 6 | 6 | | +| SIGIOT | 6 | 6 | 6 | 6 | | +| SIGBUS | 7 | 10 | 10 | 10 | | +| SIGEMT | - | 7 | 7 | - | | +| SIGFPE | 8 | 8 | 8 | 8 | | +| SIGKILL | 9 | 9 | 9 | 9 | | +| SIGUSR1 | 10 | 30 | 16 | 16 | | +| SIGSEGV | 11 | 11 | 11 | 11 | | +| SIGUSR2 | 12 | 31 | 17 | 17 | | +| SIGPIPE | 13 | 13 | 13 | 13 | | +| SIGALRM | 14 | 14 | 14 | 14 | | +| SIGTERM | 15 | 15 | 15 | 15 | | +| SIGSTKFLT | 16 | - | - | 7 | | +| SIGCHLD | 17 | 20 | 18 | 18 | | +| SIGCLD | - | - | 18 | - | | +| SIGCONT | 18 | 19 | 25 | 26 | | +| SIGSTOP | 19 | 17 | 23 | 24 | | +| SIGTSTP | 20 | 18 | 24 | 25 | | +| SIGTTIN | 21 | 21 | 26 | 27 | | +| SIGTTOU | 22 | 22 | 27 | 28 | | +| SIGURG | 23 | 16 | 21 | 29 | | +| SIGXCPU | 24 | 24 | 30 | 12 | | +| SIGXFSZ | 25 | 25 | 31 | 30 | | +| SIGVTALRM | 26 | 26 | 28 | 20 | | +| SIGPROF | 27 | 27 | 29 | 21 | | +| SIGWINCH | 28 | 28 | 20 | 23 | | +| SIGIO | 29 | 23 | 22 | 22 | | +| SIGPOLL | | | | | Same as SIGIO | +| SIGPWR | 30 | 29/- | 19 | 19 | | +| SIGINFO | - | 29/- | - | - | | +| SIGLOST | - | -/29 | - | - | | +| SIGSYS | 31 | 12 | 12 | 31 | | +| SIGUNUSED | 31 | - | - | 31 | | -- cgit v1.2.3