Skip to content
Browse files

[JENKINS-8856] evacuate stdout at OS level to avoid interference with

  • Loading branch information
kohsuke committed Aug 26, 2011
1 parent 9c9379e commit 3f4d01ec15ee40f94e861e9e70403ac6b16c1ed3
@@ -67,6 +67,9 @@
<li class=bug>
Jenkins didn't start on IBM JDK.
(<a href="">issue 10810</a>)
<li class=rfe>
stdin/stdout based remote slaves, such as ones launched via SSH or script, now does a better redirect to avoid interference with JVM output to stdout.
(<a href="">issue 8856</a>)

@@ -72,6 +72,10 @@
int chown(String fileName, int uid, int gid);
int chmod(String fileName, int i);

int dup(int old);
int dup2(int old, int _new);
int close(int fd);

// see
int rename(String oldname, String newname);

@@ -0,0 +1,67 @@
package jenkins.slaves;

import hudson.Extension;
import hudson.FilePath;
import hudson.model.Computer;
import hudson.model.TaskListener;
import hudson.remoting.Callable;
import hudson.remoting.Channel;
import hudson.remoting.StandardOutputStream;
import hudson.slaves.ComputerListener;
import hudson.util.jna.GNUCLibrary;

import java.lang.reflect.Constructor;
import java.util.logging.Logger;

* @author Kohsuke Kawaguchi
public class StandardOutputSwapper extends ComputerListener {
public void preOnline(Computer c, Channel channel, FilePath root, TaskListener listener) {
if (disabled) return;

try {
if ( ChannelSwapper()))
listener.getLogger().println("Evacuated stdout");
} catch (Throwable e) {
LOGGER.fine("Fatal problem swapping file descriptors " + c.getName());

private static final class ChannelSwapper implements Callable<Boolean,Exception> {
public Boolean call() throws Exception {
if (File.pathSeparatorChar==';') return false; // Windows

OutputStream o = Channel.current().getUnderlyingOutput();
if (o instanceof StandardOutputStream) {
StandardOutputStream stdout = (StandardOutputStream)o;

// duplicate the OS file descriptor and create FileOutputStream around it
int out = GNUCLibrary.LIBC.dup(1);
if (out<0) throw new IOException("Failed to dup(1)");
Constructor<FileDescriptor> c = FileDescriptor.class.getDeclaredConstructor(int.class);
FileOutputStream fos = new FileOutputStream(c.newInstance(out));

// swap it into channel so that it'll use the new file descriptor

// close fd=1 (stdout) and duplicate fd=2 (stderr) into fd=1 (stdout)
return true;
return false;

private static final Logger LOGGER = Logger.getLogger(StandardOutputSwapper.class.getName());
public static boolean disabled = Boolean.getBoolean(StandardOutputSwapper.class.getName()+".disabled");
@@ -133,7 +133,7 @@ THE SOFTWARE.

0 comments on commit 3f4d01e

Please sign in to comment.