forked from runtimeverification/k
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathKServerFrontEnd.java
More file actions
195 lines (177 loc) · 6.95 KB
/
KServerFrontEnd.java
File metadata and controls
195 lines (177 loc) · 6.95 KB
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
// Copyright (c) 2015-2019 K Team. All Rights Reserved.
package org.kframework.kserver;
import com.google.common.collect.ImmutableList;
import com.google.inject.Inject;
import com.google.inject.Injector;
import com.google.inject.Module;
import com.google.inject.Provider;
import com.martiansoftware.nailgun.NGContext;
import com.martiansoftware.nailgun.NGListeningAddress;
import com.martiansoftware.nailgun.NGServer;
import com.martiansoftware.nailgun.ThreadLocalPrintStream;
import org.fusesource.jansi.AnsiConsole;
import org.fusesource.jansi.AnsiOutputStream;
import org.kframework.main.FrontEnd;
import org.kframework.main.Main;
import org.kframework.utils.OS;
import org.kframework.utils.errorsystem.KExceptionManager;
import org.kframework.utils.file.FileUtil;
import org.kframework.utils.file.JarInfo;
import org.kframework.utils.file.TTYInfo;
import org.kframework.utils.inject.CommonModule;
import org.kframework.utils.inject.JCommanderModule;
import org.kframework.utils.inject.JCommanderModule.ExperimentalUsage;
import org.kframework.utils.inject.JCommanderModule.Usage;
import org.kframework.utils.inject.SimpleScope;
import java.io.File;
import java.io.PrintStream;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
public class KServerFrontEnd extends FrontEnd {
public static List<Module> getModules() {
List<Module> modules = new ArrayList<>();
modules.add(new KServerModule());
modules.add(new JCommanderModule());
modules.add(new CommonModule());
return modules;
}
@Inject
public KServerFrontEnd(
KExceptionManager kem,
KServerOptions options,
@Usage String usage,
@ExperimentalUsage String experimentalUsage,
JarInfo jarInfo,
Provider<FileUtil> files) {
super(kem, options.global, usage, experimentalUsage, jarInfo, files);
this.options = options;
}
private static KServerFrontEnd instance;
private static Thread threadInstance;
private static final ImmutableList<String> tools = ImmutableList.of("-kompile", "-krun", "-kast",
"-kdoc", "-kdep", "-keq", "-kprove", "-kbmc", "-kserver");
private final KServerOptions options;
private final Map<String, Injector> injectors = new HashMap<>();
private final ReadWriteLock lock = new ReentrantReadWriteLock();
@Override
protected int run() {
for (String tool : tools) {
injectors.put(tool, Main.getInjector(tool));
}
NGServer server;
File dir = null;
if (isLocal()) {
// can use more secure unix domain sockets
// we use HOME variable to get home on linux because user.home does not match HOME variable when run with sudo
// unless run with sudo -H, and we want to ensure that the installer works correctly.
String dirpath = options.socket;
if (dirpath == null) {
String home = OS.current() == OS.LINUX ? System.getenv("HOME") : System.getProperty("user.home");
dirpath = home + File.separatorChar + ".kserver";
}
dir = new File(dirpath);
dir.mkdirs();
dir.setReadable(false, false);
dir.setReadable(true, true);
dir.setWritable(false, false);
dir.setWritable(true, true);
dir.setExecutable(false, false);
dir.setExecutable(true, true);
File socket = new File(dir, "socket");
socket.deleteOnExit();
if (socket.exists()) {
System.out.println("Warning: K server already started.");
socket.delete();
}
server = new NGServer(new NGListeningAddress(socket.getAbsolutePath()), 10, 10000);
} else {
server = new NGServer(InetAddress.getLoopbackAddress(), options.port);
}
Thread t = new Thread(server);
instance = this;
threadInstance = t;
t.start();
if (isLocal()) {
System.out.println("K server started using IPC at " + dir.getAbsolutePath());
} else {
int runningPort = server.getPort();
while (runningPort == 0) {
try {
Thread.sleep(50L);
} catch (InterruptedException e) {
}
runningPort = server.getPort();
}
System.out.println("K server started on 127.0.0.1:" + options.port);
}
try {
t.join();
return 0;
} catch (InterruptedException e) {
//application is about to die
return 0;
}
}
public static KServerFrontEnd instance() {
return instance;
}
public int run(String tool, String[] args, File workingDir, Map<String, String> env) {
ThreadLocalPrintStream system_out = (ThreadLocalPrintStream) System.out;
ThreadLocalPrintStream system_err = (ThreadLocalPrintStream) System.err;
Injector injector;
lock.readLock().lock();
try {
injector = injectors.get(tool);
} finally {
lock.readLock().unlock();
}
Main launcher = injector.getInstance(Main.class);
SimpleScope requestScope = launcher.getRequestScope();
try {
requestScope.enter();
Main.seedInjector(requestScope, tool, args, workingDir, env);
TTYInfo tty = injector.getInstance(TTYInfo.class);
if (!tty.stdout) {
system_out.init(new PrintStream(new AnsiOutputStream(system_out.getPrintStream())));
}
if (!tty.stderr) {
system_err.init(new PrintStream(new AnsiOutputStream(system_err.getPrintStream())));
}
int result = launcher.runApplication();
System.out.flush();
System.err.flush();
return result;
} finally {
requestScope.exit();
}
}
public static void nailMain(NGContext context) {
KServerFrontEnd kserver = KServerFrontEnd.instance();
if (!kserver.isLocal()) {
context.assertLoopbackClient();
}
if (context.getArgs()[0].equals("shutdown")) {
System.setSecurityManager(null);
context.getNGServer().shutdown(true);
} else if (context.getArgs()[0].equals("reset")) {
kserver.lock.writeLock().lock();
try {
kserver.injectors.clear();
for (String tool : tools) {
kserver.injectors.put(tool, Main.getInjector(tool));
}
} finally {
kserver.lock.writeLock().unlock();
}
System.gc();
}
}
public boolean isLocal() {
return (OS.current() != OS.WINDOWS) || (options.socket != null);
}
}