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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
//! Extended account database module.
#[cfg(any(
    target_os = "linux",
    target_os = "macos",
    target_os = "netbsd",
    target_os = "solaris",
    target_os = "illumos"
))]
use std::ffi::CString;
#[cfg(target_os = "linux")]
use std::net::{self, IpAddr};
use std::{
    collections::{hash_set, HashSet},
    error::Error as StdError,
    fmt::{self, Display},
    io,
    path::{Path, PathBuf},
};
#[cfg(not(any(
    target_os = "linux",
    target_os = "macos",
    target_os = "netbsd",
    target_os = "solaris",
    target_os = "illumos"
)))]
use std::{
    fs::{self, File},
    io::{BufReader, Read},
    mem, slice,
};

use bstr::{BStr, BString, ByteSlice};
#[cfg(any(target_os = "linux", target_os = "netbsd"))]
use libc::__exit_status as ExitStatus;
#[cfg(all(target_os = "linux", any(target_arch = "x86_64")))]
use libc::c_int;
#[cfg(all(target_os = "linux", not(any(target_arch = "x86_64"))))]
use libc::c_long;
#[cfg(any(
    target_os = "linux",
    target_os = "macos",
    target_os = "netbsd",
    target_os = "solaris",
    target_os = "illumos"
))]
use libc::utmpxname;
#[cfg(any(target_os = "solaris", target_os = "illumos"))]
use libc::{c_int, c_short, exit_status as ExitStatus};
use libc::{endutxent, getutxent, setutxent, suseconds_t, time_t, utmpx};
use time::{Duration, OffsetDateTime as DateTime};

use super::{Pid, TimeVal};

/// Error type for [`UtmpxKind`] conversion.
#[derive(Debug, PartialEq, PartialOrd, Eq, Ord, Hash)]
pub enum Error {
    /// When the OS has not this [`UtmpxKind`] and a number related to that kind.
    OsNoKind,
    /// When the OS has no [`UtmpxKind`] related to this number.
    OsNoNumber,
}

impl StdError for Error {}

impl Display for Error {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Self::OsNoKind => {
                write!(f, "This OS has not this UtmpxKind and a number related to that kind")
            },
            Self::OsNoNumber => write!(f, "This OS has no UtmpxKind related to this number"),
        }
    }
}

/// Possible types of a [`Utmpx`] instance.
#[repr(u16)]
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum UtmpxKind {
    /// Not sure yet. (Linux and MacOS exclusive)
    Accounting,
    /// Time of a system boot.
    BootTime,
    /// A session leader exited.
    DeadProcess,
    /// No valid user accounting information.
    Empty,
    /// A process spawned by init(8).
    InitProcess,
    /// The session leader of a logged-in user.
    LoginProcess,
    /// Time after system clock change.
    NewTime,
    /// Time before system clock change.
    OldTime,
    /// Run level. Provided for compatibility, not used on NetBSD.
    RunLevel,
    /// Not sure yet. (MacOS exclusive)
    Signature,
    /// The session leader of a time of system shutdown.
    ShutdownProcess,
    // A user process.
    UserProcess,
    // Not sure yet.
    DownTime,
}

