Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

server: Use std::fs APIs instead of nix where possible #659

Merged
merged 1 commit into from
Sep 25, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 21 additions & 24 deletions wayland-server/src/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,19 @@
ffi::{OsStr, OsString},
fs::{self, File},
io,
os::unix::io::{AsFd, BorrowedFd},
os::unix::{
io::{AsRawFd, FromRawFd, RawFd},
fs::OpenOptionsExt,
io::{AsFd, BorrowedFd},
},
os::unix::{
io::{AsRawFd, RawFd},
net::{UnixListener, UnixStream},
prelude::MetadataExt,
},
path::PathBuf,
};

use nix::{
fcntl::{flock, open, FlockArg, OFlag},
sys::stat::{lstat, Mode},
unistd::unlink,
};
use nix::fcntl::{flock, FlockArg};

/// An utility representing a unix socket on which your compositor is listening for new clients
#[derive(Debug)]
Expand Down Expand Up @@ -83,18 +82,16 @@
// https://gitlab.freedesktop.org/libbsd/libbsd/-/blob/73b25a8f871b3a20f6ff76679358540f95d7dbfd/src/flopen.c#L71
loop {
// open the lockfile
let lock_fd = open(
&lock_path,
OFlag::O_CREAT | OFlag::O_CLOEXEC | OFlag::O_RDWR,
Mode::S_IRUSR | Mode::S_IWUSR | Mode::S_IRGRP | Mode::S_IWGRP,
)
.map_err(|_| BindError::PermissionDenied)?;

// SAFETY: We have just opened the file descriptor.
_lock = unsafe { File::from_raw_fd(lock_fd) };
_lock = File::options()
.create(true)
.read(true)
.write(true)
.mode(0o660)
.open(&lock_path)
.map_err(|_| BindError::PermissionDenied)?;
Comment on lines +85 to +91
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CLOEXEC set by default by the File constructor I assume?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it should set CLOEXEC regardless of what options are used.


// lock the lockfile
if flock(lock_fd, FlockArg::LockExclusiveNonblock).is_err() {
if flock(_lock.as_raw_fd(), FlockArg::LockExclusiveNonblock).is_err() {
return Err(BindError::AlreadyInUse);
}

Expand All @@ -120,17 +117,17 @@
}

// check if an old socket exists, and cleanup if relevant
match lstat(&socket_path) {
Err(nix::Error::ENOENT) => {
match socket_path.try_exists() {
Ok(false) => {
// none exist, good
}
Ok(_) => {
Ok(true) => {
// one exist, remove it
unlink(&socket_path).map_err(|_| BindError::AlreadyInUse)?;
fs::remove_file(&socket_path).map_err(|_| BindError::AlreadyInUse)?;

Check warning on line 126 in wayland-server/src/socket.rs

View check run for this annotation

Codecov / codecov/patch

wayland-server/src/socket.rs#L126

Added line #L126 was not covered by tests
}
Err(e) => {
// some error stat-ing the socket?
return Err(BindError::Io(e.into()));
return Err(BindError::Io(e));

Check warning on line 130 in wayland-server/src/socket.rs

View check run for this annotation

Codecov / codecov/patch

wayland-server/src/socket.rs#L130

Added line #L130 was not covered by tests
}
}

Expand Down Expand Up @@ -191,8 +188,8 @@

impl Drop for ListeningSocket {
fn drop(&mut self) {
let _ = unlink(&self.socket_path);
let _ = unlink(&self.lock_path);
let _ = fs::remove_file(&self.socket_path);
let _ = fs::remove_file(&self.lock_path);
}
}

Expand Down
Loading