(root)/
util-linux-2.39/
lib/
signames.c
       1  /*
       2   * No copyright is claimed.  This code is in the public domain; do with
       3   * it what you wish.
       4  
       5   * Written by:
       6   *  Sami Kerola <kerolasa@iki.fi>
       7   *  Karel Zak <kzak@redhat.com>
       8   *  Niklas Hambüchen <mail@nh2.me>
       9   */
      10  
      11  #include <ctype.h>		/* for isdigit() */
      12  #include <signal.h>
      13  #include <unistd.h>
      14  #include <string.h>
      15  #include <errno.h>
      16  
      17  #include "c.h"
      18  #include "cctype.h"
      19  #include "strutils.h"
      20  #include "signames.h"
      21  
      22  static const struct ul_signal_name {
      23  	const char *name;
      24  	int val;
      25  } ul_signames[] = {
      26  	/* POSIX signals */
      27  	{ "HUP",	SIGHUP },	/* 1 */
      28  	{ "INT",	SIGINT },	/* 2 */
      29  	{ "QUIT",	SIGQUIT },	/* 3 */
      30  	{ "ILL",	SIGILL },	/* 4 */
      31  #ifdef SIGTRAP
      32  	{ "TRAP",	SIGTRAP },	/* 5 */
      33  #endif
      34  	{ "ABRT",	SIGABRT },	/* 6 */
      35  #ifdef SIGIOT
      36  	{ "IOT",	SIGIOT },	/* 6, same as SIGABRT */
      37  #endif
      38  #ifdef SIGEMT
      39  	{ "EMT",	SIGEMT },	/* 7 (mips,alpha,sparc*) */
      40  #endif
      41  #ifdef SIGBUS
      42  	{ "BUS",	SIGBUS },	/* 7 (arm,i386,m68k,ppc), 10 (mips,alpha,sparc*) */
      43  #endif
      44  	{ "FPE",	SIGFPE },	/* 8 */
      45  	{ "KILL",	SIGKILL },	/* 9 */
      46  	{ "USR1",	SIGUSR1 },	/* 10 (arm,i386,m68k,ppc), 30 (alpha,sparc*), 16 (mips) */
      47  	{ "SEGV",	SIGSEGV },	/* 11 */
      48  	{ "USR2",	SIGUSR2 },	/* 12 (arm,i386,m68k,ppc), 31 (alpha,sparc*), 17 (mips) */
      49  	{ "PIPE",	SIGPIPE },	/* 13 */
      50  	{ "ALRM",	SIGALRM },	/* 14 */
      51  	{ "TERM",	SIGTERM },	/* 15 */
      52  #ifdef SIGSTKFLT
      53  	{ "STKFLT",	SIGSTKFLT },	/* 16 (arm,i386,m68k,ppc) */
      54  #endif
      55  	{ "CHLD",	SIGCHLD },	/* 17 (arm,i386,m68k,ppc), 20 (alpha,sparc*), 18 (mips) */
      56  #ifdef SIGCLD
      57  	{ "CLD",	SIGCLD },	/* same as SIGCHLD (mips) */
      58  #endif
      59  	{ "CONT",	SIGCONT },	/* 18 (arm,i386,m68k,ppc), 19 (alpha,sparc*), 25 (mips) */
      60  	{ "STOP",	SIGSTOP },	/* 19 (arm,i386,m68k,ppc), 17 (alpha,sparc*), 23 (mips) */
      61  	{ "TSTP",	SIGTSTP },	/* 20 (arm,i386,m68k,ppc), 18 (alpha,sparc*), 24 (mips) */
      62  	{ "TTIN",	SIGTTIN },	/* 21 (arm,i386,m68k,ppc,alpha,sparc*), 26 (mips) */
      63  	{ "TTOU",	SIGTTOU },	/* 22 (arm,i386,m68k,ppc,alpha,sparc*), 27 (mips) */
      64  #ifdef SIGURG
      65  	{ "URG",	SIGURG },	/* 23 (arm,i386,m68k,ppc), 16 (alpha,sparc*), 21 (mips) */
      66  #endif
      67  #ifdef SIGXCPU
      68  	{ "XCPU",	SIGXCPU },	/* 24 (arm,i386,m68k,ppc,alpha,sparc*), 30 (mips) */
      69  #endif
      70  #ifdef SIGXFSZ
      71  	{ "XFSZ",	SIGXFSZ },	/* 25 (arm,i386,m68k,ppc,alpha,sparc*), 31 (mips) */
      72  #endif
      73  #ifdef SIGVTALRM
      74  	{ "VTALRM",	SIGVTALRM },	/* 26 (arm,i386,m68k,ppc,alpha,sparc*), 28 (mips) */
      75  #endif
      76  #ifdef SIGPROF
      77  	{ "PROF",	SIGPROF },	/* 27 (arm,i386,m68k,ppc,alpha,sparc*), 29 (mips) */
      78  #endif
      79  #ifdef SIGWINCH
      80  	{ "WINCH",	SIGWINCH },	/* 28 (arm,i386,m68k,ppc,alpha,sparc*), 20 (mips) */
      81  #endif
      82  #ifdef SIGIO
      83  	{ "IO",		SIGIO },	/* 29 (arm,i386,m68k,ppc), 23 (alpha,sparc*), 22 (mips) */
      84  #endif
      85  #ifdef SIGPOLL
      86  	{ "POLL",	SIGPOLL },	/* same as SIGIO */
      87  #endif
      88  #ifdef SIGINFO
      89  	{ "INFO",	SIGINFO },	/* 29 (alpha) */
      90  #endif
      91  #ifdef SIGLOST
      92  	{ "LOST",	SIGLOST },	/* 29 (arm,i386,m68k,ppc,sparc*) */
      93  #endif
      94  #ifdef SIGPWR
      95  	{ "PWR",	SIGPWR },	/* 30 (arm,i386,m68k,ppc), 29 (alpha,sparc*), 19 (mips) */
      96  #endif
      97  #ifdef SIGUNUSED
      98  	{ "UNUSED",	SIGUNUSED },	/* 31 (arm,i386,m68k,ppc) */
      99  #endif
     100  #ifdef SIGSYS
     101  	{ "SYS",	SIGSYS },	/* 31 (mips,alpha,sparc*) */
     102  #endif
     103  };
     104  
     105  #ifdef SIGRTMIN
     106  static int rtsig_to_signum(const char *sig)
     107  {
     108  	int num, maxi = 0;
     109  	char *ep = NULL;
     110  
     111  	if (c_strncasecmp(sig, "min+", 4) == 0)
     112  		sig += 4;
     113  	else if (c_strncasecmp(sig, "max-", 4) == 0) {
     114  		sig += 4;
     115  		maxi = 1;
     116  	}
     117  	if (!isdigit(*sig))
     118  		return -1;
     119  	errno = 0;
     120  	num = strtol(sig, &ep, 10);
     121  	if (!ep || sig == ep || errno || num < 0)
     122  		return -1;
     123  	num = maxi ? SIGRTMAX - num : SIGRTMIN + num;
     124  	if (num < SIGRTMIN || SIGRTMAX < num)
     125  		return -1;
     126  	return num;
     127  }
     128  #endif
     129  
     130  int signame_to_signum(const char *sig)
     131  {
     132  	size_t n;
     133  
     134  	if (!c_strncasecmp(sig, "sig", 3))
     135  		sig += 3;
     136  #ifdef SIGRTMIN
     137  	/* RT signals */
     138  	if (!c_strncasecmp(sig, "rt", 2))
     139  		return rtsig_to_signum(sig + 2);
     140  #endif
     141  	/* Normal signals */
     142  	for (n = 0; n < ARRAY_SIZE(ul_signames); n++) {
     143  		if (!c_strcasecmp(ul_signames[n].name, sig))
     144  			return ul_signames[n].val;
     145  	}
     146  	return -1;
     147  }
     148  
     149  const char *signum_to_signame(int signum)
     150  {
     151  	size_t n;
     152  
     153  	for (n = 0; n < ARRAY_SIZE(ul_signames); n++) {
     154  		if (ul_signames[n].val == signum) {
     155  			return ul_signames[n].name;
     156  		}
     157  	}
     158  
     159  	return NULL;
     160  }
     161  
     162  int get_signame_by_idx(size_t idx, const char **signame, int *signum)
     163  {
     164  	if (idx >= ARRAY_SIZE(ul_signames))
     165  		return -1;
     166  
     167  	if (signame)
     168  		*signame = ul_signames[idx].name;
     169  	if (signum)
     170  		*signum = ul_signames[idx].val;
     171  	return 0;
     172  
     173  }
     174