procList.cpp 5.1 KB
Newer Older
D
duke 已提交
1
/*
2
 * Copyright (c) 2000, 2001, Oracle and/or its affiliates. All rights reserved.
D
duke 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
19 20 21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
D
duke 已提交
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
 *
 */

#include "procList.hpp"
#include "nt4internals.hpp"
#include "isNT4.hpp"
#include "toolHelp.hpp"
#include <assert.h>

using namespace std;
using namespace NT4;

typedef void ProcListImplFunc(ProcEntryList& processes);

void procListImplNT4(ProcEntryList& processes);
void procListImplToolHelp(ProcEntryList& processes);

ProcEntry::ProcEntry(ULONG pid, USHORT nameLength, WCHAR* name) {
  this->pid = pid;
  this->nameLength = nameLength;
  this->name = new WCHAR[nameLength];
  memcpy(this->name, name, nameLength * sizeof(WCHAR));
}

ProcEntry::ProcEntry(ULONG pid, USHORT nameLength, char* name) {
  this->pid = pid;
  this->nameLength = nameLength;
  this->name = new WCHAR[nameLength];
  int j = 0;
  for (int i = 0; i < nameLength; i++) {
    // FIXME: what is the proper promotion from ASCII to UNICODE?
    this->name[i] = name[i] & 0xFF;
  }
}

ProcEntry::ProcEntry(const ProcEntry& arg) {
  name = NULL;
  copyFrom(arg);
}

ProcEntry&
ProcEntry::operator=(const ProcEntry& arg) {
  copyFrom(arg);
  return *this;
}

ProcEntry::~ProcEntry() {
  delete[] name;
}

void
ProcEntry::copyFrom(const ProcEntry& arg) {
  if (name != NULL) {
    delete[] name;
  }
  pid = arg.pid;
  nameLength = arg.nameLength;
  name = new WCHAR[nameLength];
  memcpy(name, arg.name, nameLength * sizeof(WCHAR));
}

ULONG
ProcEntry::getPid() {
  return pid;
}

USHORT
ProcEntry::getNameLength() {
  return nameLength;
}

WCHAR*
ProcEntry::getName() {
  return name;
}

void
procList(ProcEntryList& processes) {
  static ProcListImplFunc* impl = NULL;

  if (impl == NULL) {
    // See which operating system we're on
    impl = (isNT4() ? &procListImplNT4 : &procListImplToolHelp);
  }

  assert(impl != NULL);

  (*impl)(processes);
}

void
procListImplNT4(ProcEntryList& processes) {
  using namespace NT4;

  static ZwQuerySystemInformationFunc* query = NULL;

  if (query == NULL) {
    HMODULE ntDLL = loadNTDLL();
    query =
      (ZwQuerySystemInformationFunc*) GetProcAddress(ntDLL,
                                                     "ZwQuerySystemInformation");
    assert(query != NULL);
  }

  ULONG n = 0x100;
  PSYSTEM_PROCESSES sp = new SYSTEM_PROCESSES[n];
  while ((*query)(SystemProcessesAndThreadsInformation,
                  sp, n * sizeof(SYSTEM_PROCESSES), 0) == STATUS_INFO_LENGTH_MISMATCH) {
    delete[] sp;
    n *= 2;
    sp = new SYSTEM_PROCESSES[n];
  }

  bool done = false;
  for (PSYSTEM_PROCESSES p = sp; !done;
       p = PSYSTEM_PROCESSES(PCHAR(p) + p->NextEntryDelta)) {
    processes.push_back(ProcEntry(p->ProcessId,
                                  p->ProcessName.Length / 2,
                                  p->ProcessName.Buffer));
    done = p->NextEntryDelta == 0;
  }
}

void
procListImplToolHelp(ProcEntryList& processes) {
  using namespace ToolHelp;

  static CreateToolhelp32SnapshotFunc* snapshotFunc = NULL;
  static Process32FirstFunc*           firstFunc    = NULL;
  static Process32NextFunc*            nextFunc     = NULL;

  if (snapshotFunc == NULL) {
    HMODULE dll = loadDLL();

    snapshotFunc =
      (CreateToolhelp32SnapshotFunc*) GetProcAddress(dll,
                                                     "CreateToolhelp32Snapshot");

    firstFunc = (Process32FirstFunc*) GetProcAddress(dll,
                                                     "Process32First");

    nextFunc = (Process32NextFunc*) GetProcAddress(dll,
                                                   "Process32Next");

    assert(snapshotFunc != NULL);
    assert(firstFunc    != NULL);
    assert(nextFunc     != NULL);
  }

  HANDLE snapshot = (*snapshotFunc)(TH32CS_SNAPPROCESS, 0 /* ignored */);
  if (snapshot == (HANDLE) -1) {
    // Error occurred during snapshot
    return;
  }

  // Iterate
  PROCESSENTRY32 proc;
  if ((*firstFunc)(snapshot, &proc)) {
    do {
      // FIXME: could make this uniform to the NT version by cutting
      // off the path name just before the executable name
      processes.push_back(ProcEntry(proc.th32ProcessID,
                                    strlen(proc.szExeFile),
                                    proc.szExeFile));
    } while ((*nextFunc)(snapshot, &proc));
  }

  CloseHandle(snapshot);
}