Signal number to name?

后端 未结 3 483
长情又很酷
长情又很酷 2021-02-03 21:34

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

相关标签:
3条回答
  • 2021-02-03 21:54

    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
    
    0 讨论(0)
  • 2021-02-03 21:56

    May be, you can declare a global array, like this

    char *signame[]={"INVALID", "SIGHUP", "SIGINT", "SIGQUIT", "SIGILL", "SIGTRAP", "SIGABRT", "SIGBUS", "SIGFPE", "SIGKILL", "SIGUSR1", "SIGSEGV", "SIGUSR2", "SIGPIPE", "SIGALRM", "SIGTERM", "SIGSTKFLT", "SIGCHLD", "SIGCONT", "SIGSTOP", "SIGTSTP", "SIGTTIN", "SIGTTOU", "SIGURG", "SIGXCPU", "SIGXFSZ", "SIGVTALRM", "SIGPROF", "SIGWINCH", "SIGPOLL", "SIGPWR", "SIGSYS", NULL};
    

    and can use it to print signal name in signal handler, like

    void sig_handler(int signum){
         printf("Received signal : %s\n", signame[signum]);
    }
    
    0 讨论(0)
  • 2021-02-03 22:06

    Tested below code on Ubuntu 16.04 and MIPS and it works fine.

    #include <signal.h>
    #include <string.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    extern const char * const sys_siglist[];
    
    void upcase(char *s)
    {
        while (*s)
        {
            *s = toupper(*s);
            s++;
        }
    }
    
    int main(void)
    {
        int sig;
        /*
           NSIG returns number of signals available in a system
           and it may vary according to platforms;Found on Ubuntu-16.04 it return 65
           where as in MIPS it is 31; Found in both the platforms it leads to core dump
           after signal 31 so limiting scanning of signal till 31 instead of using NSIG
        */
        for (sig = 1; sig < 32; sig++)
        {
            char *str = strdup(sys_siglist[sig]);
            if (!str)
                return -1;
    
            upcase(str);
            printf("%2d -> SIG%s\n", sig, str);
    
            free(str);
        }
    
        return 0;
    }
    

    The output of the above code on Ubuntu-16.04 (Intel x86_64 GNU/Linux):

     1 -> SIGHANGUP
     2 -> SIGINTERRUPT
     3 -> SIGQUIT
     4 -> SIGILLEGAL INSTRUCTION
     5 -> SIGTRACE/BREAKPOINT TRAP
     6 -> SIGABORTED
     7 -> SIGBUS ERROR
     8 -> SIGFLOATING POINT EXCEPTION
     9 -> SIGKILLED
    10 -> SIGUSER DEFINED SIGNAL 1
    11 -> SIGSEGMENTATION FAULT
    12 -> SIGUSER DEFINED SIGNAL 2
    13 -> SIGBROKEN PIPE
    14 -> SIGALARM CLOCK
    15 -> SIGTERMINATED
    16 -> SIGSTACK FAULT
    17 -> SIGCHILD EXITED
    18 -> SIGCONTINUED
    19 -> SIGSTOPPED (SIGNAL)
    20 -> SIGSTOPPED
    21 -> SIGSTOPPED (TTY INPUT)
    22 -> SIGSTOPPED (TTY OUTPUT)
    23 -> SIGURGENT I/O CONDITION
    24 -> SIGCPU TIME LIMIT EXCEEDED
    25 -> SIGFILE SIZE LIMIT EXCEEDED
    26 -> SIGVIRTUAL TIMER EXPIRED
    27 -> SIGPROFILING TIMER EXPIRED
    28 -> SIGWINDOW CHANGED
    29 -> SIGI/O POSSIBLE
    30 -> SIGPOWER FAILURE
    31 -> SIGBAD SYSTEM CALL
    

    The output of the above code on busybox (MIPS, Cavium):

     1 -> SIGHANGUP
     2 -> SIGINTERRUPT
     3 -> SIGQUIT
     4 -> SIGILLEGAL INSTRUCTION
     5 -> SIGTRACE/BREAKPOINT TRAP
     6 -> SIGABORTED
     7 -> SIGEMT TRAP
     8 -> SIGFLOATING POINT EXCEPTION
     9 -> SIGKILLED
    10 -> SIGBUS ERROR
    11 -> SIGSEGMENTATION FAULT
    12 -> SIGBAD SYSTEM CALL
    13 -> SIGBROKEN PIPE
    14 -> SIGALARM CLOCK
    15 -> SIGTERMINATED
    16 -> SIGUSER DEFINED SIGNAL 1
    17 -> SIGUSER DEFINED SIGNAL 2
    18 -> SIGCHILD EXITED
    19 -> SIGPOWER FAILURE
    20 -> SIGWINDOW CHANGED
    21 -> SIGURGENT I/O CONDITION
    22 -> SIGI/O POSSIBLE
    23 -> SIGSTOPPED (SIGNAL)
    24 -> SIGSTOPPED
    25 -> SIGCONTINUED
    26 -> SIGSTOPPED (TTY INPUT)
    27 -> SIGSTOPPED (TTY OUTPUT)
    28 -> SIGVIRTUAL TIMER EXPIRED
    29 -> SIGPROFILING TIMER EXPIRED
    30 -> SIGCPU TIME LIMIT EXCEEDED
    31 -> SIGFILE SIZE LIMIT EXCEEDED
    
    0 讨论(0)
提交回复
热议问题