Socket file

control-2
jackun 2 years ago
parent 44a9a8fa0f
commit 0df5052afe
No known key found for this signature in database
GPG Key ID: 119DB3F1D05A9ED3

@ -150,7 +150,9 @@ class MsgParser:
return parsed
def control(args):
if args.socket:
if os.path.exists(args.socket):
address = args.socket
elif args.socket:
address = '\0' + args.socket
else:
address = DEFAULT_SERVER_ADDRESS

@ -17,6 +17,29 @@
#include <sys/un.h>
#include <unistd.h>
int
os_socket_listen_file(const char *path, int count)
{
int s = socket(AF_UNIX, SOCK_STREAM, 0);
if (s < 0)
return -1;
unlink(path);
struct sockaddr_un addr;
memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_UNIX;
strncpy(addr.sun_path, path, sizeof(addr.sun_path) - 1);
int ret = bind(s, (struct sockaddr*)&addr, sizeof(addr));
if (ret < 0)
return -1;
listen(s, count);
return s;
}
int
os_socket_listen_abstract(const char *path, int count)
{
@ -80,6 +103,12 @@ os_socket_close(int s)
}
#else
int
os_socket_listen_file(const char *path, int count)
{
errno = -ENOSYS;
return -1;
}
int
os_socket_listen_abstract(const char *path, int count)

@ -21,6 +21,7 @@ extern "C" {
int os_socket_accept(int s);
int os_socket_listen_file(const char *path, int count);
int os_socket_listen_abstract(const char *path, int count);
ssize_t os_socket_recv(int socket, void *buffer, size_t length, int flags);

@ -96,13 +96,19 @@ parse_position(const char *str)
static int
parse_control(const char *str)
{
int ret = -1;
std::string path(str);
size_t npos = path.find("%p");
if (npos != std::string::npos)
path.replace(npos, 2, std::to_string(getpid()));
SPDLOG_DEBUG("Socket: {}", path);
int ret = os_socket_listen_abstract(path.c_str(), 1);
if (path[0] == '/')
ret = os_socket_listen_file(path.c_str(), 1);
if (ret < 0) {
SPDLOG_WARN("Couldn't create socket file at '{}'", str);
ret = os_socket_listen_abstract(path.c_str(), 1);
}
if (ret < 0) {
SPDLOG_ERROR("Couldn't create socket pipe at '{}'", str);
SPDLOG_ERROR("ERROR: '{}'", strerror(errno));

Loading…
Cancel
Save