2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
#include <AK/DeprecatedString.h>
|
2020-02-06 15:04:03 +01:00
|
|
|
#include <LibCore/ArgsParser.h>
|
|
|
|
|
#include <LibCore/ProcessStatisticsReader.h>
|
2022-02-08 23:21:18 +01:00
|
|
|
#include <LibCore/System.h>
|
|
|
|
|
#include <LibMain/Main.h>
|
2019-06-07 11:49:31 +02:00
|
|
|
#include <stdio.h>
|
2020-03-08 12:05:14 +01:00
|
|
|
#include <string.h>
|
2019-06-07 11:49:31 +02:00
|
|
|
#include <unistd.h>
|
2019-05-13 05:31:23 -07:00
|
|
|
|
2023-05-14 06:05:54 +01:00
|
|
|
struct Options {
|
|
|
|
|
bool single_shot { false };
|
|
|
|
|
Optional<pid_t> pid_to_omit;
|
|
|
|
|
StringView process_name;
|
2023-05-14 06:06:11 +01:00
|
|
|
StringView pid_separator { " "sv };
|
2023-05-14 06:05:54 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static ErrorOr<int> pid_of(Options const& options)
|
2019-05-13 05:31:23 -07:00
|
|
|
{
|
|
|
|
|
bool displayed_at_least_one = false;
|
|
|
|
|
|
2022-12-08 14:50:31 +01:00
|
|
|
auto all_processes = TRY(Core::ProcessStatisticsReader::get_all());
|
2019-06-07 11:49:31 +02:00
|
|
|
|
2022-12-08 14:50:31 +01:00
|
|
|
for (auto& it : all_processes.processes) {
|
2023-05-14 06:05:54 +01:00
|
|
|
if (it.name != options.process_name || options.pid_to_omit == it.pid)
|
|
|
|
|
continue;
|
2019-05-13 05:31:23 -07:00
|
|
|
|
2023-05-14 06:06:11 +01:00
|
|
|
if (displayed_at_least_one)
|
|
|
|
|
out("{}{}"sv, options.pid_separator, it.pid);
|
|
|
|
|
else
|
|
|
|
|
out("{}"sv, it.pid);
|
|
|
|
|
|
2023-05-14 06:05:54 +01:00
|
|
|
displayed_at_least_one = true;
|
|
|
|
|
|
|
|
|
|
if (options.single_shot)
|
|
|
|
|
break;
|
2019-05-13 05:31:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (displayed_at_least_one)
|
2021-05-31 15:43:25 +01:00
|
|
|
outln();
|
2019-05-13 05:31:23 -07:00
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-08 23:21:18 +01:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments args)
|
2019-05-13 05:31:23 -07:00
|
|
|
{
|
2022-02-08 23:21:18 +01:00
|
|
|
TRY(Core::System::pledge("stdio rpath"));
|
2022-10-14 21:56:19 +03:00
|
|
|
TRY(Core::System::unveil("/sys/kernel/processes", "r"));
|
2022-02-08 23:21:18 +01:00
|
|
|
TRY(Core::System::unveil("/etc/passwd", "r"));
|
|
|
|
|
TRY(Core::System::unveil(nullptr, nullptr));
|
|
|
|
|
|
2023-05-14 06:05:54 +01:00
|
|
|
Options options;
|
2019-06-07 11:49:31 +02:00
|
|
|
|
2020-02-02 12:34:39 +01:00
|
|
|
Core::ArgsParser args_parser;
|
2023-05-14 06:05:54 +01:00
|
|
|
args_parser.add_option(Core::ArgsParser::Option {
|
|
|
|
|
.argument_mode = Core::ArgsParser::OptionArgumentMode::Required,
|
|
|
|
|
.help_string = "Omit the given PID, or the parent process if the special value %PPID is passed",
|
|
|
|
|
.short_name = 'o',
|
|
|
|
|
.value_name = "pid",
|
|
|
|
|
.accept_value = [&options](auto omit_pid_value) {
|
|
|
|
|
if (omit_pid_value.is_empty())
|
|
|
|
|
return false;
|
2019-06-07 11:49:31 +02:00
|
|
|
|
2023-05-14 06:05:54 +01:00
|
|
|
if (omit_pid_value == "%PPID"sv) {
|
|
|
|
|
options.pid_to_omit = getppid();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2019-05-13 05:31:23 -07:00
|
|
|
|
2023-03-01 00:11:43 +03:30
|
|
|
auto number = omit_pid_value.to_uint();
|
2023-05-14 06:05:54 +01:00
|
|
|
if (!number.has_value())
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
options.pid_to_omit = number.value();
|
|
|
|
|
return true;
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
args_parser.add_option(options.single_shot, "Only return one pid", nullptr, 's');
|
2023-05-14 06:06:11 +01:00
|
|
|
args_parser.add_option(options.pid_separator, "Use `separator` to separate multiple pids", nullptr, 'S', "separator");
|
2023-05-14 06:05:54 +01:00
|
|
|
args_parser.add_positional_argument(options.process_name, "Process name to search for", "process-name");
|
|
|
|
|
|
|
|
|
|
args_parser.parse(args);
|
|
|
|
|
return pid_of(options);
|
2019-05-13 05:31:23 -07:00
|
|
|
}
|