/// A struct that represents a __user__ account, where user can be humam users or other
/// parts of the system that requires the usage of account structure, like some daemons.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Utmpx {
    /// User login name.
    user: BString,
    /// Host name.
    host: BString,
    /// Process id creating the entry.
    pid: Pid,
    /// Record identifier. (/etc/inittab id)
    id: BString,
    /// Device name. (console/tty, lnxx)
    line: BString,
    /// Type of the entry.
    ut_type: UtmpxKind,
    /// The time entry was created.
    timeval: TimeVal, // tv
    #[cfg(any(
        target_os = "linux",
        target_os = "netbsd",
        target_os = "solaris",
        target_os = "illumos"
    ))]
    exit: ExitStatus,
    /// Session ID. (used for windowing)
    #[cfg(all(target_os = "linux", any(target_arch = "x86_64")))]
    session: c_int,
    /// Session ID. (used for windowing)
    #[cfg(any(target_os = "solaris", target_os = "illumos"))]
    session: c_int,
    /// Session ID. (used for windowing)
    #[cfg(all(target_os = "linux", not(any(target_arch = "x86_64"))))]
    session: c_long,
    /// Session ID. (used for windowing)
    #[cfg(any(target_os = "netbsd", target_os = "dragonfly"))]
    session: u16,
    /// Internet address of remote host. Looks like that if it's IPV4 `addr_v6[0]` is
    /// non-zero and the rest is zero and if is IPV6 all indexes are non-zero.
    #[cfg(target_os = "linux")]
    addr_v6: [i32; 4],
    #[cfg(target_os = "netbsd")]
    ss: libc::sockaddr_storage,
    #[cfg(any(target_os = "solaris", target_os = "illumos"))]
    syslen: c_short,
}

impl Utmpx {
    /// Get user name.
    #[inline]
    pub fn user(&self) -> &BStr {
        self.user.as_bstr()
    }

    /// Get host name.
    #[inline]
    pub fn host(&self) -> &BStr {
        self.host.as_bstr()
    }

    /// Get the process ID.
    #[inline]
    pub const fn process_id(&self) -> Pid {
        self.pid
    }

    /// Get the record ID.
    #[inline]
    pub fn id(&self) -> &BStr {
        self.id.as_bstr()
    }

    /// Get the device name of the entry (usually a tty or console).
    #[inline]
    pub fn device_name(&self) -> &BStr {
        self.line.as_bstr()
    }

    /// Get the type kind if the entry.
    #[inline]
    pub const fn entry_type(&self) -> UtmpxKind {
        self.ut_type
    }

    /// Get the time where the entry was created. (often login time)
    #[inline]
    pub const fn timeval(&self) -> TimeVal {
        self.timeval
    }
    
    /// Check if the device name of the entry is really active. Works around a quirk of
    /// BSD systems where the entry type would be USER_PROCESS after it gets killed.
    #[inline]
    pub fn is_active(&self) -> bool {
        let mut path = PathBuf::from("/dev");
        path.push(&self.line.to_string());
        path.exists()
    }

    /// Get the time where the entry was created (often login time) in a more complete
    /// structure.
    #[inline]
    pub fn login_time(&self) -> DateTime {
        DateTime::from_unix_timestamp(self.timeval.tv_sec as i64)
            + Duration::microseconds(self.timeval.tv_usec as i64)
    }

    /// Get the session ID of the entry.
    #[cfg(all(target_os = "linux", any(target_arch = "x86_64")))]
    #[inline]
    pub const fn session(&self) -> c_int {
        self.session
    }

    /// Get the session ID of the entry.
    #[cfg(any(target_os = "solaris", target_os = "illumos"))]
    #[inline]
    pub const fn session(&self) -> c_int {
        self.session
    }

    /// Get the session ID of the entry.
    #[cfg(all(target_os = "linux", not(any(target_arch = "x86_64"))))]
    #[inline]
    pub const fn session(&self) -> c_long {
        self.session
    }

    /// Get the session ID of the entry.
    #[cfg(any(target_os = "netbsd", target_os = "dragonfly"))]
    #[inline]
    pub const fn session(&self) -> u16 {
        self.session
    }

    /// Get the IP address of the entry.
    #[cfg(target_os = "linux")]
    #[inline]
    pub fn address(&self) -> IpAddr {
        match self.addr_v6 {
            // In the man pages said that when it's IPV4, only the first number is set, otherwise it
            // is IPV6
            [x, 0, 0, 0] => IpAddr::V4(net::Ipv4Addr::from(x as u32)),
            [x, y, w, z] => {
                let x = x.to_be_bytes();
                let y = y.to_be_bytes();
                let w = w.to_be_bytes();
                let z = z.to_be_bytes();
                IpAddr::from([
                    x[0], x[1], x[2], x[3], y[0], y[1], y[2], y[3], w[0], w[1], w[2], w[3], z[0],
                    z[1], z[2], z[3],
                ])
            },
        }
    }

