~goorzhel/radm

ref: b4ca8d0dde821420d66d488db969c0f7c5da5f6b radm/src/main.rs -rw-r--r-- 1.9 KiB
b4ca8d0d — Antonio Gurgel 0.3.0 4 months 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
//! rstdm (RuSt Terminal Display Manager) is a console-based display manager.

#![warn(missing_docs)]

#[macro_use]
extern crate log;

use std::str::FromStr;

use anyhow::{anyhow, Context, Result};
use chvt::chvt;

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

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

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

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

    info!("Starting rstdm.");
    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 auth = login().context("Couldn't log user in")?;
    let user = User::from_str(auth.username())?;

    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 = user
        .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 {}/rstdm-stderr.log",
            exit,
            xdg.get_state_home().to_string_lossy()
        )),
    }
}