From 8254e702c06f84d09b09d1ba7095b73bc604df80 Mon Sep 17 00:00:00 2001 From: sndyuk Date: Tue, 3 Aug 2021 21:28:25 +0900 Subject: [PATCH] Ensure pid file ends with line separator --- daemonize-tests/tests/tests.rs | 4 +++- daemonize/src/lib.rs | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/daemonize-tests/tests/tests.rs b/daemonize-tests/tests/tests.rs index 18179d275..317f334f0 100644 --- a/daemonize-tests/tests/tests.rs +++ b/daemonize-tests/tests/tests.rs @@ -38,7 +38,9 @@ fn pid() { .pid_file(&path) .sleep(std::time::Duration::from_secs(5)) .run(); - let pid = std::fs::read_to_string(&path).unwrap().parse().unwrap(); + let pid_content = std::fs::read_to_string(&path).unwrap(); + assert!(pid_content.ends_with('\n')); + let pid = pid_content[..pid_content.len() - 1].parse().unwrap(); assert_eq!(result.unwrap().pid, pid); let result = Tester::new().pid_file(&path).run(); diff --git a/daemonize/src/lib.rs b/daemonize/src/lib.rs index 28a5750a2..c9d77e3e0 100644 --- a/daemonize/src/lib.rs +++ b/daemonize/src/lib.rs @@ -537,7 +537,7 @@ unsafe fn chown_pid_file( unsafe fn write_pid_file(fd: libc::c_int) -> Result<(), ErrorKind> { let pid = libc::getpid(); - let pid_buf = format!("{}", pid).into_bytes(); + let pid_buf = format!("{}\n", pid).into_bytes(); let pid_length = pid_buf.len(); let pid_c = CString::new(pid_buf).unwrap(); check_err(libc::ftruncate(fd, 0), ErrorKind::TruncatePidfile)?;