001/*
002 * (C) Copyright 2013-2016 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Florent Guillaume
018 */
019package org.nuxeo.launcher.process;
020
021import static java.nio.charset.StandardCharsets.UTF_8;
022
023import java.io.IOException;
024import java.util.Collections;
025import java.util.List;
026import java.util.regex.Matcher;
027import java.util.regex.Pattern;
028
029import org.apache.commons.io.IOUtils;
030
031public class SolarisProcessManager extends UnixProcessManager {
032
033    protected static final String SOLARIS_11 = "5.11";
034
035    protected static final String SOLARIS_10 = "5.10";
036
037    protected static final String[] SOLARIS_11_PS = { "/usr/bin/ps", "auxww" };
038
039    protected static final String[] SOLARIS_10_PS = { "/usr/ucb/ps", "auxww" };
040
041    protected static final Pattern PS_OUTPUT_LINE = Pattern.compile("^" + "[^\\s]+\\s+" // USER
042            + "([0-9]+)\\s+" // PID
043            + "[0-9.\\s]+" // %CPU %MEM SZ RSS (may be collapsed)
044            + "[^\\s]+\\s+" // TT (no starting digit)
045            + "[^\\s]+\\s+" // S
046            + "[^\\s]+\\s+" // START
047            + "[^\\s]+\\s+" // TIME
048            + "(.*)$" // COMMAND
049    );
050
051    protected String solarisVersion;
052
053    public String getSolarisVersion() {
054        if (solarisVersion == null) {
055            List<String> lines;
056            try {
057                lines = execute(new String[] { "/usr/bin/uname", "-r" });
058            } catch (IOException e) {
059                lines = Collections.emptyList();
060            }
061            if (lines.isEmpty()) {
062                solarisVersion = "?";
063            } else {
064                solarisVersion = lines.get(0).trim();
065            }
066        }
067        return solarisVersion;
068    }
069
070    @Override
071    protected String[] psCommand() {
072        if (SOLARIS_11.equals(getSolarisVersion())) {
073            return SOLARIS_11_PS;
074        }
075        return null;
076    }
077
078    public Matcher getLineMatcher(String line) {
079        return PS_OUTPUT_LINE.matcher(line);
080    }
081
082    @Override
083    public String findPid(String regex) throws IOException {
084        if (SOLARIS_11.equals(getSolarisVersion())) {
085            Pattern commandPattern = Pattern.compile(regex);
086            for (String line : execute(psCommand())) {
087                Matcher lineMatcher = getLineMatcher(line);
088                if (lineMatcher.matches()) {
089                    String pid = lineMatcher.group(1);
090                    String command = lineMatcher.group(2);
091                    Matcher commandMatcher = commandPattern.matcher(command);
092                    if (commandMatcher.find()) {
093                        return pid;
094                    }
095                }
096            }
097        } else {
098            throw new RuntimeException("Unsupported Solaris version: " + solarisVersion);
099        }
100        return null;
101    }
102
103    protected List<String> execute(String... command) throws IOException {
104        Process process = new ProcessBuilder(command).start();
105        return IOUtils.readLines(process.getInputStream(), UTF_8);
106    }
107
108}