Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

shard_connection: Support abstract Unix socket clients #257

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions shard_connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ verify_request::~verify_request(void)

shard_connection::shard_connection(unsigned int id, connections_manager* conns_man, benchmark_config* config,
struct event_base* event_base, abstract_protocol* abs_protocol) :
m_address(NULL), m_port(NULL), m_unix_sockaddr(NULL),
m_address(NULL), m_port(NULL), m_unix_sockaddr(NULL), m_unix_sockaddrlen(0),
m_bev(NULL), m_event_timer(NULL), m_request_per_cur_interval(0), m_pending_resp(0), m_connection_state(conn_disconnected),
m_hello(setup_done), m_authentication(setup_done), m_db_selection(setup_done), m_cluster_slots(setup_done) {
m_id = id;
Expand All @@ -140,9 +140,19 @@ shard_connection::shard_connection(unsigned int id, connections_manager* conns_m
m_unix_sockaddr = (struct sockaddr_un *) malloc(sizeof(struct sockaddr_un));
assert(m_unix_sockaddr != NULL);

memset(m_unix_sockaddr, 0, sizeof(struct sockaddr_un));
m_unix_sockaddr->sun_family = AF_UNIX;
strncpy(m_unix_sockaddr->sun_path, m_config->unix_socket, sizeof(m_unix_sockaddr->sun_path)-1);
m_unix_sockaddr->sun_path[sizeof(m_unix_sockaddr->sun_path)-1] = '\0';

// Consider any Unix socket path prefixed with @ an abstract socket.
// In this scenario, sun_path[0] must be a null byte, and addrlen should be terminated at
// exactly the socket name length; see the manpage for unix(7).
if (m_config->unix_socket[0] == '@') {
strncpy(&m_unix_sockaddr->sun_path[1], &m_config->unix_socket[1], strlen(m_config->unix_socket)-1);
m_unix_sockaddrlen = offsetof(struct sockaddr_un, sun_path) + strlen(m_config->unix_socket);
} else {
strncpy(m_unix_sockaddr->sun_path, m_config->unix_socket, sizeof(m_unix_sockaddr->sun_path)-1);
m_unix_sockaddrlen = sizeof(struct sockaddr_un);
}
}

m_protocol = abs_protocol->clone();
Expand Down Expand Up @@ -290,7 +300,7 @@ int shard_connection::connect(struct connect_info* addr) {

if (bufferevent_socket_connect(m_bev,
m_unix_sockaddr ? (struct sockaddr *) m_unix_sockaddr : addr->ci_addr,
m_unix_sockaddr ? sizeof(struct sockaddr_un) : addr->ci_addrlen) == -1) {
m_unix_sockaddr ? m_unix_sockaddrlen : addr->ci_addrlen) == -1) {
disconnect();

benchmark_error_log("connect failed, error = %s\n", strerror(errno));
Expand Down
1 change: 1 addition & 0 deletions shard_connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ class shard_connection {
std::string m_readable_id;

struct sockaddr_un* m_unix_sockaddr;
int m_unix_sockaddrlen;
struct bufferevent *m_bev;
struct event_base* m_event_base;
struct event* m_event_timer;
Expand Down