1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
|
// Copyright (c) 2008 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/process_util.h"
#include <ctype.h>
#include <dirent.h>
#include <fcntl.h>
#include <unistd.h>
#include <string>
#include <sys/types.h>
#include <sys/wait.h>
#include "base/file_util.h"
#include "base/logging.h"
#include "base/string_tokenizer.h"
#include "base/string_util.h"
#include "base/time.h"
namespace {
enum ParsingState {
KEY_NAME,
KEY_VALUE
};
} // namespace
namespace base {
bool LaunchApp(const std::vector<std::string>& argv,
const file_handle_mapping_vector& fds_to_remap,
bool wait, ProcessHandle* process_handle) {
bool retval = true;
char* argv_copy[argv.size() + 1];
for (size_t i = 0; i < argv.size(); i++) {
argv_copy[i] = new char[argv[i].size() + 1];
strcpy(argv_copy[i], argv[i].c_str());
}
argv_copy[argv.size()] = NULL;
// Make sure we don't leak any FDs to the child process by marking all FDs
// as close-on-exec.
SetAllFDsToCloseOnExec();
int pid = fork();
if (pid == 0) {
for (file_handle_mapping_vector::const_iterator it = fds_to_remap.begin();
it != fds_to_remap.end();
++it) {
int src_fd = it->first;
int dest_fd = it->second;
if (src_fd == dest_fd) {
int flags = fcntl(src_fd, F_GETFD);
if (flags != -1) {
fcntl(src_fd, F_SETFD, flags & ~FD_CLOEXEC);
}
} else {
dup2(src_fd, dest_fd);
}
}
execvp(argv_copy[0], argv_copy);
} else if (pid < 0) {
retval = false;
} else {
if (wait)
waitpid(pid, 0, 0);
if (process_handle)
*process_handle = pid;
}
for (size_t i = 0; i < argv.size(); i++)
delete[] argv_copy[i];
return retval;
}
bool LaunchApp(const CommandLine& cl,
bool wait, bool start_hidden,
ProcessHandle* process_handle) {
file_handle_mapping_vector no_files;
return LaunchApp(cl.argv(), no_files, wait, process_handle);
}
NamedProcessIterator::NamedProcessIterator(const std::wstring& executable_name,
const ProcessFilter* filter)
: executable_name_(executable_name), filter_(filter) {
procfs_dir_ = opendir("/proc");
}
NamedProcessIterator::~NamedProcessIterator() {
if (procfs_dir_) {
closedir(procfs_dir_);
procfs_dir_ = NULL;
}
}
const ProcessEntry* NamedProcessIterator::NextProcessEntry() {
bool result = false;
do {
result = CheckForNextProcess();
} while (result && !IncludeEntry());
if (result)
return &entry_;
return NULL;
}
bool NamedProcessIterator::CheckForNextProcess() {
// TODO(port): skip processes owned by different UID
dirent* slot = 0;
const char* openparen;
const char* closeparen;
// Arbitrarily guess that there will never be more than 200 non-process
// files in /proc. Hardy has 53.
int skipped = 0;
const int kSkipLimit = 200;
while (skipped < kSkipLimit) {
slot = readdir(procfs_dir_);
// all done looking through /proc?
if (!slot)
return false;
// If not a process, keep looking for one.
bool notprocess = false;
int i;
for (i = 0; i < NAME_MAX && slot->d_name[i]; ++i) {
if (!isdigit(slot->d_name[i])) {
notprocess = true;
break;
}
}
if (i == NAME_MAX || notprocess) {
skipped++;
continue;
}
// Read the process's status.
char buf[NAME_MAX + 12];
sprintf(buf, "/proc/%s/stat", slot->d_name);
FILE *fp = fopen(buf, "r");
if (!fp)
return false;
const char* result = fgets(buf, sizeof(buf), fp);
fclose(fp);
if (!result)
return false;
// Parse the status. It is formatted like this:
// %d (%s) %c %d ...
// pid (name) runstate ppid
// To avoid being fooled by names containing a closing paren, scan
// backwards.
openparen = strchr(buf, '(');
closeparen = strrchr(buf, ')');
if (!openparen || !closeparen)
return false;
char runstate = closeparen[2];
// Is the process in 'Zombie' state, i.e. dead but waiting to be reaped?
// Allowed values: D R S T Z
if (runstate != 'Z')
break;
// Nope, it's a zombie; somebody isn't cleaning up after their children.
// (e.g. WaitForProcessesToExit doesn't clean up after dead children yet.)
// There could be a lot of zombies, can't really decrement i here.
}
if (skipped >= kSkipLimit) {
NOTREACHED();
return false;
}
entry_.pid = atoi(slot->d_name);
entry_.ppid = atoi(closeparen + 3);
// TODO(port): read pid's commandline's $0, like killall does. Using the
// short name between openparen and closeparen won't work for long names!
int len = closeparen - openparen - 1;
if (len > NAME_MAX)
len = NAME_MAX;
memcpy(entry_.szExeFile, openparen + 1, len);
entry_.szExeFile[len] = 0;
return true;
}
bool NamedProcessIterator::IncludeEntry() {
// TODO(port): make this also work for non-ASCII filenames
if (WideToASCII(executable_name_) != entry_.szExeFile)
return false;
if (!filter_)
return true;
return filter_->Includes(entry_.pid, entry_.ppid);
}
// To have /proc/self/io file you must enable CONFIG_TASK_IO_ACCOUNTING
// in your kernel configuration.
bool ProcessMetrics::GetIOCounters(IoCounters* io_counters) {
std::string proc_io_contents;
if (!file_util::ReadFileToString(L"/proc/self/io", &proc_io_contents))
return false;
(*io_counters).OtherOperationCount = 0;
(*io_counters).OtherTransferCount = 0;
StringTokenizer tokenizer(proc_io_contents, ": \n");
ParsingState state = KEY_NAME;
std::string last_key_name;
while (tokenizer.GetNext()) {
switch (state) {
case KEY_NAME:
last_key_name = tokenizer.token();
state = KEY_VALUE;
break;
case KEY_VALUE:
DCHECK(!last_key_name.empty());
if (last_key_name == "syscr") {
(*io_counters).ReadOperationCount = StringToInt64(tokenizer.token());
} else if (last_key_name == "syscw") {
(*io_counters).WriteOperationCount = StringToInt64(tokenizer.token());
} else if (last_key_name == "rchar") {
(*io_counters).ReadTransferCount = StringToInt64(tokenizer.token());
} else if (last_key_name == "wchar") {
(*io_counters).WriteTransferCount = StringToInt64(tokenizer.token());
}
state = KEY_NAME;
break;
}
}
return true;
}
} // namespace base
|