[PR Feedback] Pull platform abstraction up to PIDFILE_PERMISSIONS
Since this is the only meaningful difference between the platforms, it
makes sense to restrict the conditional compilation to this most
narrow scope.
@@ -16,6 +16,8 @@ use habitat_common::{outputln,
#[cfg(unix)]
use habitat_core::os::users;
use habitat_core::{fs,
+ fs::{AtomicWriter,
+ Permissions},
os::process::{self,
Pid},
service::ServiceGroup};
@@ -36,6 +38,15 @@ use std::{fs::File,
static LOGKEY: &str = "SV";
+// We only set PID file permissions on Unix-like systems. On Windows,
+// the file will inherit the permissions of the parent directory. In
+// this case, the parent directory should already allow broad reading
+// of the PID file.
+#[cfg(windows)]
+const PIDFILE_PERMISSIONS: Permissions = Permissions::Standard;
+#[cfg(not(windows))]
+const PIDFILE_PERMISSIONS: Permissions = Permissions::Explicit(0o644);
+
#[derive(Debug)]
pub struct Supervisor {
service_group: ServiceGroup,
@@ -255,20 +266,9 @@ impl Supervisor {
debug!(target: "pidfile_tracing", "Creating PID file for child {} -> {}",
pid_file.display(),
pid);
-
- #[cfg(windows)]
- fs::atomic_write(pid_file, pid.to_string())?;
- #[cfg(not(windows))]
- {
- // We only set PID file permissions on unix-like systems. On
- // windows, the file will inherit the permissions of the
- // parent directory. In this case, the parent directory should
- // already allow broad reading of the PID file.
- const PIDFILE_PERMISSIONS: u32 = 0o644;
- let w = fs::AtomicWriter::new_with_permissions(pid_file, fs::Permissions::Explicit(PIDFILE_PERMISSIONS))?;
+ let w = AtomicWriter::new_with_permissions(pid_file, PIDFILE_PERMISSIONS)?;
w.with_writer(|f| f.write_all(pid.to_string().as_ref()))?;
}
- }
Ok(())
}
