Replace strsigname() with sig2str(), emulating it as needed.

This commit is contained in:
Todd C. Miller
2012-08-29 14:25:09 -04:00
parent 71e2d8290b
commit ab7dda035a
10 changed files with 95 additions and 51 deletions

View File

@@ -51,13 +51,13 @@ compat/regress/fnmatch/fnm_test.in
compat/regress/glob/files
compat/regress/glob/globtest.c
compat/regress/glob/globtest.in
compat/sig2str.c
compat/siglist.in
compat/snprintf.c
compat/stdbool.h
compat/strlcat.c
compat/strlcpy.c
compat/strsignal.c
compat/strsigname.c
compat/timespec.h
compat/utime.h
compat/utimes.c

View File

@@ -204,6 +204,9 @@ nanosleep.lo: $(srcdir)/nanosleep.c $(top_builddir)/config.h \
$(LIBTOOL) --mode=compile $(CC) -c -o $@ $(CPPFLAGS) $(CFLAGS) $(PIE_CFLAGS) $(DEFS) $(srcdir)/nanosleep.c
pw_dup.lo: $(srcdir)/pw_dup.c $(top_builddir)/config.h
$(LIBTOOL) --mode=compile $(CC) -c -o $@ $(CPPFLAGS) $(CFLAGS) $(PIE_CFLAGS) $(DEFS) $(srcdir)/pw_dup.c
sig2str.lo: $(srcdir)/sig2str.c $(top_builddir)/config.h $(incdir)/missing.h \
$(incdir)/gettext.h
$(LIBTOOL) --mode=compile $(CC) -c -o $@ $(CPPFLAGS) $(CFLAGS) $(PIE_CFLAGS) $(DEFS) $(srcdir)/sig2str.c
siglist.lo: siglist.c $(top_builddir)/config.h $(incdir)/missing.h
$(LIBTOOL) --mode=compile $(CC) -c -o $@ $(CPPFLAGS) $(CFLAGS) $(PIE_CFLAGS) $(DEFS) siglist.c
snprintf.lo: $(srcdir)/snprintf.c $(top_builddir)/config.h $(incdir)/missing.h
@@ -215,9 +218,6 @@ strlcpy.lo: $(srcdir)/strlcpy.c $(top_builddir)/config.h $(incdir)/missing.h
strsignal.lo: $(srcdir)/strsignal.c $(top_builddir)/config.h \
$(incdir)/missing.h $(incdir)/gettext.h
$(LIBTOOL) --mode=compile $(CC) -c -o $@ $(CPPFLAGS) $(CFLAGS) $(PIE_CFLAGS) $(DEFS) $(srcdir)/strsignal.c
strsigname.lo: $(srcdir)/strsigname.c $(top_builddir)/config.h \
$(incdir)/missing.h $(incdir)/gettext.h
$(LIBTOOL) --mode=compile $(CC) -c -o $@ $(CPPFLAGS) $(CFLAGS) $(PIE_CFLAGS) $(DEFS) $(srcdir)/strsigname.c
utimes.lo: $(srcdir)/utimes.c $(top_builddir)/config.h \
$(top_srcdir)/compat/utime.h $(incdir)/missing.h
$(LIBTOOL) --mode=compile $(CC) -c -o $@ $(CPPFLAGS) $(CFLAGS) $(PIE_CFLAGS) $(DEFS) $(srcdir)/utimes.c

View File

