Skip to content

std: Allow to spawn a process as a session leader on UNIX #26470

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 4, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/libstd/sys/unix/ext/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,17 @@ pub trait CommandExt {
/// the same semantics as the `uid` field.
#[stable(feature = "rust1", since = "1.0.0")]
fn gid(&mut self, id: gid_t) -> &mut process::Command;

/// 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.
#[unstable(feature = "process_session_leader", reason = "recently added")]
fn session_leader(&mut self, on: bool) -> &mut process::Command;
}

#[stable(feature = "rust1", since = "1.0.0")]
Expand All @@ -45,6 +56,11 @@ impl CommandExt for process::Command {
self.as_inner_mut().gid = Some(id);
self
}

fn session_leader(&mut self, on: bool) -> &mut process::Command {
self.as_inner_mut().session_leader = on;
self
}
}

/// Unix-specific extensions to `std::process::ExitStatus`
Expand Down
6 changes: 3 additions & 3 deletions src/libstd/sys/unix/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub struct Command {
pub cwd: Option<CString>,
pub uid: Option<uid_t>,
pub gid: Option<gid_t>,
pub detach: bool, // not currently exposed in std::process
pub session_leader: bool,
}

impl Command {
Expand All @@ -48,7 +48,7 @@ impl Command {
cwd: None,
uid: None,
gid: None,
detach: false,
session_leader: false,
}
}

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