~goorzhel/radm

radm/src/main.rs -rw-r--r-- 2.1 KiB
d11d708e — Antonio Gurgel 0.6.4 a month ago
                                                                                
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
//! radm is a console-based display manager.

#![warn(missing_docs)]

use std::str::FromStr;

use anyhow::{anyhow, Context, Result};
use chvt::chvt;
use log::{error, info};
use nix::sys::signal::SigHandler;

mod auth;
mod cli;
mod pam;
mod session;
mod system;
mod tui;
mod user;

use crate::{
    auth::login,
    cli::{init_logger, Options},
    session::{choose_session, find_sessions},
    system::{drop_privileges, prepare_xdg_runtime, toggle_signals},
    tui::{prompt, EchoInput},
    user::User,
};

fn main() {
    if let Err(e) = run().context("radm encountered an error") {
        eprintln!("{:#}", e);
        error!("{:#}", e);
        prompt("Press Enter to quit radm", EchoInput::Silent).unwrap();
    }
}

fn run() -> Result<()> {
    let opts = Options::new();
    init_logger(&opts)?;

    info!("Starting radm {}.", env!("CARGO_PKG_VERSION"));
    println!(include_str!("wordmark.txt"));

    let sessions = find_sessions(&opts.session_dirs).context("Couldn't gather sessions")?;

    if let Some(tty_id) = opts.focus_on_start {
        if let Err(e) = chvt(tty_id) {
            error!("Couldn't focus on TTY: {}", e)
        };
    }

    let autologin_gid = opts.autologin_gid()?;

    toggle_signals(SigHandler::SigIgn).context("Couldn't ignore SIGINT/QUIT")?;
    let auth = login(autologin_gid).context("Couldn't log user in")?;
    let user = User::from_str(auth.username())?;
    toggle_signals(SigHandler::SigDfl).context("Couldn't unignore SIGINT/QUIT")?;

    let xdg = user
        .load_environment()
        .context("Couldn't load environment")?;

    prepare_xdg_runtime(&user).context("Couldn't prepare XDG runtime")?;
    drop_privileges(&user).context("Couldn't drop privileges")?;
    println!();

    let session = choose_session(&sessions, &xdg).context("Couldn't choose session")?;
    let exit = user
        .launch_session(&session, &xdg, opts.dbus)
        .context("Couldn't launch session")?;

    match exit.success() {
        true => Ok(()),
        false => Err(anyhow!(
            "Session exited {}; check {}/radm-stderr.log",
            exit,
            xdg.get_state_home().to_string_lossy()
        )),
    }
}