Skip to main content

arti/
process.rs

1//! Code to adjust process-related parameters.
2
3use tracing::error;
4
5use crate::ArtiConfig;
6
7/// Set our current maximum-file limit to a large value, if we can.
8///
9/// Since we're going to be used as a proxy, we're likely to need a
10/// _lot_ of simultaneous sockets.
11///
12/// # Limitations
13///
14/// This doesn't actually do anything on windows.
15#[cfg_attr(feature = "experimental-api", visibility::make(pub))]
16#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
17pub(crate) fn use_max_file_limit(config: &ArtiConfig) {
18    match rlimit::increase_nofile_limit(config.system.max_files) {
19        Ok(n) => tracing::debug!("Increased process file limit to {}", n),
20        Err(e) => tor_error::warn_report!(e, "Error while increasing file limit"),
21    }
22}
23
24/// Check that we are not running as "root".
25///
26/// If we are, give an error message, and exit.
27pub(crate) fn exit_if_root() {
28    if running_as_root() {
29        error!(
30            "You are running Arti as root. You don't need to, and \
31             you probably shouldn't. \
32             To run as root anyway, set application.allow_running_as_root."
33        );
34        std::process::exit(1);
35    }
36}
37
38/// Return true if we seem to be running as root.
39fn running_as_root() -> bool {
40    #[cfg(target_family = "unix")]
41    unsafe {
42        libc::geteuid() == 0
43    }
44    #[cfg(not(target_family = "unix"))]
45    false
46}
47
48/// Return an async stream that reports an event whenever we get a `SIGHUP`
49/// signal.
50///
51/// Note that the signal-handling backend can coalesce signals; this is normal.
52#[cfg(target_family = "unix")]
53pub(crate) fn sighup_stream() -> crate::Result<impl futures::Stream<Item = ()>> {
54    cfg_if::cfg_if! {
55        if #[cfg(feature="tokio")] {
56            use tokio_crate::signal::unix as s;
57            let mut signal = s::signal(s::SignalKind::hangup())?;
58            Ok(futures::stream::poll_fn(move |ctx| signal.poll_recv(ctx)))
59        } else if #[cfg(feature="async-std")] {
60            use async_signal::{Signal, Signals};
61            use futures::stream::StreamExt as _;
62            let signals = Signals::new(&[Signal::Hup])?;
63            Ok(signals.map(|_| ()))
64        } else {
65            // Not backend, so we won't ever get a SIGHUP.
66            Ok(futures::stream::pending())
67        }
68    }
69}