From 0df5052afebacba29f992df3234211388a0cb619 Mon Sep 17 00:00:00 2001 From: jackun Date: Thu, 7 Apr 2022 13:37:36 +0300 Subject: [PATCH] Socket file --- bin/mangohud-control.py | 4 +++- src/mesa/util/os_socket.c | 29 +++++++++++++++++++++++++++++ src/mesa/util/os_socket.h | 1 + src/overlay_params.cpp | 8 +++++++- 4 files changed, 40 insertions(+), 2 deletions(-) diff --git a/bin/mangohud-control.py b/bin/mangohud-control.py index cca0c6ec..c64755ad 100644 --- a/bin/mangohud-control.py +++ b/bin/mangohud-control.py @@ -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 diff --git a/src/mesa/util/os_socket.c b/src/mesa/util/os_socket.c index 98ef0132..af10e8bc 100644 --- a/src/mesa/util/os_socket.c +++ b/src/mesa/util/os_socket.c @@ -17,6 +17,29 @@ #include #include +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) diff --git a/src/mesa/util/os_socket.h b/src/mesa/util/os_socket.h index 0d6f8749..8c4c7f08 100644 --- a/src/mesa/util/os_socket.h +++ b/src/mesa/util/os_socket.h @@ -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); diff --git a/src/overlay_params.cpp b/src/overlay_params.cpp index 38edeb31..2d1a6f87 100644 --- a/src/overlay_params.cpp +++ b/src/overlay_params.cpp @@ -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));