2
* Copyright (C) 2014-2015 Canonical, Ltd.
4
* This program is free software: you can redistribute it and/or modify it under
5
* the terms of the GNU Lesser General Public License version 3, as published by
6
* the Free Software Foundation.
8
* This program is distributed in the hope that it will be useful, but WITHOUT
9
* ANY WARRANTY; without even the implied warranties of MERCHANTABILITY,
10
* SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11
* Lesser General Public License for more details.
13
* You should have received a copy of the GNU Lesser General Public License
14
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17
#include "proc_info.h"
21
#include <QRegularExpression>
26
ProcInfo::~ProcInfo() {
29
std::unique_ptr<ProcInfo::CommandLine> ProcInfo::commandLine(pid_t pid) {
30
QFile cmdline(QString("/proc/%1/cmdline").arg(pid));
31
if (!cmdline.open(QIODevice::ReadOnly | QIODevice::Text)) {
35
return std::unique_ptr<CommandLine>(new CommandLine{ cmdline.readLine().replace('\0', ' ') });
37
QStringList ProcInfo::CommandLine::asStringList() const {
38
return QString(m_command.data()).split(' ');
41
bool ProcInfo::CommandLine::startsWith(char const* prefix) const {
42
return m_command.startsWith(prefix);
45
bool ProcInfo::CommandLine::contains(char const* prefix) const {
46
return m_command.contains(prefix);
49
QString ProcInfo::CommandLine::getParameter(const char* name) const {
50
QString pattern = QRegularExpression::escape(name) + "(\\S+)";
51
QRegularExpression regExp(pattern);
52
QRegularExpressionMatch regExpMatch = regExp.match(m_command);
54
if (!regExpMatch.hasMatch()) {
58
return QString(regExpMatch.captured(1));