summaryrefslogtreecommitdiff
path: root/cli/util/unix.rs
blob: 2fa3c206360b9d1e2a2a667adf62c84e193ec9a2 (plain)
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
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.

/// Raise soft file descriptor limit to hard file descriptor limit.
/// This is the difference between `ulimit -n` and `ulimit -n -H`.
pub fn raise_fd_limit() {
  #[cfg(unix)]
  // TODO(bartlomieju):
  #[allow(clippy::undocumented_unsafe_blocks)]
  unsafe {
    let mut limits = libc::rlimit {
      rlim_cur: 0,
      rlim_max: 0,
    };

    if 0 != libc::getrlimit(libc::RLIMIT_NOFILE, &mut limits) {
      return;
    }

    if limits.rlim_cur == libc::RLIM_INFINITY {
      return;
    }

    // No hard limit? Do a binary search for the effective soft limit.
    if limits.rlim_max == libc::RLIM_INFINITY {
      let mut min = limits.rlim_cur;
      let mut max = 1 << 20;

      while min + 1 < max {
        limits.rlim_cur = min + (max - min) / 2;
        match libc::setrlimit(libc::RLIMIT_NOFILE, &limits) {
          0 => min = limits.rlim_cur,
          _ => max = limits.rlim_cur,
        }
      }

      return;
    }

    // Raise the soft limit to the hard limit.
    if limits.rlim_cur < limits.rlim_max {
      limits.rlim_cur = limits.rlim_max;
      libc::setrlimit(libc::RLIMIT_NOFILE, &limits);
    }
  }
}

pub fn prepare_stdio() {
  #[cfg(unix)]
  // SAFETY: Save current state of stdio and restore it when we exit.
  unsafe {
    use libc::atexit;
    use libc::tcgetattr;
    use libc::tcsetattr;
    use libc::termios;

    let mut termios = std::mem::zeroed::<termios>();
    if tcgetattr(libc::STDIN_FILENO, &mut termios) == 0 {
      static mut ORIG_TERMIOS: Option<termios> = None;
      ORIG_TERMIOS = Some(termios);

      extern "C" fn reset_stdio() {
        // SAFETY: Reset the stdio state.
        unsafe { tcsetattr(libc::STDIN_FILENO, 0, &ORIG_TERMIOS.unwrap()) };
      }

      atexit(reset_stdio);
    }
  }
}