rust/chg/src/uihandler.rs
author Yuya Nishihara <yuya@tcha.org>
Sat, 11 Apr 2020 17:43:29 +0900
changeset 44756 27fe8cc1338f
parent 44755 4b0185841058
child 45620 426294d06ddc
permissions -rw-r--r--
rust-chg: clean up excessive indents Differential Revision: https://phab.mercurial-scm.org/D8450
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
39974
a9c5fc436fd5 rust-chg: add callback to handle pager and shell command requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
     1
// Copyright 2018 Yuya Nishihara <yuya@tcha.org>
a9c5fc436fd5 rust-chg: add callback to handle pager and shell command requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
     2
//
a9c5fc436fd5 rust-chg: add callback to handle pager and shell command requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
     3
// This software may be used and distributed according to the terms of the
a9c5fc436fd5 rust-chg: add callback to handle pager and shell command requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
     4
// GNU General Public License version 2 or any later version.
a9c5fc436fd5 rust-chg: add callback to handle pager and shell command requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
     5
44750
c794d0da5fb2 rust-chg: reimplement uihandler by using async-trait and tokio-0.2
Yuya Nishihara <yuya@tcha.org>
parents: 44737
diff changeset
     6
use async_trait::async_trait;
39974
a9c5fc436fd5 rust-chg: add callback to handle pager and shell command requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
     7
use std::io;
a9c5fc436fd5 rust-chg: add callback to handle pager and shell command requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
     8
use std::os::unix::io::AsRawFd;
a9c5fc436fd5 rust-chg: add callback to handle pager and shell command requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
     9
use std::os::unix::process::ExitStatusExt;
44737
e9e44e61042b rust-chg: upgrade to futures-0.3 based libraries
Yuya Nishihara <yuya@tcha.org>
parents: 44689
diff changeset
    10
use std::process::Stdio;
39974
a9c5fc436fd5 rust-chg: add callback to handle pager and shell command requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    11
use tokio;
44755
4b0185841058 rust-chg: do not terminate tokio runtime until pager exits
Yuya Nishihara <yuya@tcha.org>
parents: 44750
diff changeset
    12
use tokio::process::{Child, ChildStdin, Command};
39974
a9c5fc436fd5 rust-chg: add callback to handle pager and shell command requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    13
44689
6bef9d43cc55 rust-chg: use "crate::" to import local modules
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
    14
use crate::message::CommandSpec;
6bef9d43cc55 rust-chg: use "crate::" to import local modules
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
    15
use crate::procutil;
39974
a9c5fc436fd5 rust-chg: add callback to handle pager and shell command requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    16
a9c5fc436fd5 rust-chg: add callback to handle pager and shell command requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    17
/// Callback to process shell command requests received from server.
44750
c794d0da5fb2 rust-chg: reimplement uihandler by using async-trait and tokio-0.2
Yuya Nishihara <yuya@tcha.org>
parents: 44737
diff changeset
    18
#[async_trait]
c794d0da5fb2 rust-chg: reimplement uihandler by using async-trait and tokio-0.2
Yuya Nishihara <yuya@tcha.org>
parents: 44737
diff changeset
    19
pub trait SystemHandler {
39974
a9c5fc436fd5 rust-chg: add callback to handle pager and shell command requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    20
    type PagerStdin: AsRawFd;
a9c5fc436fd5 rust-chg: add callback to handle pager and shell command requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    21
a9c5fc436fd5 rust-chg: add callback to handle pager and shell command requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    22
    /// Handles pager command request.
a9c5fc436fd5 rust-chg: add callback to handle pager and shell command requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    23
    ///
a9c5fc436fd5 rust-chg: add callback to handle pager and shell command requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    24
    /// Returns the pipe to be attached to the server if the pager is spawned.
44750
c794d0da5fb2 rust-chg: reimplement uihandler by using async-trait and tokio-0.2
Yuya Nishihara <yuya@tcha.org>
parents: 44737
diff changeset
    25
    async fn spawn_pager(&mut self, spec: &CommandSpec) -> io::Result<Self::PagerStdin>;
39974
a9c5fc436fd5 rust-chg: add callback to handle pager and shell command requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    26
a9c5fc436fd5 rust-chg: add callback to handle pager and shell command requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    27
    /// Handles system command request.
a9c5fc436fd5 rust-chg: add callback to handle pager and shell command requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    28
    ///
a9c5fc436fd5 rust-chg: add callback to handle pager and shell command requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    29
    /// Returns command exit code (positive) or signal number (negative).
44750
c794d0da5fb2 rust-chg: reimplement uihandler by using async-trait and tokio-0.2
Yuya Nishihara <yuya@tcha.org>
parents: 44737
diff changeset
    30
    async fn run_system(&mut self, spec: &CommandSpec) -> io::Result<i32>;
39974
a9c5fc436fd5 rust-chg: add callback to handle pager and shell command requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    31
}
a9c5fc436fd5 rust-chg: add callback to handle pager and shell command requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    32
a9c5fc436fd5 rust-chg: add callback to handle pager and shell command requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    33
/// Default cHg implementation to process requests received from server.
44755
4b0185841058 rust-chg: do not terminate tokio runtime until pager exits
Yuya Nishihara <yuya@tcha.org>
parents: 44750
diff changeset
    34
