2021-07-04 12:49:10 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2021, Aziz Berkay Yesilyurt <abyesilyurt@gmail.com>
|
2023-05-30 19:51:06 +03:00
|
|
|
* Copyright (c) 2023, Tim Ledbetter <timledbetter@gmail.com>
|
2021-07-04 12:49:10 +03:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <AK/QuickSort.h>
|
|
|
|
#include <AK/Vector.h>
|
2023-05-30 19:51:26 +03:00
|
|
|
#include <LibCore/Account.h>
|
2021-07-04 12:49:10 +03:00
|
|
|
#include <LibCore/ArgsParser.h>
|
|
|
|
#include <LibCore/ProcessStatisticsReader.h>
|
2022-02-09 01:22:52 +03:00
|
|
|
#include <LibCore/System.h>
|
|
|
|
#include <LibMain/Main.h>
|
2021-07-04 12:49:10 +03:00
|
|
|
#include <LibRegex/Regex.h>
|
|
|
|
|
2022-02-09 01:22:52 +03:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments args)
|
2021-07-04 12:49:10 +03:00
|
|
|
{
|
2022-02-09 01:22:52 +03:00
|
|
|
TRY(Core::System::pledge("stdio rpath"));
|
2022-10-14 21:56:19 +03:00
|
|
|
TRY(Core::System::unveil("/sys/kernel/processes", "r"));
|
2023-05-30 19:51:26 +03:00
|
|
|
TRY(Core::System::unveil("/etc/group", "r"));
|
2022-02-09 01:22:52 +03:00
|
|
|
TRY(Core::System::unveil("/etc/passwd", "r"));
|
|
|
|
TRY(Core::System::unveil(nullptr, nullptr));
|
2021-07-04 12:49:10 +03:00
|
|
|
|
2023-05-30 19:51:18 +03:00
|
|
|
bool display_number_of_matches = false;
|
2023-05-15 19:35:02 +03:00
|
|
|
auto pid_delimiter = "\n"sv;
|
2021-07-04 12:49:10 +03:00
|
|
|
bool case_insensitive = false;
|
2023-05-30 19:51:22 +03:00
|
|
|
bool list_process_name = false;
|
2021-07-04 12:49:10 +03:00
|
|
|
bool invert_match = false;
|
2023-05-30 19:51:26 +03:00
|
|
|
HashTable<uid_t> uids_to_filter_by;
|
2023-02-28 23:41:43 +03:00
|
|
|
StringView pattern;
|
2021-07-04 12:49:10 +03:00
|
|
|
|
|
|
|
Core::ArgsParser args_parser;
|
2023-05-30 19:51:18 +03:00
|
|
|
args_parser.add_option(display_number_of_matches, "Suppress normal output and print the number of matching processes", "count", 'c');
|
2023-05-15 19:35:02 +03:00
|
|
|
args_parser.add_option(pid_delimiter, "Set the string used to delimit multiple pids", "delimiter", 'd', nullptr);
|
2023-05-30 19:51:14 +03:00
|
|
|
args_parser.add_option(case_insensitive, "Make matches case-insensitive", "ignore-case", 'i');
|
2023-05-30 19:51:22 +03:00
|
|
|
args_parser.add_option(list_process_name, "List the process name in addition to its pid", "list-name", 'l');
|
2023-05-30 19:51:26 +03:00
|
|
|
args_parser.add_option(Core::ArgsParser::Option {
|
|
|
|
.argument_mode = Core::ArgsParser::OptionArgumentMode::Required,
|
|
|
|
.help_string = "Select only processes whose UID is in the given comma-separated list. Login name or numerical user ID may be used",
|
|
|
|
.long_name = "uid",
|
|
|
|
.short_name = 'U',
|
|
|
|
.value_name = "uid-list",
|
|
|
|
.accept_value = [&uids_to_filter_by](StringView comma_separated_users) {
|
|
|
|
for (auto user_string : comma_separated_users.split_view(',')) {
|
|
|
|
auto maybe_uid = user_string.to_uint<uid_t>();
|
|
|
|
if (maybe_uid.has_value()) {
|
|
|
|
uids_to_filter_by.set(maybe_uid.value());
|
|
|
|
} else {
|
|
|
|
auto maybe_account = Core::Account::from_name(user_string, Core::Account::Read::PasswdOnly);
|
|
|
|
if (maybe_account.is_error()) {
|
|
|
|
warnln("Could not find user '{}': {}", user_string, maybe_account.error());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
uids_to_filter_by.set(maybe_account.release_value().uid());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
});
|
2021-07-04 12:49:10 +03:00
|
|
|
args_parser.add_option(invert_match, "Select non-matching lines", "invert-match", 'v');
|
|
|
|
args_parser.add_positional_argument(pattern, "Process name to search for", "process-name");
|
2022-02-09 01:22:52 +03:00
|
|
|
args_parser.parse(args);
|
2021-07-04 12:49:10 +03:00
|
|
|
|
|
|
|
PosixOptions options {};
|
|
|
|
if (case_insensitive)
|
|
|
|
options |= PosixFlags::Insensitive;
|
|
|
|
|
|
|
|
Regex<PosixExtended> re(pattern, options);
|
2021-11-06 12:33:46 +03:00
|
|
|
if (re.parser_result.error != regex::Error::NoError) {
|
2021-07-04 12:49:10 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2022-12-08 16:50:31 +03:00
|
|
|
auto all_processes = TRY(Core::ProcessStatisticsReader::get_all());
|
2021-07-04 12:49:10 +03:00
|
|
|
|
2023-05-30 19:51:22 +03:00
|
|
|
Vector<Core::ProcessStatistics> matches;
|
|
|
|
for (auto const& it : all_processes.processes) {
|
2021-07-04 12:49:10 +03:00
|
|
|
auto result = re.match(it.name, PosixFlags::Global);
|
|
|
|
if (result.success ^ invert_match) {
|
2023-05-30 19:51:26 +03:00
|
|
|
if (!uids_to_filter_by.is_empty() && !uids_to_filter_by.contains(it.uid))
|
|
|
|
continue;
|
|
|
|
|
2023-05-30 19:51:22 +03:00
|
|
|
matches.append(it);
|
2021-07-04 12:49:10 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-30 19:51:18 +03:00
|
|
|
if (display_number_of_matches) {
|
|
|
|
outln("{}", matches.size());
|
|
|
|
} else {
|
2023-05-30 19:51:22 +03:00
|
|
|
quick_sort(matches, [](auto const& a, auto const& b) { return a.pid < b.pid; });
|
2023-05-30 19:51:18 +03:00
|
|
|
auto displayed_at_least_one = false;
|
|
|
|
for (auto& match : matches) {
|
|
|
|
if (displayed_at_least_one)
|
2023-05-30 19:51:22 +03:00
|
|
|
out("{}"sv, pid_delimiter);
|
|
|
|
|
|
|
|
out("{}"sv, match.pid);
|
|
|
|
|
|
|
|
if (list_process_name)
|
|
|
|
out(" {}"sv, match.name);
|
2021-07-04 12:49:10 +03:00
|
|
|
|
2023-05-30 19:51:18 +03:00
|
|
|
displayed_at_least_one = true;
|
|
|
|
}
|
2023-05-15 19:35:02 +03:00
|
|
|
|
2023-05-30 19:51:18 +03:00
|
|
|
if (displayed_at_least_one)
|
|
|
|
outln();
|
2021-07-04 12:49:10 +03:00
|
|
|
}
|
|
|
|
|
2023-05-30 19:51:06 +03:00
|
|
|
return matches.size() > 0 ? 0 : 1;
|
2021-07-04 12:49:10 +03:00
|
|
|
}
|