diff options
author | Guy Harris <gharris@sonic.net> | 2023-05-05 04:32:51 -0700 |
---|---|---|
committer | Guy Harris <gharris@sonic.net> | 2023-05-05 15:07:11 -0700 |
commit | b10eefd47f979a339aaeb247bf47cc333aa7ba91 (patch) | |
tree | c32b76c498a7bd4f8a7c2385e36161f229ecd475 | |
parent | 3d9930b78ff77629d52129bedeceba12a2fe2088 (diff) | |
download | libpcap-b10eefd47f979a339aaeb247bf47cc333aa7ba91.tar.gz |
Make some static variables thread-local.
This should make some functions thread-safe, even if it doesn't make
them fully-reentrant; at least some of them would requiree API changes
to full reentrancy.
This should fix #1174.
-rw-r--r-- | Makefile.in | 1 | ||||
-rw-r--r-- | bpf_image.c | 4 | ||||
-rw-r--r-- | etherent.c | 4 | ||||
-rw-r--r-- | nametoaddr.c | 12 | ||||
-rw-r--r-- | pcap.c | 10 | ||||
-rw-r--r-- | thread-local.h | 69 |
6 files changed, 90 insertions, 10 deletions
diff --git a/Makefile.in b/Makefile.in index e4d14ea6..7421daa7 100644 --- a/Makefile.in +++ b/Makefile.in @@ -149,6 +149,7 @@ HDR = $(PUBHDR) \ sf-pcap.h \ sf-pcapng.h \ sunatmpos.h \ + thread-local.h \ varattrs.h GENHDR = \ diff --git a/bpf_image.c b/bpf_image.c index e48c76d5..febd07bb 100644 --- a/bpf_image.c +++ b/bpf_image.c @@ -44,6 +44,8 @@ #include "pcap-int.h" +#include "thread-local.h" + #ifdef HAVE_OS_PROTO_H #include "os-proto.h" #endif @@ -130,7 +132,7 @@ char * bpf_image(const struct bpf_insn *p, int n) { const char *op; - static char image[256]; + static thread_local char image[256]; char operand_buf[64]; const char *operand; @@ -33,6 +33,8 @@ #include <pcap/namedb.h> +#include "thread-local.h" + #ifdef HAVE_OS_PROTO_H #include "os-proto.h" #endif @@ -87,7 +89,7 @@ pcap_next_etherent(FILE *fp) u_char d; char *bp; size_t namesize; - static struct pcap_etherent e; + static thread_local struct pcap_etherent e; memset((char *)&e, 0, sizeof(e)); for (;;) { diff --git a/nametoaddr.c b/nametoaddr.c index a746cb6d..6f1dafe9 100644 --- a/nametoaddr.c +++ b/nametoaddr.c @@ -135,6 +135,8 @@ #include <pcap/namedb.h> #include "nametoaddr.h" +#include "thread-local.h" + #ifdef HAVE_OS_PROTO_H #include "os-proto.h" #endif @@ -806,16 +808,18 @@ pcap_ether_aton(const char *s) #ifndef HAVE_ETHER_HOSTTON /* * Roll our own. - * XXX - not thread-safe, because pcap_next_etherent() isn't thread- - * safe! Needs a mutex or a thread-safe pcap_next_etherent(). + * + * This should be thread-safe, as we define the static variables + * we use to be thread-local, and as pcap_next_etherent() does so + * as well. */ u_char * pcap_ether_hostton(const char *name) { register struct pcap_etherent *ep; register u_char *ap; - static FILE *fp = NULL; - static int init = 0; + static thread_local FILE *fp = NULL; + static thread_local int init = 0; if (!init) { fp = fopen(PCAP_ETHERS_FILE, "r"); @@ -65,6 +65,8 @@ struct rtentry; /* declarations in <net/if.h> */ #include "diag-control.h" +#include "thread-local.h" + #ifdef HAVE_OS_PROTO_H #include "os-proto.h" #endif @@ -3394,7 +3396,7 @@ pcap_datalink_val_to_description(int dlt) const char * pcap_datalink_val_to_description_or_dlt(int dlt) { - static char unkbuf[40]; + static thread_local char unkbuf[40]; const char *description; description = pcap_datalink_val_to_description(dlt); @@ -3660,7 +3662,7 @@ pcap_setnonblock_fd(pcap_t *p, int nonblock) const char * pcap_statustostr(int errnum) { - static char ebuf[15+10+1]; + static thread_local char ebuf[15+10+1]; switch (errnum) { @@ -3721,7 +3723,7 @@ pcap_strerror(int errnum) { #ifdef HAVE_STRERROR #ifdef _WIN32 - static char errbuf[PCAP_ERRBUF_SIZE]; + static thread_local char errbuf[PCAP_ERRBUF_SIZE]; errno_t err = strerror_s(errbuf, PCAP_ERRBUF_SIZE, errnum); if (err != 0) /* err = 0 if successful */ @@ -3733,7 +3735,7 @@ pcap_strerror(int errnum) #else extern int sys_nerr; extern const char *const sys_errlist[]; - static char errbuf[PCAP_ERRBUF_SIZE]; + static thread_local char errbuf[PCAP_ERRBUF_SIZE]; if ((unsigned int)errnum < sys_nerr) return ((char *)sys_errlist[errnum]); diff --git a/thread-local.h b/thread-local.h new file mode 100644 index 00000000..61296e56 --- /dev/null +++ b/thread-local.h @@ -0,0 +1,69 @@ +/* + * Copyright (c) 1994, 1995, 1996 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the Computer Systems + * Engineering Group at Lawrence Berkeley Laboratory. + * 4. Neither the name of the University nor of the Laboratory may be used + * to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef thread_local_h +#define thread_local_h + +/* + * This defines thread_local to specify thread-local storage, if it + * is not already defined. + * + * C11, if __STDC_NO_THREADS__ is not defined to be 1, defines + * _Thread_local to indicate thread-local storage. (You can also + * include <threads.h> to so define it, but we don't use any of + * the other stuff there.) + * + * Otherwise, we define it ourselves, based on the compiler. + * + * This is taken from https://stackoverflow.com/a/18298965/16139739. + */ +#ifndef thread_local + #if __STDC_VERSION__ >= 201112 && !defined __STDC_NO_THREADS__ + #define thread_local _Thread_local + #elif defined _WIN32 && ( \ + defined _MSC_VER || \ + defined __ICL || \ + defined __DMC__ || \ + defined __BORLANDC__ ) + #define thread_local __declspec(thread) + /* note that ICC (linux) and Clang are covered by __GNUC__ */ + #elif defined __GNUC__ || \ + defined __SUNPRO_C || \ + defined __xlC__ + #define thread_local __thread + #else + #error "Cannot define thread_local" + #endif +#endif + +#endif |