Skip to content

Commit 6afb8f5

Browse files
committed
Auto merge of #26470 - l0kod:process-session-leader, r=alexcrichton
Add a new method `CommandExt::session_leader(&mut self, on: bool)` to create a new session (cf. `setsid(2)`) for the child process. This means that the child is the leader of a new process group. The parent process remains the child reaper of the new process. This is not enough to create a daemon process. The *init* process should be the child reaper of a daemon. This can be achieved if the parent process exit. Moreover, a daemon should not have a controlling terminal. To acheive this, a session leader (the child) must spawn another process (the daemon) in the same session. cc rust-lang/rfcs#941 cc #17176
2 parents eb11d65 + b3df1e6 commit 6afb8f5

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

src/libstd/sys/unix/ext/process.rs

+16
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,17 @@ pub trait CommandExt {
3232
/// the same semantics as the `uid` field.
3333
#[stable(feature = "rust1", since = "1.0.0")]
3434
fn gid(&mut self, id: gid_t) -> &mut process::Command;
35+
36+
/// Create a new session (cf. `setsid(2)`) for the child process. This means that the child is
37+
/// the leader of a new process group. The parent process remains the child reaper of the new
38+
/// process.
39+
///
40+
/// This is not enough to create a daemon process. The *init* process should be the child
41+
/// reaper of a daemon. This can be achieved if the parent process exit. Moreover, a daemon
42+
/// should not have a controlling terminal. To acheive this, a session leader (the child) must
43+
/// spawn another process (the daemon) in the same session.
44+
#[unstable(feature = "process_session_leader", reason = "recently added")]
45+
fn session_leader(&mut self, on: bool) -> &mut process::Command;
3546
}
3647

3748
#[stable(feature = "rust1", since = "1.0.0")]
@@ -45,6 +56,11 @@ impl CommandExt for process::Command {
4556
self.as_inner_mut().gid = Some(id);
4657
self
4758
}
59+
60+
fn session_leader(&mut self, on: bool) -> &mut process::Command {
61+
self.as_inner_mut().session_leader = on;
62+
self
63+
}
4864
}
4965

5066
/// Unix-specific extensions to `std::process::ExitStatus`

src/libstd/sys/unix/process.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ pub struct Command {
3636
pub cwd: Option<CString>,
3737
pub uid: Option<uid_t>,
3838
pub gid: Option<gid_t>,
39-
pub detach: bool, // not currently exposed in std::process
39+
pub session_leader: bool,
4040
}
4141

4242
impl Command {
@@ -48,7 +48,7 @@ impl Command {
4848
cwd: None,
4949
uid: None,
5050
gid: None,
51-
detach: false,
51+
session_leader: false,
5252
}
5353
}
5454

@@ -302,7 +302,7 @@ impl Process {
302302
fail(&mut output);
303303
}
304304
}
305-
if cfg.detach {
305+
if cfg.session_leader {
306306
// Don't check the error of setsid because it fails if we're the
307307
// process leader already. We just forked so it shouldn't return
308308
// error, but ignore it anyway.

0 commit comments

Comments
 (0)