pkill.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * Copyright (c) 2022, Maxwell Trussell <maxtrussell@gmail.com>
  3. * Copyright (c) 2023, Tim Ledbetter <timledbetter@gmail.com>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/Format.h>
  8. #include <AK/Vector.h>
  9. #include <LibCore/Account.h>
  10. #include <LibCore/ArgsParser.h>
  11. #include <LibCore/ProcessStatisticsReader.h>
  12. #include <LibCore/System.h>
  13. #include <LibMain/Main.h>
  14. #include <LibRegex/Regex.h>
  15. #include <LibRegex/RegexOptions.h>
  16. #include <signal.h>
  17. #include <sys/types.h>
  18. ErrorOr<int> serenity_main(Main::Arguments args)
  19. {
  20. TRY(Core::System::pledge("stdio proc rpath"));
  21. TRY(Core::System::unveil("/sys/kernel/processes", "r"));
  22. TRY(Core::System::unveil("/etc/group", "r"));
  23. TRY(Core::System::unveil("/etc/passwd", "r"));
  24. TRY(Core::System::unveil(nullptr, nullptr));
  25. bool display_number_of_matches;
  26. bool case_insensitive = false;
  27. bool echo = false;
  28. bool exact_match = false;
  29. StringView pattern;
  30. HashTable<uid_t> uids_to_filter_by;
  31. int signal = SIGTERM;
  32. Core::ArgsParser args_parser;
  33. args_parser.add_option(display_number_of_matches, "Display the number of matching processes", "count", 'c');
  34. args_parser.add_option(case_insensitive, "Make matches case-insensitive", nullptr, 'i');
  35. args_parser.add_option(echo, "Display what is killed", "echo", 'e');
  36. args_parser.add_option(signal, "Signal number to send", "signal", 's', "number");
  37. args_parser.add_option(Core::ArgsParser::Option {
  38. .argument_mode = Core::ArgsParser::OptionArgumentMode::Required,
  39. .help_string = "Select only processes whose UID is in the given comma-separated list. Login name or numerical user ID may be used",
  40. .long_name = "uid",
  41. .short_name = 'U',
  42. .value_name = "uid-list",
  43. .accept_value = [&uids_to_filter_by](StringView comma_separated_users) {
  44. for (auto user_string : comma_separated_users.split_view(',')) {
  45. auto maybe_uid = user_string.to_uint<uid_t>();
  46. if (maybe_uid.has_value()) {
  47. uids_to_filter_by.set(maybe_uid.value());
  48. } else {
  49. auto maybe_account = Core::Account::from_name(user_string, Core::Account::Read::PasswdOnly);
  50. if (maybe_account.is_error()) {
  51. warnln("Could not find user '{}': {}", user_string, maybe_account.error());
  52. return false;
  53. }
  54. uids_to_filter_by.set(maybe_account.release_value().uid());
  55. }
  56. }
  57. return true;
  58. },
  59. });
  60. args_parser.add_option(exact_match, "Select only processes whose names match the given pattern exactly", "exact", 'x');
  61. args_parser.add_positional_argument(pattern, "Process name to search for", "process-name");
  62. args_parser.parse(args);
  63. auto all_processes = TRY(Core::ProcessStatisticsReader::get_all());
  64. PosixOptions options {};
  65. if (case_insensitive) {
  66. options |= PosixFlags::Insensitive;
  67. }
  68. StringBuilder exact_pattern_builder;
  69. if (exact_match) {
  70. exact_pattern_builder.appendff("^({})$", pattern);
  71. pattern = exact_pattern_builder.string_view();
  72. }
  73. Regex<PosixExtended> re(pattern, options);
  74. if (re.parser_result.error != regex::Error::NoError) {
  75. return 1;
  76. }
  77. Vector<Core::ProcessStatistics> matched_processes;
  78. for (auto& process : all_processes.processes) {
  79. auto result = re.match(process.name, PosixFlags::Global);
  80. if (result.success) {
  81. if (!uids_to_filter_by.is_empty() && !uids_to_filter_by.contains(process.uid))
  82. continue;
  83. matched_processes.append(process);
  84. }
  85. }
  86. for (auto& process : matched_processes) {
  87. auto result = Core::System::kill(process.pid, signal);
  88. if (result.is_error())
  89. warnln("Killing pid {} failed. {}", process.pid, result.release_error());
  90. else if (echo)
  91. outln("{} killed (pid {})", process.name, process.pid);
  92. }
  93. if (display_number_of_matches)
  94. outln("{}", matched_processes.size());
  95. return matched_processes.is_empty() ? 1 : 0;
  96. }