diff --git a/common/Makefile b/common/Makefile index a825213..2aaffb7 100644 --- a/common/Makefile +++ b/common/Makefile @@ -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) diff --git a/common/signal.c b/common/signal.c new file mode 100644 index 0000000..9119d57 --- /dev/null +++ b/common/signal.c @@ -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 diff --git a/config.h b/config.h index 8c9af73..a6a402b 100644 --- a/config.h +++ b/config.h @@ -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 diff --git a/configure.ac b/configure.ac index 9a467b3..04b6f98 100644 --- a/configure.ac +++ b/configure.ac @@ -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 diff --git a/lib/Makefile b/lib/Makefile index a43ce19..5dd83d0 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -25,5 +25,3 @@ libxtra.a: $(LIBOBJS) -rm -f libxtra.a $(AR) libxtra.a $(LIBOBJS) $(RANLIB) libxtra.a - - diff --git a/lib/bsdsignal.c b/lib/bsdsignal.c deleted file mode 100644 index 0aae136..0000000 --- a/lib/bsdsignal.c +++ /dev/null @@ -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; -} diff --git a/tftp/extern.h b/tftp/extern.h index 78474fc..9c578e6 100644 --- a/tftp/extern.h +++ b/tftp/extern.h @@ -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 diff --git a/tftp/main.c b/tftp/main.c index b2f9059..438f48a 100644 --- a/tftp/main.c +++ b/tftp/main.c @@ -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); } diff --git a/tftp/tftp.c b/tftp/tftp.c index d15da22..aecdeb9 100644 --- a/tftp/tftp.c +++ b/tftp/tftp.c @@ -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);