Bug 1016744 - Remove unused NamedProcessIterator from ipc/chromium. r=jld, rs=bsmedberg

This commit is contained in:
Jan Beich 2014-06-04 18:51:00 -04:00
parent dc4e68e58e
commit 82686e13cd
7 changed files with 0 additions and 430 deletions

View File

@ -42,30 +42,24 @@ else:
DEFINES['OS_MACOSX'] = 1
elif CONFIG['OS_ARCH'] == 'DragonFly':
OS_LIBS += [ '$(call EXPAND_LIBNAME,kvm)' ]
DEFINES.update({
'OS_DRAGONFLY': 1,
'OS_BSD': 1,
})
elif CONFIG['OS_ARCH'] == 'FreeBSD' or CONFIG['OS_ARCH'] == 'GNU_kFreeBSD':
if CONFIG['OS_ARCH'] != 'GNU_kFreeBSD':
OS_LIBS += [ '$(call EXPAND_LIBNAME,kvm)' ]
DEFINES.update({
'OS_FREEBSD': 1,
'OS_BSD': 1,
})
elif CONFIG['OS_ARCH'] == 'NetBSD':
OS_LIBS += [ '$(call EXPAND_LIBNAME,kvm)' ]
DEFINES.update({
'OS_NETBSD': 1,
'OS_BSD': 1,
})
elif CONFIG['OS_ARCH'] == 'OpenBSD':
OS_LIBS += [ '$(call EXPAND_LIBNAME,kvm)' ]
DEFINES.update({
'OS_OPENBSD': 1,
'OS_BSD': 1,

View File

@ -213,59 +213,6 @@ bool KillProcess(ProcessHandle process, int exit_code, bool wait);
// a different manner on POSIX.
bool DidProcessCrash(bool* child_exited, ProcessHandle handle);
// This class provides a way to iterate through the list of processes
// on the current machine that were started from the given executable
// name. To use, create an instance and then call NextProcessEntry()
// until it returns false.
class NamedProcessIterator {
public:
NamedProcessIterator(const std::wstring& executable_name,
const ProcessFilter* filter);
~NamedProcessIterator();
// If there's another process that matches the given executable name,
// returns a const pointer to the corresponding PROCESSENTRY32.
// If there are no more matching processes, returns NULL.
// The returned pointer will remain valid until NextProcessEntry()
// is called again or this NamedProcessIterator goes out of scope.
const ProcessEntry* NextProcessEntry();
private:
#if !defined(OS_BSD) || defined(__GLIBC__)
// Determines whether there's another process (regardless of executable)
// left in the list of all processes. Returns true and sets entry_ to
// that process's info if there is one, false otherwise.
bool CheckForNextProcess();
bool IncludeEntry();
// Initializes a PROCESSENTRY32 data structure so that it's ready for
// use with Process32First/Process32Next.
void InitProcessEntry(ProcessEntry* entry);
std::wstring executable_name_;
#endif
#if defined(OS_WIN)
HANDLE snapshot_;
bool started_iteration_;
#elif defined(OS_LINUX) || defined(__GLIBC__)
DIR *procfs_dir_;
#elif defined(OS_BSD)
std::vector<ProcessEntry> content;
size_t nextEntry;
#elif defined(OS_MACOSX)
std::vector<kinfo_proc> kinfo_procs_;
size_t index_of_kinfo_proc_;
#endif
#if !defined(OS_BSD) || defined(__GLIBC__)
ProcessEntry entry_;
const ProcessFilter* filter_;
#endif
DISALLOW_EVIL_CONSTRUCTORS(NamedProcessIterator);
};
// Provides performance metrics for a specified process (CPU usage, memory and
// IO counters). To use it, invoke CreateProcessMetrics() to get an instance
// for a specific process, then access the information with the different get

View File

@ -6,16 +6,9 @@
#include "base/process_util.h"
#include <sys/param.h>
#include <sys/sysctl.h>
#include <sys/wait.h>
#if defined(OS_DRAGONFLY) || defined(OS_FREEBSD)
#include <sys/user.h>
#endif
#include <ctype.h>
#include <fcntl.h>
#include <kvm.h>
#include <unistd.h>
#include <string>
@ -311,64 +304,4 @@ void SetCurrentProcessPrivileges(ChildPrivileges privs) {
#endif
NamedProcessIterator::NamedProcessIterator(const std::wstring& executable_name,
const ProcessFilter* filter)
{
int numEntries;
kvm_t *kvm;
std::string exe(WideToASCII(executable_name));
#if defined(OS_DRAGONFLY) || defined(OS_FREEBSD)
kvm = kvm_open(NULL, NULL, NULL, O_RDONLY, NULL);
struct kinfo_proc* procs = kvm_getprocs(kvm, KERN_PROC_UID, getuid(), &numEntries);
if (procs != NULL && numEntries > 0) {
for (int i = 0; i < numEntries; i++) {
# if defined(OS_DRAGONFLY)
if (exe != procs[i].kp_comm) continue;
if (filter && !filter->Includes(procs[i].kp_pid, procs[i].kp_ppid)) continue;
ProcessEntry e;
e.pid = procs[i].kp_pid;
e.ppid = procs[i].kp_ppid;
strlcpy(e.szExeFile, procs[i].kp_comm, sizeof e.szExeFile);
content.push_back(e);
# elif defined(OS_FREEBSD)
if (exe != procs[i].ki_comm) continue;
if (filter && !filter->Includes(procs[i].ki_pid, procs[i].ki_ppid)) continue;
ProcessEntry e;
e.pid = procs[i].ki_pid;
e.ppid = procs[i].ki_ppid;
strlcpy(e.szExeFile, procs[i].ki_comm, sizeof e.szExeFile);
content.push_back(e);
# endif
#else
kvm = kvm_open(NULL, NULL, NULL, KVM_NO_FILES, NULL);
#if defined(OS_OPENBSD)
struct kinfo_proc* procs = kvm_getprocs(kvm, KERN_PROC_UID, getuid(), sizeof(struct kinfo_proc), &numEntries);
#else
struct kinfo_proc2* procs = kvm_getproc2(kvm, KERN_PROC_UID, getuid(), sizeof(struct kinfo_proc2), &numEntries);
#endif
if (procs != NULL && numEntries > 0) {
for (int i = 0; i < numEntries; i++) {
if (exe != procs[i].p_comm) continue;
if (filter && !filter->Includes(procs[i].p_pid, procs[i].p_ppid)) continue;
ProcessEntry e;
e.pid = procs[i].p_pid;
e.ppid = procs[i].p_ppid;
strlcpy(e.szExeFile, procs[i].p_comm, sizeof e.szExeFile);
content.push_back(e);
#endif
}
}
nextEntry = 0;
kvm_close(kvm);
}
NamedProcessIterator::~NamedProcessIterator() {
}
const ProcessEntry* NamedProcessIterator::NextProcessEntry() {
if (nextEntry >= content.size()) return NULL;
return &content[nextEntry++];
}
} // namespace base

View File

@ -5,7 +5,6 @@
#include "base/process_util.h"
#include <ctype.h>
#include <dirent.h>
#include <fcntl.h>
#include <memory>
#include <unistd.h>
@ -315,119 +314,4 @@ void SetCurrentProcessPrivileges(ChildPrivileges privs) {
gProcessLog.print("==> could not chdir()\n");
}
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);
}
} // namespace base

View File

@ -8,8 +8,6 @@
#import <Cocoa/Cocoa.h>
#include <crt_externs.h>
#include <spawn.h>
#include <sys/sysctl.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <string>
@ -192,138 +190,4 @@ void SetCurrentProcessPrivileges(ChildPrivileges privs) {
}
NamedProcessIterator::NamedProcessIterator(const std::wstring& executable_name,
const ProcessFilter* filter)
: executable_name_(executable_name),
index_of_kinfo_proc_(0),
filter_(filter) {
// Get a snapshot of all of my processes (yes, as we loop it can go stale, but
// but trying to find where we were in a constantly changing list is basically
// impossible.
int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_UID, int(geteuid()) };
// Since more processes could start between when we get the size and when
// we get the list, we do a loop to keep trying until we get it.
bool done = false;
int try_num = 1;
const int max_tries = 10;
do {
// Get the size of the buffer
size_t len = 0;
if (sysctl(mib, arraysize(mib), NULL, &len, NULL, 0) < 0) {
CHROMIUM_LOG(ERROR) << "failed to get the size needed for the process list";
kinfo_procs_.resize(0);
done = true;
} else {
size_t num_of_kinfo_proc = len / sizeof(struct kinfo_proc);
// Leave some spare room for process table growth (more could show up
// between when we check and now)
num_of_kinfo_proc += 4;
kinfo_procs_.resize(num_of_kinfo_proc);
len = num_of_kinfo_proc * sizeof(struct kinfo_proc);
// Load the list of processes
if (sysctl(mib, arraysize(mib), &kinfo_procs_[0], &len, NULL, 0) < 0) {
// If we get a mem error, it just means we need a bigger buffer, so
// loop around again. Anything else is a real error and give up.
if (errno != ENOMEM) {
CHROMIUM_LOG(ERROR) << "failed to get the process list";
kinfo_procs_.resize(0);
done = true;
}
} else {
// Got the list, just make sure we're sized exactly right
size_t num_of_kinfo_proc = len / sizeof(struct kinfo_proc);
kinfo_procs_.resize(num_of_kinfo_proc);
done = true;
}
}
} while (!done && (try_num++ < max_tries));
if (!done) {
CHROMIUM_LOG(ERROR) << "failed to collect the process list in a few tries";
kinfo_procs_.resize(0);
}
}
NamedProcessIterator::~NamedProcessIterator() {
}
const ProcessEntry* NamedProcessIterator::NextProcessEntry() {
bool result = false;
do {
result = CheckForNextProcess();
} while (result && !IncludeEntry());
if (result) {
return &entry_;
}
return NULL;
}
bool NamedProcessIterator::CheckForNextProcess() {
std::string executable_name_utf8(WideToUTF8(executable_name_));
std::string data;
std::string exec_name;
for (; index_of_kinfo_proc_ < kinfo_procs_.size(); ++index_of_kinfo_proc_) {
kinfo_proc* kinfo = &kinfo_procs_[index_of_kinfo_proc_];
// Skip processes just awaiting collection
if ((kinfo->kp_proc.p_pid > 0) && (kinfo->kp_proc.p_stat == SZOMB))
continue;
int mib[] = { CTL_KERN, KERN_PROCARGS, kinfo->kp_proc.p_pid };
// Found out what size buffer we need
size_t data_len = 0;
if (sysctl(mib, arraysize(mib), NULL, &data_len, NULL, 0) < 0) {
CHROMIUM_LOG(ERROR) << "failed to figure out the buffer size for a commandline";
continue;
}
data.resize(data_len);
if (sysctl(mib, arraysize(mib), &data[0], &data_len, NULL, 0) < 0) {
CHROMIUM_LOG(ERROR) << "failed to fetch a commandline";
continue;
}
// Data starts w/ the full path null termed, so we have to extract just the
// executable name from the path.
size_t exec_name_end = data.find('\0');
if (exec_name_end == std::string::npos) {
CHROMIUM_LOG(ERROR) << "command line data didn't match expected format";
continue;
}
size_t last_slash = data.rfind('/', exec_name_end);
if (last_slash == std::string::npos)
exec_name = data.substr(0, exec_name_end);
else
exec_name = data.substr(last_slash + 1, exec_name_end - last_slash - 1);
// Check the name
if (executable_name_utf8 == exec_name) {
entry_.pid = kinfo->kp_proc.p_pid;
entry_.ppid = kinfo->kp_eproc.e_ppid;
base::strlcpy(entry_.szExeFile, exec_name.c_str(),
sizeof(entry_.szExeFile));
// Start w/ the next entry next time through
++index_of_kinfo_proc_;
// Done
return true;
}
}
return false;
}
bool NamedProcessIterator::IncludeEntry() {
// Don't need to check the name, we did that w/in CheckForNextProcess.
if (!filter_)
return true;
return filter_->Includes(entry_.pid, entry_.ppid);
}
} // namespace base

