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
|
|
|
*/
|
|
|
|
|
2020-01-27 20:25:36 +03:00
|
|
|
#include <AK/String.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>
|
|
|
|
#include <stdlib.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
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
static ErrorOr<int> pid_of(String const& process_name, bool single_shot, bool omit_pid, pid_t pid)
|
2019-05-13 05:31:23 -07:00
|
|
|
{
|
|
|
|
bool displayed_at_least_one = false;
|
|
|
|
|
2021-07-14 12:05:59 -06:00
|
|
|
auto all_processes = Core::ProcessStatisticsReader::get_all();
|
|
|
|
if (!all_processes.has_value())
|
2021-01-01 20:58:47 -07:00
|
|
|
return 1;
|
2019-06-07 11:49:31 +02:00
|
|
|
|
2021-07-14 12:05:59 -06:00
|
|
|
for (auto& it : all_processes.value().processes) {
|
2021-05-23 11:08:32 +02: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 18:47:47 +02:00
|
|
|
displayed_at_least_one = true;
|
2019-05-13 05:31:23 -07:00
|
|
|
|
2019-05-16 18:47:47 +02:00
|
|
|
if (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));
|
|
|
|
|
2020-01-27 20:25:36 +03:00
|
|
|
bool single_shot = false;
|
2022-04-01 20:58:27 +03:00
|
|
|
char const* omit_pid_value = nullptr;
|
|
|
|
char const* process_name = nullptr;
|
2019-06-07 11:49:31 +02:00
|
|
|
|
2020-02-02 12:34:39 +01:00
|
|
|
Core::ArgsParser args_parser;
|
2020-01-27 20:25:36 +03: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 11:49:31 +02:00
|
|
|
|
2022-02-08 23:21:18 +01:00
|
|
|
args_parser.parse(args);
|
2019-05-13 05:31:23 -07:00
|
|
|
|
2020-01-27 20:25:36 +03:00
|
|
|
pid_t pid_to_omit = 0;
|
|
|
|
if (omit_pid_value) {
|
2020-06-12 21:07:52 +02:00
|
|
|
if (!strcmp(omit_pid_value, "%PPID")) {
|
2020-01-27 20:25:36 +03:00
|
|
|
pid_to_omit = getppid();
|
2020-06-12 21:07:52 +02: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 21:07:52 +02:00
|
|
|
if (!number.has_value()) {
|
2021-05-31 15:43:25 +01:00
|
|
|
warnln("Invalid value for -o");
|
2022-02-08 23:21:18 +01:00
|
|
|
args_parser.print_usage(stderr, args.argv[0]);
|
2020-06-12 21:07:52 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
pid_to_omit = number.value();
|
2020-01-27 20:25:36 +03:00
|
|
|
}
|
2019-05-13 05:31:23 -07:00
|
|
|
}
|
2020-01-27 20:25:36 +03:00
|
|
|
return pid_of(process_name, single_shot, omit_pid_value != nullptr, pid_to_omit);
|
2019-05-13 05:31:23 -07:00
|
|
|
}
|