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.BufferedReader; 022import java.io.DataInputStream; 023import java.io.DataOutputStream; 024import java.io.IOException; 025import java.io.InputStreamReader; 026import java.net.SocketAddress; 027import java.nio.channels.ByteChannel; 028import java.nio.channels.Channels; 029import java.nio.channels.ServerSocketChannel; 030import java.nio.channels.SocketChannel; 031import java.nio.charset.StandardCharsets; 032import java.util.ArrayList; 033import java.util.Iterator; 034import java.util.List; 035import java.util.Map; 036import java.util.concurrent.CompletableFuture; 037import java.util.concurrent.ConcurrentHashMap; 038import java.util.concurrent.CopyOnWriteArrayList; 039import java.util.concurrent.TimeUnit; 040import java.util.concurrent.atomic.AtomicInteger; 041 042import org.slf4j.LoggerFactory; 043 044/** 045 * Implementation of the server side. 046 * The server instance is bound to a given maven repository. 047 * 048 * @since 2.0.1 049 */ 050public class IpcServer { 051 static volatile boolean forkedProcess; 052 053 /** 054 * Should the IPC server not fork? (i.e. for testing purposes) 055 * 056 * @configurationSource {@link System#getProperty(String, String)} 057 * @configurationType {@link java.lang.Boolean} 058 * @configurationDefaultValue {@link #DEFAULT_NO_FORK} 059 */ 060 public static final String SYSTEM_PROP_NO_FORK = "aether.named.ipc.nofork"; 061 062 public static final boolean DEFAULT_NO_FORK = false; 063 064 /** 065 * IPC idle timeout in seconds. If there is no IPC request during idle time, it will stop. 066 * 067 * @configurationSource {@link System#getProperty(String, String)} 068 * @configurationType {@link java.lang.Integer} 069 * @configurationDefaultValue {@link #DEFAULT_IDLE_TIMEOUT} 070 */ 071 public static final String SYSTEM_PROP_IDLE_TIMEOUT = "aether.named.ipc.idleTimeout"; 072 073 public static final int DEFAULT_IDLE_TIMEOUT = 300; 074 075 /** 076 * IPC socket family to use. 077 * 078 * @configurationSource {@link System#getProperty(String, String)} 079 * @configurationType {@link java.lang.String} 080 * @configurationDefaultValue {@link #DEFAULT_FAMILY} 081 */ 082 public static final String SYSTEM_PROP_FAMILY = "aether.named.ipc.family"; 083 084 public static final String DEFAULT_FAMILY = "unix"; 085 086 /** 087 * Should the IPC server not use native executable? 088 * 089 * @configurationSource {@link System#getProperty(String, String)} 090 * @configurationType {@link java.lang.Boolean} 091 * @configurationDefaultValue {@link #DEFAULT_NO_NATIVE} 092 */ 093 public static final String SYSTEM_PROP_NO_NATIVE = "aether.named.ipc.nonative"; 094 095 public static final boolean DEFAULT_NO_NATIVE = true; 096 097 /** 098 * The name if the IPC server native executable (without file extension like ".exe") 099 * 100 * @configurationSource {@link System#getProperty(String, String)} 101 * @configurationType {@link java.lang.String} 102 * @configurationDefaultValue {@link #DEFAULT_NATIVE_NAME} 103 */ 104 public static final String SYSTEM_PROP_NATIVE_NAME = "aether.named.ipc.nativeName"; 105 106 public static final String DEFAULT_NATIVE_NAME = "ipc-sync"; 107 108 /** 109 * Should the IPC server log debug messages? (i.e. for testing purposes) 110 * 111 * @configurationSource {@link System#getProperty(String, String)} 112 * @configurationType {@link java.lang.Boolean} 113 * @configurationDefaultValue {@link #DEFAULT_DEBUG} 114 */ 115 public static final String SYSTEM_PROP_DEBUG = "aether.named.ipc.debug"; 116 117 public static final boolean DEFAULT_DEBUG = false; 118 119 private final ServerSocketChannel serverSocket; 120 private final Map<SocketChannel, Thread> clients = new ConcurrentHashMap<>(); 121 private final AtomicInteger counter = new AtomicInteger(); 122 private final Map<String, Lock> locks = new ConcurrentHashMap<>(); 123 private final Map<String, Context> contexts = new ConcurrentHashMap<>(); 124 private static final boolean DEBUG = 125 Boolean.parseBoolean(System.getProperty(SYSTEM_PROP_DEBUG, Boolean.toString(DEFAULT_DEBUG))); 126 private final long idleTimeout; 127 private final String bootstrapToken; 128 private volatile long lastUsed; 129 private volatile boolean closing; 130 131 /** 132 * @deprecated a server created without a bootstrap token refuses {@link IpcMessages#REQUEST_STOP} requests; 133 * use {@link #IpcServer(SocketFamily, String)} instead. 134 */ 135 @Deprecated 136 public IpcServer(SocketFamily family) throws IOException { 137 this(family, null); 138 } 139 140 /** 141 * Creates a server that honors a remote stop request only when it carries the given bootstrap token, which is 142 * shared exclusively with the client that spawned this server. The rest of the protocol is unauthenticated, 143 * but destructive cross-client operations (closing foreign contexts, stopping the daemon) are refused. 144 * 145 * @since 2.0.23 146 */ 147 public IpcServer(SocketFamily family, String bootstrapToken) throws IOException { 148 this.bootstrapToken = bootstrapToken; 149 serverSocket = family.openServerSocket(); 150 long timeout = TimeUnit.SECONDS.toNanos(DEFAULT_IDLE_TIMEOUT); 151 String str = System.getProperty(SYSTEM_PROP_IDLE_TIMEOUT); 152 if (str != null) { 153 try { 154 TimeUnit unit = TimeUnit.SECONDS; 155 if (str.endsWith("ms")) { 156 unit = TimeUnit.MILLISECONDS; 157 str = str.substring(0, str.length() - 2); 158 } 159 long dur = Long.parseLong(str); 160 timeout = unit.toNanos(dur); 161 } catch (NumberFormatException e) { 162 error("Property " + SYSTEM_PROP_IDLE_TIMEOUT + " specified with invalid value: " + str, e); 163 } 164 } 165 idleTimeout = timeout; 166 } 167 168 public static void main(String[] args) throws Exception { 169 forkedProcess = true; 170 // When spawning a new process, the child process is create within 171 // the same process group. This means that a few signals are sent 172 // to the whole group. This is the case for SIGINT (Ctrl-C) and 173 // SIGTSTP (Ctrl-Z) which are both sent to all the processed in the 174 // group when initiated from the controlling terminal. 175 // This is only a problem when the client creates the daemon, but 176 // without ignoring those signals, a client being interrupted will 177 // also interrupt and kill the daemon. 178 try { 179 sun.misc.Signal.handle(new sun.misc.Signal("INT"), sun.misc.SignalHandler.SIG_IGN); 180 if (!IpcClient.IS_WINDOWS) { 181 sun.misc.Signal.handle(new sun.misc.Signal("TSTP"), sun.misc.SignalHandler.SIG_IGN); 182 } 183 } catch (Throwable t) { 184 error("Unable to ignore INT and TSTP signals", t); 185 } 186 187 String family = args[0]; 188 String tmpAddress = args[1]; 189 String rand = args[2]; 190 if ("-".equals(rand)) { 191 // the bootstrap token is passed via stdin instead of argv: process arguments are commonly visible 192 // to other local users (e.g. /proc/<pid>/cmdline), and this token authorizes stopping the daemon 193 BufferedReader reader = new BufferedReader(new InputStreamReader(System.in, StandardCharsets.UTF_8)); 194 rand = reader.readLine(); 195 if (rand == null || rand.isEmpty()) { 196 throw new IOException("Expected the bootstrap token on standard input"); 197 } 198 } 199 200 runServer(SocketFamily.valueOf(family), tmpAddress, rand); 201 } 202 203 static IpcServer runServer(SocketFamily family, String tmpAddress, String rand) throws IOException { 204 IpcServer server = new IpcServer(family, rand); 205 run(server::run, false); // this is one-off 206 String address = SocketFamily.toString(server.getLocalAddress()); 207 SocketAddress socketAddress = SocketFamily.fromString(tmpAddress); 208 try (SocketChannel socket = SocketChannel.open(socketAddress)) { 209 try (DataOutputStream dos = new DataOutputStream(Channels.newOutputStream(socket))) { 210 dos.writeUTF(rand); 211 dos.writeUTF(address); 212 dos.flush(); 213 } 214 } 215 216 return server; 217 } 218 219 private static void debug(String msg, Object... args) { 220 if (DEBUG) { 221 if (forkedProcess) { 222 System.out.println("[ipc] [debug] " + format(msg, args)); 223 } else { 224 try { 225 LoggerFactory.getLogger(IpcServer.class).debug(msg, args); 226 } catch (LinkageError e) { 227 // forkedProcess was reset to false while this call was already in flight (run()'s 228 // finally block races callers such as sendAcquireResponse() during shutdown); slf4j-api 229 // is still absent from the forked process classpath, so fall back the same way. 230 System.out.println("[ipc] [debug] " + format(msg, args)); 231 } 232 } 233 } 234 } 235 236 private static void info(String msg, Object... args) { 237 if (forkedProcess) { 238 System.out.println("[ipc] [info] " + format(msg, args)); 239 } else { 240 try { 241 LoggerFactory.getLogger(IpcServer.class).info(msg, args); 242 } catch (LinkageError e) { 243 // forkedProcess was reset to false while this call was already in flight (run()'s 244 // finally block races callers such as sendAcquireResponse() during shutdown); slf4j-api 245 // is still absent from the forked process classpath, so fall back the same way. 246 System.out.println("[ipc] [info] " + format(msg, args)); 247 } 248 } 249 } 250 251 private static void error(String msg, Throwable t) { 252 if (forkedProcess) { 253 System.out.println("[ipc] [error] " + msg); 254 t.printStackTrace(System.out); 255 } else { 256 try { 257 LoggerFactory.getLogger(IpcServer.class).error(msg, t); 258 } catch (LinkageError e) { 259 // forkedProcess was reset to false while this call was already in flight (run()'s 260 // finally block races callers such as sendAcquireResponse() during shutdown); slf4j-api 261 // is still absent from the forked process classpath, so fall back the same way. 262 System.out.println("[ipc] [error] " + msg); 263 t.printStackTrace(System.out); 264 } 265 } 266 } 267 268 private static String format(String msg, Object... args) { 269 return String.format(msg.replace("{}", "%s"), args); 270 } 271 272 private static void run(Runnable runnable, boolean daemon) { 273 Thread thread = new Thread(runnable); 274 if (daemon) { 275 thread.setDaemon(true); 276 } 277 thread.start(); 278 } 279 280 public SocketAddress getLocalAddress() throws IOException { 281 return serverSocket.getLocalAddress(); 282 } 283 284 public void run() { 285 try { 286 info("IpcServer started at {}", getLocalAddress().toString()); 287 use(); 288 run(this::expirationCheck, true); 289 while (!closing) { 290 SocketChannel socket = this.serverSocket.accept(); 291 run(() -> client(socket), false); 292 } 293 } catch (Throwable t) { 294 if (!closing) { 295 error("Error running sync server loop", t); 296 } 297 } finally { 298 forkedProcess = false; 299 } 300 } 301 302 private void client(SocketChannel socket) { 303 int c; 304 synchronized (clients) { 305 clients.put(socket, Thread.currentThread()); 306 c = clients.size(); 307 } 308 info("New client connected ({} connected)", c); 309 use(); 310 Map<String, Context> clientContexts = new ConcurrentHashMap<>(); 311 try { 312 ByteChannel wrapper = new ByteChannelWrapper(socket); 313 DataInputStream input = new DataInputStream(Channels.newInputStream(wrapper)); 314 DataOutputStream output = new DataOutputStream(Channels.newOutputStream(wrapper)); 315 while (!closing) { 316 int requestId = input.readInt(); 317 int sz = input.readInt(); 318 if (sz < 0) { 319 throw new IOException("Received invalid request size: " + sz); 320 } 321 // do not preallocate from an unauthenticated wire-supplied size; grow with actually received data 322 List<String> request = new ArrayList<>(Math.min(sz, 1024)); 323 for (int i = 0; i < sz; i++) { 324 request.add(input.readUTF()); 325 } 326 if (request.isEmpty()) { 327 throw new IOException("Received invalid request"); 328 } 329 use(); 330 String contextId; 331 Context context; 332 String command = request.remove(0); 333 switch (command) { 334 case IpcMessages.REQUEST_CONTEXT: 335 if (request.size() != 1) { 336 throw new IOException("Expected one argument for " + command + " but got " + request); 337 } 338 boolean shared = Boolean.parseBoolean(request.remove(0)); 339 context = new Context(shared); 340 contexts.put(context.id, context); 341 clientContexts.put(context.id, context); 342 synchronized (output) { 343 debug("Created context {}", context.id); 344 output.writeInt(requestId); 345 output.writeInt(2); 346 output.writeUTF(IpcMessages.RESPONSE_CONTEXT); 347 output.writeUTF(context.id); 348 output.flush(); 349 } 350 break; 351 case IpcMessages.REQUEST_ACQUIRE: 352 if (request.isEmpty()) { 353 throw new IOException( 354 "Expected at least one argument for " + command + " but got " + request); 355 } 356 contextId = request.remove(0); 357 // contexts are scoped per connection: a client may only use contexts it created itself 358 context = clientContexts.get(contextId); 359 if (context == null) { 360 throw new IOException( 361 "Unknown context: " + contextId + ". Known contexts = " + clientContexts.keySet()); 362 } 363 context.lock(request).thenRun(() -> sendAcquireResponse(output, socket, requestId, context)); 364 break; 365 case IpcMessages.REQUEST_CLOSE: 366 if (request.size() != 1) { 367 throw new IOException("Expected one argument for " + command + " but got " + request); 368 } 369 contextId = request.remove(0); 370 // contexts are scoped per connection: a client may only close contexts it created itself 371 context = clientContexts.remove(contextId); 372 if (context == null) { 373 throw new IOException( 374 "Unknown context: " + contextId + ". Known contexts = " + clientContexts.keySet()); 375 } 376 contexts.remove(contextId); 377 context.unlock(); 378 synchronized (output) { 379 debug("Closing context {}", context.id); 380 output.writeInt(requestId); 381 output.writeInt(1); 382 output.writeUTF(IpcMessages.RESPONSE_CLOSE); 383 output.flush(); 384 } 385 break; 386 case IpcMessages.REQUEST_STOP: 387 if (request.size() > 1) { 388 throw new IOException( 389 "Expected at most one argument for " + command + " but got " + request); 390 } 391 String stopToken = request.isEmpty() ? null : request.remove(0); 392 if (bootstrapToken == null || !bootstrapToken.equals(stopToken)) { 393 // the protocol is otherwise unauthenticated: only the client that spawned this 394 // server (and thus knows the bootstrap token) may stop it for everybody else 395 throw new IOException("Stop request rejected: missing or invalid bootstrap token"); 396 } 397 synchronized (output) { 398 debug("Stopping server"); 399 output.writeInt(requestId); 400 output.writeInt(1); 401 output.writeUTF(IpcMessages.RESPONSE_STOP); 402 output.flush(); 403 } 404 close(); 405 break; 406 default: 407 throw new IOException("Unknown request: " + request.get(0)); 408 } 409 } 410 } catch (Throwable t) { 411 if (!closing) { 412 error("Error processing request", t); 413 } 414 } finally { 415 if (!closing) { 416 info("Client disconnecting..."); 417 } 418 clientContexts.values().forEach(context -> { 419 contexts.remove(context.id); 420 context.unlock(); 421 }); 422 try { 423 socket.close(); 424 } catch (IOException ioException) { 425 // ignore 426 } 427 synchronized (clients) { 428 clients.remove(socket); 429 c = clients.size(); 430 } 431 if (!closing) { 432 info("{} clients remained", c); 433 } 434 } 435 } 436 437 private void sendAcquireResponse(DataOutputStream output, SocketChannel socket, int requestId, Context context) { 438 try { 439 synchronized (output) { 440 debug("Locking in context {}", context.id); 441 output.writeInt(requestId); 442 output.writeInt(1); 443 output.writeUTF(IpcMessages.RESPONSE_ACQUIRE); 444 output.flush(); 445 } 446 } catch (IOException e) { 447 try { 448 socket.close(); 449 } catch (IOException ioException) { 450 e.addSuppressed(ioException); 451 } 452 error("Error writing lock response", e); 453 } 454 } 455 456 private void use() { 457 lastUsed = System.nanoTime(); 458 } 459 460 private void expirationCheck() { 461 while (true) { 462 long current = System.nanoTime(); 463 long left = (lastUsed + idleTimeout) - current; 464 if (clients.isEmpty() && left < 0) { 465 info("IpcServer expired, closing"); 466 close(); 467 break; 468 } else { 469 try { 470 Thread.sleep(Math.max(1, TimeUnit.NANOSECONDS.toMillis(left))); 471 } catch (InterruptedException e) { 472 info("IpcServer expiration check interrupted, closing"); 473 close(); 474 break; 475 } 476 } 477 } 478 } 479 480 void close() { 481 closing = true; 482 try { 483 serverSocket.close(); 484 } catch (IOException e) { 485 error("Error closing server socket", e); 486 } 487 clients.forEach((s, t) -> { 488 try { 489 s.close(); 490 } catch (IOException e) { 491 // ignore 492 } 493 t.interrupt(); 494 }); 495 } 496 497 static class Waiter { 498 final Context context; 499 final CompletableFuture<Void> future; 500 501 Waiter(Context context, CompletableFuture<Void> future) { 502 this.context = context; 503 this.future = future; 504 } 505 } 506 507 static class Lock { 508 509 final String key; 510 511 List<Context> holders; 512 List<Waiter> waiters; 513 514 Lock(String key) { 515 this.key = key; 516 } 517 518 public synchronized CompletableFuture<Void> lock(Context context) { 519 if (holders == null) { 520 holders = new ArrayList<>(); 521 } 522 if (holders.isEmpty() || holders.get(0).shared && context.shared) { 523 holders.add(context); 524 return CompletableFuture.completedFuture(null); 525 } 526 if (waiters == null) { 527 waiters = new ArrayList<>(); 528 } 529 530 CompletableFuture<Void> future = new CompletableFuture<>(); 531 waiters.add(new Waiter(context, future)); 532 return future; 533 } 534 535 public void unlock(Context context) { 536 List<CompletableFuture<Void>> toComplete; 537 synchronized (this) { 538 toComplete = new ArrayList<>(); 539 if (holders.remove(context)) { 540 while (waiters != null 541 && !waiters.isEmpty() 542 && (holders.isEmpty() || holders.get(0).shared && waiters.get(0).context.shared)) { 543 Waiter waiter = waiters.remove(0); 544 holders.add(waiter.context); 545 toComplete.add(waiter.future); 546 } 547 } else if (waiters != null) { 548 for (Iterator<Waiter> it = waiters.iterator(); it.hasNext(); ) { 549 Waiter waiter = it.next(); 550 if (waiter.context == context) { 551 it.remove(); 552 waiter.future.cancel(false); 553 } 554 } 555 } 556 } 557 toComplete.forEach(f -> f.complete(null)); 558 } 559 560 public synchronized boolean isEmpty() { 561 return (holders == null || holders.isEmpty()) && (waiters == null || waiters.isEmpty()); 562 } 563 } 564 565 class Context { 566 567 final String id; 568 final boolean shared; 569 final List<String> locks = new CopyOnWriteArrayList<>(); 570 571 Context(boolean shared) { 572 this.id = String.format("%08x", counter.incrementAndGet()); 573 this.shared = shared; 574 } 575 576 public CompletableFuture<?> lock(List<String> keys) { 577 locks.addAll(keys); 578 CompletableFuture<?>[] futures = keys.stream() 579 .map(k -> IpcServer.this.locks.computeIfAbsent(k, Lock::new)) 580 .map(l -> l.lock(this)) 581 .toArray(CompletableFuture[]::new); 582 return CompletableFuture.allOf(futures); 583 } 584 585 public void unlock() { 586 locks.stream() 587 .map(k -> IpcServer.this.locks.computeIfAbsent(k, Lock::new)) 588 .forEach(l -> { 589 l.unlock(this); 590 IpcServer.this.locks.compute(l.key, (k, v) -> (v == l && v.isEmpty()) ? null : v); 591 }); 592 } 593 } 594}