private void diagnoseFailedLoad(Exception cause) {
        Process proc = process.getProcess();

        try {
            int val = proc.exitValue();
            new RuntimeException("Jenkins died loading. Exit code " + val, cause);
        } catch (IllegalThreadStateException _) {
            // Process alive
        }

        // Try to get stacktrace
        Class<?> clazz;
        Field pidField;
        try {
            clazz = Class.forName("java.lang.UNIXProcess");
            pidField = clazz.getDeclaredField("pid");
            pidField.setAccessible(true);
        } catch (Exception e) {
            LinkageError x = new LinkageError();
            x.initCause(e);
            throw x;
        }

        if (clazz.isAssignableFrom(proc.getClass())) {
            int pid;
            try {
                pid = (int) pidField.get(proc);
            } catch (IllegalArgumentException | IllegalAccessException e) {
                throw new AssertionError(e);
            }

            try {
                Process jstack = new ProcessBuilder("jstack", String.valueOf(pid)).start();
                if (jstack.waitFor() == 0) {
                    StringWriter writer = new StringWriter();
                    IOUtils.copy(jstack.getInputStream(), writer);
                    RuntimeException ex = new RuntimeException(
                            cause.getMessage() + "\n\n" + writer.toString()
                    );
                    ex.setStackTrace(cause.getStackTrace());
                    throw ex;
                }
            } catch (IOException | InterruptedException e) {
                throw new AssertionError(e);
            }
        }

        throw new Error(cause);
    }
Пример #2
0
  /**
   * Checks if address can be reached using one argument InetAddress.isReachable() version or ping
   * command if failed.
   *
   * @param addr Address to check.
   * @param reachTimeout Timeout for the check.
   * @return {@code True} if address is reachable.
   */
  public static boolean reachableByPing(InetAddress addr, int reachTimeout) {
    try {
      if (addr.isReachable(reachTimeout)) return true;

      String cmd = String.format("ping -%s 1 %s", U.isWindows() ? "n" : "c", addr.getHostAddress());

      Process myProc = Runtime.getRuntime().exec(cmd);

      myProc.waitFor();

      return myProc.exitValue() == 0;
    } catch (IOException ignore) {
      return false;
    } catch (InterruptedException ignored) {
      Thread.currentThread().interrupt();

      return false;
    }
  }