library: Remove signal name from library

Procps library previously held functions that were about either
listing or finding signal names. These are not really the right
location for a library about reading procfs.

This patch handles signal related functions in two ways:

For functions purely found in skill, these have been moved back
into this binary as they are used nowhere else.

For functions used across the binaries, these have been moved
into include/signals.h and lib/signals.c. Besides formatting,
these functions are largely the same.

To assist the skill functions, two functions to access the
signal map array have been added to lib/signals.c
This commit is contained in:
Craig Small 2015-06-29 21:52:51 +10:00
parent 8ded6c5739
commit 56399212c8
10 changed files with 1548 additions and 1579 deletions

View File

@ -78,7 +78,7 @@ endif
if BUILD_KILL
bin_PROGRAMS += kill
dist_man_MANS += kill.1
kill_SOURCES = skill.c lib/strutils.c lib/fileutils.c lib/nsutils.c
kill_SOURCES = skill.c lib/strutils.c lib/fileutils.c lib/nsutils.c lib/signals.c
else
EXTRA_DIST += kill.1
endif
@ -102,6 +102,7 @@ top_top_SOURCES = \
top/top_nls.h \
top/top_nls.c \
lib/fileutils.c
lib/signals.c
top_top_LDADD = $(LDADD) @NCURSES_LIBS@ $(DL_LIB)
endif
@ -109,8 +110,8 @@ if BUILD_SKILL
bin_PROGRAMS += \
skill \
snice
skill_SOURCES = skill.c lib/strutils.c lib/fileutils.c lib/nsutils.c
snice_SOURCES = skill.c lib/strutils.c lib/fileutils.c lib/nsutils.c
skill_SOURCES = skill.c lib/strutils.c lib/fileutils.c lib/nsutils.c lib/signals.c
snice_SOURCES = skill.c lib/strutils.c lib/fileutils.c lib/nsutils.c lib/signals.c
dist_man_MANS += \
skill.1 \
snice.1
@ -121,8 +122,8 @@ else
endif
free_SOURCES = free.c lib/strutils.c lib/fileutils.c
pgrep_SOURCES = pgrep.c lib/fileutils.c lib/nsutils.c
pkill_SOURCES = pgrep.c lib/fileutils.c lib/nsutils.c
pgrep_SOURCES = pgrep.c lib/fileutils.c lib/nsutils.c lib/signals.c
pkill_SOURCES = pgrep.c lib/fileutils.c lib/nsutils.c lib/signals.c
pmap_SOURCES = pmap.c lib/fileutils.c
pwdx_SOURCES = pwdx.c lib/fileutils.c
pwdx_LDADD=
@ -167,8 +168,6 @@ proc_libprocps_la_SOURCES = \
proc/readproc.h \
proc/readstat.c \
proc/readstat.h \
proc/sig.c \
proc/sig.h \
proc/slab.c \
proc/slab.h \
proc/sysinfo.c \
@ -190,7 +189,6 @@ proc_libprocps_la_include_HEADERS = \
proc/procps.h \
proc/pwcache.h \
proc/readproc.h \
proc/sig.h \
proc/slab.h \
proc/sysinfo.h \
proc/uptime.h \
@ -221,7 +219,8 @@ ps_pscommand_SOURCES = \
ps/select.c \
ps/sortformat.c \
ps/stacktrace.c \
lib/fileutils.c
lib/fileutils.c \
lib/signals.c
# lib/test_* binaries
noinst_PROGRAMS = \

View File

@ -4,5 +4,6 @@ dist_noinst_HEADERS = \
nls.h \
nsutils.h \
rpmatch.h \
signals.h \
strutils.h \
xalloc.h

View File

