001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.eclipse.aether.named.ipc; 020 021import java.io.Closeable; 022import java.io.DataInputStream; 023import java.io.DataOutputStream; 024import java.io.EOFException; 025import java.io.File; 026import java.io.FileWriter; 027import java.io.IOException; 028import java.io.InterruptedIOException; 029import java.io.OutputStream; 030import java.io.PrintWriter; 031import java.io.RandomAccessFile; 032import java.net.SocketAddress; 033import java.net.URL; 034import java.nio.channels.ByteChannel; 035import java.nio.channels.Channels; 036import java.nio.channels.FileLock; 037import java.nio.channels.ServerSocketChannel; 038import java.nio.channels.SocketChannel; 039import java.nio.charset.StandardCharsets; 040import java.nio.file.Files; 041import java.nio.file.Path; 042import java.nio.file.Paths; 043import java.security.SecureRandom; 044import java.util.ArrayList; 045import java.util.Arrays; 046import java.util.Collection; 047import java.util.List; 048import java.util.Locale; 049import java.util.Map; 050import java.util.Objects; 051import java.util.concurrent.CompletableFuture; 052import java.util.concurrent.ConcurrentHashMap; 053import java.util.concurrent.ExecutionException; 054import java.util.concurrent.ExecutorService; 055import java.util.concurrent.Executors; 056import java.util.concurrent.Future; 057import java.util.concurrent.TimeUnit; 058import java.util.concurrent.TimeoutException; 059import java.util.concurrent.atomic.AtomicInteger; 060 061import static org.eclipse.aether.named.ipc.IpcMessages.REQUEST_ACQUIRE; 062import static org.eclipse.aether.named.ipc.IpcMessages.REQUEST_CLOSE; 063import static org.eclipse.aether.named.ipc.IpcMessages.REQUEST_CONTEXT; 064import static org.eclipse.aether.named.ipc.IpcMessages.REQUEST_STOP; 065import static org.eclipse.aether.named.ipc.IpcMessages.RESPONSE_ACQUIRE; 066import static org.eclipse.aether.named.ipc.IpcMessages.RESPONSE_CLOSE; 067import static org.eclipse.aether.named.ipc.IpcMessages.RESPONSE_CONTEXT; 068import static org.eclipse.aether.named.ipc.IpcMessages.RESPONSE_STOP; 069 070/** 071 * Client side implementation. 072 * The client instance is bound to a given maven repository. 073 * 074 * @since 2.0.1 075 */ 076public class IpcClient { 077 078 static final boolean IS_WINDOWS = 079 System.getProperty("os.name").toLowerCase(Locale.ENGLISH).contains("win"); 080 081 private static final SecureRandom SECURE_RANDOM = new SecureRandom(); 082 083 protected volatile boolean initialized; 084 085 /** 086 * Set by {@link #close()}: the owning factory shut down, so no new connection is made (and no new server is 087 * spawned) on behalf of a lock that is still held. 088 */ 089 protected volatile boolean closed; 090 091 protected final Path lockPath; 092 protected final Path logPath; 093 protected final Path syncPath; 094 protected final boolean noFork; 095 096 protected volatile SocketChannel socket; 097 protected volatile DataOutputStream output; 098 protected volatile DataInputStream input; 099 protected volatile Thread receiver; 100 protected volatile Process process; 101 102 protected final AtomicInteger requestId = new AtomicInteger(); 103 protected final Map<Integer, CompletableFuture<List<String>>> responses = new ConcurrentHashMap<>(); 104 105 /** 106 * The bootstrap token shared with the server this client spawned (if it spawned one): the only credential 107 * accepted by the server for a stop request. Remains {@code null} when this client attached to an already 108 * running server, which consequently cannot be stopped from here. 109 */ 110 protected volatile String bootstrapToken; 111 112 IpcClient(Path lockPath, Path logPath, Path syncPath) { 113 this.lockPath = lockPath; 114 this.logPath = logPath; 115 this.syncPath = syncPath; 116 this.noFork = Boolean.parseBoolean( 117 System.getProperty(IpcServer.SYSTEM_PROP_NO_FORK, Boolean.toString(IpcServer.DEFAULT_NO_FORK))); 118 } 119 120 void ensureInitialized() throws IOException { 121 if (!initialized) { 122 // caller must block on this method 123 synchronized (this) { 124 if (closed) { 125 throw new IOException("IPC client is closed"); 126 } 127 if (!initialized) { 128 socket = createClient(); 129 ByteChannel wrapper = new ByteChannelWrapper(socket); 130 input = new DataInputStream(Channels.newInputStream(wrapper)); 131 output = new DataOutputStream(Channels.newOutputStream(wrapper)); 132 receiver = new Thread(this::receive); 133 receiver.setDaemon(true); 134 receiver.start(); 135 initialized = true; 136 } 137 } 138 } 139 } 140 141 private synchronized DataInputStream getInput() { 142 return input; 143 } 144 145 private synchronized DataOutputStream getOutput() { 146 return output; 147 } 148 149 SocketChannel createClient() throws IOException { 150 SocketFamily family = 151 SocketFamily.valueOf(System.getProperty(IpcServer.SYSTEM_PROP_FAMILY, IpcServer.DEFAULT_FAMILY)); 152 153 Path lockPath = this.lockPath.toAbsolutePath().normalize(); 154 Path lockFile = 155 lockPath.resolve(".maven-resolver-ipc-lock-" + family.name().toLowerCase(Locale.ENGLISH)); 156 if (!Files.isRegularFile(lockFile)) { 157 if (!Files.isDirectory(lockFile.getParent())) { 158 Files.createDirectories(lockFile.getParent()); 159 } 160 } 161 162 try (RandomAccessFile raf = new RandomAccessFile(lockFile.toFile(), "rw")) { 163 try (FileLock lock = raf.getChannel().lock()) { 164 String line = raf.readLine(); 165 if (line != null) { 166 try { 167 SocketAddress address = SocketFamily.fromString(line); 168 return SocketChannel.open(address); 169 } catch (IOException e) { 170 // ignore 171 } 172 } 173 174 ServerSocketChannel ss = family.openServerSocket(); 175 String tmpaddr = SocketFamily.toString(ss.getLocalAddress()); 176 // the token authorizes stopping the daemon: it must not be guessable by other local users 177 String rand = Long.toHexString(SECURE_RANDOM.nextLong()) + Long.toHexString(SECURE_RANDOM.nextLong()); 178 String nativeName = 179 System.getProperty(IpcServer.SYSTEM_PROP_NATIVE_NAME, IpcServer.DEFAULT_NATIVE_NAME); 180 String syncCmd = IS_WINDOWS ? nativeName + ".exe" : nativeName; 181 182 boolean debug = Boolean.parseBoolean( 183 System.getProperty(IpcServer.SYSTEM_PROP_DEBUG, Boolean.toString(IpcServer.DEFAULT_DEBUG))); 184 boolean noNative = Boolean.parseBoolean(System.getProperty( 185 IpcServer.SYSTEM_PROP_NO_NATIVE, Boolean.toString(IpcServer.DEFAULT_NO_NATIVE))); 186 if (!noNative) { 187 noNative = !Files.isExecutable(syncPath.resolve(syncCmd)); 188 } 189 Closeable close; 190 Path logFile = logPath.resolve("resolver-ipcsync-" + rand + ".log"); 191 List<String> args = new ArrayList<>(); 192 if (noNative) { 193 if (noFork) { 194 IpcServer server = IpcServer.runServer(family, tmpaddr, rand); 195 close = server::close; 196 } else { 197 String javaHome = System.getenv("JAVA_HOME"); 198 if (javaHome == null) { 199 javaHome = System.getProperty("java.home"); 200 } 201 String javaCmd = IS_WINDOWS ? "bin\\java.exe" : "bin/java"; 202 String java = Paths.get(javaHome) 203 .resolve(javaCmd) 204 .toAbsolutePath() 205 .toString(); 206 args.add(java); 207 String classpath = getJarPath(getClass()) + File.pathSeparator + getJarPath(IpcServer.class); 208 args.add("-cp"); 209 args.add(classpath); 210 String timeout = System.getProperty(IpcServer.SYSTEM_PROP_IDLE_TIMEOUT); 211 if (timeout != null) { 212 args.add("-D" + IpcServer.SYSTEM_PROP_IDLE_TIMEOUT + "=" + timeout); 213 } 214 args.add("-D" + IpcServer.SYSTEM_PROP_DEBUG + "=" + debug); 215 args.add(IpcServer.class.getName()); 216 args.add(family.name()); 217 args.add(tmpaddr); 218 // the bootstrap token is passed via stdin ("-" placeholder in argv): process arguments 219 // are commonly visible to other local users (e.g. /proc/<pid>/cmdline) 220 args.add("-"); 221 ProcessBuilder processBuilder = new ProcessBuilder(); 222 ProcessBuilder.Redirect discard = ProcessBuilder.Redirect.to(logFile.toFile()); 223 Files.createDirectories(logPath); 224 this.process = processBuilder 225 .directory(lockFile.getParent().toFile()) 226 .command(args) 227 .redirectOutput(discard) 228 .redirectError(discard) 229 .start(); 230 writeBootstrapToken(this.process, rand); 231 close = this.process::destroyForcibly; 232 } 233 } else { 234 args.add(syncPath.resolve(syncCmd).toString()); 235 String timeout = System.getProperty(IpcServer.SYSTEM_PROP_IDLE_TIMEOUT); 236 if (timeout != null) { 237 args.add("-D" + IpcServer.SYSTEM_PROP_IDLE_TIMEOUT + "=" + timeout); 238 } 239 args.add("-D" + IpcServer.SYSTEM_PROP_DEBUG + "=" + debug); 240 args.add(family.name()); 241 args.add(tmpaddr); 242 // see above: the bootstrap token goes via stdin, not argv 243 args.add("-"); 244 ProcessBuilder processBuilder = new ProcessBuilder(); 245 ProcessBuilder.Redirect discard = ProcessBuilder.Redirect.to(logFile.toFile()); 246 Files.createDirectories(logPath); 247 this.process = processBuilder 248 .directory(lockFile.getParent().toFile()) 249 .command(args) 250 .redirectOutput(discard) 251 .redirectError(discard) 252 .start(); 253 writeBootstrapToken(this.process, rand); 254 close = this.process::destroyForcibly; 255 } 256 257 ExecutorService es = Executors.newSingleThreadExecutor(); 258 Future<String[]> future = es.submit(() -> { 259 SocketChannel s = ss.accept(); 260 DataInputStream dis = new DataInputStream(Channels.newInputStream(s)); 261 String rand2 = dis.readUTF(); 262 String addr2 = dis.readUTF(); 263 return new String[] {rand2, addr2}; 264 }); 265 String[] res; 266 try { 267 res = future.get(5, TimeUnit.SECONDS); 268 } catch (Exception e) { 269 try (PrintWriter writer = new PrintWriter(new FileWriter(logFile.toFile(), true))) { 270 writer.println("Arguments:"); 271 args.forEach(writer::println); 272 writer.println(); 273 writer.println("Exception:"); 274 e.printStackTrace(writer); 275 } 276 close.close(); 277 throw e; 278 } finally { 279 es.shutdownNow(); 280 ss.close(); 281 } 282 if (!Objects.equals(rand, res[0])) { 283 close.close(); 284 throw new IllegalStateException("IpcServer did not respond with the correct random"); 285 } 286 this.bootstrapToken = rand; 287 288 SocketAddress addr = SocketFamily.fromString(res[1]); 289 SocketChannel socket = SocketChannel.open(addr); 290 291 raf.seek(0); 292 raf.writeBytes(res[1] + "\n"); 293 return socket; 294 } catch (Exception e) { 295 throw new RuntimeException("Unable to create and connect to lock server", e); 296 } 297 } 298 } 299 300 private static void writeBootstrapToken(Process process, String token) throws IOException { 301 try (OutputStream os = process.getOutputStream()) { 302 os.write((token + "\n").getBytes(StandardCharsets.UTF_8)); 303 } 304 } 305 306 private String getJarPath(Class<?> clazz) { 307 String classpath; 308 String className = clazz.getName().replace('.', '/') + ".class"; 309 URL resource = clazz.getResource("/" + className); 310 if (resource == null) { 311 throw new IllegalStateException("Unable to find resource for class " + clazz.getName()); 312 } 313 String url = resource.toString(); 314 if (url.startsWith("jar:")) { 315 url = url.substring("jar:".length(), url.indexOf("!/")); 316 if (url.startsWith("file:")) { 317 classpath = url.substring("file:".length()); 318 } else { 319 throw new IllegalStateException(); 320 } 321 } else if (url.startsWith("file:")) { 322 classpath = url.substring("file:".length(), url.indexOf(className)); 323 } else { 324 throw new IllegalStateException(); 325 } 326 if (IS_WINDOWS) { 327 if (classpath.startsWith("/")) { 328 classpath = classpath.substring(1); 329 } 330 classpath = classpath.replace('/', '\\'); 331 } 332 333 return classpath; 334 } 335 336 void receive() { 337 try { 338 while (true) { 339 DataInputStream in = getInput(); 340 if (in == null) { 341 throw new IOException("Connection closed"); 342 } 343 int id = in.readInt(); 344 int sz = in.readInt(); 345 List<String> s = new ArrayList<>(Math.max(0, Math.min(sz, 1024))); 346 for (int i = 0; i < sz; i++) { 347 s.add(in.readUTF()); 348 } 349 CompletableFuture<List<String>> f = responses.remove(id); 350 if (f == null) { 351 continue; 352 } 353 if (s.isEmpty()) { 354 f.completeExceptionally(new IOException("Protocol error: empty response")); 355 continue; 356 } 357 f.complete(s); 358 } 359 } catch (EOFException e) { 360 close(new IOException("Server disconnected", e)); 361 } catch (Exception e) { 362 close(e); 363 } 364 } 365 366 List<String> send(List<String> request, long time, TimeUnit unit) throws TimeoutException, IOException { 367 ensureInitialized(); 368 DataOutputStream out = getOutput(); 369 if (out == null) { 370 throw new IOException("Connection closed"); 371 } 372 int id = requestId.incrementAndGet(); 373 CompletableFuture<List<String>> response = new CompletableFuture<>(); 374 responses.put(id, response); 375 synchronized (out) { 376 out.writeInt(id); 377 out.writeInt(request.size()); 378 for (String s : request) { 379 out.writeUTF(s); 380 } 381 out.flush(); 382 } 383 try { 384 return response.get(time, unit); 385 } catch (InterruptedException e) { 386 responses.remove(id); 387 throw (IOException) new InterruptedIOException("Interrupted").initCause(e); 388 } catch (ExecutionException e) { 389 throw new IOException("Execution error", e); 390 } catch (TimeoutException e) { 391 responses.remove(id); 392 throw e; 393 } 394 } 395 396 void close() { 397 closed = true; 398 try { 399 if (noFork) { 400 stopServer(); 401 } 402 } finally { 403 close(new IOException("Closing")); 404 } 405 } 406 407 synchronized void close(Throwable e) { 408 initialized = false; 409 if (socket != null) { 410 try { 411 socket.close(); 412 } catch (IOException t) { 413 e.addSuppressed(t); 414 } 415 socket = null; 416 input = null; 417 output = null; 418 } 419 if (receiver != null && Thread.currentThread() != receiver) { 420 receiver.interrupt(); 421 try { 422 receiver.join(1000); 423 } catch (InterruptedException t) { 424 e.addSuppressed(t); 425 } 426 } 427 responses.values().forEach(f -> f.completeExceptionally(e)); 428 responses.clear(); 429 } 430 431 String newContext(boolean shared, long time, TimeUnit unit) throws TimeoutException { 432 RuntimeException error = new RuntimeException("Unable to create new sync context"); 433 for (int i = 0; i < 2; i++) { 434 try { 435 List<String> response = send(Arrays.asList(REQUEST_CONTEXT, Boolean.toString(shared)), time, unit); 436 if (response.size() != 2 || !RESPONSE_CONTEXT.equals(response.get(0))) { 437 throw new IOException("Unexpected response: " + response); 438 } 439 return response.get(1); 440 } catch (TimeoutException e) { 441 throw e; 442 } catch (Exception e) { 443 close(e); 444 error.addSuppressed(e); 445 } 446 } 447 throw error; 448 } 449 450 void lock(String contextId, Collection<String> keys, long time, TimeUnit unit) throws TimeoutException { 451 try { 452 List<String> req = new ArrayList<>(keys.size() + 2); 453 req.add(REQUEST_ACQUIRE); 454 req.add(contextId); 455 req.addAll(keys); 456 List<String> response = send(req, time, unit); 457 if (response.size() != 1 || !RESPONSE_ACQUIRE.equals(response.get(0))) { 458 throw new IOException("Unexpected response: " + response); 459 } 460 } catch (TimeoutException e) { 461 throw e; 462 } catch (Exception e) { 463 close(e); 464 throw new RuntimeException("Unable to perform lock (contextId = " + contextId + ")", e); 465 } 466 } 467 468 void unlock(String contextId) { 469 try { 470 List<String> response = send(Arrays.asList(REQUEST_CLOSE, contextId), 10, TimeUnit.SECONDS); 471 if (response.size() != 1 || !RESPONSE_CLOSE.equals(response.get(0))) { 472 throw new IOException("Unexpected response: " + response); 473 } 474 } catch (Exception e) { 475 close(e); 476 throw new RuntimeException("Unable to unlock (contextId = " + contextId + ")", e); 477 } 478 } 479 480 /** 481 * To be used in tests to stop server immediately. Should not be used outside of tests. 482 */ 483 void stopServer() { 484 String token = bootstrapToken; 485 try { 486 List<String> response = send(List.of(REQUEST_STOP, token == null ? "" : token), 30, TimeUnit.SECONDS); 487 if (response.size() != 1 || !RESPONSE_STOP.equals(response.get(0))) { 488 throw new IOException("Unexpected response: " + response); 489 } 490 491 Process forkedProcess = process; 492 if (forkedProcess != null) { 493 if (!forkedProcess.waitFor(30, TimeUnit.SECONDS)) { 494 forkedProcess.destroyForcibly(); 495 if (!forkedProcess.waitFor(5, TimeUnit.SECONDS)) { 496 throw new IOException("Unable to stop forked server process"); 497 } 498 } 499 process = null; 500 } 501 } catch (Exception e) { 502 close(e); 503 throw new RuntimeException("Unable to stop server", e); 504 } 505 } 506 507 @Override 508 public String toString() { 509 return "IpcClient{" 510 + "lockPath=" + lockPath + "," 511 + "syncServerPath=" + syncPath + "," 512 + "address='" + getAddress() + "'}"; 513 } 514 515 private String getAddress() { 516 SocketChannel s = socket; 517 if (s == null) { 518 return "[closed]"; 519 } 520 try { 521 return SocketFamily.toString(s.getLocalAddress()); 522 } catch (IOException e) { 523 return "[not bound]"; 524 } 525 } 526}