From: Otto Moerbeek Date: Mon, 17 Jun 2019 09:12:07 +0000 (+0200) Subject: errno review wip X-Git-Tag: dnsdist-1.4.0-rc3~39^2~9 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a2a81d42c33c392a14e369add5d8174dca0a5f30;p=thirdparty%2Fpdns.git errno review wip --- diff --git a/pdns/bpf-filter.cc b/pdns/bpf-filter.cc index e797a04c9b..ed5ba4727e 100644 --- a/pdns/bpf-filter.cc +++ b/pdns/bpf-filter.cc @@ -116,7 +116,7 @@ int bpf_prog_load(enum bpf_prog_type prog_type, return res; } } - throw std::runtime_error("Error loading BPF program: (" + std::string(strerror(errno)) + "):\n" + std::string(log_buf)); + throw std::runtime_error("Error loading BPF program: (" + stringerror() + "):\n" + std::string(log_buf)); } return res; } @@ -141,22 +141,22 @@ BPFFilter::BPFFilter(uint32_t maxV4Addresses, uint32_t maxV6Addresses, uint32_t { d_v4map.fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(uint32_t), sizeof(uint64_t), (int) maxV4Addresses); if (d_v4map.fd == -1) { - throw std::runtime_error("Error creating a BPF v4 map of size " + std::to_string(maxV4Addresses) + ": " + std::string(strerror(errno))); + throw std::runtime_error("Error creating a BPF v4 map of size " + std::to_string(maxV4Addresses) + ": " + stringerror()); } d_v6map.fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(struct KeyV6), sizeof(uint64_t), (int) maxV6Addresses); if (d_v6map.fd == -1) { - throw std::runtime_error("Error creating a BPF v6 map of size " + std::to_string(maxV6Addresses) + ": " + std::string(strerror(errno))); + throw std::runtime_error("Error creating a BPF v6 map of size " + std::to_string(maxV6Addresses) + ": " + stringerror()); } d_qnamemap.fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(struct QNameKey), sizeof(struct QNameValue), (int) maxQNames); if (d_qnamemap.fd == -1) { - throw std::runtime_error("Error creating a BPF qname map of size " + std::to_string(maxQNames) + ": " + std::string(strerror(errno))); + throw std::runtime_error("Error creating a BPF qname map of size " + std::to_string(maxQNames) + ": " + stringerror()); } d_filtermap.fd = bpf_create_map(BPF_MAP_TYPE_PROG_ARRAY, sizeof(uint32_t), sizeof(uint32_t), 1); if (d_filtermap.fd == -1) { - throw std::runtime_error("Error creating a BPF program map of size 1: " + std::string(strerror(errno))); + throw std::runtime_error("Error creating a BPF program map of size 1: " + stringerror()); } struct bpf_insn main_filter[] = { @@ -169,7 +169,7 @@ BPFFilter::BPFFilter(uint32_t maxV4Addresses, uint32_t maxV6Addresses, uint32_t "GPL", 0); if (d_mainfilter.fd == -1) { - throw std::runtime_error("Error loading BPF main filter: " + std::string(strerror(errno))); + throw std::runtime_error("Error loading BPF main filter: " + stringerror()); } struct bpf_insn qname_filter[] = { @@ -182,13 +182,13 @@ BPFFilter::BPFFilter(uint32_t maxV4Addresses, uint32_t maxV6Addresses, uint32_t "GPL", 0); if (d_qnamefilter.fd == -1) { - throw std::runtime_error("Error loading BPF qname filter: " + std::string(strerror(errno))); + throw std::runtime_error("Error loading BPF qname filter: " + stringerror()); } uint32_t key = 0; int res = bpf_update_elem(d_filtermap.fd, &key, &d_qnamefilter.fd, BPF_ANY); if (res != 0) { - throw std::runtime_error("Error updating BPF filters map: " + std::string(strerror(errno))); + throw std::runtime_error("Error updating BPF filters map: " + stringrerror()); } } @@ -197,7 +197,7 @@ void BPFFilter::addSocket(int sock) int res = setsockopt(sock, SOL_SOCKET, SO_ATTACH_BPF, &d_mainfilter.fd, sizeof(d_mainfilter.fd)); if (res != 0) { - throw std::runtime_error("Error attaching BPF filter to this socket: " + std::string(strerror(errno))); + throw std::runtime_error("Error attaching BPF filter to this socket: " + stringerror()); } } @@ -206,7 +206,7 @@ void BPFFilter::removeSocket(int sock) int res = setsockopt(sock, SOL_SOCKET, SO_DETACH_BPF, &d_mainfilter.fd, sizeof(d_mainfilter.fd)); if (res != 0) { - throw std::runtime_error("Error detaching BPF filter from this socket: " + std::string(strerror(errno))); + throw std::runtime_error("Error detaching BPF filter from this socket: " + stringerror()); } } @@ -255,7 +255,7 @@ void BPFFilter::block(const ComboAddress& addr) } if (res != 0) { - throw std::runtime_error("Error adding blocked address " + addr.toString() + ": " + std::string(strerror(errno))); + throw std::runtime_error("Error adding blocked address " + addr.toString() + ": " + stringerror()); } } @@ -285,7 +285,7 @@ void BPFFilter::unblock(const ComboAddress& addr) } if (res != 0) { - throw std::runtime_error("Error removing blocked address " + addr.toString() + ": " + std::string(strerror(errno))); + throw std::runtime_error("Error removing blocked address " + addr.toString() + ": " + stringerror()); } } diff --git a/pdns/capabilities.cc b/pdns/capabilities.cc index c269099a31..0abb742a3c 100644 --- a/pdns/capabilities.cc +++ b/pdns/capabilities.cc @@ -40,7 +40,7 @@ void dropCapabilities() if (cap_set_proc(caps) != 0) { cap_free(caps); - throw std::runtime_error("Unable to drop capabilities: " + std::string(strerror(errno))); + throw std::runtime_error("Unable to drop capabilities: " + stringerror()); } cap_free(caps); diff --git a/pdns/dns_random.cc b/pdns/dns_random.cc index 48b910c8f4..79d0d4156d 100644 --- a/pdns/dns_random.cc +++ b/pdns/dns_random.cc @@ -172,14 +172,14 @@ static void dns_random_setup(bool force=false) if (chosen_rng == RNG_URANDOM) { urandom_fd = open(rdev.c_str(), O_RDONLY); if (urandom_fd == -1) - throw std::runtime_error("Cannot open " + rdev + ": " + std::string(strerror(errno))); + throw std::runtime_error("Cannot open " + rdev + ": " + stringerror()); } #if defined(HAVE_KISS_RNG) if (chosen_rng == RNG_KISS) { unsigned int seed; urandom_fd = open(rdev.c_str(), O_RDONLY); if (urandom_fd == -1) - throw std::runtime_error("Cannot open " + rdev + ": " + std::string(strerror(errno))); + throw std::runtime_error("Cannot open " + rdev + ": " + stringerror()); if (read(urandom_fd, &seed, sizeof(seed)) < 0) { (void)close(urandom_fd); throw std::runtime_error("Cannot read random device"); @@ -264,7 +264,7 @@ uint32_t dns_random(uint32_t upper_bound) { uint32_t num = 0; do { if (getrandom(&num, sizeof(num), 0) != sizeof(num)) - throw std::runtime_error("getrandom() failed: " + std::string(strerror(errno))); + throw std::runtime_error("getrandom() failed: " + stringerror()); } while(num < min); diff --git a/pdns/dnsdist-lua-actions.cc b/pdns/dnsdist-lua-actions.cc index 2943ad3189..849a2b8bab 100644 --- a/pdns/dnsdist-lua-actions.cc +++ b/pdns/dnsdist-lua-actions.cc @@ -561,7 +561,7 @@ public: else d_fp = fopen(str.c_str(), "w"); if(!d_fp) - throw std::runtime_error("Unable to open file '"+str+"' for logging: "+std::string(strerror(errno))); + throw std::runtime_error("Unable to open file '"+str+"' for logging: "+stringerror()); if(!buffered) setbuf(d_fp, 0); } diff --git a/pdns/dnsdist-lua-bindings.cc b/pdns/dnsdist-lua-bindings.cc index d29081e100..93caad5912 100644 --- a/pdns/dnsdist-lua-bindings.cc +++ b/pdns/dnsdist-lua-bindings.cc @@ -355,7 +355,7 @@ void setupLuaBindings(bool client) int fd = open(fname.c_str(), O_CREAT | O_EXCL | O_WRONLY, 0660); if (fd < 0) { - g_outputBuffer = "Error opening dump file for writing: " + string(strerror(errno)) + "\n"; + g_outputBuffer = "Error opening dump file for writing: " + stringerror() + "\n"; return; } diff --git a/pdns/iputils.cc b/pdns/iputils.cc index f58d0e31b2..2010a6e5f9 100644 --- a/pdns/iputils.cc +++ b/pdns/iputils.cc @@ -352,10 +352,11 @@ size_t sendMsgWithOptions(int fd, const char* buffer, size_t len, const ComboAdd return res; } else if (res == -1) { - if (errno == EINTR) { + int err = errno; + if (err == EINTR) { continue; } - else if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINPROGRESS || errno == ENOTCONN) { + else if (err == EAGAIN || err == EWOULDBLOCK || err == EINPROGRESS || err == ENOTCONN) { /* EINPROGRESS might happen with non blocking socket, especially with TCP Fast Open */ return sent; diff --git a/pdns/lock.hh b/pdns/lock.hh index 3529ee3ae3..895d346e06 100644 --- a/pdns/lock.hh +++ b/pdns/lock.hh @@ -68,8 +68,7 @@ public: int err; if((err = pthread_rwlock_wrlock(d_lock))) { - errno = err; - throw PDNSException("error acquiring rwlock wrlock: "+stringerror()); + throw PDNSException("error acquiring rwlock wrlock: "+stringerror(err)); } } ~WriteLock() @@ -109,8 +108,7 @@ public: d_havelock=false; int err; if((err = pthread_rwlock_trywrlock(d_lock)) && err!=EBUSY) { - errno = err; - throw PDNSException("error acquiring rwlock tryrwlock: "+stringerror()); + throw PDNSException("error acquiring rwlock tryrwlock: "+stringerror(err)); } d_havelock=(err==0); } @@ -158,8 +156,7 @@ public: int err; if((err = pthread_rwlock_tryrdlock(d_lock)) && err!=EBUSY) { - errno = err; - throw PDNSException("error acquiring rwlock tryrdlock: "+stringerror()); + throw PDNSException("error acquiring rwlock tryrdlock: "+stringerror(err)); } d_havelock=(err==0); } @@ -201,8 +198,7 @@ public: int err; if((err = pthread_rwlock_rdlock(d_lock))) { - errno = err; - throw PDNSException("error acquiring rwlock readlock: "+stringerror()); + throw PDNSException("error acquiring rwlock readlock: "+stringerror(err)); } } ~ReadLock() diff --git a/pdns/misc.cc b/pdns/misc.cc index 1f90bbcbd1..027856dfc1 100644 --- a/pdns/misc.cc +++ b/pdns/misc.cc @@ -525,14 +525,14 @@ string bitFlip(const string &str) return ret; } -string stringerror() +string stringerror(int err) { - return strerror(errno); + return strerror(err); } -string netstringerror() +string stringerror() { - return stringerror(); + return strerror(errno); } void cleanSlashes(string &str) diff --git a/pdns/misc.hh b/pdns/misc.hh index ee255e9b90..7aeb70e312 100644 --- a/pdns/misc.hh +++ b/pdns/misc.hh @@ -20,7 +20,6 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ #pragma once -#include #include #include #include @@ -155,8 +154,8 @@ const string toLower(const string &upper); const string toLowerCanonic(const string &upper); bool IpToU32(const string &str, uint32_t *ip); string U32ToIP(uint32_t); +string stringerror(int); string stringerror(); -string netstringerror(); string itoa(int i); string uitoa(unsigned int i); string bitFlip(const string &str); @@ -283,7 +282,7 @@ inline double getTime() inline void unixDie(const string &why) { - throw runtime_error(why+": "+strerror(errno)); + throw runtime_error(why+": "+stringerror()); } string makeHexDump(const string& str); diff --git a/pdns/pdns_recursor.cc b/pdns/pdns_recursor.cc index 491ae26bd9..cd4ed89e8b 100644 --- a/pdns/pdns_recursor.cc +++ b/pdns/pdns_recursor.cc @@ -513,8 +513,10 @@ static void setSocketBuffer(int fd, int optname, uint32_t size) return; } - if (setsockopt(fd, SOL_SOCKET, optname, (char*)&size, sizeof(size)) < 0 ) - g_log<d_socket)) { addCMsgSrcAddr(&msgh, cbuf, &dc->d_local, 0); } - if(sendmsg(dc->d_socket, &msgh, 0) < 0 && g_logCommonErrors) - g_log<getRemote()<<" failed with: "<d_socket, &msgh, 0) < 0 && g_logCommonErrors) { + int err = errno; + g_log << Logger::Warning << "Sending UDP reply to client " << dc->getRemote() << " failed with: " + << strerror(err) << endl; + } if(variableAnswer || sr.wasVariable()) { g_stats.variableResponses++; @@ -1695,9 +1703,10 @@ static void startDoResolve(void *p) if(wret == 0) g_log<getRemote()<getRemote()<<": "<< strerror(errno) <getRemote() << ": " << strerror(err) << endl; + } else if((unsigned int)wret != 2 + packet.size()) g_log<getRemote()<<" for "<d_mdp.d_qname<<" (size="<< (2 + packet.size()) <<", sent "<= sizeof(struct dnsheader)) { struct dnsheader tmpdh; memcpy(&tmpdh, response.c_str(), sizeof(tmpdh)); @@ -2557,7 +2569,8 @@ static void makeTCPServerSockets(deferredAdd_t& deferredAdds, std::set& tcp exit(1); } if(sin.sin6.sin6_family == AF_INET6 && setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &tmp, sizeof(tmp)) < 0) { - g_log<& tcp #ifdef TCP_FASTOPEN int fastOpenQueueSize = ::arg().asNum("tcp-fast-open"); if (setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &fastOpenQueueSize, sizeof fastOpenQueueSize) < 0) { - g_log< 0) { if (!setPipeBufferSize(threadInfos.pipes.writeQueriesToThread, pipeBufferSize)) { - g_log< 0) { g_log<(boost::bind(pleaseDumpNSSpeeds, fd)); @@ -273,7 +273,7 @@ static string doDumpCache(T begin, T end) int fd=open(fname.c_str(), O_CREAT | O_EXCL | O_WRONLY, 0660); if(fd < 0) - return "Error opening dump file for writing: "+string(strerror(errno))+"\n"; + return "Error opening dump file for writing: "+stringerror()+"\n"; uint64_t total = 0; try { total = broadcastAccFunction(boost::bind(pleaseDump, fd)); @@ -295,7 +295,7 @@ static string doDumpEDNSStatus(T begin, T end) int fd=open(fname.c_str(), O_CREAT | O_EXCL | O_WRONLY, 0660); if(fd < 0) - return "Error opening dump file for writing: "+string(strerror(errno))+"\n"; + return "Error opening dump file for writing: "+stringerror()+"\n"; uint64_t total = 0; try { total = broadcastAccFunction(boost::bind(pleaseDumpEDNSMap, fd)); @@ -331,13 +331,13 @@ static string doDumpRPZ(T begin, T end) int fd = open(fname.c_str(), O_CREAT | O_EXCL | O_WRONLY, 0660); if(fd < 0) { - return "Error opening dump file for writing: "+string(strerror(errno))+"\n"; + return "Error opening dump file for writing: "+stringerror()+"\n"; } auto fp = std::unique_ptr(fdopen(fd, "w"), fclose); if (!fp) { close(fd); - return "Error converting file descriptor: "+string(strerror(errno))+"\n"; + return "Error converting file descriptor: "+stringerror()+"\n"; } zone->dump(fp.get()); @@ -356,7 +356,7 @@ static string doDumpThrottleMap(T begin, T end) int fd=open(fname.c_str(), O_CREAT | O_EXCL | O_WRONLY, 0660); if(fd < 0) - return "Error opening dump file for writing: "+string(strerror(errno))+"\n"; + return "Error opening dump file for writing: "+stringerror()+"\n"; uint64_t total = 0; try { total = broadcastAccFunction(boost::bind(pleaseDumpThrottleMap, fd));