Util: Consolidate linux service detection code

This commit is contained in:
zzz
2016-11-06 00:49:34 +00:00
parent 11e86110e7
commit e8de1daf65
6 changed files with 21 additions and 10 deletions

View File

@ -201,7 +201,7 @@ public class ConfigServiceHandler extends FormHandler {
*/ */
public boolean shouldShowSystray() { public boolean shouldShowSystray() {
return ! return !
(RouterConsoleRunner.DAEMON_USER.equals(System.getProperty("user.name")) || (SystemVersion.isLinuxService() ||
(SystemVersion.isWindows() && _context.hasWrapper() && WrapperManager.isLaunchedAsService()) || (SystemVersion.isWindows() && _context.hasWrapper() && WrapperManager.isLaunchedAsService()) ||
// headless=true is forced in i2prouter script to prevent useless dock icon; // headless=true is forced in i2prouter script to prevent useless dock icon;
// must fix this first // must fix this first

View File

@ -131,7 +131,6 @@ public class RouterConsoleRunner implements RouterApp {
private static final int MAX_THREADS = 24; private static final int MAX_THREADS = 24;
private static final int MAX_IDLE_TIME = 90*1000; private static final int MAX_IDLE_TIME = 90*1000;
private static final String THREAD_NAME = "RouterConsole Jetty"; private static final String THREAD_NAME = "RouterConsole Jetty";
public static final String DAEMON_USER = "i2psvc";
public static final String PROP_DTG_ENABLED = "desktopgui.enabled"; public static final String PROP_DTG_ENABLED = "desktopgui.enabled";
/** /**
@ -273,7 +272,7 @@ public class RouterConsoleRunner implements RouterApp {
private void startTrayApp() { private void startTrayApp() {
// if no permissions, don't even try // if no permissions, don't even try
// isLaunchedAsService() always returns true on Linux // isLaunchedAsService() always returns true on Linux
if (DAEMON_USER.equals(System.getProperty("user.name")) || if (SystemVersion.isLinuxService() ||
(SystemVersion.isWindows() && _context.hasWrapper() && WrapperManager.isLaunchedAsService())) { (SystemVersion.isWindows() && _context.hasWrapper() && WrapperManager.isLaunchedAsService())) {
// required true for jrobin to work // required true for jrobin to work
System.setProperty("java.awt.headless", "true"); System.setProperty("java.awt.headless", "true");

View File

@ -16,6 +16,11 @@ import net.i2p.I2PAppContext;
*/ */
public abstract class SystemVersion { public abstract class SystemVersion {
/*
* @since 0.9.28
*/
public static final String DAEMON_USER = "i2psvc";
private static final boolean _isWin = System.getProperty("os.name").startsWith("Win"); private static final boolean _isWin = System.getProperty("os.name").startsWith("Win");
private static final boolean _isMac = System.getProperty("os.name").startsWith("Mac"); private static final boolean _isMac = System.getProperty("os.name").startsWith("Mac");
private static final boolean _isArm = System.getProperty("os.arch").startsWith("arm"); private static final boolean _isArm = System.getProperty("os.arch").startsWith("arm");
@ -29,6 +34,8 @@ public abstract class SystemVersion {
private static final boolean _isOpenJDK; private static final boolean _isOpenJDK;
private static final boolean _is64; private static final boolean _is64;
private static final boolean _hasWrapper = System.getProperty("wrapper.version") != null; private static final boolean _hasWrapper = System.getProperty("wrapper.version") != null;
private static final boolean _isLinuxService = !_isWin && !_isMac &&
DAEMON_USER.equals(System.getProperty("user.name"));
private static final boolean _oneDotSix; private static final boolean _oneDotSix;
private static final boolean _oneDotSeven; private static final boolean _oneDotSeven;
@ -193,6 +200,13 @@ public abstract class SystemVersion {
return _is64; return _is64;
} }
/*
* @since 0.9.28
*/
public static boolean isLinuxService() {
return _isLinuxService;
}
/** /**
* Identical to android.os.Build.VERSION.SDK_INT. * Identical to android.os.Build.VERSION.SDK_INT.
* For use outside of Android code. * For use outside of Android code.
@ -268,6 +282,7 @@ public abstract class SystemVersion {
System.out.println("Mac : " + isMac()); System.out.println("Mac : " + isMac());
System.out.println("Gentoo : " + isGentoo()); System.out.println("Gentoo : " + isGentoo());
System.out.println("GNU : " + isGNU()); System.out.println("GNU : " + isGNU());
System.out.println("Linux Svc: " + isLinuxService());
System.out.println("OpenJDK : " + isOpenJDK()); System.out.println("OpenJDK : " + isOpenJDK());
System.out.println("Windows : " + isWindows()); System.out.println("Windows : " + isWindows());
System.out.println("Wrapper : " + hasWrapper()); System.out.println("Wrapper : " + hasWrapper());

View File

@ -53,7 +53,6 @@ public class WorkingDir {
private final static String WORKING_DIR_DEFAULT = ".i2p"; private final static String WORKING_DIR_DEFAULT = ".i2p";
private final static String WORKING_DIR_DEFAULT_DAEMON = "i2p-config"; private final static String WORKING_DIR_DEFAULT_DAEMON = "i2p-config";
/** we do a couple of things differently if this is the username */ /** we do a couple of things differently if this is the username */
private final static String DAEMON_USER = "i2psvc";
private static final String PROP_WRAPPER_LOG = "wrapper.logfile"; private static final String PROP_WRAPPER_LOG = "wrapper.logfile";
private static final String DEFAULT_WRAPPER_LOG = "wrapper.log"; private static final String DEFAULT_WRAPPER_LOG = "wrapper.log";
/** Feb 16 2006 */ /** Feb 16 2006 */
@ -93,7 +92,7 @@ public class WorkingDir {
dirf = new SecureDirectory(home, WORKING_DIR_DEFAULT_MAC); dirf = new SecureDirectory(home, WORKING_DIR_DEFAULT_MAC);
} }
} else { } else {
if (DAEMON_USER.equals(System.getProperty("user.name"))) if (SystemVersion.isLinuxService())
dirf = new SecureDirectory(home, WORKING_DIR_DEFAULT_DAEMON); dirf = new SecureDirectory(home, WORKING_DIR_DEFAULT_DAEMON);
else else
dirf = new SecureDirectory(home, WORKING_DIR_DEFAULT); dirf = new SecureDirectory(home, WORKING_DIR_DEFAULT);
@ -306,7 +305,7 @@ public class WorkingDir {
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new SecureFileOutputStream(newFile), "UTF-8"))); out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new SecureFileOutputStream(newFile), "UTF-8")));
out.println("# Modified by I2P User dir migration script"); out.println("# Modified by I2P User dir migration script");
String s = null; String s = null;
boolean isDaemon = DAEMON_USER.equals(System.getProperty("user.name")); boolean isDaemon = SystemVersion.isLinuxService();
while ((s = DataHelper.readLine(in)) != null) { while ((s = DataHelper.readLine(in)) != null) {
// readLine() doesn't strip \r // readLine() doesn't strip \r
if (s.endsWith("\r")) if (s.endsWith("\r"))

View File

@ -53,8 +53,7 @@ public class OOMListener implements I2PThread.OOMEventListener {
// Can't find any System property or wrapper property that gives // Can't find any System property or wrapper property that gives
// you the actual config file path, have to guess // you the actual config file path, have to guess
String path; String path;
if (!SystemVersion.isWindows() && !SystemVersion.isMac() && if (SystemVersion.isLinuxService()) {
"i2psvc".equals(System.getProperty("user.name"))) {
path = "/etc/i2p"; path = "/etc/i2p";
} else { } else {
path = _context.getBaseDir().toString(); path = _context.getBaseDir().toString();

View File

@ -106,8 +106,7 @@ class BloomFilterIVValidator implements IVValidator {
// you the actual config file path, have to guess // you the actual config file path, have to guess
// TODO if !SystemVersion.hasWrapper ... // TODO if !SystemVersion.hasWrapper ...
String path; String path;
if (!SystemVersion.isWindows() && !SystemVersion.isMac() && if (SystemVersion.isLinuxService()) {
"i2psvc".equals(System.getProperty("user.name"))) {
path = "/etc/i2p"; path = "/etc/i2p";
} else { } else {
path = _context.getBaseDir().toString(); path = _context.getBaseDir().toString();