Skip to content

Commit

Permalink
socket set flag SOCK_CLOEXEC
Browse files Browse the repository at this point in the history
  • Loading branch information
张晨 authored and mingzc0508 committed May 24, 2019
1 parent 1dc65a9 commit 2d974f1
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 4 deletions.
2 changes: 2 additions & 0 deletions include/flora-agent.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ class Agent : public ClientCallback {

void disconnected();

int get_socket() const;

private:
void run();

Expand Down
2 changes: 2 additions & 0 deletions include/flora-cli.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ class Client {
std::function<void(int32_t, Response &)> &cb,
uint32_t timeout = 0) = 0;

virtual int get_socket() const = 0;

static int32_t connect(const char *uri, ClientCallback *cb,
uint32_t msg_buf_size,
std::shared_ptr<Client> &result);
Expand Down
7 changes: 7 additions & 0 deletions src/cli.cc
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,13 @@ int32_t Client::call(const char *name, shared_ptr<Caps> &msg,
return call(name, msg, target, cb, timeout);
}

int Client::get_socket() const {
auto conn = static_pointer_cast<SocketConn>(connection);
if (conn)
return conn->get_socket();
return -1;
}

bool Client::handle_monitor_list_all(shared_ptr<Caps> &resp) {
vector<MonitorListItem> items;
if (ResponseParser::parse_monitor_list_all(resp, items) < 0) {
Expand Down
2 changes: 2 additions & 0 deletions src/cli.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ class Client : public flora::Client {
int32_t call(const char *name, std::shared_ptr<Caps> &msg, const char *target,
std::function<void(int32_t, Response &)> &cb, uint32_t timeout);

int get_socket() const;

private:
int32_t auth(const std::string &extra, uint32_t flags);

Expand Down
7 changes: 7 additions & 0 deletions src/flora-agent.cc
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,13 @@ void Agent::recv_call(const char *name, shared_ptr<Caps> &msg,

void Agent::disconnected() { destroy_client(); }

int Agent::get_socket() const {
auto cli = flora_cli;
if (cli)
return cli->get_socket();
return -1;
}

} // namespace flora

using namespace flora;
Expand Down
18 changes: 18 additions & 0 deletions src/sock-conn.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,20 @@ SocketConn::~SocketConn() {
}

bool SocketConn::connect(const std::string &name) {
#ifdef __APPLE__
int fd = socket(AF_UNIX, SOCK_STREAM, 0);
#else
int fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
#endif
if (fd < 0) {
KLOGE(TAG, "socket create failed: %s", strerror(errno));
return false;
}
#ifdef __APPLE__
auto f = fcntl(fd, F_GETFD);
f |= FD_CLOEXEC;
fcntl(fd, F_SETFD, f);
#endif
// set socket recv timeout
if (recv_timeout > 0) {
struct timeval tv;
Expand All @@ -50,11 +59,20 @@ bool SocketConn::connect(const std::string &name) {
}

bool SocketConn::connect(const std::string &host, int32_t port) {
#ifdef __APPLE__
int fd = socket(AF_INET, SOCK_STREAM, 0);
#else
int fd = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0);
#endif
if (fd < 0) {
KLOGE(TAG, "socket create failed: %s", strerror(errno));
return false;
}
#ifdef __APPLE__
auto f = fcntl(fd, F_GETFD);
f |= FD_CLOEXEC;
fcntl(fd, F_SETFD, f);
#endif
// set socket recv timeout
if (recv_timeout > 0) {
struct timeval tv;
Expand Down
4 changes: 3 additions & 1 deletion src/sock-conn.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ class SocketConn : public Connection {

void close() override;

bool closed() const override { return !sock_ready; }
inline bool closed() const override { return !sock_ready; }

inline int get_socket() const { return sock; }

private:
int sock = -1;
Expand Down
40 changes: 37 additions & 3 deletions src/sock-poll.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <chrono>
#include <netdb.h>
#include <netinet/in.h>
#include <fcntl.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
Expand Down Expand Up @@ -94,13 +95,28 @@ int32_t SocketPoll::do_poll(fd_set *rfds, int max_fd) {
static int unix_accept(int lfd) {
sockaddr_un addr;
socklen_t addr_len = sizeof(addr);
return accept(lfd, (sockaddr *)&addr, &addr_len);
#ifdef __APPLE__
auto fd = accept(lfd, (sockaddr *)&addr, &addr_len);
auto f = fcntl(fd, F_GETFD);
f |= FD_CLOEXEC;
fcntl(fd, F_SETFD, f);
return fd;
#else
return accept4(lfd, (sockaddr *)&addr, &addr_len, SOCK_CLOEXEC);
#endif
}

static int tcp_accept(int lfd, uint64_t& tag) {
sockaddr_in addr;
socklen_t addr_len = sizeof(addr);
#ifdef __APPLE__
auto fd = accept(lfd, (sockaddr *)&addr, &addr_len);
auto f = fcntl(fd, F_GETFD);
f |= FD_CLOEXEC;
fcntl(fd, F_SETFD, f);
#else
auto fd = accept4(lfd, (sockaddr *)&addr, &addr_len, SOCK_CLOEXEC);
#endif
if (fd < 0)
return fd;
tag = TagHelper::create(addr);
Expand Down Expand Up @@ -182,9 +198,18 @@ shared_ptr<Adapter> SocketPoll::do_accept(int lfd) {
}

bool SocketPoll::init_unix_socket() {
int fd = socket(PF_UNIX, SOCK_STREAM, 0);
#ifdef __APPLE__
int fd = socket(AF_UNIX, SOCK_STREAM, 0);
#else
int fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
#endif
if (fd < 0)
return false;
#ifdef __APPLE__
auto f = fcntl(fd, F_GETFD);
f |= FD_CLOEXEC;
fcntl(fd, F_SETFD, f);
#endif
struct sockaddr_un addr;
memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_UNIX;
Expand All @@ -201,9 +226,18 @@ bool SocketPoll::init_unix_socket() {
}

bool SocketPoll::init_tcp_socket() {
int fd = socket(PF_INET, SOCK_STREAM, 0);
#ifdef __APPLE__
int fd = socket(AF_INET, SOCK_STREAM, 0);
#else
int fd = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0);
#endif
if (fd < 0)
return false;
#ifdef __APPLE__
auto f = fcntl(fd, F_GETFD);
f |= FD_CLOEXEC;
fcntl(fd, F_SETFD, f);
#endif
int v = 1;
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &v, sizeof(v));
struct sockaddr_in addr;
Expand Down

0 comments on commit 2d974f1

Please sign in to comment.