    /// Get exit status of the entry.
    #[cfg(any(
        target_os = "linux",
        target_os = "netbsd",
        target_os = "solaris",
        target_os = "illumos"
    ))]
    #[inline]
    pub const fn exit_status(&self) -> ExitStatus {
        self.exit
    }
}

impl From<utmpx> for Utmpx {
    /// Converts [`utmpx`] to [`Utmpx`].
    ///
    /// # Panic
    /// This function may panic when converting a number to UtmpxKind. Since we get the
    /// number from the OS it should never panic, but if the OS drastically change, it
    /// may panic.
    #[inline]
    fn from(c_utmpx: utmpx) -> Self {
        #[cfg(not(any(target_os = "netbsd", target_os = "dragonfly")))]
        let user = {
            let cstr: Vec<_> =
                c_utmpx.ut_user.iter().map(|cc| *cc as u8).filter(|cc| cc != &b'\0').collect();
            BString::from(cstr)
        };

        #[cfg(any(target_os = "netbsd", target_os = "dragonfly"))]
        let user = {
            let cstr: Vec<_> =
                c_utmpx.ut_name.iter().map(|cc| *cc as u8).filter(|cc| cc != &b'\0').collect();
            BString::from(cstr)
        };

        let host = {
            let cstr: Vec<_> =
                c_utmpx.ut_host.iter().map(|cc| *cc as u8).filter(|cc| cc != &b'\0').collect();
            BString::from(cstr.as_bytes())
        };

        let pid = c_utmpx.ut_pid;

        let id = {
            let cstr: Vec<_> =
                c_utmpx.ut_id.iter().map(|cc| *cc as u8).filter(|cc| cc != &b'\0').collect();
            BString::from(cstr)
        };

        let line = {
            let cstr: Vec<_> =
                c_utmpx.ut_line.iter().map(|cc| *cc as u8).filter(|cc| cc != &b'\0').collect();
            BString::from(cstr.as_bytes())
        };

        let ut_type = match UtmpxKind::try_from(c_utmpx.ut_type) {
            Ok(ut) => ut,
            Err(err) => panic!("{}", err),
        };

        let timeval = TimeVal {
            tv_sec: c_utmpx.ut_tv.tv_sec as time_t,
            tv_usec: c_utmpx.ut_tv.tv_usec as suseconds_t,
        };

        #[cfg(any(
            target_os = "linux",
            target_os = "netbsd",
            target_os = "dragonfly",
            target_os = "solaris",
            target_os = "illumos"
        ))]
        let session = c_utmpx.ut_session;

        #[cfg(target_os = "linux")]
        let addr_v6 = c_utmpx.ut_addr_v6;

        #[cfg(any(
            target_os = "linux",
            target_os = "netbsd",
            target_os = "solaris",
            target_os = "illumos"
        ))]
        let exit = c_utmpx.ut_exit;

        #[cfg(any(target_os = "netbsd"))]
        let ss = c_utmpx.ut_ss;

        #[cfg(any(target_os = "solaris", target_os = "illumos"))]
        let syslen = c_utmpx.ut_syslen;

        Utmpx {
            user,
            host,
            pid,
            id,
            line,
            ut_type,
            timeval,
            #[cfg(any(
                target_os = "linux",
                target_os = "netbsd",
                target_os = "solaris",
                target_os = "illumos"
            ))]
            exit,
            #[cfg(any(
                target_os = "linux",
                target_os = "netbsd",
                target_os = "dragonfly",
                target_os = "solaris",
                target_os = "illumos"
            ))]
            session,
            #[cfg(target_os = "linux")]
            addr_v6,
            #[cfg(target_os = "netbsd")]
            ss,
            #[cfg(any(target_os = "solaris", target_os = "illumos"))]
            syslen,
        }
    }
}

