1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
//! [`EventLoginLogin`]

use std::ffi::OsStr;

use endpoint_sec_sys::{es_event_login_login_t, uid_t};

/// Authenticated login event from `/usr/bin/login`.
#[doc(alias = "es_event_login_login_t")]
pub struct EventLoginLogin<'a> {
    /// Raw event
    pub(crate) raw: &'a es_event_login_login_t,
}

impl<'a> EventLoginLogin<'a> {
    /// True iff login was successful.
    #[inline(always)]
    pub fn success(&self) -> bool {
        self.raw.success
    }

    /// Optional. Failure message generated.
    #[inline(always)]
    pub fn failure_message(&self) -> &'a OsStr {
        // Safety: 'a tied to self, object obtained through ES
        unsafe { self.raw.failure_message.as_os_str() }
    }

    /// Username used for login.
    #[inline(always)]
    pub fn username(&self) -> &'a OsStr {
        // Safety: 'a tied to self, object obtained through ES
        unsafe { self.raw.username.as_os_str() }
    }

    /// Describes whether or not the uid of the user logged in is available.
    #[inline(always)]
    pub fn has_uid(&self) -> bool {
        self.raw.has_uid
    }

    /// UID of user that was logged in.
    #[inline(always)]
    pub fn uid(&self) -> Option<uid_t> {
        // Safety: access is gated on documented conditions
        #[allow(clippy::unnecessary_lazy_evaluations)]
        self.has_uid().then(|| unsafe { self.raw.anon0.uid })
    }
}

// Safety: safe to send acrosss threads: does not contain any interior mutability nor depend on current thread state
unsafe impl Send for EventLoginLogin<'_> {}

impl_debug_eq_hash_with_functions!(
    EventLoginLogin<'a>;
    success, failure_message, username, has_uid, uid,
);