Search in sources :

Example 1 with SafeProcess

use of org.eclipse.kura.core.util.SafeProcess in project kura by eclipse.

the class InstallImpl method installDp.

public void installDp(DeploymentPackageInstallOptions options, File dpFile) throws KuraException {
    SafeProcess proc = null;
    try {
        installDeploymentPackageInternal(dpFile);
        installCompleteAsync(options, dpFile.getName());
        s_logger.info("Install completed!");
        if (options.isReboot()) {
            Thread.sleep(options.getRebootDelay());
            proc = ProcessUtil.exec("reboot");
        }
    } catch (Exception e) {
        s_logger.info("Install failed!");
        installFailedAsync(options, dpFile.getName(), e);
    } finally {
        if (proc != null) {
            ProcessUtil.destroy(proc);
        }
    }
}
Also used : SafeProcess(org.eclipse.kura.core.util.SafeProcess) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) DeploymentException(org.osgi.service.deploymentadmin.DeploymentException) KuraException(org.eclipse.kura.KuraException)

Example 2 with SafeProcess

use of org.eclipse.kura.core.util.SafeProcess in project kura by eclipse.

the class InstallImpl method installSh.

public void installSh(DeploymentPackageOptions options, File shFile) throws KuraException {
    updateInstallPersistance(shFile.getName(), options);
    // Script Exec
    SafeProcess proc = null;
    try {
        proc = ProcessUtil.exec("chmod 700 " + shFile.getCanonicalPath());
    } catch (IOException e) {
        throw new KuraException(KuraErrorCode.INTERNAL_ERROR);
    } finally {
        if (proc != null) {
            ProcessUtil.destroy(proc);
        }
    }
    SafeProcess proc2 = null;
    try {
        proc2 = ProcessUtil.exec(shFile.getCanonicalPath());
    } catch (IOException e) {
        throw new KuraException(KuraErrorCode.INTERNAL_ERROR);
    } finally {
        if (proc2 != null) {
            ProcessUtil.destroy(proc2);
        }
    }
}
Also used : KuraException(org.eclipse.kura.KuraException) SafeProcess(org.eclipse.kura.core.util.SafeProcess) IOException(java.io.IOException)

Example 3 with SafeProcess

use of org.eclipse.kura.core.util.SafeProcess in project kura by eclipse.

the class IptablesConfig method save.

/*
     * Saves (using iptables-save) the current iptables config into /etc/sysconfig/iptables
     */
public static void save() throws KuraException {
    SafeProcess proc = null;
    BufferedReader br = null;
    PrintWriter out = null;
    try {
        int status = -1;
        proc = ProcessUtil.exec("iptables-save");
        status = proc.waitFor();
        if (status != 0) {
            s_logger.error("save() :: failed - {}", LinuxProcessUtil.getInputStreamAsString(proc.getErrorStream()));
            throw new KuraException(KuraErrorCode.INTERNAL_ERROR, "Failed to execute the iptable-save command");
        }
        String line = null;
        br = new BufferedReader(new InputStreamReader(proc.getInputStream()));
        out = new PrintWriter(FIREWALL_CONFIG_FILE_NAME);
        while ((line = br.readLine()) != null) {
            out.println(line);
        }
        s_logger.debug("iptablesSave() :: completed!, status={}", status);
    } catch (Exception e) {
        throw new KuraException(KuraErrorCode.INTERNAL_ERROR, e);
    } finally {
        if (out != null) {
            out.flush();
            out.close();
        }
        if (br != null) {
            try {
                br.close();
            } catch (IOException e) {
                s_logger.error("iptablesSave() :: failed to close BufferedReader - {}", e);
            }
        }
        if (proc != null) {
            ProcessUtil.destroy(proc);
        }
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) KuraException(org.eclipse.kura.KuraException) SafeProcess(org.eclipse.kura.core.util.SafeProcess) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) IOException(java.io.IOException) KuraException(org.eclipse.kura.KuraException) PrintWriter(java.io.PrintWriter)

Example 4 with SafeProcess

use of org.eclipse.kura.core.util.SafeProcess in project kura by eclipse.

the class LinuxNetworkUtil method getWifiBitrate.

/*
     * Returns 0 if the interface is not found or on error
     */
