Skip to content

Commit

Permalink
Merge pull request #113 from jelu/thrsafe
Browse files Browse the repository at this point in the history
Use _r thread safe functions when possible
  • Loading branch information
jelu committed Jan 4, 2017
2 parents fd928a5 + 47b1e1a commit c02a170
Show file tree
Hide file tree
Showing 11 changed files with 194 additions and 39 deletions.
6 changes: 4 additions & 2 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ dsc_SOURCES = base64.c \
xmalloc.c \
inX_addr.c \
pcap-thread/pcap_thread.c \
parse_conf.c
parse_conf.c \
compat.c
dist_dsc_SOURCES = base64.h \
byteorder.h \
certain_qnames_index.h \
Expand Down Expand Up @@ -108,7 +109,8 @@ dist_dsc_SOURCES = base64.h \
pcap_layers/pcap_layers.h \
pcap-thread/pcap_thread.h \
parse_conf.h \
config_hooks.h
config_hooks.h \
compat.h
dsc_LDADD = $(PTHREAD_LIBS)
man1_MANS = dsc.1
man5_MANS = dsc.conf.5
Expand Down
68 changes: 68 additions & 0 deletions src/compat.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright (c) 2016-2017, OARC, Inc.
* Copyright (c) 2007, The Measurement Factory, Inc.
* Copyright (c) 2007, Internet Systems Consortium, Inc.
* 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. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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
* COPYRIGHT HOLDER 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.
*/

#include "config.h"

#include "compat.h"

#include <string.h>
#include <errno.h>

const char* dsc_strerror(int errnum, char* buf, size_t buflen) {
if (!buf || buflen < 2) {
return "dsc_strerror() invalid arguments";
}

memset(buf, 0, buflen);

#if (_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && ! _GNU_SOURCE
/* XSI-compliant version */
{
int ret = strerror_r(errnum, buf, buflen);
if (ret > 0) {
(void)strerror_r(ret, buf, buflen);
}
else {
(void)strerror_r(errno, buf, buflen);
}
}
#else
/* GNU-specific version */
buf = strerror_r(errnum, buf, buflen);
#endif

return buf;
}
44 changes: 44 additions & 0 deletions src/compat.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright (c) 2016-2017, OARC, Inc.
* Copyright (c) 2007, The Measurement Factory, Inc.
* Copyright (c) 2007, Internet Systems Consortium, Inc.
* 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. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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
* COPYRIGHT HOLDER 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 __dsc_compat_h
#define __dsc_compat_h

#include <stddef.h>

const char* dsc_strerror(int errnum, char* buf, size_t buflen);

#endif /* __dsc_compat_h */
23 changes: 17 additions & 6 deletions src/config_hooks.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
#include "syslog_debug.h"
#include "hashtbl.h"
#include "pcap.h"
#include "compat.h"

extern int promisc_flag;
extern int monitor_flag;
Expand Down Expand Up @@ -100,8 +101,9 @@ set_run_dir(const char *dir)
{
dsyslogf(LOG_INFO, "setting current directory to %s", dir);
if (chdir(dir) < 0) {
char errbuf[512];
perror(dir);
dsyslogf(LOG_ERR, "chdir: %s: %s", dir, strerror(errno));
dsyslogf(LOG_ERR, "chdir: %s: %s", dir, dsc_strerror(errno, errbuf, sizeof(errbuf)));
return 0;
}
return 1;
Expand Down Expand Up @@ -136,7 +138,8 @@ set_statistics_interval (const char *s)
dsyslogf(LOG_INFO, "Setting statistics interval to: %s", s);
statistics_interval = strtoull(s, NULL, 10);
if (statistics_interval == ULLONG_MAX) {
dsyslogf(LOG_ERR, "strtoull: %s", strerror(errno));
char errbuf[512];
dsyslogf(LOG_ERR, "strtoull: %s", dsc_strerror(errno, errbuf, sizeof(errbuf)));
return 0;
}
if (!statistics_interval) {
Expand Down Expand Up @@ -247,51 +250,59 @@ set_dump_reports_on_exit(void)
int
set_geoip_v4_dat(const char * dat, int options)
{
char errbuf[512];

geoip_v4_options = options;
if ( (geoip_v4_dat = strdup(dat)) ) {
dsyslogf(LOG_INFO, "GeoIP v4 dat %s %d", geoip_v4_dat, geoip_v4_options);
return 1;
}

dsyslogf(LOG_ERR, "unable to set GeoIP v4 dat, strdup: %s", strerror(errno));
dsyslogf(LOG_ERR, "unable to set GeoIP v4 dat, strdup: %s", dsc_strerror(errno, errbuf, sizeof(errbuf)));
return 0;
}

int
set_geoip_v6_dat(const char * dat, int options)
{
char errbuf[512];

geoip_v6_options = options;
if ( (geoip_v6_dat = strdup(dat)) ) {
dsyslogf(LOG_INFO, "GeoIP v6 dat %s %d", geoip_v6_dat, geoip_v6_options);
return 1;
}

dsyslogf(LOG_ERR, "unable to set GeoIP v6 dat, strdup: %s", strerror(errno));
dsyslogf(LOG_ERR, "unable to set GeoIP v6 dat, strdup: %s", dsc_strerror(errno, errbuf, sizeof(errbuf)));
return 0;
}

int
set_geoip_asn_v4_dat(const char * dat, int options)
{
char errbuf[512];

geoip_asn_v4_options = options;
if ( (geoip_asn_v4_dat = strdup(dat)) ) {
dsyslogf(LOG_INFO, "GeoIP ASN v4 dat %s %d", geoip_asn_v4_dat, geoip_asn_v4_options);
return 1;
}

dsyslogf(LOG_ERR, "unable to set GeoIP ASN v4 dat, strdup: %s", strerror(errno));
dsyslogf(LOG_ERR, "unable to set GeoIP ASN v4 dat, strdup: %s", dsc_strerror(errno, errbuf, sizeof(errbuf)));
return 0;
}

int
set_geoip_asn_v6_dat(const char * dat, int options)
{
char errbuf[512];

geoip_asn_v6_options = options;
if ( (geoip_asn_v6_dat = strdup(dat)) ) {
dsyslogf(LOG_INFO, "GeoIP ASN v6 dat %s %d", geoip_asn_v6_dat, geoip_asn_v6_options);
return 1;
}

dsyslogf(LOG_ERR, "unable to set GeoIP ASN v6 dat, strdup: %s", strerror(errno));
dsyslogf(LOG_ERR, "unable to set GeoIP ASN v6 dat, strdup: %s", dsc_strerror(errno, errbuf, sizeof(errbuf)));
return 0;
}
Loading

0 comments on commit c02a170

Please sign in to comment.