Changeset View
Changeset View
Standalone View
Standalone View
src/lib/util/kprocesslist_unix.cpp
Show First 20 Lines • Show All 83 Lines • ▼ Show 20 Line(s) | 80 | if (endOfPid >= 0 && endOfState >= 0 && endOfUser >= 0) { | |||
---|---|---|---|---|---|
84 | QString command = line.right(line.size()-endOfName-1); | 84 | QString command = line.right(line.size()-endOfName-1); | ||
85 | rc.push_back(KProcessInfo(pid, command, name, user)); | 85 | rc.push_back(KProcessInfo(pid, command, name, user)); | ||
86 | } | 86 | } | ||
87 | } | 87 | } | ||
88 | 88 | | |||
89 | return rc; | 89 | return rc; | ||
90 | } | 90 | } | ||
91 | 91 | | |||
92 | } // unnamed namespace | 92 | bool getProcessInfo(const QString& procId, KProcessInfo& processInfo) | ||
93 | | ||||
94 | // Determine UNIX processes by reading "/proc". Default to ps if | | |||
95 | // it does not exist | | |||
96 | KProcessInfoList KProcessList::processInfoList() | | |||
97 | { | 93 | { | ||
98 | const QDir procDir(QStringLiteral("/proc/")); | 94 | if (!isUnixProcessId(procId)) | ||
95 | return false; | ||||
99 | #ifdef Q_OS_FREEBSD | 96 | #ifdef Q_OS_FREEBSD | ||
100 | QString statusFileName(QStringLiteral("/status")); | 97 | QString statusFileName(QStringLiteral("/status")); | ||
101 | #else | 98 | #else | ||
102 | QString statusFileName(QStringLiteral("/stat")); | 99 | QString statusFileName(QStringLiteral("/stat")); | ||
103 | #endif | 100 | #endif | ||
104 | if (!procDir.exists()) | | |||
105 | return unixProcessListPS(); | | |||
106 | KProcessInfoList rc; | | |||
107 | const QStringList procIds = procDir.entryList(); | | |||
108 | if (procIds.isEmpty()) | | |||
109 | return rc; | | |||
110 | for (const QString &procId : procIds) { | | |||
111 | if (!isUnixProcessId(procId)) | | |||
112 | continue; | | |||
113 | QString filename = QStringLiteral("/proc/"); | 101 | QString filename = QStringLiteral("/proc/"); | ||
114 | filename += procId; | 102 | filename += procId; | ||
115 | filename += statusFileName; | 103 | filename += statusFileName; | ||
116 | QFile file(filename); | 104 | QFile file(filename); | ||
117 | if (!file.open(QIODevice::ReadOnly)) | 105 | if (!file.open(QIODevice::ReadOnly)) | ||
118 | continue; // process may have exited | 106 | return false; // process may have exited | ||
119 | 107 | | |||
120 | const QStringList data = QString::fromLocal8Bit(file.readAll()).split(QLatin1Char(' ')); | 108 | const QStringList data = QString::fromLocal8Bit(file.readAll()).split(QLatin1Char(' ')); | ||
121 | qint64 pid = procId.toUInt(); | 109 | qint64 pid = procId.toUInt(); | ||
122 | QString name = data.at(1); | 110 | QString name = data.at(1); | ||
123 | if (name.startsWith(QLatin1Char('(')) && name.endsWith(QLatin1Char(')'))) { | 111 | if (name.startsWith(QLatin1Char('(')) && name.endsWith(QLatin1Char(')'))) { | ||
124 | name.truncate(name.size() - 1); | 112 | name.truncate(name.size() - 1); | ||
125 | name.remove(0, 1); | 113 | name.remove(0, 1); | ||
126 | } | 114 | } | ||
Show All 19 Lines | 126 | if (!cmd.isEmpty()) { | |||
146 | } | 134 | } | ||
147 | name = QString::fromLocal8Bit(cmd.mid(processNameStart, zeroIndex - processNameStart)); | 135 | name = QString::fromLocal8Bit(cmd.mid(processNameStart, zeroIndex - processNameStart)); | ||
148 | 136 | | |||
149 | cmd.replace('\0', ' '); | 137 | cmd.replace('\0', ' '); | ||
150 | command = QString::fromLocal8Bit(cmd).trimmed(); | 138 | command = QString::fromLocal8Bit(cmd).trimmed(); | ||
151 | } | 139 | } | ||
152 | } | 140 | } | ||
153 | cmdFile.close(); | 141 | cmdFile.close(); | ||
142 | processInfo = KProcessInfo(pid, command, name, user); | ||||
143 | return true; | ||||
144 | } | ||||
154 | 145 | | |||
155 | rc.push_back(KProcessInfo(pid, command, name, user)); | 146 | } // unnamed namespace | ||
147 | | ||||
148 | // Determine UNIX processes by reading "/proc". Default to ps if | ||||
149 | // it does not exist | ||||
150 | KProcessInfoList KProcessList::processInfoList() | ||||
151 | { | ||||
152 | const QDir procDir(QStringLiteral("/proc/")); | ||||
153 | if (!procDir.exists()) | ||||
154 | return unixProcessListPS(); | ||||
155 | KProcessInfoList rc; | ||||
156 | const QStringList procIds = procDir.entryList(); | ||||
157 | if (procIds.isEmpty()) | ||||
apol: checking if it's empty is unnecessary. | |||||
158 | return rc; | ||||
159 | for (const QString &procId : procIds) { | ||||
160 | KProcessInfo processInfo; | ||||
161 | if (getProcessInfo(procId, processInfo)) { | ||||
162 | rc.push_back(processInfo); | ||||
163 | } | ||||
156 | } | 164 | } | ||
157 | return rc; | 165 | return rc; | ||
158 | } | 166 | } | ||
167 | | ||||
168 | KProcessInfo KProcessList::processInfo(qint64 pid) | ||||
169 | { | ||||
170 | KProcessInfo processInfo; | ||||
171 | getProcessInfo(QString(QLatin1String("%1")).arg(pid), processInfo); | ||||
apol: Use QString::number() | |||||
172 | return processInfo; | ||||
173 | } |
checking if it's empty is unnecessary.