@ -11,22 +11,18 @@
* GNU Library General Public License for more details.
*/
#include <proc/procps.h>
__BEGIN_DECLS
extern const int number_of_signals;
extern const char *get_sigtable_name(int row);
extern const int get_sigtable_num(int row);
/* return -1 on failure */
extern int signal_name_to_number(const char *__restrict name);
extern const char *signal_number_to_name(int signo);
extern int print_given_signals(int argc, const char *__restrict const *__restrict argv, int max_line);
extern char *strtosig(const char *__restrict s);
extern void pretty_print_signals(void);
extern void unix_print_signals(void);
__END_DECLS
#endif

200
lib/signals.c Normal file
View File

@ -0,0 +1,200 @@
/*
* signals.c - signal name, and number, conversions
* Copyright 1998-2003 by Albert Cahalan
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <ctype.h>
#include <signal.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "signals.h"
#include "c.h"
/* Linux signals:
*
* SIGSYS is required by Unix98.
* SIGEMT is part of SysV, BSD, and ancient UNIX tradition.
*
* They are provided by these Linux ports: alpha, mips, sparc, and sparc64.
* You get SIGSTKFLT and SIGUNUSED instead on i386, m68k, ppc, and arm.
* (this is a Linux & libc bug -- both must be fixed)
*
* Total garbage: SIGIO SIGINFO SIGIOT SIGLOST SIGCLD
* (popular ones are handled as aliases)
* Nearly garbage: SIGSTKFLT SIGUNUSED (nothing else to fill slots)
*/
/* Linux 2.3.29 replaces SIGUNUSED with the standard SIGSYS signal */
#ifndef SIGSYS
# warning Standards require that <signal.h> define SIGSYS
# define SIGSYS SIGUNUSED
#endif
/* If we see both, it is likely SIGSTKFLT (junk) was replaced. */
#ifdef SIGEMT
# undef SIGSTKFLT
#endif
#ifndef SIGRTMIN
# warning Standards require that <signal.h> define SIGRTMIN; assuming 32
# define SIGRTMIN 32
#endif
/* It seems the SPARC libc does not know the kernel supports SIGPWR. */
#ifndef SIGPWR
# warning Your header files lack SIGPWR. (assuming it is number 29)
# define SIGPWR 29
#endif
typedef struct mapstruct {
const char *name;
int num;
} mapstruct;
static const mapstruct sigtable[] = {
{"ABRT", SIGABRT}, /* IOT */
{"ALRM", SIGALRM},
{"BUS", SIGBUS},
{"CHLD", SIGCHLD}, /* CLD */
{"CONT", SIGCONT},
#ifdef SIGEMT
{"EMT", SIGEMT},
#endif
{"FPE", SIGFPE},
{"HUP", SIGHUP},
{"ILL", SIGILL},
{"INT", SIGINT},
{"KILL", SIGKILL},
{"PIPE", SIGPIPE},
{"POLL", SIGPOLL}, /* IO */
{"PROF", SIGPROF},
{"PWR", SIGPWR},
{"QUIT", SIGQUIT},
{"SEGV", SIGSEGV},
#ifdef SIGSTKFLT
{"STKFLT", SIGSTKFLT},
#endif
{"STOP", SIGSTOP},
{"SYS", SIGSYS}, /* UNUSED */
{"TERM", SIGTERM},
{"TRAP", SIGTRAP},
{"TSTP", SIGTSTP},
{"TTIN", SIGTTIN},
{"TTOU", SIGTTOU},
{"URG", SIGURG},
{"USR1", SIGUSR1},
{"USR2", SIGUSR2},
{"VTALRM", SIGVTALRM},
{"WINCH", SIGWINCH},
{"XCPU", SIGXCPU},
{"XFSZ", SIGXFSZ}
};
const int number_of_signals = sizeof(sigtable)/sizeof(mapstruct);
static int compare_signal_names(const void *a, const void *b){
return strcasecmp( ((const mapstruct*)a)->name, ((const mapstruct*)b)->name );
}
const char *get_sigtable_name(int row)
{
if (row < 0 || row >= number_of_signals)
return NULL;
return sigtable[row].name;
}
const int get_sigtable_num(int row)
{
if (row < 0 || row >= number_of_signals)
return -1;
return sigtable[row].num;
}
/* return -1 on failure */
int signal_name_to_number(const char *restrict name){
long val;
int offset;
/* clean up name */
if(!strncasecmp(name,"SIG",3))
name += 3;
if(!strcasecmp(name,"CLD"))
return SIGCHLD;
if(!strcasecmp(name,"IO"))
return SIGPOLL;
if(!strcasecmp(name,"IOT"))
return SIGABRT;
/* search the table */
{
const mapstruct ms = {name,0};
const mapstruct *restrict const ptr = bsearch(
&ms,
sigtable,
number_of_signals,
sizeof(mapstruct),
compare_signal_names);
if(ptr)
return ptr->num;
}
if(!strcasecmp(name,"RTMIN"))
return SIGRTMIN;
if(!strcasecmp(name,"EXIT"))
return 0;
if(!strcasecmp(name,"NULL"))
return 0;
offset = 0;
if(!strncasecmp(name,"RTMIN+",6)) {
name += 6;
offset = SIGRTMIN;
}
/* not found, so try as a number */
{
char *endp;
val = strtol(name,&endp,10);
if(*endp || endp==name)
return -1; /* not valid */
}
if(val+SIGRTMIN>127)
return -1; /* not valid */
return val+offset;
}
const char *signal_number_to_name(int signo)
{
static char buf[32];
int n = number_of_signals;
signo &= 0x7f; /* need to process exit values too */
while (n--) {
if(sigtable[n].num==signo)
return sigtable[n].name;
}
if (signo == SIGRTMIN)
return "RTMIN";
if (signo)
sprintf(buf, "RTMIN+%d", signo-SIGRTMIN);
else
strcpy(buf,"0"); /* AIX has NULL; Solaris has EXIT */
return buf;
}

