Skip to content

Commit

Permalink
Added a hook to rewire the writing part of the channel
Browse files Browse the repository at this point in the history
if it's connected to stdout.

This allows the channel to be more robust and free from JVM interference
that writes something to stdout (such as SIGQUIT thread dump.)
  • Loading branch information
kohsuke committed Aug 26, 2011
1 parent 5a0b01d commit fad8c38
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 2 deletions.
22 changes: 22 additions & 0 deletions src/main/java/hudson/remoting/Channel.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@
public class Channel implements VirtualChannel, IChannel {
private final ObjectInputStream ois;
private final ObjectOutputStream oos;
/**
* {@link OutputStream} that's given to the constructor. This is the hand-off with the lower layer.
*/
private final OutputStream underlyingOutput;

/**
* Human readable description of where this channel is connected to. Used during diagnostic output
* and error reports.
Expand Down Expand Up @@ -358,6 +363,7 @@ public Channel(String name, ExecutorService exec, Mode mode, InputStream is, Out
this.name = name;
this.executor = exec;
this.isRestricted = restricted;
this.underlyingOutput = os;

if (base==null)
base = getClass().getClassLoader();
Expand Down Expand Up @@ -918,6 +924,22 @@ public Object waitForRemoteProperty(Object key) throws InterruptedException {
return remoteChannel.waitForProperty(key);
}

/**
* Obtain the output stream passed to the constructor.
*
* @deprecated
* Future version of the remoting module may add other modes of creating channel
* that doesn't involve stream pair. Therefore, we aren't committing to this method.
* This method isn't a part of the committed API of the channel class.
* @return
* While the current version always return a non-null value, the caller must not
* make that assumption for the above reason. This method may return null in the future version
* to indicate that the {@link Channel} is not sitting on top of a stream pair.
*/
public OutputStream getUnderlyingOutput() {
return underlyingOutput;
}

/**
* Starts a local to remote port forwarding (the equivalent of "ssh -L").
*
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/hudson/remoting/Launcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -361,9 +361,9 @@ private void runWithStdinStdout() throws IOException, InterruptedException {

// this will prevent programs from accidentally writing to System.out
// and messing up the stream.
OutputStream os = System.out;
OutputStream os = new StandardOutputStream();
System.setOut(System.err);
main(System.in,os, mode,ping);
main(System.in, os, mode, ping);
}

private static void ttyCheck() {
Expand Down
96 changes: 96 additions & 0 deletions src/main/java/hudson/remoting/StandardOutputStream.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* The MIT License
*
* Copyright (c) 2011, CloudBees, Inc.
*
* 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.remoting;

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;

/**
* Hint that indicates that we are using {@code stdout} with fd=1 as the stream to write to for the channel.
*
* <p>
* Using stdin/stdout pair for the communication is very convenient for setting up a remote channel,
* so Jenkins tends to do that, but even with our using {@link System#setOut(PrintStream)} to avoid
* other parts of the Java code to accidentally corrupt the stream, file descriptor 1 continues to
* point to the stream we use, so native code in JVM can still accidentally pollute the stream.
*
* <p>
* Fixing that requires the use of dup and close POSIX API calls, which we can only do after the communication
* gets established and JNA is brought in via remoting. Having {@link Launcher} uses this wrapper class allows
* us to do that without recreating the stream.
*
* @author Kohsuke Kawaguchi
*/
public class StandardOutputStream extends OutputStream {
private OutputStream out;
private boolean swapped;

public StandardOutputStream() {
this.out = System.out;
}

/**
* Atomically swaps the underlying stream to another stream and returns it.
*/
public synchronized OutputStream swap(OutputStream another) {
// if the stream is already swapped, the caller's implicit assumption that this represents stdout
// is wrong, so we raise an error.
if (swapped)
throw new IllegalStateException();
OutputStream old = out;
out = another;
swapped = true;
return old;
}

public synchronized boolean isSwapped() {
return swapped;
}

@Override
public synchronized void write(int b) throws IOException {
out.write(b);
}

@Override
public synchronized void write(byte[] b) throws IOException {
out.write(b);
}

@Override
public synchronized void write(byte[] b, int off, int len) throws IOException {
out.write(b, off, len);
}

@Override
public synchronized void flush() throws IOException {
out.flush();
}

@Override
public synchronized void close() throws IOException {
out.close();
}
}

0 comments on commit fad8c38

Please sign in to comment.