Changeset View
Changeset View
Standalone View
Standalone View
src/lib/util/kprocesslist_unix_procstat_p.h
- This file was added.
1 | /************************************************************************** | ||||
---|---|---|---|---|---|
2 | ** | ||||
3 | ** This file is part of the KDE Frameworks | ||||
4 | ** | ||||
5 | ** Copyright (c) 2019 Tobias C. Berner <tcberner@FreeBSD.org> | ||||
6 | ** | ||||
7 | ** GNU Lesser General Public License Usage | ||||
8 | ** | ||||
9 | ** This file may be used under the terms of the GNU Lesser General Public | ||||
10 | ** License version 2.1 as published by the Free Software Foundation and | ||||
11 | ** appearing in the file LICENSE.LGPL included in the packaging of this file. | ||||
12 | ** Please review the following information to ensure the GNU Lesser General | ||||
13 | ** Public License version 2.1 requirements will be met: | ||||
14 | ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | ||||
15 | ** | ||||
16 | **************************************************************************/ | ||||
17 | | ||||
18 | #pragma once | ||||
19 | | ||||
20 | #include <sys/types.h> | ||||
21 | #include <sys/sysctl.h> | ||||
22 | #include <sys/param.h> | ||||
23 | #include <sys/queue.h> | ||||
24 | #include <sys/user.h> | ||||
25 | #include <libprocstat.h> | ||||
26 | | ||||
27 | namespace KProcessList | ||||
28 | { | ||||
29 | struct ProcStat | ||||
30 | { | ||||
31 | public: | ||||
32 | struct procstat *pstat; | ||||
33 | ProcStat() | ||||
34 | { | ||||
35 | pstat = procstat_open_sysctl(); | ||||
36 | } | ||||
37 | | ||||
38 | ~ProcStat() | ||||
39 | { | ||||
40 | procstat_close(pstat); | ||||
41 | } | ||||
42 | | ||||
43 | operator bool() const | ||||
44 | { | ||||
45 | return pstat; | ||||
46 | } | ||||
47 | }; | ||||
48 | | ||||
49 | struct ProcStatProcesses | ||||
50 | { | ||||
51 | private: | ||||
52 | ProcStat& parent; | ||||
53 | unsigned int proc_count; | ||||
54 | struct kinfo_proc *procs; | ||||
55 | public: | ||||
56 | ProcStatProcesses(ProcStat& pstat) : parent(pstat) | ||||
57 | { | ||||
58 | procs = procstat_getprocs(parent.pstat, KERN_PROC_PROC, 0, &proc_count); | ||||
59 | } | ||||
60 | | ||||
61 | ~ProcStatProcesses() | ||||
62 | { | ||||
63 | if (procs) | ||||
64 | { | ||||
65 | procstat_freeprocs(parent.pstat, procs); | ||||
66 | } | ||||
67 | } | ||||
68 | | ||||
69 | operator bool() const | ||||
70 | { | ||||
71 | return procs && proc_count > 0; | ||||
72 | } | ||||
73 | | ||||
74 | unsigned int count() const | ||||
75 | { | ||||
76 | return proc_count; | ||||
77 | } | ||||
78 | | ||||
79 | class ProcessIterator | ||||
80 | { | ||||
81 | private: | ||||
82 | const ProcStatProcesses& processes; | ||||
83 | unsigned int pos; | ||||
84 | public: | ||||
85 | ProcessIterator(const ProcStatProcesses& processes, unsigned int pos) : processes(processes), pos(pos) {}; | ||||
86 | | ||||
87 | bool operator!=(const ProcessIterator& other) const { return pos != other.pos; } | ||||
88 | | ||||
89 | ProcessIterator& operator++() | ||||
90 | { | ||||
91 | if (pos < processes.count()) | ||||
92 | { | ||||
93 | ++pos; | ||||
94 | } | ||||
95 | return *this; | ||||
96 | } | ||||
97 | | ||||
98 | const KProcessInfo operator*() | ||||
99 | { | ||||
100 | QStringList command_line; | ||||
101 | QString command; | ||||
102 | char pathname[PATH_MAX]; | ||||
103 | struct kinfo_proc *proc = &processes.procs[pos]; | ||||
104 | if (procstat_getpathname(processes.parent.pstat, proc, pathname, sizeof(pathname)) != 0) { | ||||
105 | command = QString::fromLocal8Bit(pathname); | ||||
106 | } else { | ||||
107 | command = QString::fromLocal8Bit(proc->ki_comm); | ||||
108 | } | ||||
109 | | ||||
110 | char **args; | ||||
111 | args = procstat_getargv(processes.parent.pstat, proc, 0); | ||||
112 | if (args) { | ||||
113 | for (int i = 0; args[i] != nullptr; i++) { | ||||
114 | command_line << QString::fromLocal8Bit(args[i]); | ||||
115 | } | ||||
116 | } | ||||
117 | | ||||
118 | pid_t pid = proc->ki_pid; | ||||
119 | QString user = QString::fromLocal8Bit(proc->ki_login); | ||||
120 | return KProcessInfo(pid, command_line.join(QString::fromLocal8Bit(" ")), command, user); | ||||
121 | } | ||||
122 | }; | ||||
123 | | ||||
124 | ProcessIterator begin() const { return ProcessIterator(*this, 0); } | ||||
125 | ProcessIterator end() const { return ProcessIterator(*this, this->count()); } | ||||
126 | }; | ||||
127 | } |