[JGIT PATCH 5/6] Add timeouts to anonymous git:// daemon

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



If the initial command line isn't received within the configured
timeout period, the connection is dropped, and the service thread
is able to terminate.

The timeout is also pushed down to the service implementations,
so they can abort if the client doesn't speak to them within the
configured time span.

Signed-off-by: Shawn O. Pearce <spearce@xxxxxxxxxxx>
---
 .../src/org/spearce/jgit/transport/Daemon.java     |   26 ++++++++++++++++---
 .../org/spearce/jgit/transport/DaemonClient.java   |   12 +++++++--
 2 files changed, 31 insertions(+), 7 deletions(-)

diff --git a/org.spearce.jgit/src/org/spearce/jgit/transport/Daemon.java b/org.spearce.jgit/src/org/spearce/jgit/transport/Daemon.java
index be27732..3101d6f 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/transport/Daemon.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/transport/Daemon.java
@@ -37,8 +37,6 @@
 
 package org.spearce.jgit.transport;
 
-import java.io.BufferedInputStream;
-import java.io.BufferedOutputStream;
 import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
@@ -79,6 +77,8 @@
 
 	private Thread acceptThread;
 
+	private int timeout;
+
 	/** Configure a daemon to listen on any available network port. */
 	public Daemon() {
 		this(null);
@@ -108,6 +108,7 @@ protected void execute(final DaemonClient dc,
 							final Repository db) throws IOException {
 						final UploadPack rp = new UploadPack(db);
 						final InputStream in = dc.getInputStream();
+						rp.setTimeout(Daemon.this.getTimeout());
 						rp.upload(in, dc.getOutputStream(), null);
 					}
 				}, new DaemonService("receive-pack", "receivepack") {
@@ -127,6 +128,7 @@ protected void execute(final DaemonClient dc,
 						final String name = "anonymous";
 						final String email = name + "@" + host;
 						rp.setRefLogIdent(new PersonIdent(name, email));
+						rp.setTimeout(Daemon.this.getTimeout());
 						rp.receive(in, dc.getOutputStream(), null);
 					}
 				} };
@@ -213,6 +215,23 @@ synchronized (exportBase) {
 		}
 	}
 
+	/** @return timeout (in seconds) before aborting an IO operation. */
+	public int getTimeout() {
+		return timeout;
+	}
+
+	/**
+	 * Set the timeout before willing to abort an IO call.
+	 *
+	 * @param seconds
+	 *            number of seconds to wait (with no data transfer occurring)
+	 *            before aborting an IO read or write operation with the
+	 *            connected client.
+	 */
+	public void setTimeout(final int seconds) {
+		timeout = seconds;
+	}
+
 	/**
 	 * Start this daemon on a background thread.
 	 *
@@ -280,8 +299,7 @@ private void startClient(final Socket s) {
 		new Thread(processors, "Git-Daemon-Client " + peer.toString()) {
 			public void run() {
 				try {
-					dc.execute(new BufferedInputStream(s.getInputStream()),
-							new BufferedOutputStream(s.getOutputStream()));
+					dc.execute(s);
 				} catch (IOException e) {
 					// Ignore unexpected IO exceptions from clients
 					e.printStackTrace();
diff --git a/org.spearce.jgit/src/org/spearce/jgit/transport/DaemonClient.java b/org.spearce.jgit/src/org/spearce/jgit/transport/DaemonClient.java
index e80d86b..52b2ae0 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/transport/DaemonClient.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/transport/DaemonClient.java
@@ -37,10 +37,13 @@
 
 package org.spearce.jgit.transport;
 
+import java.io.BufferedInputStream;
+import java.io.BufferedOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.net.InetAddress;
+import java.net.Socket;
 
 /** Active network client of {@link Daemon}. */
 public class DaemonClient {
@@ -80,11 +83,13 @@ public OutputStream getOutputStream() {
 		return rawOut;
 	}
 
-	void execute(final InputStream in, final OutputStream out)
+	void execute(final Socket sock)
 			throws IOException {
-		rawIn = in;
-		rawOut = out;
+		rawIn = new BufferedInputStream(sock.getInputStream());
+		rawOut = new BufferedOutputStream(sock.getOutputStream());
 
+		if (0 < daemon.getTimeout())
+			sock.setSoTimeout(daemon.getTimeout() * 1000);
 		String cmd = new PacketLineIn(rawIn).readStringRaw();
 		final int nul = cmd.indexOf('\0');
 		if (nul >= 0) {
@@ -98,6 +103,7 @@ void execute(final InputStream in, final OutputStream out)
 		final DaemonService srv = getDaemon().matchService(cmd);
 		if (srv == null)
 			return;
+		sock.setSoTimeout(0);
 		srv.execute(this, cmd);
 	}
 }
-- 
1.6.3.2.416.g04d0

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]