ProcessUtil { public static void pidCheck(String pidDir, String run) throws ConfigurationException { String dir = pidDir == null ? "/var/run" : pidDir; try { final File propsFile = PropertiesUtil.findConfigFile("environment.properties"); if (propsFile == null) { s_logger.debug("environment.properties could not be opened"); } else { final Properties props = PropertiesUtil.loadFromFile(propsFile); dir = props.getProperty("paths.pid"); if (dir == null) { dir = pidDir == null ? "/var/run" : pidDir; } } } catch (IOException e) { s_logger.debug("environment.properties could not be opened"); } final File pidFile = new File(dir + File.separator + run); try { if (!pidFile.createNewFile()) { if (!pidFile.exists()) { throw new ConfigurationException("Unable to write to " + pidFile.getAbsolutePath() + ". Are you sure you're running as root?"); } final String pidLine = FileUtils.readFileToString(pidFile).trim(); if (pidLine.isEmpty()) { throw new ConfigurationException("Java process is being started twice. If this is not true, remove " + pidFile.getAbsolutePath()); } try { final long pid = Long.parseLong(pidLine); final Script script = new Script("bash", 120000, s_logger); script.add("-c", "ps -p " + pid); final String result = script.execute(); if (result == null) { throw new ConfigurationException("Java process is being started twice. If this is not true, remove " + pidFile.getAbsolutePath()); } if (!pidFile.delete()) { throw new ConfigurationException("Java process is being started twice. If this is not true, remove " + pidFile.getAbsolutePath()); } if (!pidFile.createNewFile()) { throw new ConfigurationException("Java process is being started twice. If this is not true, remove " + pidFile.getAbsolutePath()); } } catch (final NumberFormatException e) { throw new ConfigurationException("Java process is being started twice. If this is not true, remove " + pidFile.getAbsolutePath()); } } pidFile.deleteOnExit(); final Script script = new Script("bash", 120000, s_logger); script.add("-c", "echo $PPID"); final OutputInterpreter.OneLineParser parser = new OutputInterpreter.OneLineParser(); script.execute(parser); final String pid = parser.getLine(); FileUtils.writeStringToFile(pidFile, pid + "\n"); } catch (final IOException e) { throw new CloudRuntimeException("Unable to create the " + pidFile.getAbsolutePath() + ". Are you running as root?", e); } }  static void pidCheck(String pidDir, String run); static String dumpStack();  }
@Test public void pidCheck() throws ConfigurationException, IOException { Assume.assumeTrue(SystemUtils.IS_OS_LINUX); FileUtils.writeStringToFile(pidFile, "123456\n"); ProcessUtil.pidCheck(pidFile.getParent(), pidFile.getName()); String pidStr = FileUtils.readFileToString(pidFile); Assert.assertFalse("pid can not be blank", pidStr.isEmpty()); int pid = Integer.parseInt(pidStr.trim()); int maxPid = Integer.parseInt(FileUtils.readFileToString(new File("/proc/sys/kernel/pid_max")).trim()); Assert.assertTrue(pid <= maxPid); }
@Test(expected = ConfigurationException.class) public void pidCheckBadPid() throws ConfigurationException, IOException { Assume.assumeTrue(SystemUtils.IS_OS_LINUX); FileUtils.writeStringToFile(pidFile, "intentionally not number"); ProcessUtil.pidCheck(pidFile.getParent(), pidFile.getName()); }
@Test(expected = ConfigurationException.class) public void pidCheckEmptyPid() throws ConfigurationException, IOException { Assume.assumeTrue(SystemUtils.IS_OS_LINUX); FileUtils.writeStringToFile(pidFile, "intentionally not number"); ProcessUtil.pidCheck(pidFile.getParent(), pidFile.getName()); }