View File

@ -406,54 +406,6 @@ void SetCurrentProcessPrivileges(ChildPrivileges privs) {
}
NamedProcessIterator::NamedProcessIterator(const std::wstring& executable_name,
const ProcessFilter* filter)
: started_iteration_(false),
executable_name_(executable_name),
filter_(filter) {
snapshot_ = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
}
NamedProcessIterator::~NamedProcessIterator() {
CloseHandle(snapshot_);
}
const ProcessEntry* NamedProcessIterator::NextProcessEntry() {
bool result = false;
do {
result = CheckForNextProcess();
} while (result && !IncludeEntry());
if (result) {
return &entry_;
}
return NULL;
}
bool NamedProcessIterator::CheckForNextProcess() {
InitProcessEntry(&entry_);
if (!started_iteration_) {
started_iteration_ = true;
return !!Process32First(snapshot_, &entry_);
}
return !!Process32Next(snapshot_, &entry_);
}
bool NamedProcessIterator::IncludeEntry() {
return _wcsicmp(executable_name_.c_str(), entry_.szExeFile) == 0 &&
(!filter_ || filter_->Includes(entry_.th32ProcessID,
entry_.th32ParentProcessID));
}
void NamedProcessIterator::InitProcessEntry(ProcessEntry* entry) {
memset(entry, 0, sizeof(*entry));
entry->dwSize = sizeof(*entry);
}
///////////////////////////////////////////////////////////////////////////////
// ProcesMetrics

View File

@ -202,10 +202,6 @@ EXTRA_DSO_LDOPTS += -lelf -ldemangle
endif
endif
ifneq (,$(filter DragonFly FreeBSD NetBSD OpenBSD,$(OS_ARCH)))
OS_LIBS += $(call EXPAND_LIBNAME,kvm)
endif
ifeq ($(OS_ARCH),FreeBSD)
OS_LIBS += $(call EXPAND_LIBNAME,util)
endif