@@ -1,5 +1,5 @@
/*
* Copyright (c) 2009-2012 Todd C. Miller <Todd.Miller@courtesan.com>
* Copyright (c) 2012 Todd C. Miller <Todd.Miller@courtesan.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
@@ -18,22 +18,34 @@
#include <sys/types.h>
#include <errno.h>
#include <stdio.h>
#ifdef STDC_HEADERS
# include <stdlib.h>
# include <stddef.h>
#else
# ifdef HAVE_STDLIB_H
# include <stdlib.h>
# endif
#endif /* STDC_HEADERS */
#ifdef HAVE_STRING_H
# include <string.h>
#endif /* HAVE_STRING_H */
#ifdef HAVE_STRINGS_H
# include <strings.h>
#endif /* HAVE_STRINGS_H */
#include <signal.h>
#include "missing.h"
#define DEFAULT_TEXT_DOMAIN "sudo"
#include "gettext.h"
#if defined(HAVE_DECL_SYS_SIGNAME) && HAVE_DECL_SYS_SIGNAME == 1
# define sudo_sys_signame sys_signame
# define sudo_sys_signame sys_signame
#elif defined(HAVE_DECL__SYS_SIGNAME) && HAVE_DECL__SYS_SIGNAME == 1
# define sudo_sys_signame _sys_signame
# define sudo_sys_signame _sys_signame
#elif defined(HAVE_DECL___SYS_SIGNAME) && HAVE_DECL___SYS_SIGNAME == 1
# define sudo_sys_signame __sys_signame
# define sudo_sys_signame __sys_signame
#elif defined(HAVE_DECL_SYS_SIGABBREV) && HAVE_DECL_SYS_SIGABBREV == 1
# define sudo_sys_signame sys_sigabbrev
# define sudo_sys_signame sys_sigabbrev
#else
# ifdef HAVE_SYS_SIGABBREV
/* sys_sigabbrev is not declared by glibc */
@@ -43,13 +55,22 @@ extern const char *const sudo_sys_signame[NSIG];
#endif
/*
* Return signal name
* Translate signal number to name.
*/
char *
strsigname(int signo)
int
sig2str(int signo, char *signame)
{
if (signo > 0 && signo < NSIG && sudo_sys_signame[signo] != NULL)
return (char *)sudo_sys_signame[signo];
/* XXX - should be "Unknown signal: %d" */
return _("Unknown signal");
#if defined(SIGRTMIN) && defined(SIGRTMAX)
/* Realtime signal support as per Solaris. */
if (signo >= SIGRTMIN && signo <= SIGRTMAX) {
snprintf(signame, SIG2STR_MAX, "RTMIN+%d", (signo - SIGRTMIN));
return 0;
}
#endif
if (signo > 0 && signo < NSIG && sudo_sys_signame[signo] != NULL) {
strlcpy(signame, sudo_sys_signame[signo], SIG2STR_MAX);
return 0;
}
errno = EINVAL;
return -1;
}

View File

@@ -491,6 +491,9 @@
/* Define to 1 if you have the `sia_ses_init' function. */
#undef HAVE_SIA_SES_INIT
/* Define to 1 if you have the `sig2str' function. */
#undef HAVE_SIG2STR
/* Define to 1 if the system has the type `sigaction_t'. */
#undef HAVE_SIGACTION_T
@@ -539,9 +542,6 @@
/* Define to 1 if you have the `strsignal' function. */
#undef HAVE_STRSIGNAL
/* Define to 1 if you have the `strsigname' function. */
#undef HAVE_STRSIGNAME
/* Define to 1 if `d_type' is a member of `struct dirent'. */
#undef HAVE_STRUCT_DIRENT_D_TYPE

12
configure vendored
View File

@@ -18320,19 +18320,19 @@ fi
done
for ac_func in strsigname
for ac_func in sig2str
do :
ac_fn_c_check_func "$LINENO" "strsigname" "ac_cv_func_strsigname"
if test "x$ac_cv_func_strsigname" = xyes; then :
ac_fn_c_check_func "$LINENO" "sig2str" "ac_cv_func_sig2str"
if test "x$ac_cv_func_sig2str" = xyes; then :
cat >>confdefs.h <<_ACEOF
#define HAVE_STRSIGNAME 1
#define HAVE_SIG2STR 1
_ACEOF
else
case " $LIBOBJS " in
*" strsigname.$ac_objext "* ) ;;
*) LIBOBJS="$LIBOBJS strsigname.$ac_objext"
*" sig2str.$ac_objext "* ) ;;
*) LIBOBJS="$LIBOBJS sig2str.$ac_objext"
;;
esac

View File

@@ -2570,10 +2570,10 @@ AC_INCLUDES_DEFAULT
])
dnl
dnl Check for strsigname() or sys_signame
dnl Check for sig2str(), sys_signame or sys_sigabbrev
dnl
AC_CHECK_FUNCS(strsigname, [], [
AC_LIBOBJ(strsigname)
AC_CHECK_FUNCS(sig2str, [], [
AC_LIBOBJ(sig2str)
HAVE_SIGNAME="false"
AC_CHECK_DECLS([sys_signame, _sys_signame, __sys_signame, sys_sigabbrev], [
HAVE_SIGNAME="true"

View File

@@ -284,6 +284,11 @@ extern int errno;
# endif
#endif
/* For sig2str() */
#ifndef SIG2STR_MAX
# define SIG2STR_MAX 32
#endif
#ifndef WCOREDUMP
# define WCOREDUMP(x) ((x) & 0x80)
#endif
@@ -376,8 +381,8 @@ int unsetenv(const char *);
#ifndef HAVE_STRSIGNAL
char *strsignal(int);
#endif
#ifndef HAVE_STRSIGNAME
char *strsigname(int);
#ifndef HAVE_SIG2STR
int sig2str(int, char *);
#endif
#endif /* _SUDO_MISSING_H */

View File

@@ -55,7 +55,7 @@ sub mkdep {
$makefile =~ s:\@SUDOERS_OBJS\@:bsm_audit.lo linux_audit.lo ldap.lo plugin_error.lo sssd.lo:;
# XXX - fill in AUTH_OBJS from contents of the auth dir instead
$makefile =~ s:\@AUTH_OBJS\@:afs.lo aix_auth.lo bsdauth.lo dce.lo fwtk.lo getspwuid.lo kerb5.lo pam.lo passwd.lo rfc1938.lo secureware.lo securid5.lo sia.lo:;
$makefile =~ s:\@LTLIBOBJS\@:closefrom.lo dlopen.lo fnmatch.lo getcwd.lo getgrouplist.lo getline.lo getprogname.lo glob.lo isblank.lo memrchr.lo mksiglist.lo mksigname.lo mktemp.lo nanosleep.lo pw_dup.lo siglist.lo snprintf.lo strlcat.lo strlcpy.lo strsignal.lo strsigname.lo utimes.lo globtest.o fnm_test.o:;
$makefile =~ s:\@LTLIBOBJS\@:closefrom.lo dlopen.lo fnmatch.lo getcwd.lo getgrouplist.lo getline.lo getprogname.lo glob.lo isblank.lo memrchr.lo mksiglist.lo mksigname.lo mktemp.lo nanosleep.lo pw_dup.lo sig2str.lo siglist.lo snprintf.lo strlcat.lo strlcpy.lo strsignal.lo utimes.lo globtest.o fnm_test.o:;
# Parse OBJS lines
my %objs;

View File

@@ -506,6 +506,7 @@ do_tty_io:
static int
handle_signals(int sv[2], pid_t child, int log_io, struct command_status *cstat)
{
char signame[SIG2STR_MAX];
unsigned char signo;
ssize_t nread;
int status;
@@ -531,7 +532,9 @@ handle_signals(int sv[2], pid_t child, int log_io, struct command_status *cstat)
cstat->val = errno;
debug_return_int(-1);
}
sudo_debug_printf(SUDO_DEBUG_DIAG, "received SIG%s", strsigname(signo));
if (sig2str(signo, signame) == -1)
snprintf(signame, sizeof(signame), "%d", signo);
sudo_debug_printf(SUDO_DEBUG_DIAG, "received SIG%s", signame);
if (signo == SIGCHLD) {
/*
* If logging I/O, child is the intermediate process,
@@ -575,10 +578,8 @@ handle_signals(int sv[2], pid_t child, int log_io, struct command_status *cstat)
sa.sa_handler = SIG_DFL;
sigaction(SIGTSTP, &sa, NULL);
}
if (kill(getpid(), signo) != 0) {
warning("kill(%d, SIG%s)", (int)getpid(),
strsigname(signo));
}
if (kill(getpid(), signo) != 0)
warning("kill(%d, SIG%s)", (int)getpid(), signame);
if (signo == SIGTSTP)
sigaction(SIGTSTP, &osa, NULL);
if (fd != -1) {
@@ -607,7 +608,7 @@ handle_signals(int sv[2], pid_t child, int log_io, struct command_status *cstat)
if (signo == SIGALRM)
terminate_command(child, false);
else if (kill(child, signo) != 0)
warning("kill(%d, SIG%s)", (int)child, strsigname(signo));
warning("kill(%d, SIG%s)", (int)child, signame);
}
}
}
@@ -620,6 +621,7 @@ handle_signals(int sv[2], pid_t child, int log_io, struct command_status *cstat)
static void
forward_signals(int sock)
{
char signame[SIG2STR_MAX];
struct sigforward *sigfwd;
struct command_status cstat;
ssize_t nsent;
@@ -627,9 +629,10 @@ forward_signals(int sock)
while (!tq_empty(&sigfwd_list)) {
sigfwd = tq_first(&sigfwd_list);
if (sig2str(sigfwd->signo, signame) == -1)
snprintf(signame, sizeof(signame), "%d", sigfwd->signo);
sudo_debug_printf(SUDO_DEBUG_INFO,
"sending SIG%s to child over backchannel",
strsigname(sigfwd->signo));
"sending SIG%s to child over backchannel", signame);
cstat.type = CMD_SIGNO;
cstat.val = sigfwd->signo;
do {
@@ -662,10 +665,12 @@ static void
schedule_signal(int signo)
{
struct sigforward *sigfwd;
char signame[SIG2STR_MAX];
debug_decl(schedule_signal, SUDO_DEBUG_EXEC)
sudo_debug_printf(SUDO_DEBUG_DIAG, "forwarding SIG%s to child",
strsigname(signo));
if (sig2str(signo, signame) == -1)
snprintf(signame, sizeof(signame), "%d", signo);
sudo_debug_printf(SUDO_DEBUG_DIAG, "forwarding SIG%s to child", signame);
sigfwd = ecalloc(1, sizeof(*sigfwd));
sigfwd->prev = sigfwd;

View File

@@ -335,6 +335,7 @@ check_foreground(void)
int
suspend_parent(int signo)
{
char signame[SIG2STR_MAX];
sigaction_t sa, osa;
int n, oldmode = ttymode, rval = 0;
debug_decl(suspend_parent, SUDO_DEBUG_EXEC);
@@ -372,16 +373,18 @@ suspend_parent(int signo)
} while (!n && errno == EINTR);
}
if (sig2str(signo, signame) == -1)
snprintf(signame, sizeof(signame), "%d", signo);
/* Suspend self and continue command when we resume. */
zero_bytes(&sa, sizeof(sa));
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_INTERRUPT; /* do not restart syscalls */
sa.sa_handler = SIG_DFL;
sigaction(signo, &sa, &osa);
sudo_debug_printf(SUDO_DEBUG_INFO, "kill parent SIG%s",
strsigname(signo));
sudo_debug_printf(SUDO_DEBUG_INFO, "kill parent SIG%s", signame);
if (killpg(ppgrp, signo) != 0)
warning("killpg(%d, SIG%s)", (int)ppgrp, strsigname(signo));
warning("killpg(%d, SIG%s)", (int)ppgrp, signame);
/* Check foreground/background status on resume. */
check_foreground();
@@ -823,12 +826,16 @@ fd_set_iobs(fd_set *fdsr, fd_set *fdsw)
static void
deliver_signal(pid_t pid, int signo, bool from_parent)
{
char signame[SIG2STR_MAX];
int status;
debug_decl(deliver_signal, SUDO_DEBUG_EXEC);
if (sig2str(signo, signame) == -1)
snprintf(signame, sizeof(signame), "%d", signo);
/* Handle signal from parent. */
sudo_debug_printf(SUDO_DEBUG_INFO, "received SIG%s%s",
strsigname(signo), from_parent ? " from parent" : "");
signame, from_parent ? " from parent" : "");
switch (signo) {
case SIGALRM:
terminate_command(pid, true);
@@ -904,19 +911,25 @@ handle_sigchld(int backchannel, struct command_status *cstat)
} while (pid == -1 && errno == EINTR);
if (pid == cmnd_pid) {
if (cstat->type != CMD_ERRNO) {
char signame[SIG2STR_MAX];
cstat->type = CMD_WSTATUS;
cstat->val = status;
if (WIFSTOPPED(status)) {
if (sig2str(WSTOPSIG(status), signame) == -1)
snprintf(signame, sizeof(signame), "%d", WSTOPSIG(status));
sudo_debug_printf(SUDO_DEBUG_INFO,
"command stopped, SIG%s", strsigname(WSTOPSIG(status)));
"command stopped, SIG%s", signame);
do {
cmnd_pgrp = tcgetpgrp(io_fds[SFD_SLAVE]);
} while (cmnd_pgrp == -1 && errno == EINTR);
if (send_status(backchannel, cstat) == -1)
return alive; /* XXX */
} else if (WIFSIGNALED(status)) {
if (sig2str(WTERMSIG(status), signame) == -1)
snprintf(signame, sizeof(signame), "%d", WTERMSIG(status));
sudo_debug_printf(SUDO_DEBUG_INFO,
"command killed, SIG%s", strsigname(WTERMSIG(status)));
"command killed, SIG%s", signame);
} else {
sudo_debug_printf(SUDO_DEBUG_INFO, "command exited: %d",
WEXITSTATUS(status));