From 43acf7f42f4474d2c66815e9c1b392b4d3175594 Mon Sep 17 00:00:00 2001 From: John Nunley Date: Sat, 22 Jul 2023 02:32:27 -0700 Subject: [PATCH] Replace libc with rustix in some modules Unfortunately this isn't a total removal, for two reasons: - We still need "libc" for the Xlib XIM implementation, for locales. - BSD requires libc to check for main-threadedness. First one we can likely resolve in the near future, not so sure about the second one without using some weird pthreads trick. --- Cargo.toml | 4 +- src/platform_impl/linux/mod.rs | 4 +- .../linux/x11/event_processor.rs | 11 ++-- src/platform_impl/linux/x11/mod.rs | 2 +- src/platform_impl/linux/x11/window.rs | 53 +++++++------------ src/platform_impl/linux/x11/xdisplay.rs | 2 +- 6 files changed, 32 insertions(+), 44 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index cfaf593b05..264eec8cf4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -36,7 +36,7 @@ rustdoc-args = ["--cfg", "docsrs"] [features] default = ["x11", "wayland", "wayland-dlopen", "wayland-csd-adwaita"] -x11 = ["x11-dl", "bytemuck", "rustix", "percent-encoding", "xkbcommon-dl/x11", "x11rb"] +x11 = ["x11-dl", "bytemuck", "percent-encoding", "xkbcommon-dl/x11", "x11rb"] wayland = ["wayland-client", "wayland-backend", "wayland-protocols", "sctk", "fnv", "memmap2"] wayland-dlopen = ["wayland-backend/dlopen"] wayland-csd-adwaita = ["sctk-adwaita", "sctk-adwaita/ab_glyph"] @@ -123,7 +123,7 @@ wayland-client = { version = "0.30.0", optional = true } wayland-backend = { version = "0.1.0", default_features = false, features = ["client_system"], optional = true } wayland-protocols = { version = "0.30.0", features = [ "staging"], optional = true } calloop = "0.10.5" -rustix = { version = "0.38.4", default-features = false, features = ["std", "system", "process"], optional = true } +rustix = { version = "0.38.4", default-features = false, features = ["std", "system", "thread", "process"] } x11-dl = { version = "2.18.5", optional = true } x11rb = { version = "0.12.0", default-features = false, features = ["allow-unsafe-code", "dl-libxcb", "xinput", "xkb"], optional = true } xkbcommon-dl = "0.4.0" diff --git a/src/platform_impl/linux/mod.rs b/src/platform_impl/linux/mod.rs index fa3ef92843..47c57700a5 100644 --- a/src/platform_impl/linux/mod.rs +++ b/src/platform_impl/linux/mod.rs @@ -935,9 +935,7 @@ fn sticky_exit_callback( #[cfg(target_os = "linux")] fn is_main_thread() -> bool { - use libc::{c_long, getpid, syscall, SYS_gettid}; - - unsafe { syscall(SYS_gettid) == getpid() as c_long } + rustix::thread::gettid() == rustix::process::getpid() } #[cfg(any(target_os = "dragonfly", target_os = "freebsd", target_os = "openbsd"))] diff --git a/src/platform_impl/linux/x11/event_processor.rs b/src/platform_impl/linux/x11/event_processor.rs index b049dd378e..e2cee68ff3 100644 --- a/src/platform_impl/linux/x11/event_processor.rs +++ b/src/platform_impl/linux/x11/event_processor.rs @@ -1,6 +1,11 @@ -use std::{cell::RefCell, collections::HashMap, rc::Rc, slice, sync::Arc}; - -use libc::{c_char, c_int, c_long, c_ulong}; +use std::{ + cell::RefCell, + collections::HashMap, + os::raw::{c_char, c_int, c_long, c_ulong}, + rc::Rc, + slice, + sync::Arc, +}; use x11rb::protocol::xproto::{self, ConnectionExt as _}; use x11rb::x11_utils::Serialize; diff --git a/src/platform_impl/linux/x11/mod.rs b/src/platform_impl/linux/x11/mod.rs index fd7df53a4f..14e7905695 100644 --- a/src/platform_impl/linux/x11/mod.rs +++ b/src/platform_impl/linux/x11/mod.rs @@ -230,7 +230,7 @@ impl EventLoop { xconn.display, &mut xinput_major_ver, &mut xinput_minor_ver, - ) != ffi::Success as libc::c_int + ) != ffi::Success as std::os::raw::c_int { panic!( "X server has XInput extension {xinput_major_ver}.{xinput_minor_ver} but does not support XInput2", diff --git a/src/platform_impl/linux/x11/window.rs b/src/platform_impl/linux/x11/window.rs index 75774f8dcd..7d58fc75e4 100644 --- a/src/platform_impl/linux/x11/window.rs +++ b/src/platform_impl/linux/x11/window.rs @@ -1,14 +1,12 @@ use std::{ cmp, env, ffi::CString, - mem::{replace, MaybeUninit}, + mem::replace, os::raw::*, path::Path, - ptr, slice, sync::{Arc, Mutex, MutexGuard}, }; -use libc; use raw_window_handle::{RawDisplayHandle, RawWindowHandle, XlibDisplayHandle, XlibWindowHandle}; use x11rb::{ connection::Connection, @@ -542,41 +540,28 @@ impl UnownedWindow { let atoms = self.xconn.atoms(); let pid_atom = atoms[_NET_WM_PID]; let client_machine_atom = atoms[WM_CLIENT_MACHINE]; - unsafe { - // 64 would suffice for Linux, but 256 will be enough everywhere (as per SUSv2). For instance, this is - // the limit defined by OpenBSD. - const MAXHOSTNAMELEN: usize = 256; - // `assume_init` is safe here because the array consists of `MaybeUninit` values, - // which do not require initialization. - let mut buffer: [MaybeUninit; MAXHOSTNAMELEN] = - MaybeUninit::uninit().assume_init(); - let status = libc::gethostname(buffer.as_mut_ptr() as *mut c_char, buffer.len()); - if status != 0 { - return Ok(None); - } - ptr::write(buffer[MAXHOSTNAMELEN - 1].as_mut_ptr() as *mut u8, b'\0'); // a little extra safety - let hostname_length = libc::strlen(buffer.as_ptr() as *const c_char); - let hostname = slice::from_raw_parts(buffer.as_ptr() as *const c_char, hostname_length); + // Get the hostname and the PID. + let uname = rustix::system::uname(); + let pid = rustix::process::getpid(); - self.xconn - .change_property( - self.xwindow, - pid_atom, - xproto::Atom::from(xproto::AtomEnum::CARDINAL), - xproto::PropMode::REPLACE, - &[libc::getpid() as util::Cardinal], - )? - .ignore_error(); - let flusher = self.xconn.change_property( + self.xconn + .change_property( self.xwindow, - client_machine_atom, - xproto::Atom::from(xproto::AtomEnum::STRING), + pid_atom, + xproto::Atom::from(xproto::AtomEnum::CARDINAL), xproto::PropMode::REPLACE, - &hostname[0..hostname_length], - ); - flusher.map(Some) - } + &[pid.as_raw_nonzero().get() as util::Cardinal], + )? + .ignore_error(); + let flusher = self.xconn.change_property( + self.xwindow, + client_machine_atom, + xproto::Atom::from(xproto::AtomEnum::STRING), + xproto::PropMode::REPLACE, + uname.nodename().to_bytes(), + ); + flusher.map(Some) } fn set_window_types( diff --git a/src/platform_impl/linux/x11/xdisplay.rs b/src/platform_impl/linux/x11/xdisplay.rs index 016f0586bf..7535daba17 100644 --- a/src/platform_impl/linux/x11/xdisplay.rs +++ b/src/platform_impl/linux/x11/xdisplay.rs @@ -46,7 +46,7 @@ unsafe impl Send for XConnection {} unsafe impl Sync for XConnection {} pub type XErrorHandler = - Option libc::c_int>; + Option std::os::raw::c_int>; impl XConnection { pub fn new(error_handler: XErrorHandler) -> Result {