/// A collection of Utmpx entries.
#[derive(Debug)]
pub struct UtmpxSet(HashSet<Utmpx>);

impl UtmpxSet {
    /// Creates a new collection over a utmpx entry binary file.
    ///
    /// # Errors
    /// If a internal call set a errno (I/O OS error), an error variant will be returned.
    #[cfg(any(
        target_os = "linux",
        target_os = "macos",
        target_os = "netbsd",
        target_os = "solaris",
        target_os = "illumos"
    ))]
    #[cfg_attr(feature = "inline-more", inline)]
    pub fn from_file(path: impl AsRef<Path>) -> io::Result<Self> {
        let file = {
            let str = path.as_ref().to_str().unwrap_or("");
            CString::new(str).unwrap_or_default()
        };

        let mut set = HashSet::new();

        unsafe {
            let res = utmpxname(file.as_ptr());

            if res != 0 {
                return Err(io::Error::last_os_error());
            }

            loop {
                let ut = getutxent();
                if ut.is_null() {
                    break;
                } else {
                    let utm = Utmpx::from(*ut);
                    set.insert(utm);
                }
            }

            endutxent();
        }

        Ok(UtmpxSet(set))
    }

    /// Creates a new collection over a utmpx entry binary file.
    ///
    /// # Errors
    /// If a internal call set a errno (I/O OS error), an error variant will be returned.
    #[cfg(not(any(
        target_os = "linux",
        target_os = "macos",
        target_os = "netbsd",
        target_os = "solaris",
        target_os = "illumos"
    )))]
    pub fn from_file(path: impl AsRef<Path>) -> io::Result<Self> {
        let struct_size = mem::size_of::<utmpx>();
        let num_bytes = fs::metadata(&path)?.len() as usize;
        let num_structs = num_bytes / struct_size;
        let mut reader = BufReader::new(File::open(&path)?);
        let mut vec: Vec<utmpx> = Vec::with_capacity(num_structs);
        let mut set = HashSet::with_capacity(num_structs);

        unsafe {
            let mut buffer = slice::from_raw_parts_mut(vec.as_mut_ptr() as *mut u8, num_bytes);
            reader.read_exact(&mut buffer)?;
            vec.set_len(num_structs);
        }

        for raw_utm in vec {
            set.insert(Utmpx::from(raw_utm));
        }

        Ok(UtmpxSet(set))
    }

    /// Creates a new collection geting all entries from the running system.
    #[cfg_attr(feature = "inline-more", inline)]
    pub fn system() -> Self {
        let mut set = HashSet::new();

        unsafe {
            setutxent();

            loop {
                let ut = getutxent();
                if ut.is_null() {
                    break;
                } else {
                    let utm = Utmpx::from(*ut);
                    set.insert(utm);
                }
            }

            endutxent();
        }

        UtmpxSet(set)
    }

    /// Returns `true` if collection nas no elements.
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }

    /// Creates a iterator over it's entries.
    #[inline]
    pub fn iter(&self) -> hash_set::Iter<'_, Utmpx> {
        self.0.iter()
    }

    /// Size of the collection.
    #[inline]
    pub fn len(&self) -> usize {
        self.0.len()
    }
}

impl IntoIterator for UtmpxSet {
    type IntoIter = hash_set::IntoIter<Utmpx>;
    type Item = Utmpx;

    #[inline]
    fn into_iter(self) -> Self::IntoIter {
        self.0.into_iter()
    }
}

/// A iterator over the [`Utmpx`].
#[derive(Debug)]
pub struct UtmpxIter;

impl UtmpxIter {
    /// Creates an iterator of the entries from the running system.
    #[inline]
    pub fn system() -> Self {
        unsafe { setutxent() };
        Self
    }

