提交 a2204317 编写于 作者: D Daniel Weber

[JENKINS-9104] Add ProcessKillingVeto extension point

This allows extensions to veto killing of certain processes.

Issue 9104 is not yet solved by this, it is only part of the solution. The
rest should be taken care of in plugins.
上级 7e1a9215
/*
* The MIT License
*
* Copyright (c) 2015, Daniel Weber
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package hudson.util;
import hudson.ExtensionPoint;
import hudson.util.ProcessTreeRemoting.IOSProcess;
import java.util.Collections;
import java.util.List;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import jenkins.model.Jenkins;
/**
* Allows extensions to veto killing processes. If at least one extension vetoes
* the killing of a process, it will not be killed. This can be useful to keep
* daemon processes alive. An example is mspdbsrv.exe used by Microsoft
* compilers.
*
* See JENKINS-9104
*
* @since TODO
*
* @author Daniel Weber <daniel.weber.dev@gmail.com>
*/
public abstract class ProcessKillingVeto implements ExtensionPoint {
/**
* Describes the cause for a process killing veto.
*/
public static class VetoCause {
private final String message;
/**
* @param message A string describing the reason for the veto
*/
public VetoCause(@Nonnull String message) {
this.message = message;
}
/**
* @return A string describing the reason for the veto.
*/
public @Nonnull String getMessage() {
return message;
}
}
/**
* @return All ProcessKillingVeto extensions currently registered. An empty
* list if Jenkins is not available, never null.
*/
public static List<ProcessKillingVeto> all() {
Jenkins jenkins = Jenkins.getInstance();
if (jenkins == null)
return Collections.emptyList();
return jenkins.getExtensionList(ProcessKillingVeto.class);
}
/**
* Ask the extension whether it vetoes killing of the given process
*
* @param p The process that is about to be killed
* @return a {@link VetoCause} if the process should <em>not</em> be killed,
* null else.
*/
@CheckForNull
public abstract VetoCause vetoProcessKilling(@Nonnull IOSProcess p);
}
......@@ -32,6 +32,7 @@ import hudson.Util;
import hudson.remoting.Channel;
import hudson.remoting.VirtualChannel;
import hudson.slaves.SlaveComputer;
import hudson.util.ProcessKillingVeto.VetoCause;
import hudson.util.ProcessTree.OSProcess;
import hudson.util.ProcessTreeRemoting.IOSProcess;
import hudson.util.ProcessTreeRemoting.IProcessTree;
......@@ -56,6 +57,7 @@ import java.util.SortedMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.CheckForNull;
import static com.sun.jna.Pointer.NULL;
import static hudson.util.jna.GNUCLibrary.LIBC;
import static java.util.logging.Level.FINE;
......@@ -227,6 +229,22 @@ public abstract class ProcessTree implements Iterable<OSProcess>, IProcessTree,
*/
public abstract void killRecursively() throws InterruptedException;
/**
* @return The first non-null {@link VetoCause} provided by a process killing veto extension for this OSProcess.
* null if no one objects killing the process.
*/
protected @CheckForNull VetoCause getVeto() {
for (ProcessKillingVeto vetoExtension : ProcessKillingVeto.all()) {
VetoCause cause = vetoExtension.vetoProcessKilling(this);
if (cause != null) {
if (LOGGER.isLoggable(FINEST))
LOGGER.finest("Killing of pid " + getPid() + " vetoed by " + vetoExtension.getClass().getName() + ": " + cause.getMessage());
return cause;
}
}
return null;
}
/**
* Gets the command-line arguments of this process.
*
......@@ -363,6 +381,8 @@ public abstract class ProcessTree implements Iterable<OSProcess>, IProcessTree,
}
public void kill() throws InterruptedException {
if (getVeto() != null)
return;
proc.destroy();
killByKiller();
}
......@@ -398,12 +418,18 @@ public abstract class ProcessTree implements Iterable<OSProcess>, IProcessTree,
}
public void killRecursively() throws InterruptedException {
if (getVeto() != null)
return;
LOGGER.finer("Killing recursively "+getPid());
p.killRecursively();
killByKiller();
}
public void kill() throws InterruptedException {
if (getVeto() != null)
return;
LOGGER.finer("Killing "+getPid());
p.kill();
killByKiller();
......@@ -537,6 +563,8 @@ public abstract class ProcessTree implements Iterable<OSProcess>, IProcessTree,
* Tries to kill this process.
*/
public void kill() throws InterruptedException {
if (getVeto() != null)
return;
try {
int pid = getPid();
LOGGER.fine("Killing pid="+pid);
......@@ -557,6 +585,7 @@ public abstract class ProcessTree implements Iterable<OSProcess>, IProcessTree,
}
public void killRecursively() throws InterruptedException {
// We kill individual processes of a tree, so handling vetoes inside #kill() is enough for UnixProcess es
LOGGER.fine("Recursively killing pid="+getPid());
for (OSProcess p : getChildren())
p.killRecursively();
......
package hudson.util;
import static org.junit.Assert.*;
import static org.junit.Assume.*;
import java.io.File;
import hudson.Functions;
import hudson.model.FreeStyleBuild;
import hudson.model.FreeStyleProject;
import hudson.tasks.Maven;
import hudson.tasks.Shell;
import hudson.util.ProcessTreeRemoting.IOSProcess;
import org.junit.After;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.ExtractResourceSCM;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.TestExtension;
import com.google.common.collect.ImmutableMap;
public class ProcessTreeKillerTest {
@Rule
public JenkinsRule j = new JenkinsRule();
private Process process;
@After
public void tearDown() throws Exception {
if (null != process)
process.destroy();
}
@Test
public void manualAbortProcess() throws Exception {
ProcessTree.enabled = true;
......@@ -55,4 +69,41 @@ public class ProcessTreeKillerTest {
j.assertLogNotContains("sleep 100000", processJob.scheduleBuild2(0).get());
}
@Test
@Issue("JENKINS-9104")
public void considersKillingVetos() throws Exception {
// on some platforms where we fail to list any processes, this test will
// just not work
assumeTrue(ProcessTree.get() != ProcessTree.DEFAULT);
// kick off a process we (shouldn't) kill
ProcessBuilder pb = new ProcessBuilder();
pb.environment().put("cookie", "testKeepDaemonsAlive");
if (File.pathSeparatorChar == ';') {
pb.command("cmd");
} else {
pb.command("sleep", "5m");
}
process = pb.start();
ProcessTree processTree = ProcessTree.get();
processTree.killAll(ImmutableMap.of("cookie", "testKeepDaemonsAlive"));
try {
process.exitValue();
fail("Process should have been excluded from the killing");
} catch (IllegalThreadStateException e) {
// Means the process is still running
}
}
@TestExtension("considersKillingVetos")
public static class VetoAllKilling extends ProcessKillingVeto {
@Override
public VetoCause vetoProcessKilling(IOSProcess p) {
return new VetoCause("Peace on earth");
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册