mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 18:47:34 +00:00
pgrep: Add -U
option to filter matches by UID or login name
This commit is contained in:
parent
cb6a2d60d3
commit
821bf5e071
2 changed files with 33 additions and 1 deletions
|
@ -5,7 +5,7 @@ pgrep - look up processes based on name
|
||||||
## Synopsis
|
## Synopsis
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
$ pgrep [--count] [-d delimiter] [--ignore-case] [--list-name] [--invert-match] <process-name>
|
$ pgrep [--count] [-d delimiter] [--ignore-case] [--list-name] [--uid uid-list] [--invert-match] <process-name>
|
||||||
```
|
```
|
||||||
|
|
||||||
## Options
|
## Options
|
||||||
|
@ -14,6 +14,8 @@ $ pgrep [--count] [-d delimiter] [--ignore-case] [--list-name] [--invert-match]
|
||||||
* `-d`, `--delimiter`: Set the string used to delimit multiple pids
|
* `-d`, `--delimiter`: Set the string used to delimit multiple pids
|
||||||
* `-i`, `--ignore-case`: Make matches case-insensitive
|
* `-i`, `--ignore-case`: Make matches case-insensitive
|
||||||
* `-l`, `--list-name`: List the process name in addition to its pid
|
* `-l`, `--list-name`: List the process name in addition to its pid
|
||||||
|
* `-U uid-list`, `--uid uid-list`: Select only processes whose UID is in the given comma-separated list. Login name or numerical user ID may be used
|
||||||
|
|
||||||
* `-v`, `--invert-match`: Select non-matching lines
|
* `-v`, `--invert-match`: Select non-matching lines
|
||||||
|
|
||||||
## Arguments
|
## Arguments
|
||||||
|
|
|
@ -7,6 +7,7 @@
|
||||||
|
|
||||||
#include <AK/QuickSort.h>
|
#include <AK/QuickSort.h>
|
||||||
#include <AK/Vector.h>
|
#include <AK/Vector.h>
|
||||||
|
#include <LibCore/Account.h>
|
||||||
#include <LibCore/ArgsParser.h>
|
#include <LibCore/ArgsParser.h>
|
||||||
#include <LibCore/ProcessStatisticsReader.h>
|
#include <LibCore/ProcessStatisticsReader.h>
|
||||||
#include <LibCore/System.h>
|
#include <LibCore/System.h>
|
||||||
|
@ -17,6 +18,7 @@ ErrorOr<int> serenity_main(Main::Arguments args)
|
||||||
{
|
{
|
||||||
TRY(Core::System::pledge("stdio rpath"));
|
TRY(Core::System::pledge("stdio rpath"));
|
||||||
TRY(Core::System::unveil("/sys/kernel/processes", "r"));
|
TRY(Core::System::unveil("/sys/kernel/processes", "r"));
|
||||||
|
TRY(Core::System::unveil("/etc/group", "r"));
|
||||||
TRY(Core::System::unveil("/etc/passwd", "r"));
|
TRY(Core::System::unveil("/etc/passwd", "r"));
|
||||||
TRY(Core::System::unveil(nullptr, nullptr));
|
TRY(Core::System::unveil(nullptr, nullptr));
|
||||||
|
|
||||||
|
@ -25,6 +27,7 @@ ErrorOr<int> serenity_main(Main::Arguments args)
|
||||||
bool case_insensitive = false;
|
bool case_insensitive = false;
|
||||||
bool list_process_name = false;
|
bool list_process_name = false;
|
||||||
bool invert_match = false;
|
bool invert_match = false;
|
||||||
|
HashTable<uid_t> uids_to_filter_by;
|
||||||
StringView pattern;
|
StringView pattern;
|
||||||
|
|
||||||
Core::ArgsParser args_parser;
|
Core::ArgsParser args_parser;
|
||||||
|
@ -32,6 +35,30 @@ ErrorOr<int> serenity_main(Main::Arguments args)
|
||||||
args_parser.add_option(pid_delimiter, "Set the string used to delimit multiple pids", "delimiter", 'd', nullptr);
|
args_parser.add_option(pid_delimiter, "Set the string used to delimit multiple pids", "delimiter", 'd', nullptr);
|
||||||
args_parser.add_option(case_insensitive, "Make matches case-insensitive", "ignore-case", 'i');
|
args_parser.add_option(case_insensitive, "Make matches case-insensitive", "ignore-case", 'i');
|
||||||
args_parser.add_option(list_process_name, "List the process name in addition to its pid", "list-name", 'l');
|
args_parser.add_option(list_process_name, "List the process name in addition to its pid", "list-name", 'l');
|
||||||
|
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;
|
||||||
|
},
|
||||||
|
});
|
||||||
args_parser.add_option(invert_match, "Select non-matching lines", "invert-match", 'v');
|
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");
|
args_parser.add_positional_argument(pattern, "Process name to search for", "process-name");
|
||||||
args_parser.parse(args);
|
args_parser.parse(args);
|
||||||
|
@ -51,6 +78,9 @@ ErrorOr<int> serenity_main(Main::Arguments args)
|
||||||
for (auto const& it : all_processes.processes) {
|
for (auto const& it : all_processes.processes) {
|
||||||
auto result = re.match(it.name, PosixFlags::Global);
|
auto result = re.match(it.name, PosixFlags::Global);
|
||||||
if (result.success ^ invert_match) {
|
if (result.success ^ invert_match) {
|
||||||
|
if (!uids_to_filter_by.is_empty() && !uids_to_filter_by.contains(it.uid))
|
||||||
|
continue;
|
||||||
|
|
||||||
matches.append(it);
|
matches.append(it);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue