MCPcopy Create free account
hub / github.com/bitcoin/bitcoin / RaiseFileDescriptorLimit

Function RaiseFileDescriptorLimit

src/util/fs_helpers.cpp:159–194  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

157}
158
159int RaiseFileDescriptorLimit(int min_fd)
160{
161 Assert(min_fd >= 0);
162#if defined(WIN32)
163 return 2048;
164#else
165 struct rlimit limitFD;
166 if (getrlimit(RLIMIT_NOFILE, &limitFD) != -1) {
167 // If the current soft limit is already higher, don't raise it
168 if (limitFD.rlim_cur != RLIM_INFINITY && std::cmp_less(limitFD.rlim_cur, min_fd)) {
169 const auto current_limit{limitFD.rlim_cur};
170 static_assert(std::in_range<rlim_t>(std::numeric_limits<int>::max()));
171 limitFD.rlim_cur = static_cast<rlim_t>(min_fd);
172 // Don't raise soft limit beyond hard limit
173 if ((limitFD.rlim_max != RLIM_INFINITY) && (limitFD.rlim_cur > limitFD.rlim_max)) {
174 limitFD.rlim_cur = limitFD.rlim_max;
175 }
176 if (current_limit != limitFD.rlim_cur) {
177 setrlimit(RLIMIT_NOFILE, &limitFD);
178 getrlimit(RLIMIT_NOFILE, &limitFD);
179 }
180 }
181 // Check the (possibly raised) current soft limit against the special
182 // value of RLIM_INFINITY. Some platforms implement this as the maximum
183 // uint64, others as int64 (-1). Avoid casting even if the return type
184 // is changed to uint64_t. We also cap unlikely but possible values
185 // that would overflow int.
186 if (limitFD.rlim_cur == RLIM_INFINITY ||
187 std::cmp_greater_equal(limitFD.rlim_cur, std::numeric_limits<int>::max())) {
188 return std::numeric_limits<int>::max();
189 }
190 return static_cast<int>(limitFD.rlim_cur);
191 }
192 return min_fd; // getrlimit failed, assume it's fine
193#endif
194}
195
196/**
197 * this function tries to make a particular range of a file allocated (corresponding to disk space)

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected