Mercurial > hg
annotate rust/chg/src/locator.rs @ 44671:bb936e25a84a
rust-chg: spawn server process if not running
This is the minimal reimplementation of gethgcmd(), execcmdserver(),
retryconnectcmdserver(), and connectcmdserver() in chg.c.
No config validation is implemented yet. And some Py3 workarounds would
be missing as this is the code I wrote in 2018.
Differential Revision: https://phab.mercurial-scm.org/D8360
author | Yuya Nishihara <yuya@tcha.org> |
---|---|
date | Sat, 06 Oct 2018 20:10:44 +0900 |
parents | c11d98cff883 |
children | 7bf45ed9e25e |
rev | line source |
---|---|
39976
44840bcc411a
rust-chg: port basic socket path handling from cHg of C
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
1 // Copyright 2011, 2018 Yuya Nishihara <yuya@tcha.org> |
44840bcc411a
rust-chg: port basic socket path handling from cHg of C
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
2 // |
44840bcc411a
rust-chg: port basic socket path handling from cHg of C
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
3 // This software may be used and distributed according to the terms of the |
44840bcc411a
rust-chg: port basic socket path handling from cHg of C
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
4 // GNU General Public License version 2 or any later version. |
44840bcc411a
rust-chg: port basic socket path handling from cHg of C
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
5 |
44840bcc411a
rust-chg: port basic socket path handling from cHg of C
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
6 //! Utility for locating command-server process. |
44840bcc411a
rust-chg: port basic socket path handling from cHg of C
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
7 |
44671
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
8 use futures::future::{self, Either, Loop}; |
39976
44840bcc411a
rust-chg: port basic socket path handling from cHg of C
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
9 use std::env; |
40289
7d3285f799cc
rust-chg: add struct holding information needed to spawn server process
Yuya Nishihara <yuya@tcha.org>
parents:
39976
diff
changeset
|
10 use std::ffi::{OsStr, OsString}; |
39976
44840bcc411a
rust-chg: port basic socket path handling from cHg of C
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
11 use std::fs::{self, DirBuilder}; |
44840bcc411a
rust-chg: port basic socket path handling from cHg of C
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
12 use std::io; |
40289
7d3285f799cc
rust-chg: add struct holding information needed to spawn server process
Yuya Nishihara <yuya@tcha.org>
parents:
39976
diff
changeset
|
13 use std::os::unix::ffi::{OsStrExt, OsStringExt}; |
39976
44840bcc411a
rust-chg: port basic socket path handling from cHg of C
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
14 use std::os::unix::fs::{DirBuilderExt, MetadataExt}; |
44840bcc411a
rust-chg: port basic socket path handling from cHg of C
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
15 use std::path::{Path, PathBuf}; |
44671
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
16 use std::process::{self, Command}; |
40289
7d3285f799cc
rust-chg: add struct holding information needed to spawn server process
Yuya Nishihara <yuya@tcha.org>
parents:
39976
diff
changeset
|
17 use std::time::Duration; |
44671
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
18 use tokio::prelude::*; |
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
19 use tokio_hglib::UnixClient; |
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
20 use tokio_process::{Child, CommandExt}; |
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
21 use tokio_timer; |
39976
44840bcc411a
rust-chg: port basic socket path handling from cHg of C
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
22 |
44840bcc411a
rust-chg: port basic socket path handling from cHg of C
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
23 use super::procutil; |
44840bcc411a
rust-chg: port basic socket path handling from cHg of C
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
24 |
40289
7d3285f799cc
rust-chg: add struct holding information needed to spawn server process
Yuya Nishihara <yuya@tcha.org>
parents:
39976
diff
changeset
|
25 /// Helper to connect to and spawn a server process. |
7d3285f799cc
rust-chg: add struct holding information needed to spawn server process
Yuya Nishihara <yuya@tcha.org>
parents:
39976
diff
changeset
|
26 #[derive(Clone, Debug)] |
7d3285f799cc
rust-chg: add struct holding information needed to spawn server process
Yuya Nishihara <yuya@tcha.org>
parents:
39976
diff
changeset
|
27 pub struct Locator { |
7d3285f799cc
rust-chg: add struct holding information needed to spawn server process
Yuya Nishihara <yuya@tcha.org>
parents:
39976
diff
changeset
|
28 hg_command: OsString, |
7d3285f799cc
rust-chg: add struct holding information needed to spawn server process
Yuya Nishihara <yuya@tcha.org>
parents:
39976
diff
changeset
|
29 current_dir: PathBuf, |
7d3285f799cc
rust-chg: add struct holding information needed to spawn server process
Yuya Nishihara <yuya@tcha.org>
parents:
39976
diff
changeset
|
30 env_vars: Vec<(OsString, OsString)>, |
7d3285f799cc
rust-chg: add struct holding information needed to spawn server process
Yuya Nishihara <yuya@tcha.org>
parents:
39976
diff
changeset
|
31 process_id: u32, |
7d3285f799cc
rust-chg: add struct holding information needed to spawn server process
Yuya Nishihara <yuya@tcha.org>
parents:
39976
diff
changeset
|
32 base_sock_path: PathBuf, |
7d3285f799cc
rust-chg: add struct holding information needed to spawn server process
Yuya Nishihara <yuya@tcha.org>
parents:
39976
diff
changeset
|
33 timeout: Duration, |
7d3285f799cc
rust-chg: add struct holding information needed to spawn server process
Yuya Nishihara <yuya@tcha.org>
parents:
39976
diff
changeset
|
34 } |
7d3285f799cc
rust-chg: add struct holding information needed to spawn server process
Yuya Nishihara <yuya@tcha.org>
parents:
39976
diff
changeset
|
35 |
7d3285f799cc
rust-chg: add struct holding information needed to spawn server process
Yuya Nishihara <yuya@tcha.org>
parents:
39976
diff
changeset
|
36 impl Locator { |
7d3285f799cc
rust-chg: add struct holding information needed to spawn server process
Yuya Nishihara <yuya@tcha.org>
parents:
39976
diff
changeset
|
37 /// Creates locator capturing the current process environment. |
7d3285f799cc
rust-chg: add struct holding information needed to spawn server process
Yuya Nishihara <yuya@tcha.org>
parents:
39976
diff
changeset
|
38 /// |
7d3285f799cc
rust-chg: add struct holding information needed to spawn server process
Yuya Nishihara <yuya@tcha.org>
parents:
39976
diff
changeset
|
39 /// If no `$CHGSOCKNAME` is specified, the socket directory will be |
7d3285f799cc
rust-chg: add struct holding information needed to spawn server process
Yuya Nishihara <yuya@tcha.org>
parents:
39976
diff
changeset
|
40 /// created as necessary. |
7d3285f799cc
rust-chg: add struct holding information needed to spawn server process
Yuya Nishihara <yuya@tcha.org>
parents:
39976
diff
changeset
|
41 pub fn prepare_from_env() -> io::Result<Locator> { |
7d3285f799cc
rust-chg: add struct holding information needed to spawn server process
Yuya Nishihara <yuya@tcha.org>
parents:
39976
diff
changeset
|
42 Ok(Locator { |
7d3285f799cc
rust-chg: add struct holding information needed to spawn server process
Yuya Nishihara <yuya@tcha.org>
parents:
39976
diff
changeset
|
43 hg_command: default_hg_command(), |
7d3285f799cc
rust-chg: add struct holding information needed to spawn server process
Yuya Nishihara <yuya@tcha.org>
parents:
39976
diff
changeset
|
44 current_dir: env::current_dir()?, |
7d3285f799cc
rust-chg: add struct holding information needed to spawn server process
Yuya Nishihara <yuya@tcha.org>
parents:
39976
diff
changeset
|
45 env_vars: env::vars_os().collect(), |
7d3285f799cc
rust-chg: add struct holding information needed to spawn server process
Yuya Nishihara <yuya@tcha.org>
parents:
39976
diff
changeset
|
46 process_id: process::id(), |
7d3285f799cc
rust-chg: add struct holding information needed to spawn server process
Yuya Nishihara <yuya@tcha.org>
parents:
39976
diff
changeset
|
47 base_sock_path: prepare_server_socket_path()?, |
7d3285f799cc
rust-chg: add struct holding information needed to spawn server process
Yuya Nishihara <yuya@tcha.org>
parents:
39976
diff
changeset
|
48 timeout: default_timeout(), |
7d3285f799cc
rust-chg: add struct holding information needed to spawn server process
Yuya Nishihara <yuya@tcha.org>
parents:
39976
diff
changeset
|
49 }) |
7d3285f799cc
rust-chg: add struct holding information needed to spawn server process
Yuya Nishihara <yuya@tcha.org>
parents:
39976
diff
changeset
|
50 } |
7d3285f799cc
rust-chg: add struct holding information needed to spawn server process
Yuya Nishihara <yuya@tcha.org>
parents:
39976
diff
changeset
|
51 |
7d3285f799cc
rust-chg: add struct holding information needed to spawn server process
Yuya Nishihara <yuya@tcha.org>
parents:
39976
diff
changeset
|
52 /// Temporary socket path for this client process. |
7d3285f799cc
rust-chg: add struct holding information needed to spawn server process
Yuya Nishihara <yuya@tcha.org>
parents:
39976
diff
changeset
|
53 fn temp_sock_path(&self) -> PathBuf { |
7d3285f799cc
rust-chg: add struct holding information needed to spawn server process
Yuya Nishihara <yuya@tcha.org>
parents:
39976
diff
changeset
|
54 let src = self.base_sock_path.as_os_str().as_bytes(); |
44668
c11d98cff883
rust-chg: add brief comment about initial capacity of temp_sock_path()
Yuya Nishihara <yuya@tcha.org>
parents:
43818
diff
changeset
|
55 let mut buf = Vec::with_capacity(src.len() + 6); // "{src}.{pid}".len() |
40289
7d3285f799cc
rust-chg: add struct holding information needed to spawn server process
Yuya Nishihara <yuya@tcha.org>
parents:
39976
diff
changeset
|
56 buf.extend_from_slice(src); |
7d3285f799cc
rust-chg: add struct holding information needed to spawn server process
Yuya Nishihara <yuya@tcha.org>
parents:
39976
diff
changeset
|
57 buf.extend_from_slice(format!(".{}", self.process_id).as_bytes()); |
7d3285f799cc
rust-chg: add struct holding information needed to spawn server process
Yuya Nishihara <yuya@tcha.org>
parents:
39976
diff
changeset
|
58 OsString::from_vec(buf).into() |
7d3285f799cc
rust-chg: add struct holding information needed to spawn server process
Yuya Nishihara <yuya@tcha.org>
parents:
39976
diff
changeset
|
59 } |
44671
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
60 |
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
61 /// Connects to the server. |
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
62 /// |
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
63 /// The server process will be spawned if not running. |
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
64 pub fn connect(self) -> impl Future<Item = (Self, UnixClient), Error = io::Error> { |
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
65 self.try_connect() |
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
66 } |
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
67 |
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
68 /// Tries to connect to the existing server, or spawns new if not running. |
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
69 fn try_connect(self) -> impl Future<Item = (Self, UnixClient), Error = io::Error> { |
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
70 debug!("try connect to {}", self.base_sock_path.display()); |
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
71 UnixClient::connect(self.base_sock_path.clone()).then(|res| match res { |
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
72 Ok(client) => Either::A(future::ok((self, client))), |
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
73 Err(_) => Either::B(self.spawn_connect()), |
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
74 }) |
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
75 } |
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
76 |
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
77 /// Spawns new server process and connects to it. |
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
78 /// |
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
79 /// The server will be spawned at the current working directory, then |
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
80 /// chdir to "/", so that the server will load configs from the target |
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
81 /// repository. |
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
82 fn spawn_connect(self) -> impl Future<Item = (Self, UnixClient), Error = io::Error> { |
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
83 let sock_path = self.temp_sock_path(); |
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
84 debug!("start cmdserver at {}", sock_path.display()); |
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
85 Command::new(&self.hg_command) |
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
86 .arg("serve") |
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
87 .arg("--cmdserver") |
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
88 .arg("chgunix") |
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
89 .arg("--address") |
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
90 .arg(&sock_path) |
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
91 .arg("--daemon-postexec") |
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
92 .arg("chdir:/") |
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
93 .current_dir(&self.current_dir) |
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
94 .env_clear() |
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
95 .envs(self.env_vars.iter().cloned()) |
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
96 .env("CHGINTERNALMARK", "") |
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
97 .spawn_async() |
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
98 .into_future() |
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
99 .and_then(|server| self.connect_spawned(server, sock_path)) |
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
100 .and_then(|(loc, client, sock_path)| { |
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
101 debug!( |
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
102 "rename {} to {}", |
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
103 sock_path.display(), |
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
104 loc.base_sock_path.display() |
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
105 ); |
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
106 fs::rename(&sock_path, &loc.base_sock_path)?; |
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
107 Ok((loc, client)) |
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
108 }) |
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
109 } |
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
110 |
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
111 /// Tries to connect to the just spawned server repeatedly until timeout |
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
112 /// exceeded. |
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
113 fn connect_spawned( |
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
114 self, |
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
115 server: Child, |
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
116 sock_path: PathBuf, |
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
117 ) -> impl Future<Item = (Self, UnixClient, PathBuf), Error = io::Error> { |
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
118 debug!("try connect to {} repeatedly", sock_path.display()); |
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
119 let connect = future::loop_fn(sock_path, |sock_path| { |
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
120 UnixClient::connect(sock_path.clone()).then(|res| { |
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
121 match res { |
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
122 Ok(client) => Either::A(future::ok(Loop::Break((client, sock_path)))), |
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
123 Err(_) => { |
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
124 // try again with slight delay |
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
125 let fut = tokio_timer::sleep(Duration::from_millis(10)) |
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
126 .map(|()| Loop::Continue(sock_path)) |
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
127 .map_err(|err| io::Error::new(io::ErrorKind::Other, err)); |
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
128 Either::B(fut) |
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
129 } |
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
130 } |
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
131 }) |
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
132 }); |
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
133 |
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
134 // waits for either connection established or server failed to start |
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
135 connect |
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
136 .select2(server) |
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
137 .map_err(|res| res.split().0) |
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
138 .timeout(self.timeout) |
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
139 .map_err(|err| { |
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
140 err.into_inner().unwrap_or_else(|| { |
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
141 io::Error::new( |
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
142 io::ErrorKind::TimedOut, |
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
143 "timed out while connecting to server", |
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
144 ) |
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
145 }) |
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
146 }) |
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
147 .and_then(|res| { |
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
148 match res { |
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
149 Either::A(((client, sock_path), server)) => { |
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
150 server.forget(); // continue to run in background |
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
151 Ok((self, client, sock_path)) |
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
152 } |
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
153 Either::B((st, _)) => Err(io::Error::new( |
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
154 io::ErrorKind::Other, |
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
155 format!("server exited too early: {}", st), |
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
156 )), |
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
157 } |
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
158 }) |
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
159 } |
40289
7d3285f799cc
rust-chg: add struct holding information needed to spawn server process
Yuya Nishihara <yuya@tcha.org>
parents:
39976
diff
changeset
|
160 } |
7d3285f799cc
rust-chg: add struct holding information needed to spawn server process
Yuya Nishihara <yuya@tcha.org>
parents:
39976
diff
changeset
|
161 |
39976
44840bcc411a
rust-chg: port basic socket path handling from cHg of C
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
162 /// Determines the server socket to connect to. |
44840bcc411a
rust-chg: port basic socket path handling from cHg of C
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
163 /// |
44840bcc411a
rust-chg: port basic socket path handling from cHg of C
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
164 /// If no `$CHGSOCKNAME` is specified, the socket directory will be created |
44840bcc411a
rust-chg: port basic socket path handling from cHg of C
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
165 /// as necessary. |
44671
bb936e25a84a
rust-chg: spawn server process if not running
Yuya Nishihara <yuya@tcha.org>
parents:
44668
diff
changeset
|
166 fn prepare_server_socket_path() -> io::Result<PathBuf> { |
39976
44840bcc411a
rust-chg: port basic socket path handling from cHg of C
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
167 if let Some(s) = env::var_os("CHGSOCKNAME") { |
44840bcc411a
rust-chg: port basic socket path handling from cHg of C
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
168 Ok(PathBuf::from(s)) |
44840bcc411a
rust-chg: port basic socket path handling from cHg of C
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
169 } else { |
44840bcc411a
rust-chg: port basic socket path handling from cHg of C
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
170 let mut path = default_server_socket_dir(); |
44840bcc411a
rust-chg: port basic socket path handling from cHg of C
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
171 create_secure_dir(&path)?; |
44840bcc411a
rust-chg: port basic socket path handling from cHg of C
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
172 path.push("server"); |
44840bcc411a
rust-chg: port basic socket path handling from cHg of C
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
173 Ok(path) |
44840bcc411a
rust-chg: port basic socket path handling from cHg of C
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
174 } |
44840bcc411a
rust-chg: port basic socket path handling from cHg of C
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
175 } |
44840bcc411a
rust-chg: port basic socket path handling from cHg of C
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
176 |
44840bcc411a
rust-chg: port basic socket path handling from cHg of C
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
177 /// Determines the default server socket path as follows. |
44840bcc411a
rust-chg: port basic socket path handling from cHg of C
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
178 /// |
44840bcc411a
rust-chg: port basic socket path handling from cHg of C
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
179 /// 1. `$XDG_RUNTIME_DIR/chg` |
44840bcc411a
rust-chg: port basic socket path handling from cHg of C
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
180 /// 2. `$TMPDIR/chg$UID` |
44840bcc411a
rust-chg: port basic socket path handling from cHg of C
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
181 /// 3. `/tmp/chg$UID` |
44840bcc411a
rust-chg: port basic socket path handling from cHg of C
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
182 pub fn default_server_socket_dir() -> PathBuf { |
44840bcc411a
rust-chg: port basic socket path handling from cHg of C
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
183 // XDG_RUNTIME_DIR should be ignored if it has an insufficient permission. |
44840bcc411a
rust-chg: port basic socket path handling from cHg of C
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
184 // https://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html |
44840bcc411a
rust-chg: port basic socket path handling from cHg of C
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
185 if let Some(Ok(s)) = env::var_os("XDG_RUNTIME_DIR").map(check_secure_dir) { |
44840bcc411a
rust-chg: port basic socket path handling from cHg of C
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
186 let mut path = PathBuf::from(s); |
44840bcc411a
rust-chg: port basic socket path handling from cHg of C
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
187 path.push("chg"); |
44840bcc411a
rust-chg: port basic socket path handling from cHg of C
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
188 path |
44840bcc411a
rust-chg: port basic socket path handling from cHg of C
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
189 } else { |
44840bcc411a
rust-chg: port basic socket path handling from cHg of C
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
190 let mut path = env::temp_dir(); |
44840bcc411a
rust-chg: port basic socket path handling from cHg of C
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
191 path.push(format!("chg{}", procutil::get_effective_uid())); |
44840bcc411a
rust-chg: port basic socket path handling from cHg of C
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
192 path |
44840bcc411a
rust-chg: port basic socket path handling from cHg of C
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
193 } |
44840bcc411a
rust-chg: port basic socket path handling from cHg of C
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
194 } |
44840bcc411a
rust-chg: port basic socket path handling from cHg of C
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
195 |
40289
7d3285f799cc
rust-chg: add struct holding information needed to spawn server process
Yuya Nishihara <yuya@tcha.org>
parents:
39976
diff
changeset
|
196 /// Determines the default hg command. |
7d3285f799cc
rust-chg: add struct holding information needed to spawn server process
Yuya Nishihara <yuya@tcha.org>
parents:
39976
diff
changeset
|
197 pub fn default_hg_command() -> OsString { |
7d3285f799cc
rust-chg: add struct holding information needed to spawn server process
Yuya Nishihara <yuya@tcha.org>
parents:
39976
diff
changeset
|
198 // TODO: maybe allow embedding the path at compile time (or load from hgrc) |
43818
ce088b38f92b
rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40289
diff
changeset
|
199 env::var_os("CHGHG") |
ce088b38f92b
rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40289
diff
changeset
|
200 .or(env::var_os("HG")) |
ce088b38f92b
rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40289
diff
changeset
|
201 .unwrap_or(OsStr::new("hg").to_owned()) |
40289
7d3285f799cc
rust-chg: add struct holding information needed to spawn server process
Yuya Nishihara <yuya@tcha.org>
parents:
39976
diff
changeset
|
202 } |
7d3285f799cc
rust-chg: add struct holding information needed to spawn server process
Yuya Nishihara <yuya@tcha.org>
parents:
39976
diff
changeset
|
203 |
7d3285f799cc
rust-chg: add struct holding information needed to spawn server process
Yuya Nishihara <yuya@tcha.org>
parents:
39976
diff
changeset
|
204 fn default_timeout() -> Duration { |
43818
ce088b38f92b
rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40289
diff
changeset
|
205 let secs = env::var("CHGTIMEOUT") |
ce088b38f92b
rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40289
diff
changeset
|
206 .ok() |
ce088b38f92b
rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40289
diff
changeset
|
207 .and_then(|s| s.parse().ok()) |
ce088b38f92b
rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40289
diff
changeset
|
208 .unwrap_or(60); |
40289
7d3285f799cc
rust-chg: add struct holding information needed to spawn server process
Yuya Nishihara <yuya@tcha.org>
parents:
39976
diff
changeset
|
209 Duration::from_secs(secs) |
7d3285f799cc
rust-chg: add struct holding information needed to spawn server process
Yuya Nishihara <yuya@tcha.org>
parents:
39976
diff
changeset
|
210 } |
7d3285f799cc
rust-chg: add struct holding information needed to spawn server process
Yuya Nishihara <yuya@tcha.org>
parents:
39976
diff
changeset
|
211 |
39976
44840bcc411a
rust-chg: port basic socket path handling from cHg of C
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
212 /// Creates a directory which the other users cannot access to. |
44840bcc411a
rust-chg: port basic socket path handling from cHg of C
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
213 /// |
44840bcc411a
rust-chg: port basic socket path handling from cHg of C
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
214 /// If the directory already exists, tests its permission. |
44840bcc411a
rust-chg: port basic socket path handling from cHg of C
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
215 fn create_secure_dir<P>(path: P) -> io::Result<()> |
43818
ce088b38f92b
rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40289
diff
changeset
|
216 where |
ce088b38f92b
rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40289
diff
changeset
|
217 P: AsRef<Path>, |
39976
44840bcc411a
rust-chg: port basic socket path handling from cHg of C
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
218 { |
43818
ce088b38f92b
rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40289
diff
changeset
|
219 DirBuilder::new() |
ce088b38f92b
rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40289
diff
changeset
|
220 .mode(0o700) |
ce088b38f92b
rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40289
diff
changeset
|
221 .create(path.as_ref()) |
ce088b38f92b
rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40289
diff
changeset
|
222 .or_else(|err| { |
ce088b38f92b
rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40289
diff
changeset
|
223 if err.kind() == io::ErrorKind::AlreadyExists { |
ce088b38f92b
rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40289
diff
changeset
|
224 check_secure_dir(path).map(|_| ()) |
ce088b38f92b
rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40289
diff
changeset
|
225 } else { |
ce088b38f92b
rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40289
diff
changeset
|
226 Err(err) |
ce088b38f92b
rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40289
diff
changeset
|
227 } |
ce088b38f92b
rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40289
diff
changeset
|
228 }) |
39976
44840bcc411a
rust-chg: port basic socket path handling from cHg of C
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
229 } |
44840bcc411a
rust-chg: port basic socket path handling from cHg of C
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
230 |
44840bcc411a
rust-chg: port basic socket path handling from cHg of C
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
231 fn check_secure_dir<P>(path: P) -> io::Result<P> |
43818
ce088b38f92b
rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40289
diff
changeset
|
232 where |
ce088b38f92b
rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40289
diff
changeset
|
233 P: AsRef<Path>, |
39976
44840bcc411a
rust-chg: port basic socket path handling from cHg of C
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
234 { |
44840bcc411a
rust-chg: port basic socket path handling from cHg of C
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
235 let a = fs::symlink_metadata(path.as_ref())?; |
44840bcc411a
rust-chg: port basic socket path handling from cHg of C
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
236 if a.is_dir() && a.uid() == procutil::get_effective_uid() && (a.mode() & 0o777) == 0o700 { |
44840bcc411a
rust-chg: port basic socket path handling from cHg of C
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
237 Ok(path) |
44840bcc411a
rust-chg: port basic socket path handling from cHg of C
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
238 } else { |
44840bcc411a
rust-chg: port basic socket path handling from cHg of C
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
239 Err(io::Error::new(io::ErrorKind::Other, "insecure directory")) |
44840bcc411a
rust-chg: port basic socket path handling from cHg of C
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
240 } |
44840bcc411a
rust-chg: port basic socket path handling from cHg of C
Yuya Nishihara <yuya@tcha.org>
parents:
diff
changeset
|
241 } |