1366
pgrep.c

File diff suppressed because it is too large Load Diff

View File

@ -40,7 +40,6 @@ global:
meminfo;
openproc;
page_bytes;
pretty_print_signals;
print_uptime;
put_slabinfo;
readeither;
@ -49,13 +48,9 @@ global:
readproctab3;
readproctab;
readtask;
signal_name_to_number;
signal_number_to_name;
smp_num_cpus;
sprint_uptime;
strtosig;
tty_to_dev;
unix_print_signals;
user_from_uid;
uptime;
sprint_uptime;

View File

@ -1,294 +0,0 @@
/*
* sig.c - signal name, and number, conversions
* Copyright 1998-2003 by Albert Cahalan
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <ctype.h>
#include <signal.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "sig.h"
#include "c.h"
/* Linux signals:
*
* SIGSYS is required by Unix98.
* SIGEMT is part of SysV, BSD, and ancient UNIX tradition.
*
* They are provided by these Linux ports: alpha, mips, sparc, and sparc64.
* You get SIGSTKFLT and SIGUNUSED instead on i386, m68k, ppc, and arm.
* (this is a Linux & libc bug -- both must be fixed)
*
* Total garbage: SIGIO SIGINFO SIGIOT SIGLOST SIGCLD
* (popular ones are handled as aliases)
* Nearly garbage: SIGSTKFLT SIGUNUSED (nothing else to fill slots)
*/
/* Linux 2.3.29 replaces SIGUNUSED with the standard SIGSYS signal */
#ifndef SIGSYS
# warning Standards require that <signal.h> define SIGSYS
# define SIGSYS SIGUNUSED
#endif
/* If we see both, it is likely SIGSTKFLT (junk) was replaced. */
#ifdef SIGEMT
# undef SIGSTKFLT
#endif
#ifndef SIGRTMIN
# warning Standards require that <signal.h> define SIGRTMIN; assuming 32
# define SIGRTMIN 32
#endif
/* It seems the SPARC libc does not know the kernel supports SIGPWR. */
#ifndef SIGPWR
# warning Your header files lack SIGPWR. (assuming it is number 29)
# define SIGPWR 29
#endif
typedef struct mapstruct {
const char *name;
int num;
} mapstruct;
static const mapstruct sigtable[] = {
{"ABRT", SIGABRT}, /* IOT */
{"ALRM", SIGALRM},
{"BUS", SIGBUS},
{"CHLD", SIGCHLD}, /* CLD */
{"CONT", SIGCONT},
#ifdef SIGEMT
{"EMT", SIGEMT},
#endif
{"FPE", SIGFPE},
{"HUP", SIGHUP},
{"ILL", SIGILL},
{"INT", SIGINT},
{"KILL", SIGKILL},
{"PIPE", SIGPIPE},
{"POLL", SIGPOLL}, /* IO */
{"PROF", SIGPROF},
{"PWR", SIGPWR},
{"QUIT", SIGQUIT},
{"SEGV", SIGSEGV},
#ifdef SIGSTKFLT
{"STKFLT", SIGSTKFLT},
#endif
{"STOP", SIGSTOP},
{"SYS", SIGSYS}, /* UNUSED */
{"TERM", SIGTERM},
{"TRAP", SIGTRAP},
{"TSTP", SIGTSTP},
{"TTIN", SIGTTIN},
{"TTOU", SIGTTOU},
{"URG", SIGURG},
{"USR1", SIGUSR1},
{"USR2", SIGUSR2},
{"VTALRM", SIGVTALRM},
{"WINCH", SIGWINCH},
{"XCPU", SIGXCPU},
{"XFSZ", SIGXFSZ}
};
static const int number_of_signals = sizeof(sigtable)/sizeof(mapstruct);
static int compare_signal_names(const void *a, const void *b){
return strcasecmp( ((const mapstruct*)a)->name, ((const mapstruct*)b)->name );
}
/* return -1 on failure */
int signal_name_to_number(const char *restrict name){
long val;
int offset;
/* clean up name */
if(!strncasecmp(name,"SIG",3)) name += 3;
if(!strcasecmp(name,"CLD")) return SIGCHLD;
if(!strcasecmp(name,"IO")) return SIGPOLL;
if(!strcasecmp(name,"IOT")) return SIGABRT;
/* search the table */
{
const mapstruct ms = {name,0};
const mapstruct *restrict const ptr = bsearch(
&ms,
sigtable,
number_of_signals,
sizeof(mapstruct),
compare_signal_names
);
if(ptr) return ptr->num;
}
if(!strcasecmp(name,"RTMIN")) return SIGRTMIN;
if(!strcasecmp(name,"EXIT")) return 0;
if(!strcasecmp(name,"NULL")) return 0;
offset = 0;
if(!strncasecmp(name,"RTMIN+",6)){
name += 6;
offset = SIGRTMIN;
}
/* not found, so try as a number */
{
char *endp;
val = strtol(name,&endp,10);
if(*endp || endp==name) return -1; /* not valid */
}
if(val+SIGRTMIN>127) return -1; /* not valid */
return val+offset;
}
const char *signal_number_to_name(int signo){
static char buf[32];
int n = number_of_signals;
signo &= 0x7f; /* need to process exit values too */
while(n--){
if(sigtable[n].num==signo) return sigtable[n].name;
}
if(signo == SIGRTMIN) return "RTMIN";
if(signo) sprintf(buf, "RTMIN+%d", signo-SIGRTMIN);
else strcpy(buf,"0"); /* AIX has NULL; Solaris has EXIT */
return buf;
}
int print_given_signals(int argc, const char *restrict const *restrict argv, int max_line){
char buf[1280]; /* 128 signals, "RTMIN+xx" is largest */
int ret = 0; /* to be used as exit code by caller */
int place = 0; /* position on this line */
int amt;
if(argc > 128) return 1;
while(argc--){
char tmpbuf[16];
const char *restrict const txt = *argv;
if(*txt >= '0' && *txt <= '9'){
long val;
char *endp;
val = strtol(txt,&endp,10);
if(*endp){
fprintf(stderr, "Signal \"%s\" not known.\n", txt);
ret = 1;
goto end;
}
amt = sprintf(tmpbuf, "%s", signal_number_to_name(val));
}else{
int sno;
sno = signal_name_to_number(txt);
if(sno == -1){
fprintf(stderr, "Signal \"%s\" not known.\n", txt);
ret = 1;
goto end;
}
amt = sprintf(tmpbuf, "%d", sno);
}
if(!place){
strcpy(buf,tmpbuf);
place = amt;
goto end;
}
if(amt+place+1 > max_line){
printf("%s\n", buf);
strcpy(buf,tmpbuf);
place = amt;
goto end;
}
sprintf(buf+place, " %s", tmpbuf);
place += amt+1;
end:
argv++;
}
if(place) printf("%s\n", buf);
return ret;
}
/* strtosig is similar to print_given_signals() with exception, that
* this function takes a string, and converts it to a signal name or
* a number string depending on which way a round conversion is
* queried. Non-existing signals return NULL. Notice that the
* returned string should be freed after use.
*/
char *strtosig(const char *restrict s){
char *converted = NULL, *copy, *p, *endp;
int i, numsignal = 0;
copy = strdup(s);
if (!copy)
xerrx(EXIT_FAILURE, "cannot duplicate string");
for (p = copy; *p != '\0'; p++)
*p = toupper(*p);
p = copy;
if (p[0] == 'S' && p[1] == 'I' && p[2] == 'G')
p += 3;
if (isdigit(*p)){
numsignal = strtol(s,&endp,10);
if(*endp || endp==s) return NULL; /* not valid */
}
if (numsignal){
for (i = 0; i < number_of_signals; i++){
if (numsignal == sigtable[i].num){
converted = strdup(sigtable[i].name);
break;
}
}
} else {
for (i = 0; i < number_of_signals; i++){
if (strcmp(p, sigtable[i].name) == 0){
converted = malloc(sizeof(char) * 8);
if (converted)
snprintf(converted, sizeof(converted) - 1, "%d", sigtable[i].num);
break;
}
}
}
free(p);
return converted;
}
void pretty_print_signals(void){
int i = 0;
while(++i <= number_of_signals){
int n;
n = printf("%2d %s", i, signal_number_to_name(i));
if(n>0 && i%7) printf("%s", " \0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0" + n);
else printf("\n");
}
if((i-1)%7) printf("\n");
}
void unix_print_signals(void){
int pos = 0;
int i = 0;
while(++i <= number_of_signals){
if(i-1) printf("%c", (pos>73)?(pos=0,'\n'):(pos++,' ') );
pos += printf("%s", signal_number_to_name(i));
}
printf("\n");
}
/* sanity check */
static int init_signal_list(void) __attribute__((constructor));
static int init_signal_list(void){
if(number_of_signals != 31){
fprintf(stderr, "WARNING: %d signals -- adjust and recompile.\n", number_of_signals);
}
return 0;
}

View File

@ -31,13 +31,13 @@
#include "../proc/alloc.h"
#include "../proc/readproc.h"
#include "../proc/sig.h"
#include "../proc/sysinfo.h"
#include "../proc/version.h"
#include "../proc/wchan.h"
#include "../include/fileutils.h"
#include "../include/c.h"
#include "../include/signals.h"
#include "common.h"
#ifndef SIGCHLD

1224
skill.c

File diff suppressed because it is too large Load Diff

View File

@ -47,6 +47,7 @@
#include <sys/types.h> // also available via <stdlib.h>
#include "../include/fileutils.h"
#include "../include/signals.h"
#include "../include/nls.h"
#include "../proc/devname.h"
@ -54,7 +55,6 @@
#include "../proc/procps.h"
#include "../proc/readproc.h"
#include "../proc/readstat.h"
#include "../proc/sig.h"
#include "../proc/sysinfo.h"
#include "../proc/version.h"
#include "../proc/wchan.h"