forge
Explore
Log in

Don't you dare go Hollow

utils/spawn: Receive grandchild PID

Browse files at this commit

13b6c74cc376c0ddff6d83f713cd32aa07a89fd7Ivan MolodetskikhParent: d8fb8d5
src/utils.rsmodified+80 -3
@@ -1,5 +1,6 @@
1 use std::ffi::{CString, OsStr};1 use std::ffi::{CString, OsStr};
2 use std::io::{self, Write};2 use std::io::{self, Write};
3+use std::os::fd::{AsFd, AsRawFd, FromRawFd, OwnedFd};
3 use std::os::unix::prelude::OsStrExt;4 use std::os::unix::prelude::OsStrExt;
4 use std::os::unix::process::CommandExt;5 use std::os::unix::process::CommandExt;
5 use std::path::PathBuf;6 use std::path::PathBuf;
@@ -14,6 +15,9 @@ use directories::UserDirs;
14 use git_version::git_version;15 use git_version::git_version;
15 use niri_config::Config;16 use niri_config::Config;
16 use smithay::output::Output;17 use smithay::output::Output;
18+use smithay::reexports::rustix;
19+use smithay::reexports::rustix::io::{close, read, retry_on_intr, write};
20+use smithay::reexports::rustix::pipe::{pipe_with, PipeFlags};
17 use smithay::reexports::rustix::time::{clock_gettime, ClockId};21 use smithay::reexports::rustix::time::{clock_gettime, ClockId};
18 use smithay::utils::{Logical, Point, Rectangle, Size};22 use smithay::utils::{Logical, Point, Rectangle, Size};
19 23
@@ -122,13 +126,40 @@ fn spawn_sync(command: impl AsRef<OsStr>, args: impl IntoIterator<Item = impl As
122 process.env_remove("RUST_LIB_BACKTRACE");126 process.env_remove("RUST_LIB_BACKTRACE");
123 }127 }
124 128
125- // Double-fork to avoid having to waitpid the child.129+ // Make a pipe to receive the grandchild PID.
130+ let (pipe_pid_read, pipe_pid_write) = pipe_with(PipeFlags::CLOEXEC)
131+ .map_err(|err| {
132+ warn!("error creating a pipe to transfer child PID: {err:?}");
133+ })
134+ .ok()
135+ .unzip();
136+
126 unsafe {137 unsafe {
127- process.pre_exec(|| {138+ // The fds will be duplicated after a fork and closed on exec or exit automatically. Get
139+ // the raw fd inside so that it's not closed any extra times.
140+ let mut pipe_pid_read_fd = pipe_pid_read.as_ref().map(|fd| fd.as_raw_fd());
141+ let mut pipe_pid_write_fd = pipe_pid_write.as_ref().map(|fd| fd.as_raw_fd());
142+
143+ // Double-fork to avoid having to waitpid the child.
144+ process.pre_exec(move || {
145+ if let Some(fd) = pipe_pid_read_fd.take() {
146+ close(fd);
147+ }
148+
149+ // Convert the our FDs to OwnedFd, which will close them in all of our fork paths.
150+ let pipe_pid_write = pipe_pid_write_fd.take().map(|fd| OwnedFd::from_raw_fd(fd));
151+
128 match libc::fork() {152 match libc::fork() {
129 -1 => return Err(io::Error::last_os_error()),153 -1 => return Err(io::Error::last_os_error()),
130 0 => (),154 0 => (),
131- _ => libc::_exit(0),155+ grandchild_pid => {
156+ // Send back the PID.
157+ if let Some(pipe) = pipe_pid_write {
158+ let _ = write_all(pipe, &grandchild_pid.to_ne_bytes());
159+ }
160+
161+ libc::_exit(0)
162+ }
132 }163 }
133 164
134 Ok(())165 Ok(())
@@ -143,6 +174,22 @@ fn spawn_sync(command: impl AsRef<OsStr>, args: impl IntoIterator<Item = impl As
143 }174 }
144 };175 };
145 176
177+ drop(pipe_pid_write);
178+
179+ // Wait for the grandchild PID.
180+ if let Some(pipe) = pipe_pid_read {
181+ let mut buf = [0; 4];
182+ match read_all(pipe, &mut buf) {
183+ Ok(()) => {
184+ let pid = i32::from_ne_bytes(buf);
185+ trace!("spawned PID: {pid}");
186+ }
187+ Err(err) => {
188+ warn!("error reading child PID: {err:?}");
189+ }
190+ }
191+ }
192+
146 match child.wait() {193 match child.wait() {
147 Ok(status) => {194 Ok(status) => {
148 if !status.success() {195 if !status.success() {
@@ -155,6 +202,36 @@ fn spawn_sync(command: impl AsRef<OsStr>, args: impl IntoIterator<Item = impl As
155 }202 }
156 }203 }
157 204
205+fn write_all(fd: impl AsFd, buf: &[u8]) -> rustix::io::Result<()> {
206+ let mut written = 0;
207+ loop {
208+ let n = retry_on_intr(|| write(&fd, &buf[written..]))?;
209+ if n == 0 {
210+ return Err(rustix::io::Errno::CANCELED);
211+ }
212+
213+ written += n;
214+ if written == buf.len() {
215+ return Ok(());
216+ }
217+ }
218+}
219+
220+fn read_all(fd: impl AsFd, buf: &mut [u8]) -> rustix::io::Result<()> {
221+ let mut start = 0;
222+ loop {
223+ let n = retry_on_intr(|| read(&fd, &mut buf[start..]))?;
224+ if n == 0 {
225+ return Err(rustix::io::Errno::CANCELED);
226+ }
227+
228+ start += n;
229+ if start == buf.len() {
230+ return Ok(());
231+ }
232+ }
233+}
234+
158 pub fn write_png_rgba8(235 pub fn write_png_rgba8(
159 w: impl Write,236 w: impl Write,
160 width: u32,237 width: u32,