-
Notifications
You must be signed in to change notification settings - Fork 5.3k
/
SocketLock.java
417 lines (364 loc) · 15 KB
/
SocketLock.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.idea;
import com.intellij.diagnostic.Activity;
import com.intellij.diagnostic.ActivityCategory;
import com.intellij.diagnostic.StartUpMeasurer;
import com.intellij.ide.CliResult;
import com.intellij.ide.IdeBundle;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.util.Disposer;
import com.intellij.openapi.util.text.StringUtilRt;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufOutputStream;
import io.netty.channel.ChannelHandlerContext;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.io.BuiltInServer;
import org.jetbrains.io.MessageDecoder;
import java.io.*;
import java.net.ConnectException;
import java.net.InetAddress;
import java.net.Socket;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.nio.charset.StandardCharsets;
import java.nio.file.*;
import java.nio.file.attribute.PosixFileAttributeView;
import java.nio.file.attribute.PosixFilePermission;
import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Function;
import static com.intellij.ide.SpecialConfigFiles.*;
public final class SocketLock {
public enum ActivationStatus {ACTIVATED, NO_INSTANCE, CANNOT_ACTIVATE}
/**
* Name of an environment variable that will be set by the Windows launcher and will contain the working directory the
* IDE was started with.
*
* This is necessary on Windows because the launcher needs to change the current directory for the JVM to load
* properly; see the details in WindowsLauncher.cpp.
*/
public static final String LAUNCHER_INITIAL_DIRECTORY_ENV_VAR = "IDEA_INITIAL_DIRECTORY";
private static final String ACTIVATE_COMMAND = "activate ";
private static final String OK_RESPONSE = "ok";
private static final String PATHS_EOT_RESPONSE = "---";
private final AtomicReference<Function<List<String>, Future<CliResult>>> myCommandProcessorRef;
private final Path myConfigPath;
private final Path mySystemPath;
private final List<AutoCloseable> myLockedFiles = new ArrayList<>(4);
private volatile CompletableFuture<BuiltInServer> myBuiltinServerFuture;
public SocketLock(@NotNull Path configPath, @NotNull Path systemPath) {
myConfigPath = configPath;
mySystemPath = systemPath;
if (myConfigPath.equals(mySystemPath)) {
throw new IllegalArgumentException("'config' and 'system' paths should point to different directories");
}
myCommandProcessorRef = new AtomicReference<>(args -> CliResult.error(Main.ACTIVATE_NOT_INITIALIZED, IdeBundle.message("activation.not.initialized")));
}
public @NotNull Path getConfigPath() {
return myConfigPath;
}
public @NotNull Path getSystemPath() {
return mySystemPath;
}
public void setCommandProcessor(@NotNull Function<List<String>, Future<CliResult>> processor) {
myCommandProcessorRef.set(processor);
}
public void dispose() {
log("enter: dispose()");
BuiltInServer server = null;
try {
server = getServer();
}
catch (Exception ignored) { }
try {
if (myLockedFiles.isEmpty()) {
lockPortFiles();
}
if (server != null) {
Disposer.dispose(server);
}
Files.deleteIfExists(myConfigPath.resolve(PORT_FILE));
Files.deleteIfExists(mySystemPath.resolve(PORT_FILE));
Files.deleteIfExists(mySystemPath.resolve(TOKEN_FILE));
unlockPortFiles();
}
catch (Exception e) {
log(e);
}
}
@Nullable BuiltInServer getServer() {
Future<BuiltInServer> future = myBuiltinServerFuture;
if (future != null) {
try {
return future.get();
}
catch (InterruptedException e) {
throw new IllegalStateException(e);
}
catch (ExecutionException e) {
throw new IllegalStateException(e.getCause());
}
}
return null;
}
@Nullable CompletableFuture<BuiltInServer> getServerFuture() {
return myBuiltinServerFuture;
}
public @NotNull Map.Entry<ActivationStatus, CliResult> lockAndTryActivate(@NotNull String @NotNull [] args) throws Exception {
log("enter: lock(config=%s system=%s)", myConfigPath, mySystemPath);
lockPortFiles();
Map<Integer, List<String>> portToPath = new HashMap<>();
readPort(myConfigPath, portToPath);
readPort(mySystemPath, portToPath);
if (!portToPath.isEmpty()) {
for (Map.Entry<Integer, List<String>> entry : portToPath.entrySet()) {
Map.Entry<ActivationStatus, CliResult> status = tryActivate(entry.getKey(), entry.getValue(), args);
if (status.getKey() != ActivationStatus.NO_INSTANCE) {
log("exit: lock(): " + status.getValue());
unlockPortFiles();
return status;
}
}
}
myBuiltinServerFuture = CompletableFuture.supplyAsync(() -> {
Activity activity = StartUpMeasurer.startActivity("built-in server launch", ActivityCategory.DEFAULT);
String token = UUID.randomUUID().toString();
Path[] lockedPaths = {myConfigPath, mySystemPath};
BuiltInServer server = BuiltInServer.Companion.start(6942, 50, false, () -> new MyChannelInboundHandler(lockedPaths, myCommandProcessorRef, token));
try {
byte[] portBytes = Integer.toString(server.getPort()).getBytes(StandardCharsets.UTF_8);
Files.write(myConfigPath.resolve(PORT_FILE), portBytes);
Files.write(mySystemPath.resolve(PORT_FILE), portBytes);
Path tokenFile = mySystemPath.resolve(TOKEN_FILE);
Files.write(tokenFile, token.getBytes(StandardCharsets.UTF_8));
PosixFileAttributeView view = Files.getFileAttributeView(tokenFile, PosixFileAttributeView.class);
if (view != null) {
try {
view.setPermissions(EnumSet.of(PosixFilePermission.OWNER_READ, PosixFilePermission.OWNER_WRITE));
}
catch (IOException e) {
log(e);
}
}
unlockPortFiles();
}
catch (RuntimeException e) {
throw e;
}
catch (Exception e) {
throw new CompletionException(e);
}
activity.end();
return server;
}, ForkJoinPool.commonPool());
log("exit: lock(): succeed");
return new AbstractMap.SimpleEntry<>(ActivationStatus.NO_INSTANCE, null);
}
/**
* <p>According to <a href="https://stackoverflow.com/a/12652718/3463676">Stack Overflow</a>, file locks should be removed on process segfault.
* According to {@link FileLock} documentation, file locks are marked as invalid on JVM termination.</p>
*
* <p>Unlocking of port files (via {@link #unlockPortFiles}) happens either after builtin server init, or on app termination.</p>
*
* <p>Because of that, we do not care about non-starting Netty leading to infinite lock handling, as the IDE is not ready to
* accept connections anyway; on app termination the locks will be released.</p>
*/
private synchronized void lockPortFiles() throws IOException {
if (!myLockedFiles.isEmpty()) {
throw new IllegalStateException("File locking must not be called twice");
}
OpenOption[] options = {StandardOpenOption.CREATE, StandardOpenOption.APPEND};
Files.createDirectories(myConfigPath);
FileChannel cc = FileChannel.open(myConfigPath.resolve(PORT_LOCK_FILE), options);
myLockedFiles.add(cc);
myLockedFiles.add(cc.lock());
Files.createDirectories(mySystemPath);
FileChannel sc = FileChannel.open(mySystemPath.resolve(PORT_LOCK_FILE), options);
myLockedFiles.add(sc);
myLockedFiles.add(sc.lock());
}
private synchronized void unlockPortFiles() throws Exception {
if (myLockedFiles.isEmpty()) {
throw new IllegalStateException("File unlocking must not be called twice");
}
for (int i = myLockedFiles.size() - 1; i >= 0; i--) {
myLockedFiles.get(i).close();
}
myLockedFiles.clear();
}
private static String readOneLine(Path file) throws IOException {
try (BufferedReader reader = Files.newBufferedReader(file)) {
return reader.readLine().trim();
}
}
private static void readPort(Path dir, Map<Integer, List<String>> portToPath) {
try {
portToPath.computeIfAbsent(Integer.parseInt(readOneLine(dir.resolve(PORT_FILE))), it -> new ArrayList<>()).add(dir.toString());
}
catch (NoSuchFileException ignore) { }
catch (Exception e) {
log(e); // no need to delete a file, it will be overwritten
}
}
private Map.Entry<ActivationStatus, CliResult> tryActivate(int portNumber, List<String> paths, String[] args) {
log("trying: port=%s", portNumber);
try (Socket socket = new Socket(InetAddress.getByName("127.0.0.1"), portNumber)) {
socket.setSoTimeout(5000);
DataInput in = new DataInputStream(socket.getInputStream());
List<String> stringList = readStringSequence(in);
// backward compatibility: requires at least one path to match
boolean result = paths.stream().anyMatch(stringList::contains);
if (result) {
try {
String token = readOneLine(mySystemPath.resolve(TOKEN_FILE));
@SuppressWarnings("IOResourceOpenedButNotSafelyClosed") DataOutputStream out = new DataOutputStream(socket.getOutputStream());
String currentDirectory = System.getenv(LAUNCHER_INITIAL_DIRECTORY_ENV_VAR);
if (currentDirectory == null) {
currentDirectory = ".";
}
out.writeUTF(ACTIVATE_COMMAND + token + '\0' + Paths.get(currentDirectory).toAbsolutePath() + '\0' + String.join("\0", args));
out.flush();
socket.setSoTimeout(0);
List<String> response = readStringSequence(in);
log("read: response=%s", String.join(";", response));
if (!response.isEmpty() && OK_RESPONSE.equals(response.get(0))) {
return new AbstractMap.SimpleEntry<>(ActivationStatus.ACTIVATED, mapResponseToCliResult(response));
}
}
catch (IOException | IllegalArgumentException e) {
log(e);
}
return new AbstractMap.SimpleEntry<>(ActivationStatus.CANNOT_ACTIVATE, null);
}
}
catch (ConnectException e) {
log("%s (stale port file?)", e.getMessage());
}
catch (IOException e) {
log(e);
}
return new AbstractMap.SimpleEntry<>(ActivationStatus.NO_INSTANCE, null);
}
private static final class MyChannelInboundHandler extends MessageDecoder {
private enum State {HEADER, CONTENT}
private final List<String> myLockedPaths;
private final AtomicReference<Function<List<String>, Future<CliResult>>> myCommandProcessorRef;
private final String myToken;
private State myState = State.HEADER;
MyChannelInboundHandler(Path[] lockedPaths, AtomicReference<Function<List<String>, Future<CliResult>>> commandProcessorRef, String token) {
myLockedPaths = new ArrayList<>(lockedPaths.length);
for (Path path : lockedPaths) {
myLockedPaths.add(path.toString());
}
myCommandProcessorRef = commandProcessorRef;
myToken = token;
}
@Override
public void channelActive(@NotNull ChannelHandlerContext context) throws Exception {
sendStringSequence(context, myLockedPaths);
}
@Override
protected void messageReceived(@NotNull ChannelHandlerContext context, @NotNull ByteBuf input) throws Exception {
while (true) {
switch (myState) {
case HEADER: {
ByteBuf buffer = getBufferIfSufficient(input, 2, context);
if (buffer == null) return;
contentLength = buffer.readUnsignedShort();
if (contentLength > 8192) {
context.close();
return;
}
myState = State.CONTENT;
break;
}
case CONTENT: {
CharSequence command = readChars(input);
if (command == null) return;
if (StringUtilRt.startsWith(command, ACTIVATE_COMMAND)) {
String data = command.subSequence(ACTIVATE_COMMAND.length(), command.length()).toString();
StringTokenizer tokenizer = new StringTokenizer(data, data.contains("\0") ? "\0" : "\uFFFD");
CliResult result;
boolean tokenOK = tokenizer.hasMoreTokens() && myToken.equals(tokenizer.nextToken());
if (tokenOK) {
List<String> list = new ArrayList<>();
while (tokenizer.hasMoreTokens()) list.add(tokenizer.nextToken());
Future<CliResult> future = myCommandProcessorRef.get().apply(list);
result = CliResult.unmap(future, Main.ACTIVATE_ERROR);
}
else {
log(new UnsupportedOperationException("unauthorized request: " + command));
result = new CliResult(Main.ACTIVATE_WRONG_TOKEN_CODE, IdeBundle.message("activation.auth.message"));
}
String exitCode = String.valueOf(result.exitCode), message = result.message;
List<String> response = message != null ? List.of(OK_RESPONSE, exitCode, message) : List.of(OK_RESPONSE, exitCode);
sendStringSequence(context, response);
}
context.close();
break;
}
}
}
}
}
private static void sendStringSequence(ChannelHandlerContext context, List<String> strings) throws IOException {
ByteBuf buffer = context.alloc().ioBuffer(1024);
boolean success = false;
try (ByteBufOutputStream out = new ByteBufOutputStream(buffer)) {
for (String s : strings) {
out.writeUTF(s);
}
out.writeUTF(PATHS_EOT_RESPONSE);
success = true;
}
finally {
if (!success) {
buffer.release();
}
}
context.writeAndFlush(buffer);
}
private static List<String> readStringSequence(DataInput in) {
List<String> result = new ArrayList<>();
while (true) {
try {
String string = in.readUTF();
log("read: path=%s", string);
if (PATHS_EOT_RESPONSE.equals(string)) {
break;
}
result.add(string);
}
catch (IOException e) {
log("read: %s", e.getMessage());
break;
}
}
return result;
}
private static CliResult mapResponseToCliResult(List<String> responseParts) throws IllegalArgumentException {
if (responseParts.size() > 3 || responseParts.size() < 2) {
throw new IllegalArgumentException("bad response: " + String.join(";", responseParts));
}
int code;
try {
code = Integer.parseInt(responseParts.get(1));
}
catch (NumberFormatException e) {
throw new IllegalArgumentException("Second part is not a parsable return code", e);
}
String message = responseParts.size() == 3 ? responseParts.get(2) : null;
return new CliResult(code, message);
}
private static void log(Exception e) {
Logger.getInstance(SocketLock.class).warn(e);
}
private static void log(String format, Object... args) {
Logger logger = Logger.getInstance(SocketLock.class);
if (logger.isDebugEnabled()) {
logger.debug(String.format(format, args));
}
}
}