Skip to content

Commit

Permalink
Merge pull request #327 from LGFae/better-socket-location-defaults
Browse files Browse the repository at this point in the history
better defaults for when we can't read env vars
  • Loading branch information
LGFae authored Aug 8, 2024
2 parents 596aa0a + 424172d commit 3eb6870
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 28 deletions.
1 change: 1 addition & 0 deletions common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ rustix = { version = "0.38", default-features = false, features = [
"shm",
"mm",
"param",
"process",
] }

[build-dependencies]
Expand Down
24 changes: 19 additions & 5 deletions common/src/ipc/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,25 @@ impl<T> IpcSocket<T> {
}

fn socket_file() -> String {
let runtime = env::var("XDG_RUNTIME_DIR");
let display = env::var("WAYLAND_DISPLAY");

let runtime = runtime.as_deref().unwrap_or("/tmp/swww");
let display = display.as_deref().unwrap_or("wayland-0");
let runtime = env::var("XDG_RUNTIME_DIR").unwrap_or_else(|_| {
let uid = rustix::process::getuid();
format!("/run/user/{}", uid.as_raw())
});

let display = if let Ok(wayland_socket) = std::env::var("WAYLAND_DISPLAY") {
let mut i = 0;
// if WAYLAND_DISPLAY is a full path, use only its final component
for (j, ch) in wayland_socket.bytes().enumerate().rev() {
if ch == b'/' {
i = j + 1;
break;
}
}
format!("{}.sock", &wayland_socket[i..])
} else {
eprintln!("WARNING: WAYLAND_DISPLAY variable not set. Defaulting to wayland-0");
"wayland-0.sock".to_string()
};

format!("{runtime}/swww-{display}.socket")
}
Expand Down
43 changes: 20 additions & 23 deletions daemon/src/wayland/globals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,53 +203,50 @@ pub fn init(pixel_format: Option<PixelFormat>) -> Initializer {
initializer
}

/// copy-pasted from `wayland-client.rs`
/// mostly copy-pasted from `wayland-client.rs`
fn connect() -> OwnedFd {
if let Ok(txt) = std::env::var("WAYLAND_SOCKET") {
// We should connect to the provided WAYLAND_SOCKET
let fd = txt
.parse::<i32>()
.expect("invalid fd in WAYLAND_SOCKET env var");
let fd = unsafe { OwnedFd::from_raw_fd(fd) };
// remove the variable so any child processes don't see it
std::env::remove_var("WAYLAND_SOCKET");

// set the CLOEXEC flag on this FD
let flags = rustix::io::fcntl_getfd(&fd);
flags
.map(|f| f | rustix::io::FdFlags::CLOEXEC)
.and_then(|f| rustix::io::fcntl_setfd(&fd, f))
.expect("failed to set flags on socket");

let socket_addr =
rustix::net::getsockname(&fd).expect("failed to get wayland socket address");
if let SocketAddrAny::Unix(addr) = socket_addr {
rustix::net::connect_unix(&fd, &addr).expect("failed to connect to unix socket");
fd
} else {
panic!("socket address is not a unix socket");
panic!("socket address {:?} is not a unix socket", socket_addr);
}
} else {
let socket_name = std::env::var_os("WAYLAND_DISPLAY")
.map(Into::<PathBuf>::into)
.expect("failed to detect wayland compositor: WAYLAND_DISPLAY not set");
let socket_name: PathBuf = std::env::var_os("WAYLAND_DISPLAY")
.unwrap_or_else(|| {
log::warn!("WAYLAND_DISPLAY is not set! Defaulting to wayland-0");
std::ffi::OsString::from("wayland-0")
})
.into();

let socket_path = if socket_name.is_absolute() {
socket_name
} else {
let mut socket_path = std::env::var_os("XDG_RUNTIME_DIR")
.map(Into::<PathBuf>::into)
.expect("failed to detect wayland compositor: XDG_RUNTIME_DIR not set");
if !socket_path.is_absolute() {
panic!("failed to detect wayland compositor: socket_path is not absolute");
}
let mut socket_path: PathBuf = std::env::var_os("XDG_RUNTIME_DIR")
.unwrap_or_else(|| {
log::warn!("XDG_RUNTIME_DIR is not set! Defaulting to /run/user/UID");
let uid = rustix::process::getuid();
std::ffi::OsString::from(format!("/run/user/{}", uid.as_raw()))
})
.into();

socket_path.push(socket_name);
socket_path
};

std::os::unix::net::UnixStream::connect(socket_path)
.expect("failed to connect to socket")
.into()
match std::os::unix::net::UnixStream::connect(&socket_path) {
Ok(stream) => stream.into(),
Err(e) => panic!("failed to connect to wayland socket at {socket_path:?}: {e}"),
}
}
}

Expand Down

0 comments on commit 3eb6870

Please sign in to comment.