fdlimit/
lib.rs

1// Copyright 2016-2020 Parity Technologies (UK) Ltd.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15/// Outcome of raising file descriptor resource limit
16pub enum Outcome {
17	/// Limit was raised successfully
18	LimitRaised {
19		/// Previous limit (likely soft limit)
20		from: u64,
21		/// New limit (likely hard limit)
22		to: u64,
23	},
24	/// Raising limit is not supported on this platform
25	Unsupported,
26}
27
28/// Errors that happen when trying to raise file descriptor resource limit
29#[derive(Debug, thiserror::Error)]
30pub enum Error {
31	/// Failed to call sysctl to get max supported value configured in sysctl
32	#[error("Failed to call sysctl to get max supported value configured in sysctl: {0}")]
33	#[cfg(any(target_os = "macos", target_os = "ios"))]
34	FailedToCallSysctl(std::io::Error),
35	/// Failed to get current limit
36	#[error("Failed to get current limit: {0}")]
37	FailedToGetLimit(std::io::Error),
38	/// Failed to set new limit
39	#[error("Failed to set new limit ({from}->{to}): {error}")]
40	FailedToSetLimit {
41		/// Current limit
42		from: u64,
43		/// New desired limit
44		to: u64,
45		/// Low level OS error
46		error: std::io::Error,
47	},
48}
49
50/// Raise the soft open file descriptor resource limit to the smaller of the
51/// kernel limit and the hard resource limit.
52///
53/// darwin_fd_limit exists to work around an issue where launchctl on Mac OS X
54/// defaults the rlimit maxfiles to 256/unlimited. The default soft limit of 256
55/// ends up being far too low for our multithreaded scheduler testing, depending
56/// on the number of cores available.
57#[cfg(any(target_os = "macos", target_os = "ios"))]
58#[allow(clippy::useless_conversion, non_camel_case_types)]
59pub fn raise_fd_limit() -> Result<Outcome, Error> {
60	use std::cmp;
61	use std::io;
62	use std::mem::size_of_val;
63	use std::ptr::null_mut;
64
65	unsafe {
66		static CTL_KERN: libc::c_int = 1;
67		static KERN_MAXFILESPERPROC: libc::c_int = 29;
68
69		// The strategy here is to fetch the current resource limits, read the
70		// kern.maxfilesperproc sysctl value, and bump the soft resource limit for
71		// maxfiles up to the sysctl value.
72
73		// Fetch the kern.maxfilesperproc value
74		let mut mib: [libc::c_int; 2] = [CTL_KERN, KERN_MAXFILESPERPROC];
75		let mut maxfiles: libc::c_int = 0;
76		let mut size: libc::size_t = size_of_val(&maxfiles) as libc::size_t;
77		if libc::sysctl(&mut mib[0], 2, &mut maxfiles as *mut _ as *mut _, &mut size, null_mut(), 0)
78			!= 0
79		{
80			return Err(Error::FailedToCallSysctl(io::Error::last_os_error()));
81		}
82
83		// Fetch the current resource limits
84		let mut rlim = libc::rlimit { rlim_cur: 0, rlim_max: 0 };
85		if libc::getrlimit(libc::RLIMIT_NOFILE, &mut rlim) != 0 {
86			return Err(Error::FailedToGetLimit(io::Error::last_os_error()));
87		}
88
89		let old_value = rlim.rlim_cur;
90
91		// Bump the soft limit to the smaller of kern.maxfilesperproc and the hard
92		// limit
93		rlim.rlim_cur = cmp::min(maxfiles as libc::rlim_t, rlim.rlim_max);
94
95		// Set our newly-increased resource limit
96		if libc::setrlimit(libc::RLIMIT_NOFILE, &rlim) != 0 {
97			return Err(Error::FailedToSetLimit {
98				from: old_value.into(),
99				to: rlim.rlim_cur.into(),
100				error: io::Error::last_os_error(),
101			});
102		}
103
104		Ok(Outcome::LimitRaised { from: old_value.into(), to: rlim.rlim_cur.into() })
105	}
106}
107
108/// Raise the soft open file descriptor resource limit to the hard resource
109/// limit.
110#[cfg(target_os = "linux")]
111#[allow(clippy::useless_conversion, non_camel_case_types)]
112pub fn raise_fd_limit() -> Result<Outcome, Error> {
113	use std::io;
114
115	unsafe {
116		// Fetch the current resource limits
117		let mut rlim = libc::rlimit { rlim_cur: 0, rlim_max: 0 };
118		if libc::getrlimit(libc::RLIMIT_NOFILE, &mut rlim) != 0 {
119			return Err(Error::FailedToGetLimit(io::Error::last_os_error()));
120		}
121
122		let old_value = rlim.rlim_cur;
123
124		// Set soft limit to hard imit
125		rlim.rlim_cur = rlim.rlim_max;
126
127		// Set our newly-increased resource limit
128		if libc::setrlimit(libc::RLIMIT_NOFILE, &rlim) != 0 {
129			return Err(Error::FailedToSetLimit {
130				from: old_value.into(),
131				to: rlim.rlim_cur.into(),
132				error: io::Error::last_os_error(),
133			});
134		}
135
136		Ok(Outcome::LimitRaised { from: old_value.into(), to: rlim.rlim_cur.into() })
137	}
138}
139
140/// Does nothing on unsupported platform
141#[cfg(not(any(target_os = "macos", target_os = "ios", target_os = "linux")))]
142pub fn raise_fd_limit() -> Result<Outcome, Error> {
143	Ok(Outcome::Unsupported)
144}