Skip to content
This repository has been archived by the owner on Jan 11, 2024. It is now read-only.

Commit

Permalink
Fixed Issue #35. Process exec streams not closed properly.
Browse files Browse the repository at this point in the history
  • Loading branch information
savageautomate committed Feb 16, 2013
1 parent d176694 commit 43381b7
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 76 deletions.
49 changes: 8 additions & 41 deletions pi4j-core/src/main/java/com/pi4j/system/NetworkInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,74 +28,41 @@
*/


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import com.pi4j.util.ExecUtil;

public class NetworkInfo {

// private constructor
private NetworkInfo() {
// forbid object construction
}

private static String[] executeCommand(String command) throws IOException, InterruptedException {
return executeCommand(command, null);
}

private static String[] executeCommand(String command, String split) throws IOException, InterruptedException {
List<String> result = new ArrayList<String>();
Process p = Runtime.getRuntime().exec(command);
p.waitFor();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = reader.readLine();
while (line != null) {
if (!line.isEmpty()) {
if (split == null || split.isEmpty()) {
result.add(line.trim());
} else {
String[] parts = line.trim().split(split);
for(String part : parts) {
if (part != null && !part.isEmpty()) {
result.add(part.trim());
}
}
}
}
line = reader.readLine();
}

if (result.size() > 0) {
return (String[])result.toArray(new String[0]);
} else {
return new String[0];
}
}

public static String getHostname() throws IOException, InterruptedException {
return executeCommand("hostname --short")[0];
return ExecUtil.execute("hostname --short")[0];
}

public static String getFQDN() throws IOException, InterruptedException {
return executeCommand("hostname --fqdn")[0];
return ExecUtil.execute("hostname --fqdn")[0];
}

public static String[] getIPAddresses() throws IOException, InterruptedException {
return executeCommand("hostname --all-ip-addresses", " ");
return ExecUtil.execute("hostname --all-ip-addresses", " ");
}

public static String getIPAddress() throws IOException, InterruptedException {
return executeCommand("hostname --ip-address")[0];
return ExecUtil.execute("hostname --ip-address")[0];
}

public static String[] getFQDNs() throws IOException, InterruptedException {
return executeCommand("hostname --all-fqdns", " ");
return ExecUtil.execute("hostname --all-fqdns", " ");
}

public static String[] getNameservers() throws IOException, InterruptedException {
String[] nameservers = executeCommand("cat /etc/resolv.conf");
String[] nameservers = ExecUtil.execute("cat /etc/resolv.conf");
List<String> result = new ArrayList<String>();
for (String nameserver : nameservers) {
if (nameserver.startsWith("nameserver")) {
Expand Down
52 changes: 17 additions & 35 deletions pi4j-core/src/main/java/com/pi4j/system/SystemInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,13 @@
*/


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.HashMap;
import java.util.Map;

import com.pi4j.util.ExecUtil;
import com.pi4j.util.StringUtil;

public class SystemInfo {
Expand All @@ -51,16 +50,14 @@ private static String getCpuInfo(String target) throws IOException, InterruptedE
// if the CPU data has not been previously acquired, then acquire it now
if (cpuInfo == null) {
cpuInfo = new HashMap<String, String>();
Process p = Runtime.getRuntime().exec("cat /proc/cpuinfo");
p.waitFor();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = reader.readLine();
while (line != null) {
String parts[] = line.split(":", 2);
if (parts.length >= 2 && !parts[0].trim().isEmpty() && !parts[1].trim().isEmpty()) {
cpuInfo.put(parts[0].trim(), parts[1].trim());
String result[] = ExecUtil.execute("cat /proc/cpuinfo");
if(result != null){
for(String line : result) {
String parts[] = line.split(":", 2);
if (parts.length >= 2 && !parts[0].trim().isEmpty() && !parts[1].trim().isEmpty()) {
cpuInfo.put(parts[0].trim(), parts[1].trim());
}
}
line = reader.readLine();
}
}

Expand Down Expand Up @@ -179,19 +176,11 @@ public Boolean run() {
private static String getBashVersionInfo() {
String versionInfo = "";
try {

String cmd = "bash --version";
Process p = Runtime.getRuntime().exec(cmd);
p.waitFor();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = reader.readLine();
if(p.exitValue() == 0) {
while(line != null) {
if(!line.isEmpty()) {
versionInfo = line; // return only first output line of version info
break;
}
line = reader.readLine();
String result[] = ExecUtil.execute("bash --version");
for(String line : result) {
if(!line.isEmpty()) {
versionInfo = line; // return only first output line of version info
break;
}
}
}
Expand All @@ -218,28 +207,21 @@ private static boolean hasReadElfTag(String tag) {
private static String getReadElfTag(String tag) {
String tagValue = null;
try {
String cmd = "/usr/bin/readelf -A /proc/self/exe";
Process p = Runtime.getRuntime().exec(cmd);
p.waitFor();
if(p.exitValue() == 0) {
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = reader.readLine();
while(line != null) {
String result[] = ExecUtil.execute("/usr/bin/readelf -A /proc/self/exe");
if(result != null){
for(String line : result) {
line = line.trim();
if (line.startsWith(tag) && line.contains(":")) {
String lineParts[] = line.split(":", 2);
if(lineParts.length > 1)
tagValue = lineParts[1].trim();
break;
}
line = reader.readLine();
}
}
}
}
catch (IOException ioe) { ioe.printStackTrace(); }
catch (InterruptedException ie) { ie.printStackTrace(); }
return tagValue;
}


}
81 changes: 81 additions & 0 deletions pi4j-core/src/main/java/com/pi4j/util/ExecUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package com.pi4j.util;

/*
* #%L
* **********************************************************************
* ORGANIZATION : Pi4J
* PROJECT : Pi4J :: Java Library (Core)
* FILENAME : ExecUtil.java
*
* This file is part of the Pi4J project. More information about
* this project can be found here: http://www.pi4j.com/
* **********************************************************************
* %%
* Copyright (C) 2012 - 2013 Pi4J
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class ExecUtil
{
public static String[] execute(String command) throws IOException, InterruptedException {
return execute(command, null);
}

public static String[] execute(String command, String split) throws IOException, InterruptedException {
List<String> result = new ArrayList<String>();
Process p = Runtime.getRuntime().exec(command);
p.waitFor();

if(p.exitValue() != 0)
return null;

InputStreamReader isr = new InputStreamReader(p.getInputStream());
BufferedReader reader = new BufferedReader(isr);
String line = reader.readLine();
while (line != null) {
if (!line.isEmpty()) {
if (split == null || split.isEmpty()) {
result.add(line.trim());
} else {
String[] parts = line.trim().split(split);
for(String part : parts) {
if (part != null && !part.isEmpty()) {
result.add(part.trim());
}
}
}
}
line = reader.readLine();
}

// close readers and stream
reader.close();
isr.close();
p.getInputStream().close();

if (result.size() > 0) {
return (String[])result.toArray(new String[0]);
} else {
return new String[0];
}
}
}

0 comments on commit 43381b7

Please sign in to comment.