Use double-fork when spawning subprocesses
Browse files at this commit80dce054cb8da4a34b1863a7642baeeebf66ebbeParent: 109668f3 changed files +54 -10
- src/input.rs +2 -6
- src/main.rs +2 -3
- src/utils.rs +50 -1
src/input.rsmodified+2 -6
| @@ -1,5 +1,3 @@ | |||
| 1 | -use std::process::Command; | ||
| 2 | - | ||
| 3 | use smithay::backend::input::{ | 1 | use smithay::backend::input::{ |
| 4 | AbsolutePositionEvent, Axis, AxisSource, ButtonState, Device, DeviceCapability, Event, | 2 | AbsolutePositionEvent, Axis, AxisSource, ButtonState, Device, DeviceCapability, Event, |
| 5 | GestureBeginEvent, GestureEndEvent, GesturePinchUpdateEvent as _, GestureSwipeUpdateEvent as _, | 3 | GestureBeginEvent, GestureEndEvent, GesturePinchUpdateEvent as _, GestureSwipeUpdateEvent as _, |
| @@ -19,7 +17,7 @@ use smithay::wayland::tablet_manager::{TabletDescriptor, TabletSeatTrait}; | |||
| 19 | 17 | ||
| 20 | use crate::config::{Action, Config, Modifiers}; | 18 | use crate::config::{Action, Config, Modifiers}; |
| 21 | use crate::niri::State; | 19 | use crate::niri::State; |
| 22 | -use crate::utils::get_monotonic_time; | 20 | +use crate::utils::{get_monotonic_time, spawn}; |
| 23 | 21 | ||
| 24 | pub enum CompositorMod { | 22 | pub enum CompositorMod { |
| 25 | Super, | 23 | Super, |
| @@ -149,9 +147,7 @@ impl State { | |||
| 149 | } | 147 | } |
| 150 | Action::Spawn(command) => { | 148 | Action::Spawn(command) => { |
| 151 | if let Some((command, args)) = command.split_first() { | 149 | if let Some((command, args)) = command.split_first() { |
| 152 | - if let Err(err) = Command::new(command).args(args).spawn() { | 150 | + spawn(command, args); |
| 153 | - warn!("error spawning {command}: {err}"); | ||
| 154 | - } | ||
| 155 | } | 151 | } |
| 156 | } | 152 | } |
| 157 | Action::Screenshot => { | 153 | Action::Screenshot => { |
src/main.rsmodified+2 -3
| @@ -25,6 +25,7 @@ use portable_atomic::Ordering; | |||
| 25 | use smithay::reexports::calloop::EventLoop; | 25 | use smithay::reexports::calloop::EventLoop; |
| 26 | use smithay::reexports::wayland_server::Display; | 26 | use smithay::reexports::wayland_server::Display; |
| 27 | use tracing_subscriber::EnvFilter; | 27 | use tracing_subscriber::EnvFilter; |
| 28 | +use utils::spawn; | ||
| 28 | 29 | ||
| 29 | #[derive(Parser)] | 30 | #[derive(Parser)] |
| 30 | #[command(author, version, about, long_about = None)] | 31 | #[command(author, version, about, long_about = None)] |
| @@ -76,9 +77,7 @@ fn main() { | |||
| 76 | let mut data = LoopData { display, state }; | 77 | let mut data = LoopData { display, state }; |
| 77 | 78 | ||
| 78 | if let Some((command, args)) = cli.command.split_first() { | 79 | if let Some((command, args)) = cli.command.split_first() { |
| 79 | - if let Err(err) = std::process::Command::new(command).args(args).spawn() { | 80 | + spawn(command, args); |
| 80 | - warn!("error spawning command: {err:?}"); | ||
| 81 | - } | ||
| 82 | } | 81 | } |
| 83 | 82 | ||
| 84 | event_loop | 83 | event_loop |
src/utils.rsmodified+50 -1
| @@ -1,6 +1,9 @@ | |||
| 1 | +use std::ffi::OsStr; | ||
| 1 | use std::fs::File; | 2 | use std::fs::File; |
| 2 | -use std::io::Read; | 3 | +use std::io::{self, Read}; |
| 4 | +use std::os::unix::process::CommandExt; | ||
| 3 | use std::path::PathBuf; | 5 | use std::path::PathBuf; |
| 6 | +use std::process::{Command, Stdio}; | ||
| 4 | use std::time::Duration; | 7 | use std::time::Duration; |
| 5 | 8 | ||
| 6 | use anyhow::{anyhow, Context}; | 9 | use anyhow::{anyhow, Context}; |
| @@ -107,3 +110,49 @@ pub fn make_screenshot_path() -> anyhow::Result<PathBuf> { | |||
| 107 | 110 | ||
| 108 | Ok(path) | 111 | Ok(path) |
| 109 | } | 112 | } |
| 113 | + | ||
| 114 | +/// Spawns the command to run independently of the compositor. | ||
| 115 | +pub fn spawn(command: impl AsRef<OsStr>, args: impl IntoIterator<Item = impl AsRef<OsStr>>) { | ||
| 116 | + let _span = tracy_client::span!(); | ||
| 117 | + | ||
| 118 | + let command = command.as_ref(); | ||
| 119 | + | ||
| 120 | + let mut process = Command::new(command); | ||
| 121 | + process | ||
| 122 | + .args(args) | ||
| 123 | + .stdin(Stdio::null()) | ||
| 124 | + .stdout(Stdio::null()) | ||
| 125 | + .stderr(Stdio::null()); | ||
| 126 | + | ||
| 127 | + // Double-fork to avoid having to waitpid the child. | ||
| 128 | + unsafe { | ||
| 129 | + process.pre_exec(|| { | ||
| 130 | + match libc::fork() { | ||
| 131 | + -1 => return Err(io::Error::last_os_error()), | ||
| 132 | + 0 => (), | ||
| 133 | + _ => libc::_exit(0), | ||
| 134 | + } | ||
| 135 | + | ||
| 136 | + Ok(()) | ||
| 137 | + }); | ||
| 138 | + } | ||
| 139 | + | ||
| 140 | + let mut child = match process.spawn() { | ||
| 141 | + Ok(child) => child, | ||
| 142 | + Err(err) => { | ||
| 143 | + warn!("error spawning {command:?}: {err:?}"); | ||
| 144 | + return; | ||
| 145 | + } | ||
| 146 | + }; | ||
| 147 | + | ||
| 148 | + match child.wait() { | ||
| 149 | + Ok(status) => { | ||
| 150 | + if !status.success() { | ||
| 151 | + warn!("child did not exit successfully: {status:?}"); | ||
| 152 | + } | ||
| 153 | + } | ||
| 154 | + Err(err) => { | ||
| 155 | + warn!("error waiting for child: {err:?}"); | ||
| 156 | + } | ||
| 157 | + } | ||
| 158 | +} | ||