Skip to content

Commit

Permalink
0005515: When installing service for systemd, make sure to specify
Browse files Browse the repository at this point in the history
absolute path to java executable when creating systemd control file
  • Loading branch information
Philip Marzullo committed Oct 5, 2022
1 parent d1b74a3 commit 0e5488d
Showing 1 changed file with 46 additions and 1 deletion.
Expand Up @@ -82,13 +82,25 @@ private void installSystemd() {
try (FileWriter writer = new FileWriter(runFile);
BufferedReader reader = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream(
"/symmetricds.systemd")))) {
String javaCommand = config.getJavaCommand();
if (javaCommand == null) {
javaCommand = "java";
}
if (! javaCommand.startsWith(File.separator)) {
if ( ! javaCommand.contains(File.separator)) {
javaCommand = getAbsolutePath(javaCommand);
} else {
String cwd = System.getProperty("user.dir");
javaCommand = cwd + File.separator + javaCommand;
}
}
String line = null;
while ((line = reader.readLine()) != null) {
line = line.replaceAll("\\$\\{wrapper.description}", config.getDescription());
line = line.replaceAll("\\$\\{wrapper.pidfile}", getWrapperPidFile());
line = line.replaceAll("\\$\\{wrapper.home}", config.getWorkingDirectory().getAbsolutePath());
line = line.replaceAll("\\$\\{wrapper.jarfile}", config.getWrapperJarPath());
line = line.replaceAll("\\$\\{wrapper.java.command}", config.getJavaCommand());
line = line.replaceAll("\\$\\{wrapper.java.command}", javaCommand);
line = line.replaceAll("\\$\\{wrapper.run.as.user}",
config.getRunAsUser() == null || config.getRunAsUser().length() == 0 ? "root" : config.getRunAsUser());
writer.write(line + "\n");
Expand Down Expand Up @@ -408,6 +420,39 @@ private int getuid(String login) {
}
return ret;
}

private String getAbsolutePath(String command) {
String ret = command;
List<String> cmd = new ArrayList<String>();
cmd.add("which");
cmd.add(command);
ProcessBuilder pb = new ProcessBuilder(cmd);
pb.redirectErrorStream(true);
Process process = null;
try {
process = pb.start();
process.waitFor();
} catch (Exception e) {
System.err.println(e.getMessage());
e.printStackTrace();
}
if (process != null) {
ArrayList<String> cmdOutput = new ArrayList<String>();
try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
String line = null;
while ((line = reader.readLine()) != null) {
cmdOutput.add(line);
}
} catch (Exception e) {
e.printStackTrace();
throw new WrapperException(Constants.RC_FAIL_EXECUTION, 0, "Unable to read from command: " + cmd, e);
}
if (cmdOutput != null && cmdOutput.size() > 0) {
ret = cmdOutput.get(0);
}
}
return ret;
}

@Override
protected void stopProcesses(boolean isStopAbandoned) {
Expand Down

0 comments on commit 0e5488d

Please sign in to comment.