Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Signal number to name?

Tags:

c

unix

How can I quickly get signal name from its number? There is strsignal(), but I just want the name, e.g. SIGUSR1

In other words, if we have macros like SIGUSR1 -> 12 do we have anything like 12 -> SIGUSR1 ?

like image 410
Mike Roll Avatar asked May 12 '13 16:05

Mike Roll


People also ask

What signal is Ctrl D?

Ctrl + D ( ^D ) means end of file. It only works at the beginning of a line (I'm simplifying a little), and has no effect if the program isn't reading input from the terminal.

What is signal number?

Definition of signal number : a naval officer's numerical order on the official seniority list.

What is signal 8 Linux?

8 SIGFPE A floating point exception happened in the program. 9 SIGKILL The process was explicitly killed by somebody wielding the kill program. 10 SIGUSR1 Left for the programmers to do whatever they want. 11 SIGSEGV An attempt was made to access memory not allocated to the process.


2 Answers

My strsignal(3) man page says you can get the names directly from the sys_signame array. Here's a simple example program I wrote to test it:

#include <signal.h> #include <string.h> #include <stdio.h> #include <stdlib.h> #include <ctype.h>  void upcase(char *s) {     while (*s)     {         *s = toupper(*s);         s++;             } }  int main(void) {         for (int sig = 1; sig < NSIG; sig++)     {         char *str = strdup(sys_signame[sig]);         if (!str)             return -1;          upcase(str);         printf("%2d -> SIG%s\n", sig, str);          free(str);     }      return 0; } 

I think this program produces the output you're looking for:

$ ./example   1 -> SIGHUP  2 -> SIGINT  3 -> SIGQUIT  4 -> SIGILL  5 -> SIGTRAP  6 -> SIGABRT  7 -> SIGEMT  8 -> SIGFPE  9 -> SIGKILL 10 -> SIGBUS 11 -> SIGSEGV 12 -> SIGSYS 13 -> SIGPIPE 14 -> SIGALRM 15 -> SIGTERM 16 -> SIGURG 17 -> SIGSTOP 18 -> SIGTSTP 19 -> SIGCONT 20 -> SIGCHLD 21 -> SIGTTIN 22 -> SIGTTOU 23 -> SIGIO 24 -> SIGXCPU 25 -> SIGXFSZ 26 -> SIGVTALRM 27 -> SIGPROF 28 -> SIGWINCH 29 -> SIGINFO 30 -> SIGUSR1 31 -> SIGUSR2 
like image 82
Carl Norum Avatar answered Sep 18 '22 18:09

Carl Norum


glib 2.32 (released on 2020-08-05) introduced the function sigabbrev_np(int). Since that version you cannot use sys_siglist[] anymore too.

From man strsignal:

The sigabbrev_np() function returns the abbreviated name of the signal, sig. For example, given the value SIGINT, it returns the string "INT".
[...]
sigdescr_np() and sigdabbrev_np() first appeared in glibc 2.32. Starting with version 2.32, the sys_siglist symbol is no longer exported by glibc.

And from the release notes:

The functions sigabbrev_np and sigdescr_np have been added. The sigabbrev_np returns the abbreviated signal name (e.g. "HUP" for SIGHUP) [...] both functions return NULL for an invalid signal number.

They should be used instead of sys_siglist or sys_sigabbrev and they are both thread and async-signal safe. These functions are GNU extensions.

like image 36
Socowi Avatar answered Sep 17 '22 18:09

Socowi