gem5 v24.0.0.0
Loading...
Searching...
No Matches
socket.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2020 The Regents of the University of California
3 * All rights reserved
4 *
5 * Copyright (c) 2002-2005 The Regents of The University of Michigan
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
10 * met: redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer;
12 * redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution;
15 * neither the name of the copyright holders nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#include "base/socket.hh"
33
34#include <netinet/in.h>
35#include <netinet/tcp.h>
36#include <sys/socket.h>
37#include <sys/types.h>
38#include <sys/un.h>
39#include <unistd.h>
40
41#include <cerrno>
42
43// check if filesystem library is available
44#if defined(__cpp_lib_filesystem) || __has_include(<filesystem>)
45 #include <filesystem>
46#else
47 // This is only reachable if we're using GCC 7 or clang versions 6
48 // through 10 (note: gem5 does not support GCC versions older than
49 // GCC 7 or clang versions older than clang 6.0 as they do not
50 // support the C++17 standard).
51 // If we're using GCC 7 or clang versions 6 through 10, we need to use
52 // <experimental/filesystem>.
53 #include <experimental/filesystem>
54 namespace std {
55 namespace filesystem = experimental::filesystem;
56 }
57#endif
58
59#include "base/logging.hh"
60#include "base/output.hh"
61#include "base/str.hh"
62#include "base/types.hh"
63#include "sim/byteswap.hh"
64
65namespace gem5
66{
67
70
72
73void
75{
76 listeningDisabled = false;
77 anyListening = false;
78 bindToLoopback = false;
79}
80
81void
83{
84 if (anyListening)
85 panic("Too late to disable all listeners, already have a listener");
86 listeningDisabled = true;
87}
88
89bool
94
95void
97{
98 if (anyListening)
99 panic("Too late to bind to loopback, already have a listener");
100 bindToLoopback = true;
101}
102
103// Wrappers to stub out SOCK_CLOEXEC/accept4 availability
104
105int
107{
108#ifdef SOCK_CLOEXEC
109 type |= SOCK_CLOEXEC;
110#endif
111 return ::socket(domain, type, protocol);
112}
113
114int
115ListenSocket::acceptCloexec(int sockfd, struct sockaddr *addr,
116 socklen_t *addrlen)
117{
118#if defined(_GNU_SOURCE) && defined(SOCK_CLOEXEC)
119 return ::accept4(sockfd, addr, addrlen, SOCK_CLOEXEC);
120#else
121 return ::accept(sockfd, addr, addrlen);
122#endif
123}
124
126//
127//
128
129ListenSocket::ListenSocket(const std::string &_name) : Named(_name) {}
130
132{
133 if (fd != -1)
134 close(fd);
135}
136
137// Open a connection. Accept will block, so if you don't want it to,
138// make sure a connection is ready before you call accept.
139int
141{
142 struct sockaddr_in sockaddr;
143 socklen_t slen = sizeof(sockaddr);
144 int sfd = acceptCloexec(fd, (struct sockaddr *)&sockaddr, &slen);
145 panic_if(sfd == -1, "%s: Failed to accept connection: %s",
146 name(), strerror(errno));
147
148 return sfd;
149}
150
151bool
152ListenSocketConfig::parseIni(const std::string &value,
153 ListenSocketConfig &retval)
154{
155 if (value.size() == 0) {
156 retval = listenSocketEmptyConfig();
157 return true;
158 } else if (value[0] == '@') {
159 retval = listenSocketUnixAbstractConfig(value.substr(1));
160 return true;
161 } else if (value[0] == 'P') {
162 std::filesystem::path p(value.substr(1));
163 retval = listenSocketUnixFileConfig(p.parent_path(), p.filename());
164 return true;
165 } else if (value[0] == '#') {
166 uint64_t port;
167 bool ret = to_number(value.substr(1), port);
168 if (!ret)
169 return false;
170 retval = listenSocketInetConfig(port);
171 return true;
172 } else {
173 panic("Can't interpret %s as a host socket.", value);
174 }
175}
176
177ListenSocketInet::ListenSocketInet(const std::string &_name, int port)
178 : ListenSocket(_name), _port(port)
179{}
180
181int
183{
184 int sfd = ListenSocket::accept();
185 if (sfd == -1)
186 return -1;
187
188 int i = 1;
189 int ret = ::setsockopt(sfd, IPPROTO_TCP, TCP_NODELAY, &i, sizeof(i));
190 warn_if(ret < 0, "ListenSocket(accept): setsockopt() TCP_NODELAY failed!");
191
192 return sfd;
193}
194
195// Create a socket and configure it for listening
196bool
198{
199 panic_if(listening, "Socket already listening!");
200
201 // only create socket if not already created by a previous call
202 if (fd == -1) {
203 fd = socketCloexec(PF_INET, SOCK_STREAM, 0);
204 panic_if(fd < 0, "Can't create socket:%s !", strerror(errno));
205 }
206
207 int i = 1;
208 int ret = ::setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
209 panic_if(ret < 0,
210 "ListenSocket(listen): setsockopt() SO_REUSEADDR failed!");
211
212 struct sockaddr_in sockaddr;
213 sockaddr.sin_family = PF_INET;
214 sockaddr.sin_addr.s_addr =
215 htobe<in_addr_t>(bindToLoopback ? INADDR_LOOPBACK : INADDR_ANY);
216 sockaddr.sin_port = htons(port);
217 // finally clear sin_zero
218 std::memset(&sockaddr.sin_zero, 0, sizeof(sockaddr.sin_zero));
219 ret = ::bind(fd, (struct sockaddr *)&sockaddr, sizeof (sockaddr));
220 if (ret != 0) {
221 panic_if(ret == -1 && errno != EADDRINUSE,
222 "ListenSocket(listen): bind() failed!");
223 return false;
224 }
225
226 if (::listen(fd, 1) == -1) {
227 panic_if(errno != EADDRINUSE,
228 "ListenSocket(listen): listen() failed!");
229 // User may decide to retry with a different port later; however, the
230 // socket is already bound to a port and the next bind will surely
231 // fail. We'll close the socket and reset fd to -1 so our user can
232 // retry with a cleaner state.
233 close(fd);
234 fd = -1;
235 return false;
236 }
237
238 setListening();
239 return true;
240}
241
242void
244{
245 while (!listen(_port)) {
246 _port++;
247 fatal_if(_port > 65536, "%s: cannot find an available port.", name());
248 }
249 ccprintf(std::cerr, "%s: Listening for connections on %s\n",
250 name(), *this);
251}
252
253void
254ListenSocketInet::output(std::ostream &os) const
255{
256 os << "port " << _port;
257}
258
261{
262 return ListenSocketConfig([port](const std::string &name) {
263 return std::make_unique<ListenSocketInet>(name, port);
264 });
265}
266
267void
268ListenSocketUnix::checkPathLength(const std::string &original, size_t max_len)
269{
270 fatal_if(original.size() > max_len,
271 "Length of socket path '%s' is %d, greater than max %d.",
272 original, original.size(), max_len);
273}
274
275void
277{
278 panic_if(listening, "%s: Socket already listening!", name());
279
280 // only create socket if not already created by previous call
281 if (fd == -1) {
282 fd = socketCloexec(PF_UNIX, SOCK_STREAM, 0);
283 panic_if(fd < 0, "%s: Can't create unix socket:%s !",
284 name(), strerror(errno));
285 }
286
287 sockaddr_un serv_addr;
288 std::memset(&serv_addr, 0, sizeof(serv_addr));
289 size_t addr_size = prepSockaddrUn(serv_addr);
290
291 fatal_if(bind(fd, (struct sockaddr *)&(serv_addr), addr_size) != 0,
292 "%s: Cannot bind unix socket %s: %s", name(), *this,
293 strerror(errno));
294
295 fatal_if(::listen(fd, 1) == -1, "%s: Failed to listen on %s: %s\n",
296 name(), *this, strerror(errno));
297
298 ccprintf(std::cerr, "%s: Listening for connections on %s\n",
299 name(), *this);
300
301 setListening();
302}
303
305 const std::string &_dir, const std::string &_fname) :
306 ListenSocketUnix(_name), dir(_dir), fname(_fname)
307{
308 checkPathLength(fname, sizeof(sockaddr_un::sun_path) - 1);
309}
310
312{
313 if (fd != -1) {
314 close(fd);
315 fd = -1;
316 unlink();
317 }
318}
319
320bool
322{
323 auto path = resolvedDir + "/" + fname;
324 return ::unlink(path.c_str()) == 0;
325}
326
327size_t
329{
330 addr.sun_family = AF_UNIX;
331 std::memcpy(addr.sun_path, fname.c_str(), fname.size());
332 return sizeof(addr.sun_path);
333}
334
335void
337{
339 warn_if(unlink(),
340 "%s: server path %s was occupied and will be replaced. Please "
341 "make sure there is no other server using the same path.",
342 name(), resolvedDir + "/" + fname);
343
344 // Make sure "dir" exists.
345 std::error_code ec;
346 std::filesystem::create_directory(resolvedDir, ec);
347 fatal_if(ec, "Failed to create directory %s", ec.message());
348
349 // Change the working directory to the directory containing the socket so
350 // that we maximize the limited space in sockaddr_un.sun_path.
351 auto cwd = std::filesystem::current_path(ec);
352 panic_if(ec, "Failed to get current working directory %s", ec.message());
353 std::filesystem::current_path(resolvedDir, ec);
354 fatal_if(ec, "Failed to change to directory %s: %s",
355 resolvedDir, ec.message());
356
358
359 std::filesystem::current_path(cwd, ec);
360 panic_if(ec, "Failed to change back working directory %s", ec.message());
361}
362
363void
365{
366 os << "socket \"" << dir << "/" << fname << "\"";
367}
368
370listenSocketUnixFileConfig(std::string dir, std::string fname)
371{
372 return ListenSocketConfig([dir, fname](const std::string &name) {
373 return std::make_unique<ListenSocketUnixFile>(name, dir, fname);
374 });
375}
376
377size_t
379{
380 addr.sun_family = AF_UNIX;
381 addr.sun_path[0] = '\0';
382 std::memcpy(&addr.sun_path[1], path.c_str(), path.size());
383 return offsetof(sockaddr_un, sun_path) + path.size() + 1;
384}
385
387 const std::string &_name, const std::string &_path) :
388 ListenSocketUnix(_name), path(_path)
389{
390 checkPathLength(path, sizeof(sockaddr_un::sun_path) - 1);
391}
392
393void
395{
396 os << "abstract socket \"" << path << "\"";
397}
398
401{
402 return ListenSocketConfig([path](const std::string &name) {
403 return std::make_unique<ListenSocketUnixAbstract>(name, path);
404 });
405}
406
407} // namespace gem5
Defines global host-dependent types: Counter, Tick, and (indirectly) {int,uint}{8,...
static bool parseIni(const std::string &value, ListenSocketConfig &retval)
Definition socket.cc:152
ListenSocketInet(const std::string &_name, int port)
Definition socket.cc:177
int accept() override
Definition socket.cc:182
void listen() override
Definition socket.cc:243
void output(std::ostream &os) const override
Definition socket.cc:254
size_t prepSockaddrUn(sockaddr_un &addr) const override
Definition socket.cc:378
ListenSocketUnixAbstract(const std::string &_name, const std::string &_path)
Definition socket.cc:386
void output(std::ostream &os) const override
Definition socket.cc:394
size_t prepSockaddrUn(sockaddr_un &addr) const override
Definition socket.cc:328
void output(std::ostream &os) const override
Definition socket.cc:364
void listen() override
Definition socket.cc:336
ListenSocketUnixFile(const std::string &_name, const std::string &_dir, const std::string &_fname)
Definition socket.cc:304
void listen() override
Definition socket.cc:276
virtual size_t prepSockaddrUn(sockaddr_un &addr) const =0
void checkPathLength(const std::string &original, size_t max_len)
Definition socket.cc:268
static bool anyListening
Definition socket.hh:54
ListenSocket(const std::string &_name)
Definition socket.cc:129
static bool bindToLoopback
Definition socket.hh:56
static bool listeningDisabled
The following variables are only used by socket unit tests: listeningDisabled, anyListening,...
Definition socket.hh:53
static void loopbackOnly()
Definition socket.cc:96
virtual int accept()
Definition socket.cc:140
static int socketCloexec(int domain, int type, int protocol)
Definition socket.cc:106
static void cleanup()
Definition socket.cc:74
static bool allDisabled()
Definition socket.cc:90
static void disableAll()
Definition socket.cc:82
void setListening()
Definition socket.hh:69
static int acceptCloexec(int sockfd, struct sockaddr *addr, socklen_t *addrlen)
Definition socket.cc:115
Interface for things with names.
Definition named.hh:39
virtual std::string name() const
Definition named.hh:47
std::string resolve(const std::string &name) const
Returns relative file names prepended with name of this directory.
Definition output.cc:204
#define panic(...)
This implements a cprintf based panic() function.
Definition logging.hh:188
#define fatal_if(cond,...)
Conditional fatal macro that checks the supplied condition and only causes a fatal error if the condi...
Definition logging.hh:236
#define panic_if(cond,...)
Conditional panic macro that checks the supplied condition and only panics if the condition is true a...
Definition logging.hh:214
virtual ~ListenSocket()
Definition socket.cc:131
#define warn_if(cond,...)
Conditional warning macro that checks the supplied condition and only prints a warning if the conditi...
Definition logging.hh:283
Bitfield< 14, 12 > fd
Definition types.hh:150
Bitfield< 7 > i
Definition misc_types.hh:67
Bitfield< 7, 4 > domain
Bitfield< 0 > p
Bitfield< 17 > os
Definition misc.hh:838
Bitfield< 3 > addr
Definition types.hh:84
Copyright (c) 2024 - Pranith Kumar Copyright (c) 2020 Inria All rights reserved.
Definition binary32.hh:36
bool to_number(const std::string &value, Pixel &retval)
Definition pixel.hh:217
ListenSocketConfig listenSocketUnixAbstractConfig(std::string path)
Definition socket.cc:400
ListenSocketConfig listenSocketInetConfig(int port)
Definition socket.cc:260
OutputDirectory simout
Definition output.cc:62
T htobe(T value)
Definition byteswap.hh:174
ListenSocketConfig listenSocketUnixFileConfig(std::string dir, std::string fname)
Definition socket.cc:370
static ListenSocketConfig listenSocketEmptyConfig()
Definition socket.hh:137
void ccprintf(cp::Print &print)
Definition cprintf.hh:130
Overload hash function for BasicBlockRange type.
Definition binary32.hh:81
const std::string & name()
Definition trace.cc:48

Generated on Tue Jun 18 2024 16:24:01 for gem5 by doxygen 1.11.0