public static long getWifiBitrate(String ifaceName) throws KuraException {
    long bitRate = 0;
    // ignore logical interfaces like "1-1.2"
    if (Character.isDigit(ifaceName.charAt(0))) {
        return bitRate;
    }
    SafeProcess proc = null;
    BufferedReader br = null;
    String line = null;
    try {
        if (toolExists("iw")) {
            // start the process
            proc = ProcessUtil.exec("iw dev " + ifaceName + " link");
            if (proc.waitFor() != 0) {
                s_logger.warn("error executing command --- iw --- exit value = {}", proc.exitValue());
                // FIXME: why don't we fallback to iwconfig like in the case of getWifiMode()?
                return bitRate;
            } else {
                // get the output
                br = new BufferedReader(new InputStreamReader(proc.getInputStream()));
                line = null;
                while ((line = br.readLine()) != null) {
                    int index = line.indexOf("tx bitrate: ");
                    if (index > -1) {
                        s_logger.debug("line: " + line);
                        StringTokenizer st = new StringTokenizer(line.substring(index));
                        // skip 'tx'
                        st.nextToken();
                        // skip 'bitrate:'
                        st.nextToken();
                        Double rate = Double.parseDouble(st.nextToken());
                        int mult = 1;
                        String unit = st.nextToken();
                        if (unit.startsWith("kb")) {
                            mult = 1000;
                        } else if (unit.startsWith("Mb")) {
                            mult = 1000000;
                        } else if (unit.startsWith("Gb")) {
                            mult = 1000000000;
                        }
                        bitRate = (long) (rate * mult);
                        return bitRate;
                    }
                }
            }
        } else if (toolExists("iwconfig")) {
            // start the process
            proc = ProcessUtil.exec("iwconfig " + ifaceName);
            if (proc.waitFor() != 0) {
                s_logger.warn("error executing command --- iwconfig --- exit value = {}", proc.exitValue());
                // FIXME: throw exception
                return bitRate;
            }
            // get the output
            br = new BufferedReader(new InputStreamReader(proc.getInputStream()));
            line = null;
            while ((line = br.readLine()) != null) {
                int index = line.indexOf("Bit Rate=");
                if (index > -1) {
                    s_logger.debug("line: {}", line);
                    StringTokenizer st = new StringTokenizer(line.substring(index));
                    // skip 'Bit'
                    st.nextToken();
                    Double rate = Double.parseDouble(st.nextToken().substring(5));
                    int mult = 1;
                    String unit = st.nextToken();
                    if (unit.startsWith("kb")) {
                        mult = 1000;
                    } else if (unit.startsWith("Mb")) {
                        mult = 1000000;
                    } else if (unit.startsWith("Gb")) {
                        mult = 1000000000;
                    }
                    bitRate = (long) (rate * mult);
                    return bitRate;
                }
            }
        }
    } catch (IOException e) {
        throw new KuraException(KuraErrorCode.INTERNAL_ERROR, e);
    } catch (InterruptedException e) {
        throw new KuraException(KuraErrorCode.INTERNAL_ERROR, e);
    } finally {
        if (br != null) {
            try {
                br.close();
            } catch (IOException ex) {
                s_logger.error("I/O Exception while closing BufferedReader!");
            }
        }
        if (proc != null) {
            ProcessUtil.destroy(proc);
        }
    }
    return bitRate;
}
Also used : StringTokenizer(java.util.StringTokenizer) InputStreamReader(java.io.InputStreamReader) KuraException(org.eclipse.kura.KuraException) SafeProcess(org.eclipse.kura.core.util.SafeProcess) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException)

Example 5 with SafeProcess

use of org.eclipse.kura.core.util.SafeProcess in project kura by eclipse.

the class LinuxNetworkUtil method isKernelModuleLoaded.

public static boolean isKernelModuleLoaded(String interfaceName, WifiMode wifiMode) throws KuraException {
    boolean result = false;
    if (KuraConstants.ReliaGATE_10_05.getTargetName().equals(TARGET_NAME) && "wlan0".equals(interfaceName)) {
        SafeProcess proc = null;
        BufferedReader br = null;
        String cmd = "lsmod";
        try {
            s_logger.debug("Executing '{}'", cmd);
            proc = ProcessUtil.exec(cmd);
            if (proc.waitFor() != 0) {
                throw new KuraException(KuraErrorCode.OS_COMMAND_ERROR, cmd, proc.exitValue());
            }
            // get the output
            br = new BufferedReader(new InputStreamReader(proc.getInputStream()));
            String line = null;
            while ((line = br.readLine()) != null) {
                if (line.contains("bcmdhd")) {
                    result = true;
                    break;
                }
            }
        } catch (IOException e) {
            throw new KuraException(KuraErrorCode.INTERNAL_ERROR, e, "'" + cmd + "' failed");
        } catch (InterruptedException e) {
            throw new KuraException(KuraErrorCode.INTERNAL_ERROR, e, "'" + cmd + "' interrupted");
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    s_logger.warn("Failed to close process input stream", e);
                }
            }
            if (proc != null) {
                proc.destroy();
            }
        }
    }
    return result;
}
Also used : InputStreamReader(java.io.InputStreamReader) KuraException(org.eclipse.kura.KuraException) SafeProcess(org.eclipse.kura.core.util.SafeProcess) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException)

Aggregations

SafeProcess (org.eclipse.kura.core.util.SafeProcess)72 KuraException (org.eclipse.kura.KuraException)61 IOException (java.io.IOException)60 BufferedReader (java.io.BufferedReader)33 InputStreamReader (java.io.InputStreamReader)33 File (java.io.File)8 UnknownHostException (java.net.UnknownHostException)6 StringTokenizer (java.util.StringTokenizer)6 ArrayList (java.util.ArrayList)5 FileNotFoundException (java.io.FileNotFoundException)4 Date (java.util.Date)4 RouteConfig (org.eclipse.kura.net.route.RouteConfig)3 SocketException (java.net.SocketException)2 InvalidKeyException (java.security.InvalidKeyException)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 BadPaddingException (javax.crypto.BadPaddingException)2 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)2 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)2 IP4Address (org.eclipse.kura.net.IP4Address)2 IP6Address (org.eclipse.kura.net.IP6Address)2