ipc: Use xdg runtime directory for socket

u_file_get_runtime_dir falls back to /tmp if $XDG_RUNTIME_DIR is not set.

ipc: %t/monado_comp_ipc socket for systemd socket activation
This commit is contained in:
Christoph Haag 2020-10-21 22:39:46 +02:00 committed by Ryan Pavlik
parent 4ea68b89a4
commit fd6bd0f592
5 changed files with 44 additions and 13 deletions

View file

@ -20,6 +20,7 @@
#include "shared/ipc_protocol.h" #include "shared/ipc_protocol.h"
#include "client/ipc_client.h" #include "client/ipc_client.h"
#include "ipc_client_generated.h" #include "ipc_client_generated.h"
#include "util/u_file.h"
#include <stdio.h> #include <stdio.h>
#include <sys/socket.h> #include <sys/socket.h>
@ -31,7 +32,7 @@
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <unistd.h> #include <unistd.h>
#include <limits.h>
#ifdef XRT_GRAPHICS_BUFFER_HANDLE_IS_AHARDWAREBUFFER #ifdef XRT_GRAPHICS_BUFFER_HANDLE_IS_AHARDWAREBUFFER
#include "android/android_ahardwarebuffer_allocator.h" #include "android/android_ahardwarebuffer_allocator.h"
@ -126,9 +127,17 @@ ipc_connect(struct ipc_connection *ipc_c)
int socket = ret; int socket = ret;
char sock_file[PATH_MAX];
int size = u_file_get_path_in_runtime_dir(IPC_MSG_SOCK_FILE, sock_file, PATH_MAX);
if (size == -1) {
IPC_ERROR(ipc_c, "Could not get socket file name");
return -1;
}
memset(&addr, 0, sizeof(addr)); memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_UNIX; addr.sun_family = AF_UNIX;
strcpy(addr.sun_path, IPC_MSG_SOCK_FILE); strcpy(addr.sun_path, sock_file);
ret = connect(socket, (struct sockaddr *)&addr, sizeof(addr)); ret = connect(socket, (struct sockaddr *)&addr, sizeof(addr));
if (ret < 0) { if (ret < 0) {

View file

@ -20,6 +20,7 @@
#include "util/u_misc.h" #include "util/u_misc.h"
#include "util/u_debug.h" #include "util/u_debug.h"
#include "util/u_trace_marker.h" #include "util/u_trace_marker.h"
#include "util/u_file.h"
#include "shared/ipc_shmem.h" #include "shared/ipc_shmem.h"
#include "server/ipc_server.h" #include "server/ipc_server.h"
@ -39,6 +40,7 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <assert.h> #include <assert.h>
#include <limits.h>
#ifdef XRT_HAVE_SYSTEMD #ifdef XRT_HAVE_SYSTEMD
#include <systemd/sd-daemon.h> #include <systemd/sd-daemon.h>
@ -82,21 +84,30 @@ create_listen_socket(struct ipc_server_mainloop *ml, int *out_fd)
return fd; return fd;
} }
char sock_file[PATH_MAX];
int size = u_file_get_path_in_runtime_dir(IPC_MSG_SOCK_FILE, sock_file, PATH_MAX);
if (size == -1) {
U_LOG_E("Could not get socket file name");
return -1;
}
memset(&addr, 0, sizeof(addr)); memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_UNIX; addr.sun_family = AF_UNIX;
strcpy(addr.sun_path, IPC_MSG_SOCK_FILE); strcpy(addr.sun_path, sock_file);
ret = bind(fd, (struct sockaddr *)&addr, sizeof(addr)); ret = bind(fd, (struct sockaddr *)&addr, sizeof(addr));
#ifdef XRT_HAVE_LIBBSD #ifdef XRT_HAVE_LIBBSD
// no other instance is running, or we would have never arrived here // no other instance is running, or we would have never arrived here
if (ret < 0 && errno == EADDRINUSE) { if (ret < 0 && errno == EADDRINUSE) {
U_LOG_W("Removing stale socket file %s", IPC_MSG_SOCK_FILE); U_LOG_W("Removing stale socket file %s", sock_file);
ret = unlink(IPC_MSG_SOCK_FILE); ret = unlink(sock_file);
if (ret < 0) { if (ret < 0) {
U_LOG_E("Failed to remove stale socket file %s: %s", IPC_MSG_SOCK_FILE, strerror(errno)); U_LOG_E("Failed to remove stale socket file %s: %s", sock_file, strerror(errno));
return ret; return ret;
} }
ret = bind(fd, (struct sockaddr *)&addr, sizeof(addr)); ret = bind(fd, (struct sockaddr *)&addr, sizeof(addr));
@ -104,27 +115,27 @@ create_listen_socket(struct ipc_server_mainloop *ml, int *out_fd)
#endif #endif
if (ret < 0) { if (ret < 0) {
U_LOG_E("Could not bind socket to path %s: %s. Is the service running already?", IPC_MSG_SOCK_FILE, U_LOG_E("Could not bind socket to path %s: %s. Is the service running already?", sock_file,
strerror(errno)); strerror(errno));
#ifdef XRT_HAVE_SYSTEMD #ifdef XRT_HAVE_SYSTEMD
U_LOG_E("Or, is the systemd unit monado.socket or monado-dev.socket active?"); U_LOG_E("Or, is the systemd unit monado.socket or monado-dev.socket active?");
#endif #endif
if (errno == EADDRINUSE) { if (errno == EADDRINUSE) {
U_LOG_E("If monado-service is not running, delete %s before starting a new instance", U_LOG_E("If monado-service is not running, delete %s before starting a new instance",
IPC_MSG_SOCK_FILE); sock_file);
} }
close(fd); close(fd);
return ret; return ret;
} }
// Save for later // Save for later
ml->socket_filename = strdup(IPC_MSG_SOCK_FILE); ml->socket_filename = strdup(sock_file);
ret = listen(fd, IPC_MAX_CLIENTS); ret = listen(fd, IPC_MAX_CLIENTS);
if (ret < 0) { if (ret < 0) {
close(fd); close(fd);
return ret; return ret;
} }
U_LOG_D("Created listening socket %s.", IPC_MSG_SOCK_FILE); U_LOG_D("Created listening socket %s.", sock_file);
*out_fd = fd; *out_fd = fd;
return 0; return 0;
} }

