1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.aether.named.ipc;
20
21 import java.io.BufferedReader;
22 import java.io.DataInputStream;
23 import java.io.DataOutputStream;
24 import java.io.IOException;
25 import java.io.InputStreamReader;
26 import java.net.SocketAddress;
27 import java.nio.channels.ByteChannel;
28 import java.nio.channels.Channels;
29 import java.nio.channels.ServerSocketChannel;
30 import java.nio.channels.SocketChannel;
31 import java.nio.charset.StandardCharsets;
32 import java.util.ArrayList;
33 import java.util.Iterator;
34 import java.util.List;
35 import java.util.Map;
36 import java.util.concurrent.CompletableFuture;
37 import java.util.concurrent.ConcurrentHashMap;
38 import java.util.concurrent.CopyOnWriteArrayList;
39 import java.util.concurrent.TimeUnit;
40 import java.util.concurrent.atomic.AtomicInteger;
41
42 import org.slf4j.LoggerFactory;
43
44
45
46
47
48
49
50 public class IpcServer {
51 static volatile boolean forkedProcess;
52
53
54
55
56
57
58
59
60 public static final String SYSTEM_PROP_NO_FORK = "aether.named.ipc.nofork";
61
62 public static final boolean DEFAULT_NO_FORK = false;
63
64
65
66
67
68
69
70
71 public static final String SYSTEM_PROP_IDLE_TIMEOUT = "aether.named.ipc.idleTimeout";
72
73 public static final int DEFAULT_IDLE_TIMEOUT = 300;
74
75
76
77
78
79
80
81
82 public static final String SYSTEM_PROP_FAMILY = "aether.named.ipc.family";
83
84 public static final String DEFAULT_FAMILY = "unix";
85
86
87
88
89
90
91
92
93 public static final String SYSTEM_PROP_NO_NATIVE = "aether.named.ipc.nonative";
94
95 public static final boolean DEFAULT_NO_NATIVE = true;
96
97
98
99
100
101
102
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
110
111
112
113
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
133
134
135 @Deprecated
136 public IpcServer(SocketFamily family) throws IOException {
137 this(family, null);
138 }
139
140
141
142
143
144
145
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
171
172
173
174
175
176
177
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
192
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);
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
228
229
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
244
245
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
260
261
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
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
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
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
394
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
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
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 }