Example #1
0
    VirtualChannel start(TaskListener listener, String rootPassword)
        throws IOException, InterruptedException {
      final int uid = LIBC.geteuid();

      if (uid == 0) // already running as root
      return newLocalChannel();

      String javaExe = System.getProperty("java.home") + "/bin/java";
      File slaveJar = Which.jarFile(Launcher.class);

      ArgumentListBuilder args = new ArgumentListBuilder().add(javaExe);
      if (slaveJar.isFile()) args.add("-jar").add(slaveJar);
      else // in production code this never happens, but during debugging this is convenientud
      args.add("-cp").add(slaveJar).add(hudson.remoting.Launcher.class.getName());

      if (rootPassword == null) {
        // try sudo, in the hope that the user has the permission to do so without password
        return new LocalLauncher(listener)
            .launchChannel(
                args.prepend(sudoExe()).toCommandArray(),
                listener.getLogger(),
                null,
                Collections.<String, String>emptyMap());
      } else {
        // try sudo with the given password. Also run in pfexec so that we can elevate the
        // privileges
        Process proc = sudoWithPass(args);
        return Channels.forProcess(
            args.toStringWithQuote(), Computer.threadPoolForRemoting, proc, listener.getLogger());
      }
    }