public ProcessBuilder getProcessBuilder ( ) { if ( spawned ) return builder ; // throw new IllegalStateException("Cannot call spawn twice!"); if ( runAs != null ) { String command = cmd . get ( 0 ) ; if ( command . charAt ( 0 ) == ' ' && ! SudoFeature . hasArgumentsEnd ( ) ) throw new IllegalArgumentException ( "Command to runAs starts with - but this version of sudo does not support the -- argument end token: this command cannot, therefore, be executed securely. Command was: '" + command + "'" ) ; // Pass the environment in through an "env" command: if ( this . environment . size ( ) > 0 ) { for ( final String key : this . environment . keySet ( ) ) { final String value = this . environment . get ( key ) ; final String var = key + "=" + value ; addToCmd ( 0 , var ) ; } // cmd.add(0,"env"); addToCmd ( 0 , "env" ) ; } // cmd.add(0, "--"); // doesn't work everywhere // cmd.add(0, runAs); // cmd.add(0, "-u"); // cmd.add(0, "-n"); // Never prompt for a password: we simply cannot provide one // cmd.add(0, "sudo"); if ( SudoFeature . hasArgumentsEnd ( ) ) addToCmd ( 0 , "--" ) ; // If possible tell sudo to run non-interactively String noninteractive = SudoFeature . hasNonInteractive ( ) ? "-n" : null ; if ( this . runAs . equals ( SUPERUSER_IDENTIFIER ) ) { addToCmd ( 0 , "sudo" , noninteractive ) ; } else addToCmd ( 0 , "sudo" , noninteractive , "-u" , runAs ) ; } else { builder . environment ( ) . putAll ( this . environment ) ; } spawned = true ; if ( log . isInfoEnabled ( ) ) { log . info ( "ProcessBuilder created for command: " + join ( " " , cmd ) ) ; } return builder ; }
Returns a ProcessBuilder for use in a manual launching