use of hudson.util.jna.SHELLEXECUTEINFO in project hudson-2.x by hudson.
the class WindowsSlaveInstaller method runElevated.
/**
* Invokes slave.exe with a SCM management command.
*
* <p>
* If it fails in a way that indicates the presence of UAC, retry in an UAC compatible manner.
*/
static int runElevated(File slaveExe, String command, TaskListener out, File pwd) throws IOException, InterruptedException {
try {
return new LocalLauncher(out).launch().cmds(slaveExe, command).stdout(out).pwd(pwd).join();
} catch (IOException e) {
if (e.getMessage().contains("CreateProcess") && e.getMessage().contains("=740")) {
// fall through
} else {
throw e;
}
}
// error code 740 is ERROR_ELEVATION_REQUIRED, indicating that
// we run in UAC-enabled Windows and we need to run this in an elevated privilege
SHELLEXECUTEINFO sei = new SHELLEXECUTEINFO();
sei.fMask = SEE_MASK_NOCLOSEPROCESS;
sei.lpVerb = "runas";
sei.lpFile = slaveExe.getAbsolutePath();
sei.lpParameters = "/redirect redirect.log " + command;
sei.lpDirectory = pwd.getAbsolutePath();
sei.nShow = SW_HIDE;
if (!Shell32.INSTANCE.ShellExecuteEx(sei))
throw new IOException("Failed to shellExecute: " + Native.getLastError());
try {
return Kernel32Utils.waitForExitProcess(sei.hProcess);
} finally {
FileInputStream fin = new FileInputStream(new File(pwd, "redirect.log"));
IOUtils.copy(fin, out.getLogger());
fin.close();
}
}
Aggregations