Skip to main content

relibc/header/utmp/
mod.rs

1//! `utmp.h` implementation.
2//!
3//! Non-POSIX, see <https://www.man7.org/linux/man-pages/man3/openpty.3.html>.
4
5use crate::{
6    header::{sys_ioctl, unistd},
7    platform::types::{c_int, c_void},
8};
9
10/// See <https://www.man7.org/linux/man-pages/man3/openpty.3.html>.
11#[unsafe(no_mangle)]
12pub unsafe extern "C" fn login_tty(fd: c_int) -> c_int {
13    // Create a new session
14    unistd::setsid();
15
16    // Set controlling terminal
17    let mut arg: c_int = 0;
18    if unsafe {
19        sys_ioctl::ioctl(
20            fd,
21            sys_ioctl::TIOCSCTTY,
22            core::ptr::from_mut::<c_int>(&mut arg).cast::<c_void>(),
23        )
24    } != 0
25    {
26        return -1;
27    }
28
29    // Overwrite stdio
30    unistd::dup2(fd, 0);
31    unistd::dup2(fd, 1);
32    unistd::dup2(fd, 2);
33
34    // Close if needed
35    if fd > 2 {
36        unistd::close(fd);
37    }
38
39    0
40}