Skip to content

Commit 99db48a

Browse files
Merge pull request #3 from jannau/service-autostart
Add systemd service file and udev rules to start it, drop priviledges
2 parents 52b2f97 + f0b59a0 commit 99db48a

5 files changed

Lines changed: 36 additions & 220 deletions

File tree

Cargo.lock

Lines changed: 8 additions & 216 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ edition = "2021"
66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
77

88
[dependencies]
9-
cairo-rs = "0.17"
9+
cairo-rs = { version = "0.17", default-features = false }
1010
drm = "0.9"
1111
anyhow = "1"
1212
input = "0.8"
1313
libc = "0.2"
1414
input-linux = "0.6"
1515
input-linux-sys = "0.8"
1616
nix = "0.26"
17+
privdrop = "0.5.3"
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[Unit]
2+
Description=Tiny Apple silicon touch bar daemon
3+
After=systemd-user-sessions.service [email protected] plymouth-quit.service systemd-logind.service
4+
StartLimitIntervalSec=30
5+
StartLimitBurst=2
6+
ConditionFirmware=|device-tree-compatible(apple,j293)
7+
ConditionFirmware=|device-tree-compatible(apple,j493)
8+
9+
[Service]
10+
ExecStart=/usr/bin/tiny-dfr
11+
Restart=always
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
SUBSYSTEM=="input", ATTR{name}=="MacBookPro17,1 Touch Bar", TAG+="systemd", ENV{SYSTEMD_WANTS}="tiny-dfr.service"
2+
SUBSYSTEM=="input", ATTR{name}=="Mac14,7 Touch Bar", TAG+="systemd", ENV{SYSTEMD_WANTS}="tiny-dfr.service"

src/main.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ use libc::{O_RDONLY, O_RDWR, O_WRONLY};
3333
use input_linux::{uinput::UInputHandle, EventKind, Key, SynchronizeKind};
3434
use input_linux_sys::{uinput_setup, input_id, timeval, input_event};
3535
use nix::poll::{poll, PollFd, PollFlags};
36+
use privdrop;
3637

3738
const DFR_WIDTH: i32 = 2008;
3839
const DFR_HEIGHT: i32 = 64;
@@ -298,8 +299,7 @@ fn find_backlight() -> Result<PathBuf> {
298299
Err(anyhow!("No backlight device found"))
299300
}
300301

301-
fn set_backlight(path: &Path, value: u32) {
302-
let mut file = OpenOptions::new().write(true).open(path).unwrap();
302+
fn set_backlight(mut file: &File, value: u32) {
303303
file.write(format!("{}\n", value).as_bytes()).unwrap();
304304
}
305305

@@ -395,6 +395,16 @@ fn main() {
395395
]
396396
}).unwrap();
397397
uinput.dev_create().unwrap();
398+
399+
let bl_file = OpenOptions::new().write(true).open(bl_path).unwrap();
400+
401+
privdrop::PrivDrop::default()
402+
.chroot("/var/empty")
403+
.user("nobody")
404+
.group("nobody")
405+
.apply()
406+
.unwrap_or_else(|e| { panic!("Failed to drop privileges: {}", e) });
407+
398408
let mut digitizer: Option<InputDevice> = None;
399409
let mut touches = HashMap::new();
400410
let mut last_active = Instant::now();
@@ -499,7 +509,7 @@ fn main() {
499509
};
500510
if current_bl != new_bl {
501511
current_bl = new_bl;
502-
set_backlight(&bl_path, current_bl);
512+
set_backlight(&bl_file, current_bl);
503513
}
504514
}
505515
}

0 commit comments

Comments
 (0)