From df2867d28ba2125624b14a0c57454d701824a2d2 Mon Sep 17 00:00:00 2001 From: Janne Grunau Date: Sat, 17 Jun 2023 21:20:38 +0200 Subject: [PATCH 1/3] Disable cairo's glib feature Cuts the number of used crates by one third. Signed-off-by: Janne Grunau --- Cargo.lock | 223 +---------------------------------------------------- Cargo.toml | 2 +- 2 files changed, 3 insertions(+), 222 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f146471e..5c21b546 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -37,7 +37,7 @@ checksum = "fdde5c9cd29ebd706ce1b35600920a33550e402fc998a2e53ad3b42c3c47a192" dependencies = [ "proc-macro2", "quote", - "syn 2.0.18", + "syn", ] [[package]] @@ -48,7 +48,6 @@ checksum = "ab3603c4028a5e368d09b51c8b624b9a46edcd7c3778284077a6125af73c9f0a" dependencies = [ "bitflags", "cairo-sys-rs", - "glib", "libc", "once_cell", "thiserror", @@ -60,7 +59,6 @@ version = "0.17.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "691d0c66b1fb4881be80a760cb8fe76ea97218312f9dfe2c9cc0f496ca279cb1" dependencies = [ - "glib-sys", "libc", "system-deps", ] @@ -119,135 +117,6 @@ dependencies = [ "libc", ] -[[package]] -name = "futures-channel" -version = "0.3.28" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "955518d47e09b25bbebc7a18df10b81f0c766eaf4c4f1cccef2fca5f2a4fb5f2" -dependencies = [ - "futures-core", -] - -[[package]] -name = "futures-core" -version = "0.3.28" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4bca583b7e26f571124fe5b7561d49cb2868d79116cfa0eefce955557c6fee8c" - -[[package]] -name = "futures-executor" -version = "0.3.28" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ccecee823288125bd88b4d7f565c9e58e41858e47ab72e8ea2d64e93624386e0" -dependencies = [ - "futures-core", - "futures-task", - "futures-util", -] - -[[package]] -name = "futures-macro" -version = "0.3.28" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "89ca545a94061b6365f2c7355b4b32bd20df3ff95f02da9329b34ccc3bd6ee72" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.18", -] - -[[package]] -name = "futures-task" -version = "0.3.28" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "76d3d132be6c0e6aa1534069c705a74a5997a356c0dc2f86a47765e5617c5b65" - -[[package]] -name = "futures-util" -version = "0.3.28" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "26b01e40b772d54cf6c6d721c1d1abd0647a0106a12ecaa1c186273392a69533" -dependencies = [ - "futures-core", - "futures-macro", - "futures-task", - "pin-project-lite", - "pin-utils", - "slab", -] - -[[package]] -name = "gio-sys" -version = "0.17.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0ccf87c30a12c469b6d958950f6a9c09f2be20b7773f7e70d20b867fdf2628c3" -dependencies = [ - "glib-sys", - "gobject-sys", - "libc", - "system-deps", - "winapi", -] - -[[package]] -name = "glib" -version = "0.17.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d3fad45ba8d4d2cea612b432717e834f48031cd8853c8aaf43b2c79fec8d144b" -dependencies = [ - "bitflags", - "futures-channel", - "futures-core", - "futures-executor", - "futures-task", - "futures-util", - "gio-sys", - "glib-macros", - "glib-sys", - "gobject-sys", - "libc", - "memchr", - "once_cell", - "smallvec", - "thiserror", -] - -[[package]] -name = "glib-macros" -version = "0.17.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eca5c79337338391f1ab8058d6698125034ce8ef31b72a442437fa6c8580de26" -dependencies = [ - "anyhow", - "heck", - "proc-macro-crate", - "proc-macro-error", - "proc-macro2", - "quote", - "syn 1.0.109", -] - -[[package]] -name = "glib-sys" -version = "0.17.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d80aa6ea7bba0baac79222204aa786a6293078c210abe69ef1336911d4bdc4f0" -dependencies = [ - "libc", - "system-deps", -] - -[[package]] -name = "gobject-sys" -version = "0.17.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cd34c3317740a6358ec04572c1bcfd3ac0b5b6529275fae255b237b314bb8062" -dependencies = [ - "glib-sys", - "libc", - "system-deps", -] - [[package]] name = "hashbrown" version = "0.12.3" @@ -387,12 +256,6 @@ version = "1.18.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dd8b5dd2ae5ed71462c540258bedcb51965123ad7e7ccf4b9a8cafaa4a63576d" -[[package]] -name = "pin-project-lite" -version = "0.2.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e0a7ae3ac2f1173085d398531c705756c94a4c56843785df85a60c1a0afac116" - [[package]] name = "pin-utils" version = "0.1.0" @@ -405,40 +268,6 @@ version = "0.3.27" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "26072860ba924cbfa98ea39c8c19b4dd6a4a25423dbdf219c1eca91aa0cf6964" -[[package]] -name = "proc-macro-crate" -version = "1.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7f4c021e1093a56626774e81216a4ce732a735e5bad4868a03f3ed65ca0c3919" -dependencies = [ - "once_cell", - "toml_edit", -] - -[[package]] -name = "proc-macro-error" -version = "1.0.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" -dependencies = [ - "proc-macro-error-attr", - "proc-macro2", - "quote", - "syn 1.0.109", - "version_check", -] - -[[package]] -name = "proc-macro-error-attr" -version = "1.0.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" -dependencies = [ - "proc-macro2", - "quote", - "version_check", -] - [[package]] name = "proc-macro2" version = "1.0.60" @@ -472,15 +301,6 @@ dependencies = [ "serde", ] -[[package]] -name = "slab" -version = "0.4.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6528351c9bc8ab22353f9d776db39a20288e8d6c37ef8cfe3317cf875eecfc2d" -dependencies = [ - "autocfg", -] - [[package]] name = "smallvec" version = "1.10.0" @@ -493,17 +313,6 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" -[[package]] -name = "syn" -version = "1.0.109" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" -dependencies = [ - "proc-macro2", - "quote", - "unicode-ident", -] - [[package]] name = "syn" version = "2.0.18" @@ -551,7 +360,7 @@ checksum = "f9456a42c5b0d803c8cd86e73dd7cc9edd429499f37a3550d286d5e86720569f" dependencies = [ "proc-macro2", "quote", - "syn 2.0.18", + "syn", ] [[package]] @@ -625,34 +434,6 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "579a42fc0b8e0c63b76519a339be31bed574929511fa53c1a3acae26eb258f29" -[[package]] -name = "version_check" -version = "0.9.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" - -[[package]] -name = "winapi" -version = "0.3.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" -dependencies = [ - "winapi-i686-pc-windows-gnu", - "winapi-x86_64-pc-windows-gnu", -] - -[[package]] -name = "winapi-i686-pc-windows-gnu" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" - -[[package]] -name = "winapi-x86_64-pc-windows-gnu" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" - [[package]] name = "windows-sys" version = "0.48.0" diff --git a/Cargo.toml b/Cargo.toml index 9e5cb18f..41e46b7b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,7 +6,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -cairo-rs = "0.17" +cairo-rs = { version = "0.17", default-features = false } drm = "0.9" anyhow = "1" input = "0.8" From 8747fafcd9b21640e49c16bc1d8b8e840e241442 Mon Sep 17 00:00:00 2001 From: Janne Grunau Date: Sun, 18 Jun 2023 08:24:08 +0200 Subject: [PATCH 2/3] Add systemd service file activated from udev Using the touch bar digitizer as device to trigger the start of the service. Signed-off-by: Janne Grunau --- etc/systemd/system/tiny-dfr.service | 11 +++++++++++ etc/udev/rules.d/99-touchbar-tiny-dfr.rules | 2 ++ 2 files changed, 13 insertions(+) create mode 100644 etc/systemd/system/tiny-dfr.service create mode 100644 etc/udev/rules.d/99-touchbar-tiny-dfr.rules diff --git a/etc/systemd/system/tiny-dfr.service b/etc/systemd/system/tiny-dfr.service new file mode 100644 index 00000000..c66673c7 --- /dev/null +++ b/etc/systemd/system/tiny-dfr.service @@ -0,0 +1,11 @@ +[Unit] +Description=Tiny Apple silicon touch bar daemon +After=systemd-user-sessions.service getty@tty1.service plymouth-quit.service systemd-logind.service +StartLimitIntervalSec=30 +StartLimitBurst=2 +ConditionFirmware=|device-tree-compatible(apple,j293) +ConditionFirmware=|device-tree-compatible(apple,j493) + +[Service] +ExecStart=/usr/bin/tiny-dfr +Restart=always diff --git a/etc/udev/rules.d/99-touchbar-tiny-dfr.rules b/etc/udev/rules.d/99-touchbar-tiny-dfr.rules new file mode 100644 index 00000000..5169f8fe --- /dev/null +++ b/etc/udev/rules.d/99-touchbar-tiny-dfr.rules @@ -0,0 +1,2 @@ +SUBSYSTEM=="input", ATTR{name}=="MacBookPro17,1 Touch Bar", TAG+="systemd", ENV{SYSTEMD_WANTS}="tiny-dfr.service" +SUBSYSTEM=="input", ATTR{name}=="Mac14,7 Touch Bar", TAG+="systemd", ENV{SYSTEMD_WANTS}="tiny-dfr.service" From f0b59a0a79c7f68270d35f68fcce24d14ce413f6 Mon Sep 17 00:00:00 2001 From: Janne Grunau Date: Sun, 18 Jun 2023 10:11:14 +0200 Subject: [PATCH 3/3] Drop priviledges and chroot after setup Keeps the backlight file to avoid reopening after dropping priviledges. Signed-off-by: Janne Grunau --- Cargo.lock | 11 +++++++++++ Cargo.toml | 1 + src/main.rs | 16 +++++++++++++--- 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5c21b546..8c5b762b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -268,6 +268,16 @@ version = "0.3.27" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "26072860ba924cbfa98ea39c8c19b4dd6a4a25423dbdf219c1eca91aa0cf6964" +[[package]] +name = "privdrop" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "81ed9e5437d82d5f2cde999a21571474c5f09b3d76e33eab94bf0e8e42a4fd96" +dependencies = [ + "libc", + "nix", +] + [[package]] name = "proc-macro2" version = "1.0.60" @@ -375,6 +385,7 @@ dependencies = [ "input-linux-sys", "libc", "nix", + "privdrop", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 41e46b7b..c3b2e422 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,3 +14,4 @@ libc = "0.2" input-linux = "0.6" input-linux-sys = "0.8" nix = "0.26" +privdrop = "0.5.3" diff --git a/src/main.rs b/src/main.rs index 43f5d2b9..7a7165ac 100644 --- a/src/main.rs +++ b/src/main.rs @@ -33,6 +33,7 @@ use libc::{O_RDONLY, O_RDWR, O_WRONLY}; use input_linux::{uinput::UInputHandle, EventKind, Key, SynchronizeKind}; use input_linux_sys::{uinput_setup, input_id, timeval, input_event}; use nix::poll::{poll, PollFd, PollFlags}; +use privdrop; const DFR_WIDTH: i32 = 2008; const DFR_HEIGHT: i32 = 64; @@ -298,8 +299,7 @@ fn find_backlight() -> Result { Err(anyhow!("No backlight device found")) } -fn set_backlight(path: &Path, value: u32) { - let mut file = OpenOptions::new().write(true).open(path).unwrap(); +fn set_backlight(mut file: &File, value: u32) { file.write(format!("{}\n", value).as_bytes()).unwrap(); } @@ -395,6 +395,16 @@ fn main() { ] }).unwrap(); uinput.dev_create().unwrap(); + + let bl_file = OpenOptions::new().write(true).open(bl_path).unwrap(); + + privdrop::PrivDrop::default() + .chroot("/var/empty") + .user("nobody") + .group("nobody") + .apply() + .unwrap_or_else(|e| { panic!("Failed to drop privileges: {}", e) }); + let mut digitizer: Option = None; let mut touches = HashMap::new(); let mut last_active = Instant::now(); @@ -499,7 +509,7 @@ fn main() { }; if current_bl != new_bl { current_bl = new_bl; - set_backlight(&bl_path, current_bl); + set_backlight(&bl_file, current_bl); } } }