@@ -24,9 +24,7 @@ use scripts::{init_scripts, Script, ScriptFile};
24
24
use cidr_utils:: cidr:: IpCidr ;
25
25
use colorful:: { Color , Colorful } ;
26
26
use futures:: executor:: block_on;
27
- use rlimit:: { getrlimit, setrlimit, RawRlim , Resource , Rlim } ;
28
27
use std:: collections:: HashMap ;
29
- use std:: convert:: TryInto ;
30
28
use std:: fs:: File ;
31
29
use std:: io:: { prelude:: * , BufReader } ;
32
30
use std:: net:: { IpAddr , ToSocketAddrs } ;
@@ -42,9 +40,10 @@ extern crate colorful;
42
40
extern crate dirs;
43
41
44
42
// Average value for Ubuntu
45
- const DEFAULT_FILE_DESCRIPTORS_LIMIT : RawRlim = 8000 ;
43
+ #[ cfg( unix) ]
44
+ const DEFAULT_FILE_DESCRIPTORS_LIMIT : u64 = 8000 ;
46
45
// Safest batch size based on experimentation
47
- const AVERAGE_BATCH_SIZE : RawRlim = 3000 ;
46
+ const AVERAGE_BATCH_SIZE : u16 = 3000 ;
48
47
49
48
#[ macro_use]
50
49
extern crate log;
@@ -93,8 +92,11 @@ fn main() {
93
92
std:: process:: exit ( 1 ) ;
94
93
}
95
94
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 ;
98
100
99
101
let scanner = Scanner :: new (
100
102
& ips,
@@ -328,11 +330,11 @@ fn read_ips_from_file(
328
330
Ok ( ips)
329
331
}
330
332
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 ( ) {
336
338
detail ! (
337
339
format!( "Automatically increasing ulimit value to {}." , limit) ,
338
340
opts. greppable,
@@ -347,13 +349,15 @@ fn adjust_ulimit_size(opts: &Opts) -> RawRlim {
347
349
}
348
350
}
349
351
350
- let ( rlim, _) = getrlimit ( Resource :: NOFILE ) . unwrap ( ) ;
351
-
352
- rlim. as_raw ( )
352
+ let ( soft, _) = Resource :: NOFILE . get ( ) . unwrap ( ) ;
353
+ soft
353
354
}
354
355
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 ( ) ;
357
361
358
362
// Adjust the batch size when the ulimit value is lower than the desired batch size
359
363
if ulimit < batch_size {
@@ -364,7 +368,7 @@ fn infer_batch_size(opts: &Opts, ulimit: RawRlim) -> u16 {
364
368
// When the OS supports high file limits like 8000, but the user
365
369
// selected a batch size higher than this we should reduce it to
366
370
// a lower number.
367
- if ulimit < AVERAGE_BATCH_SIZE {
371
+ if ulimit < AVERAGE_BATCH_SIZE . into ( ) {
368
372
// ulimit is smaller than aveage batch size
369
373
// user must have very small ulimit
370
374
// decrease batch size to half of ulimit
@@ -373,7 +377,7 @@ fn infer_batch_size(opts: &Opts, ulimit: RawRlim) -> u16 {
373
377
batch_size = ulimit / 2 ;
374
378
} else if ulimit > DEFAULT_FILE_DESCRIPTORS_LIMIT {
375
379
info ! ( "Batch size is now average batch size" ) ;
376
- batch_size = AVERAGE_BATCH_SIZE ;
380
+ batch_size = AVERAGE_BATCH_SIZE . into ( ) ;
377
381
} else {
378
382
batch_size = ulimit - 100 ;
379
383
}
0 commit comments