Commit Diff


commit - 55948249dc480929b23aa0eccbc667e0e396e946
commit + 9b2d8449e0a874205a43b2752ed4f2db78fa1990
blob - df799521a687049eaa726088c6b118d3da3b69c9
blob + 67a751a473f26c94c0e2d154bb46fb2e47aa1702
--- src/s6.rs
+++ src/s6.rs
@@ -2,12 +2,15 @@ use std::{
     ffi::OsStr,
     fs::{File, OpenOptions},
     io::{self, ErrorKind, Read},
-    os::{fd::AsRawFd, unix::fs::OpenOptionsExt},
+    os::{
+        fd::{AsFd, AsRawFd, BorrowedFd},
+        unix::fs::OpenOptionsExt,
+    },
     path::{Path, PathBuf},
     ptr::addr_of_mut,
 };
 
-fn locked(f: &File) -> io::Result<bool> {
+fn locked(fd: BorrowedFd) -> io::Result<bool> {
     let mut flock = libc::flock {
         l_type: libc::F_RDLCK as libc::c_short,
         l_whence: libc::SEEK_SET as libc::c_short,
@@ -15,7 +18,7 @@ fn locked(f: &File) -> io::Result<bool> {
         l_len: 0,
         l_pid: 0,
     };
-    match unsafe { libc::fcntl(f.as_raw_fd(), libc::F_GETLK, addr_of_mut!(flock)) } {
+    match unsafe { libc::fcntl(fd.as_raw_fd(), libc::F_GETLK, addr_of_mut!(flock)) } {
         -1 => Err(io::Error::last_os_error()),
         0 => Ok(flock.l_type != libc::F_UNLCK as libc::c_short),
         _ => todo!("fcntl() != -1 && fcntl != 0"),
@@ -31,22 +34,26 @@ struct SvStatus {
     ready: bool,
 }
 
+impl From<[u8; 35]> for SvStatus {
+    fn from(pack: [u8; 35]) -> Self {
+        Self {
+            pid: u64::from_be_bytes(pack[24..32].try_into().expect("8 bytes")),
+            wstat: u16::from_be_bytes(pack[32..34].try_into().expect("2 bytes")),
+            paused: pack[34] & 1 == 1,
+            finishing: pack[34] & 2 == 2,
+            wantup: pack[34] & 4 == 4,
+            ready: pack[34] & 8 == 8,
+        }
+    }
+}
+
 impl TryFrom<File> for SvStatus {
     type Error = io::Error;
 
     fn try_from(mut file: File) -> Result<Self, Self::Error> {
         let mut pack = [0; 35];
         file.read_exact(&mut pack)?;
-        Ok(Self {
-            pid: u64::from_be_bytes([
-                pack[24], pack[25], pack[26], pack[27], pack[28], pack[29], pack[30], pack[31],
-            ]),
-            wstat: u16::from_be_bytes([pack[32], pack[33]]),
-            paused: pack[34] & 1 == 1,
-            finishing: pack[34] & 2 == 2,
-            wantup: pack[34] & 4 == 4,
-            ready: pack[34] & 8 == 8,
-        })
+        Ok(SvStatus::from(pack))
     }
 }
 
@@ -87,7 +94,7 @@ impl ServiceDir {
                 ErrorKind::NotFound => Ok(false),
                 _ => Err(err),
             },
-            Ok(lock) => locked(&lock),
+            Ok(lock) => locked(lock.as_fd()),
         }
     }
     pub fn status(&self) -> io::Result<Status> {