@@ -238,6 +238,7 @@ pub fn install(no_prompt: bool, verbose: bool, quiet: bool, mut opts: InstallOpt
238
238
do_pre_install_sanity_checks ( ) ?;
239
239
do_pre_install_options_sanity_checks ( & opts) ?;
240
240
check_existence_of_rustc_or_cargo_in_path ( no_prompt) ?;
241
+ #[ cfg( unix) ]
241
242
do_anti_sudo_check ( no_prompt) ?;
242
243
243
244
let mut term = term2:: stdout ( ) ;
@@ -467,63 +468,33 @@ fn do_pre_install_options_sanity_checks(opts: &InstallOpts) -> Result<()> {
467
468
// If the user is trying to install with sudo, on some systems this will
468
469
// result in writing root-owned files to the user's home directory, because
469
470
// sudo is configured not to change $HOME. Don't let that bogosity happen.
470
- #[ allow ( dead_code ) ]
471
+ #[ cfg ( unix ) ]
471
472
fn do_anti_sudo_check ( no_prompt : bool ) -> Result < ( ) > {
472
- use std:: ffi:: OsString ;
473
-
474
- #[ cfg( unix) ]
475
- pub fn home_mismatch ( ) -> ( bool , OsString , String ) {
476
- use std:: ffi:: CStr ;
477
- use std:: mem:: MaybeUninit ;
478
- use std:: ptr;
479
-
473
+ pub fn home_mismatch ( ) -> ( bool , PathBuf , PathBuf ) {
474
+ let fallback = || ( false , PathBuf :: new ( ) , PathBuf :: new ( ) ) ;
480
475
// test runner should set this, nothing else
481
- if let Ok ( true ) = env:: var ( "RUSTUP_INIT_SKIP_SUDO_CHECK" ) . map ( |s| s == "yes" ) {
482
- return ( false , OsString :: new ( ) , String :: new ( ) ) ;
483
- }
484
- let mut buf = [ 0u8 ; 1024 ] ;
485
- let mut pwd = MaybeUninit :: < libc:: passwd > :: uninit ( ) ;
486
- let mut pwdp: * mut libc:: passwd = ptr:: null_mut ( ) ;
487
- let rv = unsafe {
488
- libc:: getpwuid_r (
489
- libc:: geteuid ( ) ,
490
- pwd. as_mut_ptr ( ) ,
491
- buf. as_mut_ptr ( ) . cast :: < libc:: c_char > ( ) ,
492
- buf. len ( ) ,
493
- ( & mut pwdp) as * mut * mut libc:: passwd ,
494
- )
495
- } ;
496
- if rv != 0 || pwdp. is_null ( ) {
497
- warn ! ( "getpwuid_r: couldn't get user data" ) ;
498
- return ( false , OsString :: new ( ) , String :: new ( ) ) ;
476
+ if env:: var_os ( "RUSTUP_INIT_SKIP_SUDO_CHECK" ) . map_or ( false , |s| s == "yes" ) {
477
+ return fallback ( ) ;
499
478
}
500
- let pwd = unsafe { pwd. assume_init ( ) } ;
501
- let pw_dir = unsafe { CStr :: from_ptr ( pwd. pw_dir ) } . to_str ( ) . ok ( ) ;
502
- let env_home = env:: var_os ( "HOME" ) ;
503
- match ( env_home, pw_dir) {
504
- ( None , _) | ( _, None ) => ( false , OsString :: new ( ) , String :: new ( ) ) ,
505
- ( Some ( eh) , Some ( pd) ) => ( eh != pd, eh, String :: from ( pd) ) ,
506
- }
507
- }
508
479
509
- #[ cfg( not( unix) ) ]
510
- pub fn home_mismatch ( ) -> ( bool , OsString , String ) {
511
- ( false , OsString :: new ( ) , String :: new ( ) )
480
+ match ( utils:: home_dir_from_passwd ( ) , env:: var_os ( "HOME" ) ) {
481
+ ( Some ( pw) , Some ( eh) ) if eh != pw => return ( true , PathBuf :: from ( eh) , pw) ,
482
+ ( None , _) => warn ! ( "getpwuid_r: couldn't get user data" ) ,
483
+ _ => { }
484
+ }
485
+ fallback ( )
512
486
}
513
487
514
- match ( home_mismatch ( ) , no_prompt ) {
515
- ( ( false , _, _) , _ ) => ( ) ,
516
- ( ( true , env_home, euid_home) , false ) => {
488
+ match home_mismatch ( ) {
489
+ ( false , _, _) => { }
490
+ ( true , env_home, euid_home) => {
517
491
err ! ( "$HOME differs from euid-obtained home directory: you may be using sudo" ) ;
518
- err ! ( "$HOME directory: {:?}" , env_home) ;
519
- err ! ( "euid-obtained home directory: {}" , euid_home) ;
520
- err ! ( "if this is what you want, restart the installation with `-y'" ) ;
521
- process:: exit ( 1 ) ;
522
- }
523
- ( ( true , env_home, euid_home) , true ) => {
524
- warn ! ( "$HOME differs from euid-obtained home directory: you may be using sudo" ) ;
525
- warn ! ( "$HOME directory: {:?}" , env_home) ;
526
- warn ! ( "euid-obtained home directory: {}" , euid_home) ;
492
+ err ! ( "$HOME directory: {}" , env_home. display( ) ) ;
493
+ err ! ( "euid-obtained home directory: {}" , euid_home. display( ) ) ;
494
+ if !no_prompt {
495
+ err ! ( "if this is what you want, restart the installation with `-y'" ) ;
496
+ process:: exit ( 1 ) ;
497
+ }
527
498
}
528
499
}
529
500
0 commit comments