001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.reef.util; 020 021import java.io.IOException; 022import java.nio.charset.StandardCharsets; 023import java.util.logging.Level; 024import java.util.logging.Logger; 025 026/** 027 * OS utils. 028 */ 029public final class OSUtils { 030 private static final Logger LOG = Logger.getLogger(OSUtils.class.getName()); 031 032 private OSUtils() { 033 } 034 035 /** 036 * Determines whether the current JVM is running on the Windows OS. 037 * 038 * @return true, if the JVM is running on Windows. false, otherwise 039 */ 040 public static boolean isWindows() { 041 return System.getProperty("os.name").toLowerCase().contains("windows"); 042 } 043 044 /** 045 * Determines whether the current JVM is running on the Unix-based OS. 046 * 047 * @return true, if the JVM is running on Linux/Mac. false, otherwise 048 */ 049 public static boolean isUnix() { 050 return isLinux() || isMac(); 051 } 052 053 /** 054 * Determines whether the current JVM is running on the Linux OS. 055 * 056 * @return true, if the JVM is running on Linux. false, otherwise 057 */ 058 public static boolean isLinux() { 059 return System.getProperty("os.name").toLowerCase().contains("linux"); 060 } 061 062 /** 063 * Determines whether the current JVM is running on the Mac OS. 064 * 065 * @return true, if the JVM is running on Mac. false, otherwise 066 */ 067 public static boolean isMac() { 068 return System.getProperty("os.name").toLowerCase().contains("mac"); 069 } 070 071 /** 072 * @return the process ID of the JVM, if running on Linux/Windows. This returns -1 for other OSs. 073 */ 074 public static long getPID() { 075 if (isUnix()) { 076 try { 077 final Process process = new ProcessBuilder() 078 .command("bash", "-c", "echo $PPID") 079 .start(); 080 final byte[] returnBytes = new byte[128]; 081 if (process.getInputStream().read(returnBytes) == -1) { 082 LOG.log(Level.FINE, "No data read because end of stream was reached"); 083 } 084 final Long result = Long.valueOf(new String(returnBytes, StandardCharsets.UTF_8).trim()); 085 process.destroy(); 086 return result; 087 } catch (final Exception e) { 088 LOG.log(Level.SEVERE, "Unable to determine PID", e); 089 return -1; 090 } 091 } else if (isWindows()) { 092 try { 093 final Process process = new ProcessBuilder() 094 .command("powershell.exe", "-NoProfile", "-Command", 095 "wmic process where processid=$pid get parentprocessid") 096 .start(); 097 final byte[] returnBytes = new byte[128]; 098 if (process.getInputStream().read(returnBytes) == -1) { 099 LOG.log(Level.FINE, "No data read because end of stream was reached"); 100 } 101 final Long result = Long.valueOf(new String(returnBytes, StandardCharsets.UTF_8).split("\n")[1].trim()); 102 process.destroy(); 103 return result; 104 } catch (final Exception e) { 105 LOG.log(Level.SEVERE, "Unable to determine PID", e); 106 return -1; 107 } 108 } else { 109 return -1; 110 } 111 } 112 113 /** 114 * Kill the process. 115 * 116 * @param pid Process id 117 * @throws IOException 118 */ 119 public static void kill(final long pid) throws IOException, InterruptedException { 120 if (isUnix()) { 121 final Process process = new ProcessBuilder() 122 .command("bash", "-c", "kill", "-9", String.valueOf(pid)) 123 .start(); 124 final int returnCode = process.waitFor(); 125 LOG.fine("Kill returned: " + returnCode); 126 } else if (isWindows()) { 127 final Process process = new ProcessBuilder() 128 .command("taskkill.exe", "/f", "/pid", String.valueOf(pid)) 129 .start(); 130 final int returnCode = process.waitFor(); 131 LOG.fine("Kill returned: " + returnCode); 132 } else { 133 throw new UnsupportedOperationException("Unable to execute kill on unknown OS"); 134 } 135 } 136 137 /** 138 * Formats the given variable for expansion by Windows (<code>%VARIABLE%</code>) or Linux (<code>$VARIABLE</code>). 139 * 140 * @param variableName 141 * @return 142 */ 143 public static String formatVariable(final String variableName) { 144 if (isWindows()) { 145 return "%" + variableName + "%"; 146 } else { 147 return "$" + variableName; 148 } 149 } 150 151}