Commit 5ae9ed9e authored by Phil Willoughby's avatar Phil Willoughby Committed by Facebook Github Bot

thread safe errno->string

Summary: `strerror` isn't thread safe, `folly::errnoStr` is.

Reviewed By: yfeldblum

Differential Revision: D9036249

fbshipit-source-id: da4ad1089f4319e62b0273ec12b4950624105a77
parent c3440ba2
...@@ -442,7 +442,7 @@ string IPAddressV6::str() const { ...@@ -442,7 +442,7 @@ string IPAddressV6::str() const {
throw IPAddressFormatException(sformat( throw IPAddressFormatException(sformat(
"Invalid address with hex '{}' with error {}", "Invalid address with hex '{}' with error {}",
detail::Bytes::toHex(bytes(), 16), detail::Bytes::toHex(bytes(), 16),
strerror(errno))); errnoStr(errno)));
} }
auto scopeId = getScopeId(); auto scopeId = getScopeId();
......
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
#include <folly/executors/thread_factory/ThreadFactory.h> #include <folly/executors/thread_factory/ThreadFactory.h>
#include <folly/String.h>
#include <folly/portability/SysResource.h> #include <folly/portability/SysResource.h>
#include <folly/portability/SysTime.h> #include <folly/portability/SysTime.h>
...@@ -46,7 +47,7 @@ class PriorityThreadFactory : public ThreadFactory { ...@@ -46,7 +47,7 @@ class PriorityThreadFactory : public ThreadFactory {
return factory_->newThread([priority, func = std::move(func)]() mutable { return factory_->newThread([priority, func = std::move(func)]() mutable {
if (setpriority(PRIO_PROCESS, 0, priority) != 0) { if (setpriority(PRIO_PROCESS, 0, priority) != 0) {
LOG(ERROR) << "setpriority failed (are you root?) with error " << errno, LOG(ERROR) << "setpriority failed (are you root?) with error " << errno,
strerror(errno); errnoStr(errno);
} }
func(); func();
}); });
......
...@@ -40,6 +40,7 @@ namespace fs = folly::fs; ...@@ -40,6 +40,7 @@ namespace fs = folly::fs;
using folly::AsyncIO; using folly::AsyncIO;
using folly::AsyncIOOp; using folly::AsyncIOOp;
using folly::AsyncIOQueue; using folly::AsyncIOQueue;
using folly::errnoStr;
namespace { namespace {
...@@ -126,7 +127,7 @@ typedef std::unique_ptr<char, void (*)(void*)> ManagedBuffer; ...@@ -126,7 +127,7 @@ typedef std::unique_ptr<char, void (*)(void*)> ManagedBuffer;
ManagedBuffer allocateAligned(size_t size) { ManagedBuffer allocateAligned(size_t size) {
void* buf; void* buf;
int rc = posix_memalign(&buf, kAlign, size); int rc = posix_memalign(&buf, kAlign, size);
CHECK_EQ(rc, 0) << strerror(rc); CHECK_EQ(rc, 0) << errnoStr(rc);
return ManagedBuffer(reinterpret_cast<char*>(buf), free); return ManagedBuffer(reinterpret_cast<char*>(buf), free);
} }
......
...@@ -760,7 +760,7 @@ void AsyncServerSocket::setupSocket(int fd, int family) { ...@@ -760,7 +760,7 @@ void AsyncServerSocket::setupSocket(int fd, int family) {
if (reusePortEnabled_ && if (reusePortEnabled_ &&
setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(int)) != 0) { setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(int)) != 0) {
LOG(ERROR) << "failed to set SO_REUSEPORT on async server socket " LOG(ERROR) << "failed to set SO_REUSEPORT on async server socket "
<< strerror(errno); << errnoStr(errno);
#ifdef WIN32 #ifdef WIN32
folly::throwSystemError(errno, "failed to bind to the async server socket"); folly::throwSystemError(errno, "failed to bind to the async server socket");
#else #else
...@@ -779,13 +779,13 @@ void AsyncServerSocket::setupSocket(int fd, int family) { ...@@ -779,13 +779,13 @@ void AsyncServerSocket::setupSocket(int fd, int family) {
(keepAliveEnabled_) ? &one : &zero, (keepAliveEnabled_) ? &one : &zero,
sizeof(int)) != 0) { sizeof(int)) != 0) {
LOG(ERROR) << "failed to set SO_KEEPALIVE on async server socket: " LOG(ERROR) << "failed to set SO_KEEPALIVE on async server socket: "
<< strerror(errno); << errnoStr(errno);
} }
// Setup FD_CLOEXEC flag // Setup FD_CLOEXEC flag
if (closeOnExec_ && (-1 == folly::setCloseOnExec(fd, closeOnExec_))) { if (closeOnExec_ && (-1 == folly::setCloseOnExec(fd, closeOnExec_))) {
LOG(ERROR) << "failed to set FD_CLOEXEC on async server socket: " LOG(ERROR) << "failed to set FD_CLOEXEC on async server socket: "
<< strerror(errno); << errnoStr(errno);
} }
// Set TCP nodelay if available, MAC OS X Hack // Set TCP nodelay if available, MAC OS X Hack
...@@ -795,7 +795,7 @@ void AsyncServerSocket::setupSocket(int fd, int family) { ...@@ -795,7 +795,7 @@ void AsyncServerSocket::setupSocket(int fd, int family) {
if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one)) != 0) { if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one)) != 0) {
// This isn't a fatal error; just log an error message and continue // This isn't a fatal error; just log an error message and continue
LOG(ERROR) << "failed to set TCP_NODELAY on async server socket: " LOG(ERROR) << "failed to set TCP_NODELAY on async server socket: "
<< strerror(errno); << errnoStr(errno);
} }
} }
#else #else
......
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
#pragma once #pragma once
#include <folly/SocketAddress.h> #include <folly/SocketAddress.h>
#include <folly/String.h>
#include <folly/io/ShutdownSocketSet.h> #include <folly/io/ShutdownSocketSet.h>
#include <folly/io/async/AsyncSocketBase.h> #include <folly/io/async/AsyncSocketBase.h>
#include <folly/io/async/AsyncTimeout.h> #include <folly/io/async/AsyncTimeout.h>
...@@ -624,7 +625,7 @@ class AsyncServerSocket : public DelayedDestruction, public AsyncSocketBase { ...@@ -624,7 +625,7 @@ class AsyncServerSocket : public DelayedDestruction, public AsyncSocketBase {
handler.socket_, SOL_SOCKET, SO_KEEPALIVE, &val, sizeof(val)) != handler.socket_, SOL_SOCKET, SO_KEEPALIVE, &val, sizeof(val)) !=
0) { 0) {
LOG(ERROR) << "failed to set SO_KEEPALIVE on async server socket: %s" LOG(ERROR) << "failed to set SO_KEEPALIVE on async server socket: %s"
<< strerror(errno); << errnoStr(errno);
} }
} }
} }
......
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
#include <folly/Format.h> #include <folly/Format.h>
#include <folly/Portability.h> #include <folly/Portability.h>
#include <folly/SocketAddress.h> #include <folly/SocketAddress.h>
#include <folly/String.h>
#include <folly/io/Cursor.h> #include <folly/io/Cursor.h>
#include <folly/io/IOBuf.h> #include <folly/io/IOBuf.h>
#include <folly/io/IOBufQueue.h> #include <folly/io/IOBufQueue.h>
...@@ -1585,7 +1586,7 @@ int AsyncSocket::setNoDelay(bool noDelay) { ...@@ -1585,7 +1586,7 @@ int AsyncSocket::setNoDelay(bool noDelay) {
int errnoCopy = errno; int errnoCopy = errno;
VLOG(2) << "failed to update TCP_NODELAY option on AsyncSocket " << this VLOG(2) << "failed to update TCP_NODELAY option on AsyncSocket " << this
<< " (fd=" << fd_ << ", state=" << state_ << " (fd=" << fd_ << ", state=" << state_
<< "): " << strerror(errnoCopy); << "): " << errnoStr(errnoCopy);
return errnoCopy; return errnoCopy;
} }
...@@ -1612,7 +1613,7 @@ int AsyncSocket::setCongestionFlavor(const std::string& cname) { ...@@ -1612,7 +1613,7 @@ int AsyncSocket::setCongestionFlavor(const std::string& cname) {
int errnoCopy = errno; int errnoCopy = errno;
VLOG(2) << "failed to update TCP_CONGESTION option on AsyncSocket " << this VLOG(2) << "failed to update TCP_CONGESTION option on AsyncSocket " << this
<< "(fd=" << fd_ << ", state=" << state_ << "(fd=" << fd_ << ", state=" << state_
<< "): " << strerror(errnoCopy); << "): " << errnoStr(errnoCopy);
return errnoCopy; return errnoCopy;
} }
...@@ -1633,7 +1634,7 @@ int AsyncSocket::setQuickAck(bool quickack) { ...@@ -1633,7 +1634,7 @@ int AsyncSocket::setQuickAck(bool quickack) {
int errnoCopy = errno; int errnoCopy = errno;
VLOG(2) << "failed to update TCP_QUICKACK option on AsyncSocket" << this VLOG(2) << "failed to update TCP_QUICKACK option on AsyncSocket" << this
<< "(fd=" << fd_ << ", state=" << state_ << "(fd=" << fd_ << ", state=" << state_
<< "): " << strerror(errnoCopy); << "): " << errnoStr(errnoCopy);
return errnoCopy; return errnoCopy;
} }
...@@ -1654,7 +1655,7 @@ int AsyncSocket::setSendBufSize(size_t bufsize) { ...@@ -1654,7 +1655,7 @@ int AsyncSocket::setSendBufSize(size_t bufsize) {
int errnoCopy = errno; int errnoCopy = errno;
VLOG(2) << "failed to update SO_SNDBUF option on AsyncSocket" << this VLOG(2) << "failed to update SO_SNDBUF option on AsyncSocket" << this
<< "(fd=" << fd_ << ", state=" << state_ << "(fd=" << fd_ << ", state=" << state_
<< "): " << strerror(errnoCopy); << "): " << errnoStr(errnoCopy);
return errnoCopy; return errnoCopy;
} }
...@@ -1672,7 +1673,7 @@ int AsyncSocket::setRecvBufSize(size_t bufsize) { ...@@ -1672,7 +1673,7 @@ int AsyncSocket::setRecvBufSize(size_t bufsize) {
int errnoCopy = errno; int errnoCopy = errno;
VLOG(2) << "failed to update SO_RCVBUF option on AsyncSocket" << this VLOG(2) << "failed to update SO_RCVBUF option on AsyncSocket" << this
<< "(fd=" << fd_ << ", state=" << state_ << "(fd=" << fd_ << ", state=" << state_
<< "): " << strerror(errnoCopy); << "): " << errnoStr(errnoCopy);
return errnoCopy; return errnoCopy;
} }
...@@ -1690,7 +1691,7 @@ int AsyncSocket::setTCPProfile(int profd) { ...@@ -1690,7 +1691,7 @@ int AsyncSocket::setTCPProfile(int profd) {
int errnoCopy = errno; int errnoCopy = errno;
VLOG(2) << "failed to set socket namespace option on AsyncSocket" << this VLOG(2) << "failed to set socket namespace option on AsyncSocket" << this
<< "(fd=" << fd_ << ", state=" << state_ << "(fd=" << fd_ << ", state=" << state_
<< "): " << strerror(errnoCopy); << "): " << errnoStr(errnoCopy);
return errnoCopy; return errnoCopy;
} }
......
...@@ -26,6 +26,7 @@ ...@@ -26,6 +26,7 @@
#include <thread> #include <thread>
#include <folly/Memory.h> #include <folly/Memory.h>
#include <folly/String.h>
#include <folly/io/async/NotificationQueue.h> #include <folly/io/async/NotificationQueue.h>
#include <folly/io/async/VirtualEventBase.h> #include <folly/io/async/VirtualEventBase.h>
#include <folly/portability/Unistd.h> #include <folly/portability/Unistd.h>
...@@ -721,7 +722,7 @@ bool EventBase::scheduleTimeout( ...@@ -721,7 +722,7 @@ bool EventBase::scheduleTimeout(
DCHECK(ev->ev_base); DCHECK(ev->ev_base);
if (event_add(ev, &tv) < 0) { if (event_add(ev, &tv) < 0) {
LOG(ERROR) << "EventBase: failed to schedule timeout: " << strerror(errno); LOG(ERROR) << "EventBase: failed to schedule timeout: " << errnoStr(errno);
return false; return false;
} }
......
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
*/ */
#include <folly/io/async/EventHandler.h> #include <folly/io/async/EventHandler.h>
#include <folly/String.h>
#include <folly/io/async/EventBase.h> #include <folly/io/async/EventBase.h>
#include <assert.h> #include <assert.h>
...@@ -90,7 +91,7 @@ bool EventHandler::registerImpl(uint16_t events, bool internal) { ...@@ -90,7 +91,7 @@ bool EventHandler::registerImpl(uint16_t events, bool internal) {
// more space). // more space).
if (event_add(&event_, nullptr) < 0) { if (event_add(&event_, nullptr) < 0) {
LOG(ERROR) << "EventBase: failed to register event handler for fd " LOG(ERROR) << "EventBase: failed to register event handler for fd "
<< event_.ev_fd << ": " << strerror(errno); << event_.ev_fd << ": " << errnoStr(errno);
// Call event_del() to make sure the event is completely uninstalled // Call event_del() to make sure the event is completely uninstalled
event_del(&event_); event_del(&event_);
return false; return false;
......
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
#include <folly/io/async/test/AsyncSSLSocketTest.h> #include <folly/io/async/test/AsyncSSLSocketTest.h>
#include <folly/SocketAddress.h> #include <folly/SocketAddress.h>
#include <folly/String.h>
#include <folly/io/Cursor.h> #include <folly/io/Cursor.h>
#include <folly/io/async/AsyncPipe.h> #include <folly/io/async/AsyncPipe.h>
#include <folly/io/async/AsyncSSLSocket.h> #include <folly/io/async/AsyncSSLSocket.h>
...@@ -109,17 +110,17 @@ constexpr size_t SSLClient::kMaxReadsPerEvent; ...@@ -109,17 +110,17 @@ constexpr size_t SSLClient::kMaxReadsPerEvent;
void getfds(int fds[2]) { void getfds(int fds[2]) {
if (socketpair(PF_LOCAL, SOCK_STREAM, 0, fds) != 0) { if (socketpair(PF_LOCAL, SOCK_STREAM, 0, fds) != 0) {
FAIL() << "failed to create socketpair: " << strerror(errno); FAIL() << "failed to create socketpair: " << errnoStr(errno);
} }
for (int idx = 0; idx < 2; ++idx) { for (int idx = 0; idx < 2; ++idx) {
int flags = fcntl(fds[idx], F_GETFL, 0); int flags = fcntl(fds[idx], F_GETFL, 0);
if (flags == -1) { if (flags == -1) {
FAIL() << "failed to get flags for socket " << idx << ": " FAIL() << "failed to get flags for socket " << idx << ": "
<< strerror(errno); << errnoStr(errno);
} }
if (fcntl(fds[idx], F_SETFL, flags | O_NONBLOCK) != 0) { if (fcntl(fds[idx], F_SETFL, flags | O_NONBLOCK) != 0) {
FAIL() << "failed to put socket " << idx FAIL() << "failed to put socket " << idx
<< " in non-blocking mode: " << strerror(errno); << " in non-blocking mode: " << errnoStr(errno);
} }
} }
} }
......
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
#include <folly/Conv.h> #include <folly/Conv.h>
#include <folly/SocketAddress.h> #include <folly/SocketAddress.h>
#include <folly/String.h>
#include <folly/io/IOBuf.h> #include <folly/io/IOBuf.h>
#include <folly/io/async/AsyncTimeout.h> #include <folly/io/async/AsyncTimeout.h>
#include <folly/io/async/AsyncUDPServerSocket.h> #include <folly/io/async/AsyncUDPServerSocket.h>
...@@ -29,6 +30,7 @@ ...@@ -29,6 +30,7 @@
using folly::AsyncTimeout; using folly::AsyncTimeout;
using folly::AsyncUDPServerSocket; using folly::AsyncUDPServerSocket;
using folly::AsyncUDPSocket; using folly::AsyncUDPSocket;
using folly::errnoStr;
using folly::EventBase; using folly::EventBase;
using folly::IOBuf; using folly::IOBuf;
using folly::SocketAddress; using folly::SocketAddress;
...@@ -537,7 +539,7 @@ TEST_F(AsyncUDPSocketTest, TestErrToNonExistentServer) { ...@@ -537,7 +539,7 @@ TEST_F(AsyncUDPSocketTest, TestErrToNonExistentServer) {
CMSG_DATA(&cmsg)); CMSG_DATA(&cmsg));
errRecvd = errRecvd =
(serr->ee_origin == SO_EE_ORIGIN_ICMP || SO_EE_ORIGIN_ICMP6); (serr->ee_origin == SO_EE_ORIGIN_ICMP || SO_EE_ORIGIN_ICMP6);
LOG(ERROR) << "errno " << strerror(serr->ee_errno); LOG(ERROR) << "errno " << errnoStr(serr->ee_errno);
} }
evb_.terminateLoopSoon(); evb_.terminateLoopSoon();
})); }));
......
...@@ -29,17 +29,17 @@ namespace folly { ...@@ -29,17 +29,17 @@ namespace folly {
void getfds(int fds[2]) { void getfds(int fds[2]) {
if (socketpair(PF_LOCAL, SOCK_STREAM, 0, fds) != 0) { if (socketpair(PF_LOCAL, SOCK_STREAM, 0, fds) != 0) {
LOG(ERROR) << "failed to create socketpair: " << strerror(errno); LOG(ERROR) << "failed to create socketpair: " << errnoStr(errno);
} }
for (int idx = 0; idx < 2; ++idx) { for (int idx = 0; idx < 2; ++idx) {
int flags = fcntl(fds[idx], F_GETFL, 0); int flags = fcntl(fds[idx], F_GETFL, 0);
if (flags == -1) { if (flags == -1) {
LOG(ERROR) << "failed to get flags for socket " << idx << ": " LOG(ERROR) << "failed to get flags for socket " << idx << ": "
<< strerror(errno); << errnoStr(errno);
} }
if (fcntl(fds[idx], F_SETFL, flags | O_NONBLOCK) != 0) { if (fcntl(fds[idx], F_SETFL, flags | O_NONBLOCK) != 0) {
LOG(ERROR) << "failed to put socket " << idx LOG(ERROR) << "failed to put socket " << idx
<< " in non-blocking mode: " << strerror(errno); << " in non-blocking mode: " << errnoStr(errno);
} }
} }
} }
......
...@@ -33,6 +33,7 @@ ...@@ -33,6 +33,7 @@
#include <folly/Conv.h> #include <folly/Conv.h>
#include <folly/ScopeGuard.h> #include <folly/ScopeGuard.h>
#include <folly/String.h>
#include <folly/portability/Unistd.h> #include <folly/portability/Unistd.h>
#include <folly/system/ThreadId.h> #include <folly/system/ThreadId.h>
...@@ -73,7 +74,7 @@ static int64_t determineSchedstatUnits() { ...@@ -73,7 +74,7 @@ static int64_t determineSchedstatUnits() {
struct utsname unameInfo; struct utsname unameInfo;
if (uname(&unameInfo) != 0) { if (uname(&unameInfo) != 0) {
LOG(ERROR) << "unable to determine jiffies/second: uname failed: %s" LOG(ERROR) << "unable to determine jiffies/second: uname failed: %s"
<< strerror(errno); << errnoStr(errno);
return -1; return -1;
} }
......
...@@ -675,10 +675,7 @@ TEST(SocketAddress, AnonymousUnix) { ...@@ -675,10 +675,7 @@ TEST(SocketAddress, AnonymousUnix) {
#define REQUIRE_ERRNO(cond, msg) \ #define REQUIRE_ERRNO(cond, msg) \
if (!(cond)) { \ if (!(cond)) { \
int _requireErrnoCopy_ = errno; \ ADD_FAILURE() << (msg) << ": " << ::folly::errnoStr(errno); \
std::ostringstream _requireMsg_; \
_requireMsg_ << (msg) << ": " << strerror(_requireErrnoCopy_); \
ADD_FAILURE(); \
} }
void testSetFromSocket( void testSetFromSocket(
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment