2020-01-18 08:38:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
#include <AK/DeprecatedString.h>
|
2020-02-06 14:04:03 +00:00
|
|
|
#include <LibCore/ArgsParser.h>
|
|
|
|
#include <LibCore/ProcessStatisticsReader.h>
|
2022-02-08 22:21:18 +00:00
|
|
|
#include <LibCore/System.h>
|
|
|
|
#include <LibMain/Main.h>
|
2019-06-07 09:49:31 +00:00
|
|
|
#include <stdio.h>
|
2020-03-08 11:05:14 +00:00
|
|
|
#include <string.h>
|
2019-06-07 09:49:31 +00:00
|
|
|
#include <unistd.h>
|
2019-05-13 12:31:23 +00:00
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
static ErrorOr<int> pid_of(DeprecatedString const& process_name, bool single_shot, bool omit_pid, pid_t pid)
|
2019-05-13 12:31:23 +00:00
|
|
|
{
|
|
|
|
bool displayed_at_least_one = false;
|
|
|
|
|
2022-12-08 13:50:31 +00:00
|
|
|
auto all_processes = TRY(Core::ProcessStatisticsReader::get_all());
|
2019-06-07 09:49:31 +00:00
|
|
|
|
2022-12-08 13:50:31 +00:00
|
|
|
for (auto& it : all_processes.processes) {
|
2021-05-23 09:08:32 +00:00
|
|
|
if (it.name == process_name) {
|
|
|
|
if (!omit_pid || it.pid != pid) {
|
2022-07-11 17:32:29 +00:00
|
|
|
out(displayed_at_least_one ? " {}"sv : "{}"sv, it.pid);
|
2019-05-16 16:47:47 +00:00
|
|
|
displayed_at_least_one = true;
|
2019-05-13 12:31:23 +00:00
|
|
|
|
2019-05-16 16:47:47 +00:00
|
|
|
if (single_shot)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-05-13 12:31:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (displayed_at_least_one)
|
2021-05-31 14:43:25 +00:00
|
|
|
outln();
|
2019-05-13 12:31:23 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-02-08 22:21:18 +00:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments args)
|
2019-05-13 12:31:23 +00:00
|
|
|
{
|
2022-02-08 22:21:18 +00:00
|
|
|
TRY(Core::System::pledge("stdio rpath"));
|
2022-10-14 18:56:19 +00:00
|
|
|
TRY(Core::System::unveil("/sys/kernel/processes", "r"));
|
2022-02-08 22:21:18 +00:00
|
|
|
TRY(Core::System::unveil("/etc/passwd", "r"));
|
|
|
|
TRY(Core::System::unveil(nullptr, nullptr));
|
|
|
|
|
2020-01-27 17:25:36 +00:00
|
|
|
bool single_shot = false;
|
2022-04-01 17:58:27 +00:00
|
|
|
char const* omit_pid_value = nullptr;
|
|
|
|
char const* process_name = nullptr;
|
2019-06-07 09:49:31 +00:00
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
Core::ArgsParser args_parser;
|
2020-01-27 17:25:36 +00:00
|
|
|
args_parser.add_option(single_shot, "Only return one pid", nullptr, 's');
|
|
|
|
args_parser.add_option(omit_pid_value, "Omit the given PID, or the parent process if the special value %PPID is passed", nullptr, 'o', "pid");
|
|
|
|
args_parser.add_positional_argument(process_name, "Process name to search for", "process-name");
|
2019-06-07 09:49:31 +00:00
|
|
|
|
2022-02-08 22:21:18 +00:00
|
|
|
args_parser.parse(args);
|
2019-05-13 12:31:23 +00:00
|
|
|
|
2020-01-27 17:25:36 +00:00
|
|
|
pid_t pid_to_omit = 0;
|
|
|
|
if (omit_pid_value) {
|
2020-06-12 19:07:52 +00:00
|
|
|
if (!strcmp(omit_pid_value, "%PPID")) {
|
2020-01-27 17:25:36 +00:00
|
|
|
pid_to_omit = getppid();
|
2020-06-12 19:07:52 +00:00
|
|
|
} else {
|
2022-07-11 19:53:29 +00:00
|
|
|
auto number = StringView { omit_pid_value, strlen(omit_pid_value) }.to_uint();
|
2020-06-12 19:07:52 +00:00
|
|
|
if (!number.has_value()) {
|
2021-05-31 14:43:25 +00:00
|
|
|
warnln("Invalid value for -o");
|
2022-02-08 22:21:18 +00:00
|
|
|
args_parser.print_usage(stderr, args.argv[0]);
|
2020-06-12 19:07:52 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
pid_to_omit = number.value();
|
2020-01-27 17:25:36 +00:00
|
|
|
}
|
2019-05-13 12:31:23 +00:00
|
|
|
}
|
2020-01-27 17:25:36 +00:00
|
|
|
return pid_of(process_name, single_shot, omit_pid_value != nullptr, pid_to_omit);
|
2019-05-13 12:31:23 +00:00
|
|
|
}
|