You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Daemon.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. /*
  2. * Copyright (C) 2008-2009, Google Inc.
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.transport;
  44. import java.io.IOException;
  45. import java.io.InputStream;
  46. import java.io.InterruptedIOException;
  47. import java.io.OutputStream;
  48. import java.net.InetAddress;
  49. import java.net.InetSocketAddress;
  50. import java.net.ServerSocket;
  51. import java.net.Socket;
  52. import java.net.SocketAddress;
  53. import org.eclipse.jgit.errors.RepositoryNotFoundException;
  54. import org.eclipse.jgit.internal.JGitText;
  55. import org.eclipse.jgit.lib.PersonIdent;
  56. import org.eclipse.jgit.lib.Repository;
  57. import org.eclipse.jgit.storage.pack.PackConfig;
  58. import org.eclipse.jgit.transport.resolver.ReceivePackFactory;
  59. import org.eclipse.jgit.transport.resolver.RepositoryResolver;
  60. import org.eclipse.jgit.transport.resolver.ServiceNotAuthorizedException;
  61. import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException;
  62. import org.eclipse.jgit.transport.resolver.UploadPackFactory;
  63. /** Basic daemon for the anonymous <code>git://</code> transport protocol. */
  64. public class Daemon {
  65. /** 9418: IANA assigned port number for Git. */
  66. public static final int DEFAULT_PORT = 9418;
  67. private static final int BACKLOG = 5;
  68. private InetSocketAddress myAddress;
  69. private final DaemonService[] services;
  70. private final ThreadGroup processors;
  71. private boolean run;
  72. private Thread acceptThread;
  73. private int timeout;
  74. private PackConfig packConfig;
  75. private volatile RepositoryResolver<DaemonClient> repositoryResolver;
  76. private volatile UploadPackFactory<DaemonClient> uploadPackFactory;
  77. private volatile ReceivePackFactory<DaemonClient> receivePackFactory;
  78. /** Configure a daemon to listen on any available network port. */
  79. public Daemon() {
  80. this(null);
  81. }
  82. /**
  83. * Configure a new daemon for the specified network address.
  84. *
  85. * @param addr
  86. * address to listen for connections on. If null, any available
  87. * port will be chosen on all network interfaces.
  88. */
  89. @SuppressWarnings("unchecked")
  90. public Daemon(final InetSocketAddress addr) {
  91. myAddress = addr;
  92. processors = new ThreadGroup("Git-Daemon"); //$NON-NLS-1$
  93. repositoryResolver = (RepositoryResolver<DaemonClient>) RepositoryResolver.NONE;
  94. uploadPackFactory = new UploadPackFactory<DaemonClient>() {
  95. public UploadPack create(DaemonClient req, Repository db)
  96. throws ServiceNotEnabledException,
  97. ServiceNotAuthorizedException {
  98. UploadPack up = new UploadPack(db);
  99. up.setTimeout(getTimeout());
  100. up.setPackConfig(getPackConfig());
  101. return up;
  102. }
  103. };
  104. receivePackFactory = new ReceivePackFactory<DaemonClient>() {
  105. public ReceivePack create(DaemonClient req, Repository db)
  106. throws ServiceNotEnabledException,
  107. ServiceNotAuthorizedException {
  108. ReceivePack rp = new ReceivePack(db);
  109. InetAddress peer = req.getRemoteAddress();
  110. String host = peer.getCanonicalHostName();
  111. if (host == null)
  112. host = peer.getHostAddress();
  113. String name = "anonymous"; //$NON-NLS-1$
  114. String email = name + "@" + host; //$NON-NLS-1$
  115. rp.setRefLogIdent(new PersonIdent(name, email));
  116. rp.setTimeout(getTimeout());
  117. return rp;
  118. }
  119. };
  120. services = new DaemonService[] {
  121. new DaemonService("upload-pack", "uploadpack") { //$NON-NLS-1$ //$NON-NLS-2$
  122. {
  123. setEnabled(true);
  124. }
  125. @Override
  126. protected void execute(final DaemonClient dc,
  127. final Repository db) throws IOException,
  128. ServiceNotEnabledException,
  129. ServiceNotAuthorizedException {
  130. UploadPack up = uploadPackFactory.create(dc, db);
  131. InputStream in = dc.getInputStream();
  132. OutputStream out = dc.getOutputStream();
  133. up.upload(in, out, null);
  134. }
  135. }, new DaemonService("receive-pack", "receivepack") { //$NON-NLS-1$ //$NON-NLS-2$
  136. {
  137. setEnabled(false);
  138. }
  139. @Override
  140. protected void execute(final DaemonClient dc,
  141. final Repository db) throws IOException,
  142. ServiceNotEnabledException,
  143. ServiceNotAuthorizedException {
  144. ReceivePack rp = receivePackFactory.create(dc, db);
  145. InputStream in = dc.getInputStream();
  146. OutputStream out = dc.getOutputStream();
  147. rp.receive(in, out, null);
  148. }
  149. } };
  150. }
  151. /** @return the address connections are received on. */
  152. public synchronized InetSocketAddress getAddress() {
  153. return myAddress;
  154. }
  155. /**
  156. * Lookup a supported service so it can be reconfigured.
  157. *
  158. * @param name
  159. * name of the service; e.g. "receive-pack"/"git-receive-pack" or
  160. * "upload-pack"/"git-upload-pack".
  161. * @return the service; null if this daemon implementation doesn't support
  162. * the requested service type.
  163. */
  164. public synchronized DaemonService getService(String name) {
  165. if (!name.startsWith("git-")) //$NON-NLS-1$
  166. name = "git-" + name; //$NON-NLS-1$
  167. for (final DaemonService s : services) {
  168. if (s.getCommandName().equals(name))
  169. return s;
  170. }
  171. return null;
  172. }
  173. /** @return timeout (in seconds) before aborting an IO operation. */
  174. public int getTimeout() {
  175. return timeout;
  176. }
  177. /**
  178. * Set the timeout before willing to abort an IO call.
  179. *
  180. * @param seconds
  181. * number of seconds to wait (with no data transfer occurring)
  182. * before aborting an IO read or write operation with the
  183. * connected client.
  184. */
  185. public void setTimeout(final int seconds) {
  186. timeout = seconds;
  187. }
  188. /** @return configuration controlling packing, may be null. */
  189. public PackConfig getPackConfig() {
  190. return packConfig;
  191. }
  192. /**
  193. * Set the configuration used by the pack generator.
  194. *
  195. * @param pc
  196. * configuration controlling packing parameters. If null the
  197. * source repository's settings will be used.
  198. */
  199. public void setPackConfig(PackConfig pc) {
  200. this.packConfig = pc;
  201. }
  202. /**
  203. * Set the resolver used to locate a repository by name.
  204. *
  205. * @param resolver
  206. * the resolver instance.
  207. */
  208. public void setRepositoryResolver(RepositoryResolver<DaemonClient> resolver) {
  209. repositoryResolver = resolver;
  210. }
  211. /**
  212. * Set the factory to construct and configure per-request UploadPack.
  213. *
  214. * @param factory
  215. * the factory. If null upload-pack is disabled.
  216. */
  217. @SuppressWarnings("unchecked")
  218. public void setUploadPackFactory(UploadPackFactory<DaemonClient> factory) {
  219. if (factory != null)
  220. uploadPackFactory = factory;
  221. else
  222. uploadPackFactory = (UploadPackFactory<DaemonClient>) UploadPackFactory.DISABLED;
  223. }
  224. /**
  225. * Set the factory to construct and configure per-request ReceivePack.
  226. *
  227. * @param factory
  228. * the factory. If null receive-pack is disabled.
  229. */
  230. @SuppressWarnings("unchecked")
  231. public void setReceivePackFactory(ReceivePackFactory<DaemonClient> factory) {
  232. if (factory != null)
  233. receivePackFactory = factory;
  234. else
  235. receivePackFactory = (ReceivePackFactory<DaemonClient>) ReceivePackFactory.DISABLED;
  236. }
  237. /**
  238. * Start this daemon on a background thread.
  239. *
  240. * @throws IOException
  241. * the server socket could not be opened.
  242. * @throws IllegalStateException
  243. * the daemon is already running.
  244. */
  245. public synchronized void start() throws IOException {
  246. if (acceptThread != null)
  247. throw new IllegalStateException(JGitText.get().daemonAlreadyRunning);
  248. final ServerSocket listenSock = new ServerSocket(
  249. myAddress != null ? myAddress.getPort() : 0, BACKLOG,
  250. myAddress != null ? myAddress.getAddress() : null);
  251. myAddress = (InetSocketAddress) listenSock.getLocalSocketAddress();
  252. run = true;
  253. acceptThread = new Thread(processors, "Git-Daemon-Accept") { //$NON-NLS-1$
  254. public void run() {
  255. while (isRunning()) {
  256. try {
  257. startClient(listenSock.accept());
  258. } catch (InterruptedIOException e) {
  259. // Test again to see if we should keep accepting.
  260. } catch (IOException e) {
  261. break;
  262. }
  263. }
  264. try {
  265. listenSock.close();
  266. } catch (IOException err) {
  267. //
  268. } finally {
  269. synchronized (Daemon.this) {
  270. acceptThread = null;
  271. }
  272. }
  273. }
  274. };
  275. acceptThread.start();
  276. }
  277. /** @return true if this daemon is receiving connections. */
  278. public synchronized boolean isRunning() {
  279. return run;
  280. }
  281. /** Stop this daemon. */
  282. public synchronized void stop() {
  283. if (acceptThread != null) {
  284. run = false;
  285. acceptThread.interrupt();
  286. }
  287. }
  288. private void startClient(final Socket s) {
  289. final DaemonClient dc = new DaemonClient(this);
  290. final SocketAddress peer = s.getRemoteSocketAddress();
  291. if (peer instanceof InetSocketAddress)
  292. dc.setRemoteAddress(((InetSocketAddress) peer).getAddress());
  293. new Thread(processors, "Git-Daemon-Client " + peer.toString()) { //$NON-NLS-1$
  294. public void run() {
  295. try {
  296. dc.execute(s);
  297. } catch (ServiceNotEnabledException e) {
  298. // Ignored. Client cannot use this repository.
  299. } catch (ServiceNotAuthorizedException e) {
  300. // Ignored. Client cannot use this repository.
  301. } catch (IOException e) {
  302. // Ignore unexpected IO exceptions from clients
  303. } finally {
  304. try {
  305. s.getInputStream().close();
  306. } catch (IOException e) {
  307. // Ignore close exceptions
  308. }
  309. try {
  310. s.getOutputStream().close();
  311. } catch (IOException e) {
  312. // Ignore close exceptions
  313. }
  314. }
  315. }
  316. }.start();
  317. }
  318. synchronized DaemonService matchService(final String cmd) {
  319. for (final DaemonService d : services) {
  320. if (d.handles(cmd))
  321. return d;
  322. }
  323. return null;
  324. }
  325. Repository openRepository(DaemonClient client, String name)
  326. throws ServiceMayNotContinueException {
  327. // Assume any attempt to use \ was by a Windows client
  328. // and correct to the more typical / used in Git URIs.
  329. //
  330. name = name.replace('\\', '/');
  331. // git://thishost/path should always be name="/path" here
  332. //
  333. if (!name.startsWith("/")) //$NON-NLS-1$
  334. return null;
  335. try {
  336. return repositoryResolver.open(client, name.substring(1));
  337. } catch (RepositoryNotFoundException e) {
  338. // null signals it "wasn't found", which is all that is suitable
  339. // for the remote client to know.
  340. return null;
  341. } catch (ServiceNotAuthorizedException e) {
  342. // null signals it "wasn't found", which is all that is suitable
  343. // for the remote client to know.
  344. return null;
  345. } catch (ServiceNotEnabledException e) {
  346. // null signals it "wasn't found", which is all that is suitable
  347. // for the remote client to know.
  348. return null;
  349. }
  350. }
  351. }