2 * Copyright 2016 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include <folly/io/ShutdownSocketSet.h>
22 #include <glog/logging.h>
24 #include <folly/FileUtil.h>
25 #include <folly/Malloc.h>
26 #include <folly/portability/Sockets.h>
30 ShutdownSocketSet::ShutdownSocketSet(int maxFd)
32 data_(static_cast<std::atomic<uint8_t>*>(
33 folly::checkedCalloc(maxFd, sizeof(std::atomic<uint8_t>)))),
34 nullFile_("/dev/null", O_RDWR) {
37 void ShutdownSocketSet::add(int fd) {
38 // Silently ignore any fds >= maxFd_, very unlikely
44 auto& sref = data_[fd];
45 uint8_t prevState = FREE;
46 CHECK(sref.compare_exchange_strong(prevState,
48 std::memory_order_acq_rel))
49 << "Invalid prev state for fd " << fd << ": " << int(prevState);
52 void ShutdownSocketSet::remove(int fd) {
58 auto& sref = data_[fd];
59 uint8_t prevState = 0;
62 prevState = sref.load(std::memory_order_relaxed);
67 std::this_thread::sleep_for(std::chrono::milliseconds(1));
70 LOG(FATAL) << "Invalid prev state for fd " << fd << ": " << int(prevState);
73 if (!sref.compare_exchange_weak(prevState,
75 std::memory_order_acq_rel)) {
80 int ShutdownSocketSet::close(int fd) {
83 return folly::closeNoInt(fd);
86 auto& sref = data_[fd];
87 uint8_t prevState = sref.load(std::memory_order_relaxed);
97 newState = MUST_CLOSE;
100 LOG(FATAL) << "Invalid prev state for fd " << fd << ": " << int(prevState);
103 if (!sref.compare_exchange_weak(prevState,
105 std::memory_order_acq_rel)) {
109 return newState == FREE ? folly::closeNoInt(fd) : 0;
112 void ShutdownSocketSet::shutdown(int fd, bool abortive) {
115 doShutdown(fd, abortive);
119 auto& sref = data_[fd];
120 uint8_t prevState = IN_USE;
121 if (!sref.compare_exchange_strong(prevState,
123 std::memory_order_acq_rel)) {
127 doShutdown(fd, abortive);
129 prevState = IN_SHUTDOWN;
130 if (sref.compare_exchange_strong(prevState,
132 std::memory_order_acq_rel)) {
136 CHECK_EQ(prevState, MUST_CLOSE)
137 << "Invalid prev state for fd " << fd << ": " << int(prevState);
139 folly::closeNoInt(fd); // ignore errors, nothing to do
141 CHECK(sref.compare_exchange_strong(prevState,
143 std::memory_order_acq_rel))
144 << "Invalid prev state for fd " << fd << ": " << int(prevState);
147 void ShutdownSocketSet::shutdownAll(bool abortive) {
148 for (int i = 0; i < maxFd_; ++i) {
149 auto& sref = data_[i];
150 if (sref.load(std::memory_order_acquire) == IN_USE) {
151 shutdown(i, abortive);
156 void ShutdownSocketSet::doShutdown(int fd, bool abortive) {
157 // shutdown() the socket first, to awaken any threads blocked on the fd
158 // (subsequent IO will fail because it's been shutdown); close()ing the
159 // socket does not wake up blockers, see
160 // http://stackoverflow.com/a/3624545/1736339
161 folly::shutdownNoInt(fd, SHUT_RDWR);
163 // If abortive shutdown is desired, we'll set the SO_LINGER option on
164 // the socket with a timeout of 0; this will cause RST to be sent on
167 struct linger l = {1, 0};
168 if (setsockopt(fd, SOL_SOCKET, SO_LINGER, &l, sizeof(l)) != 0) {
169 // Probably not a socket, ignore.
174 // We can't close() the socket, as that would be dangerous; a new file
175 // could be opened and get the same file descriptor, and then code assuming
176 // the old fd would do IO in the wrong place. We'll (atomically) dup2
177 // /dev/null onto the fd instead.
178 folly::dup2NoInt(nullFile_.fd(), fd);