Modernize: use sigaction() whenever possible. Remove uses of common.

bsd_signal() may not be defined, and there is really no reason to even
try to use it if sigaction() is avaiable; using sigaction() guarantees
the semantics we really want.

Replace uses of common variables with explicit instantiation and
extern declarations in a header file.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
This commit is contained in:
H. Peter Anvin 2024-05-29 15:34:57 -07:00
parent 9c0908a778
commit 1f4b33a1f7
9 changed files with 64 additions and 42 deletions

View file

@ -4,7 +4,7 @@ VERSION = $(shell cat ../version)
-include ../MCONFIG
include ../MRULES
OBJS = tftpsubs.$(O)
OBJS = tftpsubs.$(O) signal.$(O)
LIB = libcommon.a
all: $(LIB)

42
common/signal.c Normal file
View file

@ -0,0 +1,42 @@
/*
* signal.c
*
* Use sigaction() to simulate BSD signal()
*/
#include "config.h"
#ifdef HAVE_SIGACTION
sighandler_t tftp_signal(int signum, sighandler_t handler)
{
struct sigaction action, oldaction;
memset(&action, 0, sizeof action);
action.sa_handler = handler;
sigemptyset(&action.sa_mask);
sigaddset(&action.sa_mask, signum);
action.sa_flags = SA_RESTART;
if (sigaction(signum, &action, &oldaction))
return SIG_ERR;
return oldaction.sa_handler;
}
#elif defined(HAVE_BSD_SIGNAL)
sighandler_t tftp_signal(int signum, sighandler_t handler)
{
return bsd_signal(signum, handler);
}
#else
/* This is dangerous at best. Let's hope it works. */
sighandler_t tftp_signal(int signum, sighandler_t handler)
{
return signal(signum, handler);
}
#endif

View file

@ -1,6 +1,6 @@
/* -*- c -*- ------------------------------------------------------------- *
*
* Copyright 2001-2006 H. Peter Anvin - All Rights Reserved
* Copyright 2001-2024 H. Peter Anvin - All Rights Reserved
*
* This program is free software available under the same license
* as the "OpenBSD" operating system, distributed at
@ -279,9 +279,14 @@ typedef int socklen_t;
void *xmalloc(size_t);
char *xstrdup(const char *);
#ifndef HAVE_BSD_SIGNAL
void (*bsd_signal(int, void (*)(int))) (int);
#ifndef HAVE_SIGHANDLER_T
typedef void (*sighandler_t)(int);
#endif
#ifndef SIG_ERR
#define SIG_ERR NULL
#endif
sighandler_t tftp_signal(int, sighandler_t);
#ifndef HAVE_DUP2
int dup2(int, int);
#endif

View file

@ -89,6 +89,9 @@ AC_CHECK_FUNCS(setreuid)
AC_CHECK_FUNCS(setregid)
AC_CHECK_FUNCS(initgroups)
AC_CHECK_FUNCS(setgroups)
AC_CHECK_FUNCS(sigaction)
AC_CHECK_FUNCS(bsd_signal)
AC_CHECK_TYPES(sighandler_t)
dnl Solaris 8 has [u]intmax_t but not strtoumax(). How utterly braindamaged.
AC_CHECK_FUNCS(strtoumax)
@ -120,7 +123,6 @@ OBJROOT=`pwd`
XTRA=false
PA_SEARCH_LIBS_AND_ADD(xmalloc, iberty)
PA_SEARCH_LIBS_AND_ADD(xstrdup, iberty)
PA_SEARCH_LIBS_AND_ADD(bsd_signal, bsd, bsdsignal)
PA_SEARCH_LIBS_AND_ADD(getopt_long, getopt, getopt_long)
PA_SEARCH_LIBS_AND_ADD(getaddrinfo, [nsl resolv])
if $pa_add_getaddrinfo

View file

@ -25,5 +25,3 @@ libxtra.a: $(LIBOBJS)
-rm -f libxtra.a
$(AR) libxtra.a $(LIBOBJS)
$(RANLIB) libxtra.a

View file

@ -1,27 +0,0 @@
/*
* bsdsignal.c
*
* Use sigaction() to simulate BSD signal()
*/
#include "config.h"
void (*bsd_signal(int signum, void (*handler) (int))) (int) {
struct sigaction action, oldaction;
memset(&action, 0, sizeof action);
action.sa_handler = handler;
sigemptyset(&action.sa_mask);
sigaddset(&action.sa_mask, signum);
action.sa_flags = SA_RESTART;
if (sigaction(signum, &action, &oldaction) == -1) {
#ifdef SIG_ERR
return SIG_ERR;
#else
return NULL;
#endif
}
return oldaction.sa_handler;
}

View file

@ -31,10 +31,13 @@
* SUCH DAMAGE.
*/
#ifndef RECVFILE_H
#define RECVFILE_H
#ifndef EXTERN_H
#define EXTERN_H
#include "config.h"
void tftp_recvfile(int, const char *, const char *);
void tftp_sendfile(int, const char *, const char *);
extern sigjmp_buf toplevel;
#endif

View file

@ -305,7 +305,7 @@ int main(int argc, char *argv[])
sp->s_proto = (char *)"udp";
}
bsd_signal(SIGINT, intr);
tftp_signal(SIGINT, intr);
if (peerargc) {
/* Set peer */
@ -768,7 +768,7 @@ void intr(int sig)
{
(void)sig; /* Quiet unused warning */
bsd_signal(SIGALRM, SIG_IGN);
tftp_signal(SIGALRM, SIG_IGN);
alarm(0);
siglongjmp(toplevel, -1);
}

View file

@ -48,8 +48,7 @@ extern int maxtimeout;
#define PKTSIZE SEGSIZE+4
char ackbuf[PKTSIZE];
int timeout;
sigjmp_buf toplevel;
sigjmp_buf timeoutbuf;
static sigjmp_buf timeoutbuf;
static void nak(int, const char *);
static int makerequest(int, const char *, struct tftphdr *, const char *);
@ -85,7 +84,7 @@ void tftp_sendfile(int fd, const char *name, const char *mode)
is_request = 1; /* First packet is the actual WRQ */
amount = 0;
bsd_signal(SIGALRM, timer);
tftp_signal(SIGALRM, timer);
do {
if (is_request) {
size = makerequest(WRQ, name, dp, mode) - 4;
@ -191,7 +190,7 @@ void tftp_recvfile(int fd, const char *name, const char *mode)
firsttrip = 1;
amount = 0;
bsd_signal(SIGALRM, timer);
tftp_signal(SIGALRM, timer);
do {
if (firsttrip) {
size = makerequest(RRQ, name, ap, mode);