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 java.io.IOException;
022import java.util.Collections;
023import java.util.List;
024import java.util.regex.Matcher;
025import java.util.regex.Pattern;
026
027import org.apache.commons.io.IOUtils;
028
029public class SolarisProcessManager extends UnixProcessManager {
030
031    protected static final String SOLARIS_11 = "5.11";
032
033    protected static final String SOLARIS_10 = "5.10";
034
035    protected static final String[] SOLARIS_11_PS = { "/usr/bin/ps", "auxww" };
036
037    protected static final String[] SOLARIS_10_PS = { "/usr/ucb/ps", "auxww" };
038
039    protected static final Pattern PS_OUTPUT_LINE = Pattern.compile("^" + "[^\\s]+\\s+" // USER
040            + "([0-9]+)\\s+" // PID
041            + "[0-9.\\s]+" // %CPU %MEM SZ RSS (may be collapsed)
042            + "[^\\s]+\\s+" // TT (no starting digit)
043            + "[^\\s]+\\s+" // S
044            + "[^\\s]+\\s+" // START
045            + "[^\\s]+\\s+" // TIME
046            + "(.*)$" // COMMAND
047    );
048
049    protected String solarisVersion;
050
051    public String getSolarisVersion() {
052        if (solarisVersion == null) {
053            List<String> lines;
054            try {
055                lines = execute(new String[] { "/usr/bin/uname", "-r" });
056            } catch (IOException e) {
057                lines = Collections.emptyList();
058            }
059            if (lines.isEmpty()) {
060                solarisVersion = "?";
061            } else {
062                solarisVersion = lines.get(0).trim();
063            }
064        }
065        return solarisVersion;
066    }
067
068    @Override
069    protected String[] psCommand() {
070        if (SOLARIS_11.equals(getSolarisVersion())) {
071            return SOLARIS_11_PS;
072        }
073        return null;
074    }
075
076    public Matcher getLineMatcher(String line) {
077        return PS_OUTPUT_LINE.matcher(line);
078    }
079
080    @Override
081    public String findPid(String regex) throws IOException {
082        if (SOLARIS_11.equals(getSolarisVersion())) {
083            Pattern commandPattern = Pattern.compile(regex);
084            for (String line : execute(psCommand())) {
085                Matcher lineMatcher = getLineMatcher(line);
086                if (lineMatcher.matches()) {
087                    String pid = lineMatcher.group(1);
088                    String command = lineMatcher.group(2);
089                    Matcher commandMatcher = commandPattern.matcher(command);
090                    if (commandMatcher.find()) {
091                        return pid;
092                    }
093                }
094            }
095        } else {
096            throw new RuntimeException("Unsupported Solaris version: " + solarisVersion);
097        }
098        return null;
099    }
100
101    protected List<String> execute(String... command) throws IOException {
102        Process process = new ProcessBuilder(command).start();
103        List<String> lines = IOUtils.readLines(process.getInputStream());
104        return lines;
105    }
106
107}