    /// Creates an iterator over a utmpx entry binary file.
    ///
    /// # Errors
    /// If a internal call set a errno (I/O OS error), an error variant will be returned.
    #[cfg(any(
        target_os = "linux",
        target_os = "macos",
        target_os = "netbsd",
        target_os = "solaris",
        target_os = "illumos"
    ))]
    #[inline]
    pub fn from_file(path: impl AsRef<Path>) -> io::Result<Self> {
        let file = {
            let str = path.as_ref().to_str().unwrap_or("");
            CString::new(str).unwrap_or_default()
        };

        let res = unsafe { utmpxname(file.as_ptr()) };
        if res != 0 {
            return Err(io::Error::last_os_error());
        }

        Ok(Self)
    }
}


impl Iterator for UtmpxIter {
    type Item = Utmpx;

    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        unsafe {
            let ut = getutxent();
            if ut.is_null() {
                endutxent();
                None
            } else {
                let utm = Utmpx::from(*ut);
                Some(utm)
            }
        }
    }
}

impl std::iter::FusedIterator for UtmpxIter {}

// Extra trait
macro_rules! utmpxkind_impl_from {
    ($($t:ty)+) => (
        $(
            // Number to UtmpxKind
            #[cfg(target_os = "freebsd")]
            impl TryFrom<$t> for UtmpxKind {
                type Error = Error;
                #[inline]
                fn try_from(num: $t) -> Result<Self, Error> {
                    match num {
                        0 => Ok(Self::Empty),
                        1 => Ok(Self::BootTime),
                        2 => Ok(Self::OldTime),
                        3 => Ok(Self::NewTime),
                        4 => Ok(Self::UserProcess),
                        5 => Ok(Self::InitProcess),
                        6 => Ok(Self::LoginProcess),
                        7 => Ok(Self::DeadProcess),
                        8 => Ok(Self::ShutdownProcess),
                        _ => Err(Error::OsNoNumber),
                    }
                }
            }

            #[cfg(target_os = "netbsd")]
            impl TryFrom<$t> for UtmpxKind {
                type Error = Error;
                #[inline]
                fn try_from(num: $t) -> Result<Self, Error> {
                    match num {
                        0 => Ok(Self::Empty),
                        1 => Ok(Self::RunLevel),
                        2 => Ok(Self::BootTime),
                        3 => Ok(Self::OldTime),
                        4 => Ok(Self::NewTime),
                        5 => Ok(Self::InitProcess),
                        6 => Ok(Self::LoginProcess),
                        7 => Ok(Self::UserProcess),
                        8 => Ok(Self::DeadProcess),
                        9 => Ok(Self::Accounting),
                        10 => Ok(Self::Signature),
                        11 => Ok(Self::DownTime),
                        _ => Err(Error::OsNoNumber),
                    }
                }
            }

            #[cfg(any(target_os = "dragonfly"))]
            impl TryFrom<$t> for UtmpxKind {
                type Error = Error;
                #[inline]
                fn try_from(num: $t) -> Result<Self, Error> {
                    match num {
                        0 => Ok(Self::Empty),
                        1 => Ok(Self::RunLevel),
                        2 => Ok(Self::BootTime),
                        3 => Ok(Self::NewTime),
                        4 => Ok(Self::OldTime),
                        5 => Ok(Self::InitProcess),
                        6 => Ok(Self::LoginProcess),
                        7 => Ok(Self::UserProcess),
                        8 => Ok(Self::DeadProcess),
                        _ => Err(Error::OsNoNumber),
                    }
                }
            }

            #[cfg(any(target_os = "solaris", target_os = "illumos"))]
            impl TryFrom<$t> for UtmpxKind {
                type Error = Error;
                #[inline]
                fn try_from(num: $t) -> Result<Self, Error> {
                    match num {
                        0 => Ok(Self::Empty),
                        1 => Ok(Self::RunLevel),
                        2 => Ok(Self::BootTime),
                        3 => Ok(Self::OldTime),
                        4 => Ok(Self::NewTime),
                        5 => Ok(Self::InitProcess),
                        6 => Ok(Self::LoginProcess),
                        7 => Ok(Self::UserProcess),
                        8 => Ok(Self::DeadProcess),
                        9 => Ok(Self::Accounting),
                        10 => Ok(Self::DownTime),
                        _ => Err(Error::OsNoNumber),
                    }
                }
            }

            #[cfg(any(target_os = "linux", target_os = "macos"))]
            impl TryFrom<$t> for UtmpxKind {
                type Error = Error;
                #[inline]
                fn try_from(num: $t) -> Result<Self, Error> {
                    match num {
                        0 => Ok(Self::Empty),
                        1 => Ok(Self::RunLevel),
                        2 => Ok(Self::BootTime),
                        3 => Ok(Self::NewTime),
                        4 => Ok(Self::OldTime),
                        5 => Ok(Self::InitProcess),
                        6 => Ok(Self::LoginProcess),
                        7 => Ok(Self::UserProcess),
                        8 => Ok(Self::DeadProcess),
                        9 => Ok(Self::Accounting),
                        #[cfg(target_os = "macos")]
                        10 => Ok(Self::Signature),
                        #[cfg(target_os = "macos")]
                        11 => Ok(Self::ShutdownProcess),
                        _ => Err(Error::OsNoNumber),
                    }
                }
            }

            #[cfg(target_os = "haiku")]
            impl TryFrom<$t> for UtmpxKind {
                type Error = Error;
                #[inline]
                fn try_from(num: $t) -> Result<Self, Error> {
                    match num {
                        0 => Ok(Self::Empty),
                        1 => Ok(Self::BootTime),
                        2 => Ok(Self::OldTime),
                        3 => Ok(Self::NewTime),
                        4 => Ok(Self::UserProcess),
                        5 => Ok(Self::InitProcess),
                        6 => Ok(Self::LoginProcess),
                        7 => Ok(Self::DeadProcess),
                        _ => Err(Error::OsNoNumber),
                    }
                }
            }

            // UtmpxKind to number
            #[cfg(target_os = "freebsd")]
            impl TryFrom<UtmpxKind> for $t {
                type Error = Error;
                #[inline]
                fn try_from(utype: UtmpxKind) -> Result<Self, Error> {
                    match utype {
                        UtmpxKind::Empty => Ok(0),
                        UtmpxKind::BootTime => Ok(1),
                        UtmpxKind::OldTime => Ok(2),
                        UtmpxKind::NewTime => Ok(3),
                        UtmpxKind::UserProcess => Ok(4),
                        UtmpxKind::InitProcess => Ok(5),
                        UtmpxKind::LoginProcess => Ok(6),
                        UtmpxKind::DeadProcess => Ok(7),
                        UtmpxKind::ShutdownProcess => Ok(8),
                        _ => Err(Error::OsNoKind),
                    }
                }
            }

            #[cfg(target_os = "netbsd")]
            impl TryFrom<UtmpxKind> for $t {
                type Error = Error;
                #[inline]
                fn try_from(utype: UtmpxKind) -> Result<Self, Error> {
                    match utype {
                        UtmpxKind::Empty => Ok(0),
                        UtmpxKind::RunLevel => Ok(1),
                        UtmpxKind::BootTime => Ok(2),
                        UtmpxKind::OldTime => Ok(3),
                        UtmpxKind::NewTime => Ok(4),
                        UtmpxKind::InitProcess => Ok(5),
                        UtmpxKind::LoginProcess => Ok(6),
                        UtmpxKind::UserProcess => Ok(7),
                        UtmpxKind::DeadProcess => Ok(8),
                        UtmpxKind::Accounting => Ok(9),
                        UtmpxKind::Signature => Ok(10),
                        UtmpxKind::DownTime => Ok(11),
                        _ => Err(Error::OsNoKind),
                    }
                }
            }

            #[cfg(any(target_os = "dragonfly"))]
            impl TryFrom<UtmpxKind> for $t {
                type Error = Error;
                #[inline]
                fn try_from(utype: UtmpxKind) -> Result<Self, Error> {
                    match utype {
                        UtmpxKind::Empty => Ok(0),
                        UtmpxKind::RunLevel => Ok(1),
                        UtmpxKind::BootTime => Ok(2),
                        UtmpxKind::NewTime => Ok(3),
                        UtmpxKind::OldTime => Ok(4),
                        UtmpxKind::InitProcess => Ok(5),
                        UtmpxKind::LoginProcess => Ok(6),
                        UtmpxKind::UserProcess => Ok(7),
                        UtmpxKind::DeadProcess => Ok(8),
                        _ => Err(Error::OsNoKind),
                    }
                }
            }

            #[cfg(any(target_os = "solaris", target_os = "illumos"))]
            impl TryFrom<UtmpxKind> for $t {
                type Error = Error;
                #[inline]
                fn try_from(utype: UtmpxKind) -> Result<Self, Error> {
                    match utype {
                        UtmpxKind::Empty => Ok(0),
                        UtmpxKind::RunLevel => Ok(1),
                        UtmpxKind::BootTime => Ok(2),
                        UtmpxKind::OldTime => Ok(3),
                        UtmpxKind::NewTime => Ok(4),
                        UtmpxKind::InitProcess => Ok(5),
                        UtmpxKind::LoginProcess => Ok(6),
                        UtmpxKind::UserProcess => Ok(7),
                        UtmpxKind::DeadProcess => Ok(8),
                        UtmpxKind::Accounting => Ok(9),
                        UtmpxKind::DownTime => Ok(10),
                        _ => Err(Error::OsNoKind),
                    }
                }
            }

            #[cfg(any(target_os = "linux", target_os = "macos"))]
            impl TryFrom<UtmpxKind> for $t {
                type Error = Error;
                #[inline]
                fn try_from(utype: UtmpxKind) -> Result<Self, Error> {
                    match utype {
                        UtmpxKind::Empty => Ok(0),
                        UtmpxKind::RunLevel => Ok(1),
                        UtmpxKind::BootTime => Ok(2),
                        UtmpxKind::NewTime => Ok(3),
                        UtmpxKind::OldTime => Ok(4),
                        UtmpxKind::InitProcess => Ok(5),
                        UtmpxKind::LoginProcess => Ok(6),
                        UtmpxKind::UserProcess => Ok(7),
                        UtmpxKind::DeadProcess => Ok(8),
                        UtmpxKind::Accounting => Ok(9),
                        #[cfg(target_os = "macos")]
                        UtmpxKind::Signature => Ok(10),
                        #[cfg(target_os = "macos")]
                        UtmpxKind::ShutdownProcess => Ok(11),
                        _ => Err(Error::OsNoKind),
                    }
                }
            }

            #[cfg(target_os = "haiku")]
            impl TryFrom<UtmpxKind> for $t {
                type Error = Error;
                #[inline]
                fn try_from(utype: UtmpxKind) -> Result<Self, Error> {
                    match utype {
                        UtmpxKind::Empty => Ok(0),
                        UtmpxKind::BootTime => Ok(1),
                        UtmpxKind::OldTime => Ok(2),
                        UtmpxKind::NewTime => Ok(3),
                        UtmpxKind::UserProcess => Ok(4),
                        UtmpxKind::InitProcess => Ok(5),
                        UtmpxKind::LoginProcess => Ok(6),
                        UtmpxKind::DeadProcess => Ok(7),
                        _ => Err(Error::OsNoKind),
                    }
                }
            }
        )+
    );
}

utmpxkind_impl_from!(i8 i16 i32 i64 i128 u8 u16 u32 u64 u128);