about summary refs log tree commit diff
path: root/library/std/src/sys/process/unix/unix.rs
diff options
context:
space:
mode:
authorLevitatingBusinessMan (Rein Fernhout) <me@levitati.ng>2025-06-12 02:34:15 +0200
committerLevitatingBusinessMan (Rein Fernhout) <me@levitati.ng>2025-07-10 05:26:58 +0200
commite0f76871eddd0f1345eba7d09e5ab4812f8aa91d (patch)
tree7c536634dd9a0b2c9e5cc060839e8497108cfbff /library/std/src/sys/process/unix/unix.rs
parente703dff8fe220b78195c53478e83fb2f68d8499c (diff)
downloadrust-e0f76871eddd0f1345eba7d09e5ab4812f8aa91d.tar.gz
rust-e0f76871eddd0f1345eba7d09e5ab4812f8aa91d.zip
rust: library: Add setsid method to CommandExt trait
Add a setsid method to the CommandExt trait so that callers can create
a process in a new session and process group whilst still using the
POSIX spawn fast path.

Co-Authored-By: Harvey Hunt <harveyhunt@fb.com>
Diffstat (limited to 'library/std/src/sys/process/unix/unix.rs')
-rw-r--r--library/std/src/sys/process/unix/unix.rs14
1 files changed, 14 insertions, 0 deletions
diff --git a/library/std/src/sys/process/unix/unix.rs b/library/std/src/sys/process/unix/unix.rs
index 4f595ac9a1c..9162573ba09 100644
--- a/library/std/src/sys/process/unix/unix.rs
+++ b/library/std/src/sys/process/unix/unix.rs
@@ -340,6 +340,10 @@ impl Command {
             cvt(libc::setpgid(0, pgroup))?;
         }
 
+        if self.get_setsid() {
+            cvt(libc::setsid())?;
+        }
+
         // emscripten has no signal support.
         #[cfg(not(target_os = "emscripten"))]
         {
@@ -741,6 +745,16 @@ impl Command {
                 flags |= libc::POSIX_SPAWN_SETSIGDEF;
             }
 
+            if self.get_setsid() {
+                cfg_if::cfg_if! {
+                    if #[cfg(all(target_os = "linux", target_env = "gnu"))] {
+                        flags |= libc::POSIX_SPAWN_SETSID;
+                    } else {
+                        return Ok(None);
+                    }
+                }
+            }
+
             cvt_nz(libc::posix_spawnattr_setflags(attrs.0.as_mut_ptr(), flags as _))?;
 
             // Make sure we synchronize access to the global `environ` resource