Skip to content

Commit

Permalink
xwayland: minor fixups for stability (#8323)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Trimutex authored Nov 3, 2024
1 parent 514e0ff commit 5833abb
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
25 changes: 13 additions & 12 deletions src/xwayland/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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;

Expand Down
3 changes: 3 additions & 0 deletions src/xwayland/XWM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit 5833abb

Please sign in to comment.