View file

@ -20,7 +20,7 @@
#include "xrt/xrt_tracking.h" #include "xrt/xrt_tracking.h"
#define IPC_MSG_SOCK_FILE "/tmp/monado_comp_ipc" #define IPC_MSG_SOCK_FILE "monado_comp_ipc"
#define IPC_MAX_SWAPCHAIN_HANDLES 8 #define IPC_MAX_SWAPCHAIN_HANDLES 8
#define IPC_CRED_SIZE 1 // auth not implemented #define IPC_CRED_SIZE 1 // auth not implemented
#define IPC_BUF_SIZE 512 // must be >= largest message length in bytes #define IPC_BUF_SIZE 512 // must be >= largest message length in bytes

View file

@ -16,6 +16,9 @@
#include <unistd.h> #include <unistd.h>
#include <errno.h> #include <errno.h>
#include <sys/mman.h> #include <sys/mman.h>
#include <limits.h>
#include "util/u_file.h"
#define P(...) fprintf(stdout, __VA_ARGS__) #define P(...) fprintf(stdout, __VA_ARGS__)
#define PE(...) fprintf(stderr, __VA_ARGS__) #define PE(...) fprintf(stderr, __VA_ARGS__)
@ -212,9 +215,17 @@ do_connect(struct ipc_connection *ipc_c)
return -1; return -1;
} }
char sock_file[PATH_MAX];
int rt_size = u_file_get_path_in_runtime_dir(IPC_MSG_SOCK_FILE, sock_file, PATH_MAX);
if (rt_size == -1) {
PE("Could not get socket file name");
return -1;
}
struct sockaddr_un addr = {0}; struct sockaddr_un addr = {0};
addr.sun_family = AF_UNIX; addr.sun_family = AF_UNIX;
strcpy(addr.sun_path, IPC_MSG_SOCK_FILE); strcpy(addr.sun_path, sock_file);
ret = connect(ipc_c->imc.socket_fd, // socket ret = connect(ipc_c->imc.socket_fd, // socket
(struct sockaddr *)&addr, // address (struct sockaddr *)&addr, // address

View file

@ -7,7 +7,7 @@ ConditionUser=!root
Conflicts=@conflicts@.socket Conflicts=@conflicts@.socket
[Socket] [Socket]
ListenStream=/tmp/monado_comp_ipc ListenStream=%t/monado_comp_ipc
RemoveOnStop=true RemoveOnStop=true
[Install] [Install]