pub struct ChgUiHandler {
4b0185841058 rust-chg: do not terminate tokio runtime until pager exits
Yuya Nishihara <yuya@tcha.org>
parents: 44750
diff changeset
    35
    pager: Option<Child>,
4b0185841058 rust-chg: do not terminate tokio runtime until pager exits
Yuya Nishihara <yuya@tcha.org>
parents: 44750
diff changeset
    36
}
39974
a9c5fc436fd5 rust-chg: add callback to handle pager and shell command requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    37
a9c5fc436fd5 rust-chg: add callback to handle pager and shell command requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    38
impl ChgUiHandler {
a9c5fc436fd5 rust-chg: add callback to handle pager and shell command requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    39
    pub fn new() -> ChgUiHandler {
44755
4b0185841058 rust-chg: do not terminate tokio runtime until pager exits
Yuya Nishihara <yuya@tcha.org>
parents: 44750
diff changeset
    40
        ChgUiHandler { pager: None }
4b0185841058 rust-chg: do not terminate tokio runtime until pager exits
Yuya Nishihara <yuya@tcha.org>
parents: 44750
diff changeset
    41
    }
4b0185841058 rust-chg: do not terminate tokio runtime until pager exits
Yuya Nishihara <yuya@tcha.org>
parents: 44750
diff changeset
    42
4b0185841058 rust-chg: do not terminate tokio runtime until pager exits
Yuya Nishihara <yuya@tcha.org>
parents: 44750
diff changeset
    43
    /// Waits until the pager process exits.
4b0185841058 rust-chg: do not terminate tokio runtime until pager exits
Yuya Nishihara <yuya@tcha.org>
parents: 44750
diff changeset
    44
    pub async fn wait_pager(&mut self) -> io::Result<()> {
4b0185841058 rust-chg: do not terminate tokio runtime until pager exits
Yuya Nishihara <yuya@tcha.org>
parents: 44750
diff changeset
    45
        if let Some(p) = self.pager.take() {
4b0185841058 rust-chg: do not terminate tokio runtime until pager exits
Yuya Nishihara <yuya@tcha.org>
parents: 44750
diff changeset
    46
            p.await?;
4b0185841058 rust-chg: do not terminate tokio runtime until pager exits
Yuya Nishihara <yuya@tcha.org>
parents: 44750
diff changeset
    47
        }
4b0185841058 rust-chg: do not terminate tokio runtime until pager exits
Yuya Nishihara <yuya@tcha.org>
parents: 44750
diff changeset
    48
        Ok(())
39974
a9c5fc436fd5 rust-chg: add callback to handle pager and shell command requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    49
    }
a9c5fc436fd5 rust-chg: add callback to handle pager and shell command requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    50
}
a9c5fc436fd5 rust-chg: add callback to handle pager and shell command requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    51
44750
c794d0da5fb2 rust-chg: reimplement uihandler by using async-trait and tokio-0.2
Yuya Nishihara <yuya@tcha.org>
parents: 44737
diff changeset
    52
