From ea5fbaa8929b8c24dacdfd668401a72adb770521 Mon Sep 17 00:00:00 2001 From: Sylvain Garant Date: Thu, 23 Nov 2023 14:34:08 +0100 Subject: [PATCH] Allow chmod for non-root users --- daemonize/src/lib.rs | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/daemonize/src/lib.rs b/daemonize/src/lib.rs index 4f0f758c..ec6d6328 100644 --- a/daemonize/src/lib.rs +++ b/daemonize/src/lib.rs @@ -223,6 +223,7 @@ pub struct Daemonize { directory: PathBuf, pid_file: Option, chown_pid_file: bool, + set_group_and_user_id: bool, user: Option, group: Option, umask: Mask, @@ -239,6 +240,7 @@ impl fmt::Debug for Daemonize { .field("directory", &self.directory) .field("pid_file", &self.pid_file) .field("chown_pid_file", &self.chown_pid_file) + .field("set_group_and_user_id", &self.set_group_and_user_id) .field("user", &self.user) .field("group", &self.group) .field("umask", &self.umask) @@ -262,6 +264,7 @@ impl Daemonize<()> { directory: Path::new("/").to_owned(), pid_file: None, chown_pid_file: false, + set_group_and_user_id: true, user: None, group: None, umask: 0o027.into(), @@ -287,6 +290,12 @@ impl Daemonize { self } + /// If `set` is true, daemonize will set the effective group and user ID of the calling process, if user or group are provided + pub fn set_group_and_user_id(mut self, set: bool) -> Self { + self.set_group_and_user_id = set; + self + } + /// Change working directory to `path` or `/` by default. pub fn working_directory>(mut self, path: F) -> Self { self.directory = path.as_ref().to_owned(); @@ -414,12 +423,14 @@ impl Daemonize { change_root(root)?; } - if let Some(gid) = gid { - set_group(gid)?; - } + if self.set_group_and_user_id { + if let Some(gid) = gid { + set_group(gid)?; + } - if let Some(uid) = uid { - set_user(uid)?; + if let Some(uid) = uid { + set_user(uid)?; + } } if let Some(pid_file_fd) = pid_file_fd {