Add pid file to Environment

This commit adds the path of the PID file to the Environment. It also add it to the Security Manager since the PID file is deleted by a shutdown hook when the JVM is exited.
This commit is contained in:
Tanguy Leroux 2015-05-05 17:20:13 +02:00
parent 4f65130703
commit 3976724882
4 changed files with 33 additions and 13 deletions

View File

@ -193,32 +193,31 @@ public class Bootstrap {
public static void main(String[] args) { public static void main(String[] args) {
System.setProperty("es.logger.prefix", ""); System.setProperty("es.logger.prefix", "");
INSTANCE = new Bootstrap(); INSTANCE = new Bootstrap();
final String pidFile = System.getProperty("es.pidfile", System.getProperty("es-pidfile"));
if (pidFile != null) {
try {
PidFile.create(PathUtils.get(pidFile), true);
} catch (Exception e) {
String errorMessage = buildErrorMessage("pid", e);
sysError(errorMessage, true);
System.exit(3);
}
}
boolean foreground = System.getProperty("es.foreground", System.getProperty("es-foreground")) != null; boolean foreground = System.getProperty("es.foreground", System.getProperty("es-foreground")) != null;
// handle the wrapper system property, if its a service, don't run as a service // handle the wrapper system property, if its a service, don't run as a service
if (System.getProperty("wrapper.service", "XXX").equalsIgnoreCase("true")) { if (System.getProperty("wrapper.service", "XXX").equalsIgnoreCase("true")) {
foreground = false; foreground = false;
} }
String stage = "Settings";
Settings settings = null; Settings settings = null;
Environment environment = null; Environment environment = null;
try { try {
Tuple<Settings, Environment> tuple = initialSettings(); Tuple<Settings, Environment> tuple = initialSettings();
settings = tuple.v1(); settings = tuple.v1();
environment = tuple.v2(); environment = tuple.v2();
if (environment.pidFile() != null) {
stage = "Pid";
PidFile.create(environment.pidFile(), true);
}
stage = "Logging";
setupLogging(settings, environment); setupLogging(settings, environment);
} catch (Exception e) { } catch (Exception e) {
String errorMessage = buildErrorMessage("Setup", e); String errorMessage = buildErrorMessage(stage, e);
sysError(errorMessage, true); sysError(errorMessage, true);
System.exit(3); System.exit(3);
} }
@ -234,7 +233,7 @@ public class Bootstrap {
logger.warn("jvm uses the client vm, make sure to run `java` with the server vm for best performance by adding `-server` to the command line"); logger.warn("jvm uses the client vm, make sure to run `java` with the server vm for best performance by adding `-server` to the command line");
} }
String stage = "Initialization"; stage = "Initialization";
try { try {
if (!foreground) { if (!foreground) {
Loggers.disableConsoleLogging(); Loggers.disableConsoleLogging();

View File

@ -67,7 +67,9 @@ public class Security {
for (Path path : environment.dataWithClusterFiles()) { for (Path path : environment.dataWithClusterFiles()) {
addPath(policy, path, "read,readlink,write,delete"); addPath(policy, path, "read,readlink,write,delete");
} }
if (environment.pidFile() != null) {
addPath(policy, environment.pidFile().getParent(), "read,readlink,write,delete");
}
return policy; return policy;
} }

View File

@ -52,6 +52,9 @@ public class Environment {
private final Path logsFile; private final Path logsFile;
/** Path to the PID file (can be null if no PID file is configured) **/
private final Path pidFile;
/** List of filestores on the system */ /** List of filestores on the system */
private static final FileStore[] fileStores; private static final FileStore[] fileStores;
@ -106,6 +109,12 @@ public class Environment {
} else { } else {
logsFile = homeFile.resolve("logs"); logsFile = homeFile.resolve("logs");
} }
if (settings.get("pidfile") != null) {
pidFile = PathUtils.get(cleanPath(settings.get("pidfile")));
} else {
pidFile = null;
}
} }
/** /**
@ -151,6 +160,13 @@ public class Environment {
return logsFile; return logsFile;
} }
/**
* The PID file location (can be null if no PID file is configured)
*/
public Path pidFile() {
return pidFile;
}
/** /**
* Looks up the filestore associated with a Path. * Looks up the filestore associated with a Path.
* <p> * <p>

View File

@ -70,6 +70,7 @@ public class SecurityTests extends ElasticsearchTestCase {
settingsBuilder.put("path.plugins", path.resolve("plugins").toString()); settingsBuilder.put("path.plugins", path.resolve("plugins").toString());
settingsBuilder.putArray("path.data", path.resolve("data1").toString(), path.resolve("data2").toString()); settingsBuilder.putArray("path.data", path.resolve("data1").toString(), path.resolve("data2").toString());
settingsBuilder.put("path.logs", path.resolve("logs").toString()); settingsBuilder.put("path.logs", path.resolve("logs").toString());
settingsBuilder.put("pidfile", path.resolve("test.pid").toString());
Settings settings = settingsBuilder.build(); Settings settings = settingsBuilder.build();
Environment environment = new Environment(settings); Environment environment = new Environment(settings);
@ -105,5 +106,7 @@ public class SecurityTests extends ElasticsearchTestCase {
assertTrue(permissions.implies(new FilePermission(fakeTmpDir.toString(), "read,readlink,write,delete"))); assertTrue(permissions.implies(new FilePermission(fakeTmpDir.toString(), "read,readlink,write,delete")));
// double check we overwrote java.io.tmpdir correctly for the test // double check we overwrote java.io.tmpdir correctly for the test
assertFalse(permissions.implies(new FilePermission(realTmpDir.toString(), "read"))); assertFalse(permissions.implies(new FilePermission(realTmpDir.toString(), "read")));
// PID file: r/w
assertTrue(permissions.implies(new FilePermission(environment.pidFile().toString(), "read,readlink,write,delete")));
} }
} }