2020-01-18 08:38:21 +00:00
|
|
|
/*
|
2021-05-23 09:26:19 +00:00
|
|
|
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
|
2020-01-18 08:38:21 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2021-05-23 09:26:19 +00:00
|
|
|
#include <AK/QuickSort.h>
|
2020-03-01 16:46:06 +00:00
|
|
|
#include <LibCore/ArgsParser.h>
|
2020-02-06 14:04:03 +00:00
|
|
|
#include <LibCore/ProcessStatisticsReader.h>
|
2021-11-23 14:42:17 +00:00
|
|
|
#include <LibCore/System.h>
|
|
|
|
#include <LibMain/Main.h>
|
2022-03-26 06:22:45 +00:00
|
|
|
#include <sys/sysmacros.h>
|
2018-11-17 14:56:09 +00:00
|
|
|
#include <unistd.h>
|
2018-10-23 09:57:38 +00:00
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
static ErrorOr<DeprecatedString> determine_tty_pseudo_name()
|
2022-03-26 06:22:45 +00:00
|
|
|
{
|
|
|
|
struct stat tty_stat;
|
|
|
|
if (fstat(STDIN_FILENO, &tty_stat) < 0) {
|
|
|
|
int saved_errno = errno;
|
|
|
|
perror("fstat");
|
|
|
|
return Error::from_errno(saved_errno);
|
|
|
|
}
|
2022-03-30 08:22:27 +00:00
|
|
|
|
2022-03-26 06:22:45 +00:00
|
|
|
int tty_device_major = major(tty_stat.st_rdev);
|
|
|
|
int tty_device_minor = minor(tty_stat.st_rdev);
|
2022-03-30 08:22:27 +00:00
|
|
|
|
2022-03-26 06:22:45 +00:00
|
|
|
if (tty_device_major == 201) {
|
2022-12-04 18:02:33 +00:00
|
|
|
return DeprecatedString::formatted("pts:{}", tty_device_minor);
|
2022-03-26 06:22:45 +00:00
|
|
|
}
|
2022-03-30 08:22:27 +00:00
|
|
|
|
2022-03-26 06:22:45 +00:00
|
|
|
if (tty_device_major == 4) {
|
2022-12-04 18:02:33 +00:00
|
|
|
return DeprecatedString::formatted("tty:{}", tty_device_minor);
|
2022-03-26 06:22:45 +00:00
|
|
|
}
|
2022-03-30 08:22:27 +00:00
|
|
|
return "n/a";
|
2022-03-26 06:22:45 +00:00
|
|
|
}
|
|
|
|
|
2021-11-23 14:42:17 +00:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
2018-10-23 09:57:38 +00:00
|
|
|
{
|
2021-11-27 22:26:34 +00:00
|
|
|
TRY(Core::System::pledge("stdio rpath tty"));
|
2022-03-26 06:22:45 +00:00
|
|
|
|
|
|
|
auto this_pseudo_tty_name = TRY(determine_tty_pseudo_name());
|
2020-03-01 16:46:06 +00:00
|
|
|
|
2021-11-27 22:26:34 +00:00
|
|
|
TRY(Core::System::pledge("stdio rpath"));
|
2022-10-14 18:56:19 +00:00
|
|
|
TRY(Core::System::unveil("/sys/kernel/processes", "r"));
|
2021-11-23 14:42:17 +00:00
|
|
|
TRY(Core::System::unveil("/etc/passwd", "r"));
|
|
|
|
TRY(Core::System::unveil(nullptr, nullptr));
|
2020-02-18 09:42:04 +00:00
|
|
|
|
2020-03-01 16:46:06 +00:00
|
|
|
enum class Alignment {
|
|
|
|
Left,
|
|
|
|
Right,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Column {
|
2022-12-04 18:02:33 +00:00
|
|
|
DeprecatedString title;
|
2020-03-01 16:46:06 +00:00
|
|
|
Alignment alignment { Alignment::Left };
|
|
|
|
int width { 0 };
|
2022-12-04 18:02:33 +00:00
|
|
|
DeprecatedString buffer;
|
2020-03-01 16:46:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
bool every_process_flag = false;
|
|
|
|
bool full_format_flag = false;
|
2022-12-04 18:02:33 +00:00
|
|
|
DeprecatedString pid_list;
|
2020-03-01 16:46:06 +00:00
|
|
|
|
|
|
|
Core::ArgsParser args_parser;
|
|
|
|
args_parser.add_option(every_process_flag, "Show every process", nullptr, 'e');
|
|
|
|
args_parser.add_option(full_format_flag, "Full format", nullptr, 'f');
|
2021-08-28 09:11:51 +00:00
|
|
|
args_parser.add_option(pid_list, "A comma-separated list of PIDs. Only processes matching those PIDs will be selected", nullptr, 'q', "pid-list");
|
2021-11-23 14:42:17 +00:00
|
|
|
args_parser.parse(arguments);
|
2020-03-01 16:46:06 +00:00
|
|
|
|
|
|
|
Vector<Column> columns;
|
2019-06-02 10:07:24 +00:00
|
|
|
|
2020-03-01 16:46:06 +00:00
|
|
|
int uid_column = -1;
|
|
|
|
int pid_column = -1;
|
|
|
|
int ppid_column = -1;
|
2021-10-10 21:26:39 +00:00
|
|
|
int pgid_column = -1;
|
|
|
|
int sid_column = -1;
|
2020-03-01 16:46:06 +00:00
|
|
|
int state_column = -1;
|
|
|
|
int tty_column = -1;
|
|
|
|
int cmd_column = -1;
|
|
|
|
|
2021-06-03 15:08:03 +00:00
|
|
|
auto add_column = [&](auto title, auto alignment) {
|
|
|
|
columns.append({ title, alignment, 0, {} });
|
2020-03-01 16:46:06 +00:00
|
|
|
return columns.size() - 1;
|
|
|
|
};
|
|
|
|
|
|
|
|
if (full_format_flag) {
|
2021-06-03 15:08:03 +00:00
|
|
|
uid_column = add_column("UID", Alignment::Left);
|
|
|
|
pid_column = add_column("PID", Alignment::Right);
|
|
|
|
ppid_column = add_column("PPID", Alignment::Right);
|
2021-10-10 21:26:39 +00:00
|
|
|
pgid_column = add_column("PGID", Alignment::Right);
|
|
|
|
sid_column = add_column("SID", Alignment::Right);
|
2021-06-03 15:08:03 +00:00
|
|
|
state_column = add_column("STATE", Alignment::Left);
|
|
|
|
tty_column = add_column("TTY", Alignment::Left);
|
|
|
|
cmd_column = add_column("CMD", Alignment::Left);
|
2020-03-01 16:46:06 +00:00
|
|
|
} else {
|
2021-06-03 15:08:03 +00:00
|
|
|
pid_column = add_column("PID", Alignment::Right);
|
|
|
|
tty_column = add_column("TTY", Alignment::Left);
|
|
|
|
cmd_column = add_column("CMD", Alignment::Left);
|
2020-03-01 16:46:06 +00:00
|
|
|
}
|
|
|
|
|
2022-12-08 13:50:31 +00:00
|
|
|
auto all_processes = TRY(Core::ProcessStatisticsReader::get_all());
|
2019-07-17 19:24:47 +00:00
|
|
|
|
2022-12-08 13:50:31 +00:00
|
|
|
auto& processes = all_processes.processes;
|
2021-08-28 09:11:51 +00:00
|
|
|
|
|
|
|
if (!pid_list.is_empty()) {
|
|
|
|
every_process_flag = true;
|
|
|
|
auto string_parts = pid_list.split_view(',');
|
|
|
|
Vector<pid_t> selected_pids;
|
|
|
|
selected_pids.ensure_capacity(string_parts.size());
|
|
|
|
|
|
|
|
for (size_t i = 0; i < string_parts.size(); i++) {
|
|
|
|
auto pid = string_parts[i].to_int();
|
|
|
|
|
|
|
|
if (!pid.has_value()) {
|
|
|
|
warnln("Invalid value for -q: {}", pid_list);
|
|
|
|
warnln("Could not parse '{}' as a PID.", string_parts[i]);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
selected_pids.append(pid.value());
|
|
|
|
}
|
|
|
|
|
|
|
|
processes.remove_all_matching([&](auto& a) { return selected_pids.find(a.pid) == selected_pids.end(); });
|
|
|
|
|
2021-08-28 09:13:15 +00:00
|
|
|
auto processes_sort_predicate = [&selected_pids](auto& a, auto& b) {
|
|
|
|
return selected_pids.find_first_index(a.pid).value() < selected_pids.find_first_index(b.pid).value();
|
|
|
|
};
|
|
|
|
quick_sort(processes, processes_sort_predicate);
|
|
|
|
} else {
|
|
|
|
quick_sort(processes, [](auto& a, auto& b) { return a.pid < b.pid; });
|
|
|
|
}
|
2021-05-23 09:26:19 +00:00
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
Vector<Vector<DeprecatedString>> rows;
|
2021-11-23 14:42:17 +00:00
|
|
|
TRY(rows.try_ensure_capacity(1 + processes.size()));
|
2021-06-03 15:08:03 +00:00
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
Vector<DeprecatedString> header;
|
2021-11-23 14:42:17 +00:00
|
|
|
TRY(header.try_ensure_capacity(columns.size()));
|
2021-06-03 15:08:03 +00:00
|
|
|
for (auto& column : columns)
|
2021-11-23 14:42:17 +00:00
|
|
|
header.unchecked_append(column.title);
|
2021-06-03 15:08:03 +00:00
|
|
|
rows.append(move(header));
|
|
|
|
|
2021-07-14 18:05:59 +00:00
|
|
|
for (auto const& process : processes) {
|
2021-05-23 09:08:32 +00:00
|
|
|
auto tty = process.tty;
|
2022-03-26 06:22:45 +00:00
|
|
|
if (!every_process_flag && tty != this_pseudo_tty_name)
|
2020-03-01 16:46:06 +00:00
|
|
|
continue;
|
|
|
|
|
2021-05-23 09:08:32 +00:00
|
|
|
auto* state = process.threads.is_empty() ? "Zombie" : process.threads.first().state.characters();
|
2020-03-01 16:46:06 +00:00
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
Vector<DeprecatedString> row;
|
2021-11-23 14:42:17 +00:00
|
|
|
TRY(row.try_resize(columns.size()));
|
2021-06-03 15:08:03 +00:00
|
|
|
|
2022-03-26 06:22:45 +00:00
|
|
|
if (tty == "")
|
|
|
|
tty = "n/a";
|
|
|
|
|
2020-03-01 16:46:06 +00:00
|
|
|
if (uid_column != -1)
|
2021-06-03 15:08:03 +00:00
|
|
|
row[uid_column] = process.username;
|
2020-03-01 16:46:06 +00:00
|
|
|
if (pid_column != -1)
|
2022-12-04 18:02:33 +00:00
|
|
|
row[pid_column] = DeprecatedString::number(process.pid);
|
2020-03-01 16:46:06 +00:00
|
|
|
if (ppid_column != -1)
|
2022-12-04 18:02:33 +00:00
|
|
|
row[ppid_column] = DeprecatedString::number(process.ppid);
|
2021-10-10 21:26:39 +00:00
|
|
|
if (pgid_column != -1)
|
2022-12-04 18:02:33 +00:00
|
|
|
row[pgid_column] = DeprecatedString::number(process.pgid);
|
2021-10-10 21:26:39 +00:00
|
|
|
if (sid_column != -1)
|
2022-12-04 18:02:33 +00:00
|
|
|
row[sid_column] = DeprecatedString::number(process.sid);
|
2020-03-01 16:46:06 +00:00
|
|
|
if (tty_column != -1)
|
2021-06-03 15:08:03 +00:00
|
|
|
row[tty_column] = tty;
|
2020-03-01 16:46:06 +00:00
|
|
|
if (state_column != -1)
|
2021-06-03 15:08:03 +00:00
|
|
|
row[state_column] = state;
|
2020-03-01 16:46:06 +00:00
|
|
|
if (cmd_column != -1)
|
2021-06-03 15:08:03 +00:00
|
|
|
row[cmd_column] = process.name;
|
|
|
|
|
2021-11-23 14:42:17 +00:00
|
|
|
TRY(rows.try_append(move(row)));
|
2021-06-03 15:08:03 +00:00
|
|
|
}
|
2020-03-01 16:46:06 +00:00
|
|
|
|
2021-06-03 15:08:03 +00:00
|
|
|
for (size_t i = 0; i < columns.size(); i++) {
|
|
|
|
auto& column = columns[i];
|
|
|
|
for (auto& row : rows)
|
|
|
|
column.width = max(column.width, static_cast<int>(row[i].length()));
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto& row : rows) {
|
|
|
|
for (size_t i = 0; i < columns.size(); i++) {
|
|
|
|
auto& column = columns[i];
|
|
|
|
auto& cell_text = row[i];
|
|
|
|
if (!column.width) {
|
|
|
|
out("{}", cell_text);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (column.alignment == Alignment::Right)
|
|
|
|
out("{1:>{0}} ", column.width, cell_text);
|
|
|
|
else
|
|
|
|
out("{1:{0}} ", column.width, cell_text);
|
|
|
|
if (i != columns.size() - 1)
|
|
|
|
out(" ");
|
|
|
|
}
|
2021-05-31 14:43:25 +00:00
|
|
|
outln();
|
2018-10-23 09:57:38 +00:00
|
|
|
}
|
2019-07-17 19:24:47 +00:00
|
|
|
|
2018-10-23 09:57:38 +00:00
|
|
|
return 0;
|
|
|
|
}
|