xwayland: minor fixups for stability (#8323)

* xwayland: add inline safe closing of fds and fix LOCK_FILE_MODE permissions

* xwayland: auto recreate xwayland instance if it crashes

* xwayland: delay auto-restart until later
This commit is contained in:
trianta 2024-11-03 08:59:46 -06:00 committed by GitHub
parent 514e0ff509
commit 5833abbbd1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 16 additions and 12 deletions

View file

@ -30,7 +30,7 @@
constexpr int SOCKET_DIR_PERMISSIONS = 0755;
constexpr int SOCKET_BACKLOG = 1;
constexpr int MAX_SOCKET_RETRIES = 32;
constexpr int LOCK_FILE_MODE = 044;
constexpr int LOCK_FILE_MODE = 0444;
static bool setCloseOnExec(int fd, bool cloexec) {
int flags = fcntl(fd, F_GETFD);
@ -58,6 +58,11 @@ void cleanUpSocket(int fd, const char* path) {
unlink(path);
}
inline void closeSocketSafely(int& fd) {
if (fd >= 0)
close(fd);
}
static int createSocket(struct sockaddr_un* addr, size_t path_size) {
socklen_t size = offsetof(struct sockaddr_un, sun_path) + path_size + 1;
int fd = socket(AF_UNIX, SOCK_STREAM, 0);
@ -252,8 +257,8 @@ CXWaylandServer::~CXWaylandServer() {
if (display < 0)
return;
close(xFDs[0]);
close(xFDs[1]);
closeSocketSafely(xFDs[0]);
closeSocketSafely(xFDs[1]);
std::string lockPath = std::format("/tmp/.X{}-lock", display);
safeRemove(lockPath);
@ -283,14 +288,10 @@ void CXWaylandServer::die() {
if (pipeFd >= 0)
close(pipeFd);
if (waylandFDs[0] >= 0)
close(waylandFDs[0]);
if (waylandFDs[1] >= 0)
close(waylandFDs[1]);
if (xwmFDs[0] >= 0)
close(xwmFDs[0]);
if (xwmFDs[1] >= 0)
close(xwmFDs[1]);
closeSocketSafely(waylandFDs[0]);
closeSocketSafely(waylandFDs[1]);
closeSocketSafely(xwmFDs[0]);
closeSocketSafely(xwmFDs[1]);
// possible crash. Better to leak a bit.
//if (xwaylandClient)
@ -407,7 +408,7 @@ bool CXWaylandServer::start() {
close(notify[1]);
close(waylandFDs[1]);
close(xwmFDs[1]);
closeSocketSafely(xwmFDs[1]);
waylandFDs[1] = -1;
xwmFDs[1] = -1;

View file

@ -13,6 +13,7 @@
#include "../defines.hpp"
#include "../Compositor.hpp"
#include "../protocols/core/Seat.hpp"
#include "../managers/eventLoop/EventLoopManager.hpp"
#include "../managers/SeatManager.hpp"
#include "../protocols/XWaylandShell.hpp"
#include "../protocols/core/Compositor.hpp"
@ -691,6 +692,8 @@ int CXWM::onEvent(int fd, uint32_t mask) {
Debug::log(CRIT, "XWayland has yeeten the xwm off?!");
g_pXWayland->pWM.reset();
g_pXWayland->pServer.reset();
// Attempt to create fresh instance
g_pEventLoopManager->doLater([]() { g_pXWayland = std::make_unique<CXWayland>(true); });
return 0;
}