#[async_trait]
39974
a9c5fc436fd5 rust-chg: add callback to handle pager and shell command requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    53
impl SystemHandler for ChgUiHandler {
a9c5fc436fd5 rust-chg: add callback to handle pager and shell command requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    54
    type PagerStdin = ChildStdin;
a9c5fc436fd5 rust-chg: add callback to handle pager and shell command requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    55
44750
c794d0da5fb2 rust-chg: reimplement uihandler by using async-trait and tokio-0.2
Yuya Nishihara <yuya@tcha.org>
parents: 44737
diff changeset
    56
    async fn spawn_pager(&mut self, spec: &CommandSpec) -> io::Result<Self::PagerStdin> {
44737
e9e44e61042b rust-chg: upgrade to futures-0.3 based libraries
Yuya Nishihara <yuya@tcha.org>
parents: 44689
diff changeset
    57
        let mut pager = new_shell_command(&spec).stdin(Stdio::piped()).spawn()?;
e9e44e61042b rust-chg: upgrade to futures-0.3 based libraries
Yuya Nishihara <yuya@tcha.org>
parents: 44689
diff changeset
    58
        let pin = pager.stdin.take().unwrap();
39974
a9c5fc436fd5 rust-chg: add callback to handle pager and shell command requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    59
        procutil::set_blocking_fd(pin.as_raw_fd())?;
40119
e70b616a077b rust-chg: remove SIGCHLD handler which won't work in oxidized chg
Yuya Nishihara <yuya@tcha.org>
parents: 39974
diff changeset
    60
        // TODO: if pager exits, notify the server with SIGPIPE immediately.
e70b616a077b rust-chg: remove SIGCHLD handler which won't work in oxidized chg
Yuya Nishihara <yuya@tcha.org>
parents: 39974
diff changeset
    61
        // otherwise the server won't get SIGPIPE if it does not write
e70b616a077b rust-chg: remove SIGCHLD handler which won't work in oxidized chg
Yuya Nishihara <yuya@tcha.org>
parents: 39974
diff changeset
    62
        // anything. (issue5278)
e70b616a077b rust-chg: remove SIGCHLD handler which won't work in oxidized chg
Yuya Nishihara <yuya@tcha.org>
parents: 39974
diff changeset
    63
        // kill(peerpid, SIGPIPE);
44755
4b0185841058 rust-chg: do not terminate tokio runtime until pager exits
Yuya Nishihara <yuya@tcha.org>
parents: 44750
diff changeset
    64
        self.pager = Some(pager);
44750
c794d0da5fb2 rust-chg: reimplement uihandler by using async-trait and tokio-0.2
Yuya Nishihara <yuya@tcha.org>
parents: 44737
diff changeset
    65
        Ok(pin)
39974
a9c5fc436fd5 rust-chg: add callback to handle pager and shell command requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    66
    }
a9c5fc436fd5 rust-chg: add callback to handle pager and shell command requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    67
44750
c794d0da5fb2 rust-chg: reimplement uihandler by using async-trait and tokio-0.2
Yuya Nishihara <yuya@tcha.org>
parents: 44737
diff changeset
    68
    async fn run_system(&mut self, spec: &CommandSpec) -> io::Result<i32> {
c794d0da5fb2 rust-chg: reimplement uihandler by using async-trait and tokio-0.2
Yuya Nishihara <yuya@tcha.org>
parents: 44737
diff changeset
    69
        let status = new_shell_command(&spec).spawn()?.await?;
44756
27fe8cc1338f rust-chg: clean up excessive indents
Yuya Nishihara <yuya@tcha.org>
parents: 44755
diff changeset
    70
        let code = status
27fe8cc1338f rust-chg: clean up excessive indents
Yuya Nishihara <yuya@tcha.org>
parents: 44755
diff changeset
    71
            .code()
27fe8cc1338f rust-chg: clean up excessive indents
Yuya Nishihara <yuya@tcha.org>
parents: 44755
diff changeset
    72
            .or_else(|| status.signal().map(|n| -n))
27fe8cc1338f rust-chg: clean up excessive indents
Yuya Nishihara <yuya@tcha.org>
parents: 44755
diff changeset
    73
            .expect("either exit code or signal should be set");
27fe8cc1338f rust-chg: clean up excessive indents
Yuya Nishihara <yuya@tcha.org>
parents: 44755
diff changeset
    74
        Ok(code)
39974
a9c5fc436fd5 rust-chg: add callback to handle pager and shell command requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    75
    }
a9c5fc436fd5 rust-chg: add callback to handle pager and shell command requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    76
}
a9c5fc436fd5 rust-chg: add callback to handle pager and shell command requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    77
a9c5fc436fd5 rust-chg: add callback to handle pager and shell command requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    78
fn new_shell_command(spec: &CommandSpec) -> Command {
a9c5fc436fd5 rust-chg: add callback to handle pager and shell command requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    79
    let mut builder = Command::new("/bin/sh");
a9c5fc436fd5 rust-chg: add callback to handle pager and shell command requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    80
    builder
a9c5fc436fd5 rust-chg: add callback to handle pager and shell command requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    81
        .arg("-c")
a9c5fc436fd5 rust-chg: add callback to handle pager and shell command requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    82
        .arg(&spec.command)
a9c5fc436fd5 rust-chg: add callback to handle pager and shell command requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    83
        .current_dir(&spec.current_dir)
a9c5fc436fd5 rust-chg: add callback to handle pager and shell command requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    84
        .env_clear()
a9c5fc436fd5 rust-chg: add callback to handle pager and shell command requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    85
        .envs(spec.envs.iter().cloned());
a9c5fc436fd5 rust-chg: add callback to handle pager and shell command requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    86
    builder
43818
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40119
diff changeset
    87
}