Skip to content

Commit

Permalink
[JENKINS-9104] Add ProcessKillingVeto extension point
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
DanielWeber committed Jun 14, 2015
1 parent 7e1a921 commit a220431
Show file tree
Hide file tree
Showing 3 changed files with 173 additions and 1 deletion.
92 changes: 92 additions & 0 deletions core/src/main/java/hudson/util/ProcessKillingVeto.java
@@ -0,0 +1,92 @@
/*
* 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);
}
29 changes: 29 additions & 0 deletions core/src/main/java/hudson/util/ProcessTree.java
Expand Up @@ -32,6 +32,7 @@
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;
Expand All @@ -56,6 +57,7 @@
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;
Expand Down Expand Up @@ -227,6 +229,22 @@ void killByKiller() throws InterruptedException {
*/
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.
*
Expand Down Expand Up @@ -363,6 +381,8 @@ public void killRecursively() {
}

public void kill() throws InterruptedException {
if (getVeto() != null)
return;
proc.destroy();
killByKiller();
}
Expand Down Expand Up @@ -398,12 +418,18 @@ public OSProcess getParent() {
}

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();
Expand Down Expand Up @@ -537,6 +563,8 @@ protected final File getFile(String relativePath) {
* Tries to kill this process.
*/
public void kill() throws InterruptedException {
if (getVeto() != null)
return;
try {
int pid = getPid();
LOGGER.fine("Killing pid="+pid);
Expand All @@ -557,6 +585,7 @@ public void kill() throws InterruptedException {
}

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();
Expand Down
53 changes: 52 additions & 1 deletion test/src/test/java/hudson/util/ProcessTreeKillerTest.java
@@ -1,21 +1,35 @@
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;
Expand Down Expand Up @@ -55,4 +69,41 @@ public void processProperlyKilledUnix() throws Exception {

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");
}
}
}

0 comments on commit a220431

Please sign in to comment.