Skip to content

Commit f9f2bf6

Browse files
Nuginebee-san
andauthored
Upgrade rlimit (#449)
Co-authored-by: Bee <[email protected]>
1 parent 6fb1196 commit f9f2bf6

File tree

4 files changed

+28
-24
lines changed

4 files changed

+28
-24
lines changed

Cargo.lock

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ colored = "2.0.0"
2121
structopt = "0.3.20"
2222
async-std = "1.7.0"
2323
futures = "0.3"
24-
rlimit = "0.5.4"
24+
rlimit = "0.8.3"
2525
shell-words = "1.0.0"
2626
log = "0.4.0"
2727
env_logger = "0.8.2"

src/input.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ pub struct Opts {
117117

118118
/// Automatically ups the ULIMIT with the value you provided.
119119
#[structopt(short, long)]
120-
pub ulimit: Option<rlimit::RawRlim>,
120+
pub ulimit: Option<u64>,
121121

122122
/// The order of scanning to be performed. The "serial" option will
123123
/// scan ports in ascending order while the "random" option will scan
@@ -221,7 +221,7 @@ pub struct Config {
221221
batch_size: Option<u16>,
222222
timeout: Option<u32>,
223223
tries: Option<u8>,
224-
ulimit: Option<rlimit::RawRlim>,
224+
ulimit: Option<u64>,
225225
scan_order: Option<ScanOrder>,
226226
command: Option<Vec<String>>,
227227
scripts: Option<ScriptsRequired>,

src/main.rs

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@ use scripts::{init_scripts, Script, ScriptFile};
2424
use cidr_utils::cidr::IpCidr;
2525
use colorful::{Color, Colorful};
2626
use futures::executor::block_on;
27-
use rlimit::{getrlimit, setrlimit, RawRlim, Resource, Rlim};
2827
use std::collections::HashMap;
29-
use std::convert::TryInto;
3028
use std::fs::File;
3129
use std::io::{prelude::*, BufReader};
3230
use std::net::{IpAddr, ToSocketAddrs};
@@ -42,9 +40,10 @@ extern crate colorful;
4240
extern crate dirs;
4341

4442
// Average value for Ubuntu
45-
const DEFAULT_FILE_DESCRIPTORS_LIMIT: RawRlim = 8000;
43+
#[cfg(unix)]
44+
const DEFAULT_FILE_DESCRIPTORS_LIMIT: u64 = 8000;
4645
// Safest batch size based on experimentation
47-
const AVERAGE_BATCH_SIZE: RawRlim = 3000;
46+
const AVERAGE_BATCH_SIZE: u16 = 3000;
4847

4948
#[macro_use]
5049
extern crate log;
@@ -93,8 +92,11 @@ fn main() {
9392
std::process::exit(1);
9493
}
9594

96-
let ulimit: RawRlim = adjust_ulimit_size(&opts);
97-
let batch_size: u16 = infer_batch_size(&opts, ulimit);
95+
#[cfg(unix)]
96+
let batch_size: u16 = infer_batch_size(&opts, adjust_ulimit_size(&opts));
97+
98+
#[cfg(not(unix))]
99+
let batch_size: u16 = AVERAGE_BATCH_SIZE;
98100

99101
let scanner = Scanner::new(
100102
&ips,
@@ -328,11 +330,11 @@ fn read_ips_from_file(
328330
Ok(ips)
329331
}
330332

331-
fn adjust_ulimit_size(opts: &Opts) -> RawRlim {
332-
if opts.ulimit.is_some() {
333-
let limit: Rlim = Rlim::from_raw(opts.ulimit.unwrap());
334-
335-
if setrlimit(Resource::NOFILE, limit, limit).is_ok() {
333+
#[cfg(unix)]
334+
fn adjust_ulimit_size(opts: &Opts) -> u64 {
335+
use rlimit::Resource;
336+
if let Some(limit) = opts.ulimit {
337+
if Resource::NOFILE.set(limit, limit).is_ok() {
336338
detail!(
337339
format!("Automatically increasing ulimit value to {}.", limit),
338340
opts.greppable,
@@ -347,13 +349,15 @@ fn adjust_ulimit_size(opts: &Opts) -> RawRlim {
347349
}
348350
}
349351

350-
let (rlim, _) = getrlimit(Resource::NOFILE).unwrap();
351-
352-
rlim.as_raw()
352+
let (soft, _) = Resource::NOFILE.get().unwrap();
353+
soft
353354
}
354355

355-
fn infer_batch_size(opts: &Opts, ulimit: RawRlim) -> u16 {
356-
let mut batch_size: RawRlim = opts.batch_size.into();
356+
#[cfg(unix)]
357+
fn infer_batch_size(opts: &Opts, ulimit: u64) -> u16 {
358+
use std::convert::TryInto;
359+
360+
let mut batch_size: u64 = opts.batch_size.into();
357361

358362
// Adjust the batch size when the ulimit value is lower than the desired batch size
359363
if ulimit < batch_size {
@@ -364,7 +368,7 @@ fn infer_batch_size(opts: &Opts, ulimit: RawRlim) -> u16 {
364368
// When the OS supports high file limits like 8000, but the user
365369
// selected a batch size higher than this we should reduce it to
366370
// a lower number.
367-
if ulimit < AVERAGE_BATCH_SIZE {
371+
if ulimit < AVERAGE_BATCH_SIZE.into() {
368372
// ulimit is smaller than aveage batch size
369373
// user must have very small ulimit
370374
// decrease batch size to half of ulimit
@@ -373,7 +377,7 @@ fn infer_batch_size(opts: &Opts, ulimit: RawRlim) -> u16 {
373377
batch_size = ulimit / 2;
374378
} else if ulimit > DEFAULT_FILE_DESCRIPTORS_LIMIT {
375379
info!("Batch size is now average batch size");
376-
batch_size = AVERAGE_BATCH_SIZE;
380+
batch_size = AVERAGE_BATCH_SIZE.into();
377381
} else {
378382
batch_size = ulimit - 100;
379383
}

0 commit comments

Comments
 (0)