Skip to main content

rustix/process/
types.rs

1//! Types for use with [`rustix::process`] functions.
2//!
3//! [`rustix::process`]: crate::process
4
5#![allow(unsafe_code)]
6
7#[cfg(not(target_os = "horizon"))]
8use crate::backend::c;
9#[cfg(not(target_os = "horizon"))]
10use crate::pid::Pid;
11#[cfg(not(target_os = "horizon"))]
12use core::mem::transmute;
13
14/// File lock data structure used in [`fcntl_getlk`].
15///
16/// [`fcntl_getlk`]: crate::process::fcntl_getlk()
17#[cfg(not(target_os = "horizon"))]
18#[derive(Clone, Copy, Debug, PartialEq, Eq)]
19pub struct Flock {
20    /// Starting offset for lock
21    pub start: u64,
22    /// Number of bytes to lock
23    pub length: u64,
24    /// PID of process blocking our lock. If set to `None`, it refers to the
25    /// current process
26    pub pid: Option<Pid>,
27    /// Type of lock
28    pub typ: FlockType,
29    /// Offset type of lock
30    pub offset_type: FlockOffsetType,
31}
32
33#[cfg(not(target_os = "horizon"))]
34impl Flock {
35    pub(crate) const unsafe fn from_raw_unchecked(raw_fl: c::flock) -> Self {
36        #[cfg(not(all(target_os = "hurd", target_arch = "x86")))]
37        {
38            Self {
39                start: raw_fl.l_start as _,
40                length: raw_fl.l_len as _,
41                pid: Pid::from_raw(raw_fl.l_pid),
42                typ: transmute::<i16, FlockType>(raw_fl.l_type),
43                offset_type: transmute::<i16, FlockOffsetType>(raw_fl.l_whence),
44            }
45        }
46        #[cfg(all(target_os = "hurd", target_arch = "x86"))]
47        {
48            Self {
49                start: raw_fl.l_start as _,
50                length: raw_fl.l_len as _,
51                pid: Pid::from_raw(raw_fl.l_pid),
52                typ: transmute::<i32, FlockType>(raw_fl.l_type),
53                offset_type: transmute::<i32, FlockOffsetType>(raw_fl.l_whence),
54            }
55        }
56    }
57
58    pub(crate) fn as_raw(&self) -> c::flock {
59        let mut f: c::flock = unsafe { core::mem::zeroed() };
60        f.l_start = self.start as _;
61        f.l_len = self.length as _;
62        f.l_pid = Pid::as_raw(self.pid);
63        f.l_type = self.typ as _;
64        f.l_whence = self.offset_type as _;
65        f
66    }
67}
68
69#[cfg(not(target_os = "horizon"))]
70impl From<FlockType> for Flock {
71    fn from(value: FlockType) -> Self {
72        Self {
73            start: 0,
74            length: 0,
75            pid: None,
76            typ: value,
77            offset_type: FlockOffsetType::Set,
78        }
79    }
80}
81
82/// `F_*LCK` constants for use with [`fcntl_getlk`].
83///
84/// [`fcntl_getlk`]: crate::process::fcntl_getlk()
85#[cfg(not(target_os = "horizon"))]
86#[derive(Clone, Copy, Debug, PartialEq, Eq)]
87#[cfg_attr(not(all(target_os = "hurd", target_arch = "x86")), repr(i16))]
88#[cfg_attr(all(target_os = "hurd", target_arch = "x86"), repr(i32))]
89pub enum FlockType {
90    /// `F_RDLCK`
91    ReadLock = c::F_RDLCK as _,
92    /// `F_WRLCK`
93    WriteLock = c::F_WRLCK as _,
94    /// `F_UNLCK`
95    Unlocked = c::F_UNLCK as _,
96}
97
98/// `F_SEEK*` constants for use with [`fcntl_getlk`].
99///
100/// [`fcntl_getlk`]: crate::process::fcntl_getlk()
101#[cfg(not(target_os = "horizon"))]
102#[derive(Clone, Copy, Debug, PartialEq, Eq)]
103#[cfg_attr(not(all(target_os = "hurd", target_arch = "x86")), repr(i16))]
104#[cfg_attr(all(target_os = "hurd", target_arch = "x86"), repr(i32))]
105pub enum FlockOffsetType {
106    /// `F_SEEK_SET`
107    Set = c::SEEK_SET as _,
108    /// `F_SEEK_CUR`
109    Current = c::SEEK_CUR as _,
110    /// `F_SEEK_END`
111    End = c::SEEK_END as _,
112}