[email protected] | e89298d | 2012-07-27 05:01:29 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include <windows.h> |
| 6 | #include <stdlib.h> |
| 7 | |
| 8 | #include "base/at_exit.h" |
| 9 | #include "base/command_line.h" |
[email protected] | e89298d | 2012-07-27 05:01:29 | [diff] [blame] | 10 | #include "base/win/scoped_handle.h" |
[email protected] | bc825c0b | 2012-10-18 01:59:29 | [diff] [blame] | 11 | #include "remoting/host/logging.h" |
[email protected] | e89298d | 2012-07-27 05:01:29 | [diff] [blame] | 12 | |
| 13 | namespace { |
| 14 | |
| 15 | // "--help" or "--?" prints the usage message. |
| 16 | const char kHelpSwitchName[] = "help"; |
| 17 | const char kQuestionSwitchName[] = "?"; |
| 18 | |
| 19 | const char kUsageMessage[] = |
joedow | 900ad0c | 2017-04-20 21:31:17 | [diff] [blame] | 20 | "\n" |
| 21 | "Usage: %s <pid>\n" |
| 22 | "\n" |
| 23 | " pid - PID of the process to be crashed.\n" |
| 24 | "\n\n" |
| 25 | "Note: You may need to run this tool as SYSTEM\n" |
| 26 | " to prevent access denied errors.\n"; |
[email protected] | e89298d | 2012-07-27 05:01:29 | [diff] [blame] | 27 | |
| 28 | // Exit codes: |
| 29 | const int kSuccessExitCode = 0; |
| 30 | const int kUsageExitCode = 1; |
| 31 | const int kErrorExitCode = 2; |
| 32 | |
| 33 | void usage(const char* program_name) { |
| 34 | fprintf(stderr, kUsageMessage, program_name); |
| 35 | } |
| 36 | |
| 37 | } // namespace |
| 38 | |
| 39 | int main(int argc, char** argv) { |
[email protected] | b982d51 | 2014-05-23 09:42:02 | [diff] [blame] | 40 | base::CommandLine::Init(argc, argv); |
[email protected] | e89298d | 2012-07-27 05:01:29 | [diff] [blame] | 41 | |
| 42 | base::AtExitManager exit_manager; |
| 43 | |
[email protected] | bc825c0b | 2012-10-18 01:59:29 | [diff] [blame] | 44 | remoting::InitHostLogging(); |
[email protected] | e89298d | 2012-07-27 05:01:29 | [diff] [blame] | 45 | |
[email protected] | b982d51 | 2014-05-23 09:42:02 | [diff] [blame] | 46 | const base::CommandLine* command_line = |
| 47 | base::CommandLine::ForCurrentProcess(); |
[email protected] | e89298d | 2012-07-27 05:01:29 | [diff] [blame] | 48 | if (command_line->HasSwitch(kHelpSwitchName) || |
| 49 | command_line->HasSwitch(kQuestionSwitchName)) { |
| 50 | usage(argv[0]); |
| 51 | return kSuccessExitCode; |
| 52 | } |
| 53 | |
[email protected] | b982d51 | 2014-05-23 09:42:02 | [diff] [blame] | 54 | base::CommandLine::StringVector args = command_line->GetArgs(); |
[email protected] | e89298d | 2012-07-27 05:01:29 | [diff] [blame] | 55 | if (args.size() != 1) { |
| 56 | usage(argv[0]); |
| 57 | return kUsageExitCode; |
| 58 | } |
| 59 | |
| 60 | int pid = _wtoi(args[0].c_str()); |
| 61 | if (pid == 0) { |
| 62 | LOG(ERROR) << "Invalid process PID: " << args[0]; |
| 63 | return kErrorExitCode; |
| 64 | } |
| 65 | |
| 66 | DWORD desired_access = PROCESS_CREATE_THREAD | PROCESS_QUERY_INFORMATION | |
| 67 | PROCESS_VM_OPERATION | PROCESS_VM_WRITE | PROCESS_VM_READ; |
| 68 | base::win::ScopedHandle process; |
| 69 | process.Set(OpenProcess(desired_access, FALSE, pid)); |
| 70 | if (!process.IsValid()) { |
[email protected] | ad8cfa9 | 2014-05-21 20:06:23 | [diff] [blame] | 71 | PLOG(ERROR) << "Failed to open the process " << pid; |
[email protected] | e89298d | 2012-07-27 05:01:29 | [diff] [blame] | 72 | return kErrorExitCode; |
| 73 | } |
| 74 | |
| 75 | DWORD thread_id; |
| 76 | base::win::ScopedHandle thread; |
| 77 | thread.Set(CreateRemoteThread(process.Get(), NULL, 0, NULL, NULL, 0, |
| 78 | &thread_id)); |
| 79 | if (!thread.IsValid()) { |
[email protected] | ad8cfa9 | 2014-05-21 20:06:23 | [diff] [blame] | 80 | PLOG(ERROR) << "Failed to create a remote thread in " << pid; |
[email protected] | e89298d | 2012-07-27 05:01:29 | [diff] [blame] | 81 | return kErrorExitCode; |
| 82 | } |
| 83 | |
| 84 | return kSuccessExitCode; |
| 85 | } |