2020-07-30 21:11:17 +03:00
|
|
|
/*
|
2020-08-01 17:30:19 +03:00
|
|
|
* Copyright (c) 2020, Maciej Zygmanowski <sppmacd@pm.me>
|
2020-07-30 21:11:17 +03:00
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-07-30 21:11:17 +03:00
|
|
|
*/
|
|
|
|
|
2023-03-09 20:37:16 +03:00
|
|
|
#include <AK/CharacterTypes.h>
|
2022-12-04 21:02:33 +03:00
|
|
|
#include <AK/DeprecatedString.h>
|
2020-09-26 18:58:58 +03:00
|
|
|
#include <AK/GenericLexer.h>
|
2020-07-30 21:11:17 +03:00
|
|
|
#include <AK/JsonArray.h>
|
|
|
|
#include <AK/JsonObject.h>
|
|
|
|
#include <AK/JsonValue.h>
|
|
|
|
#include <AK/Vector.h>
|
|
|
|
#include <LibCore/ArgsParser.h>
|
2023-02-09 05:02:46 +03:00
|
|
|
#include <LibCore/File.h>
|
2020-07-30 21:11:17 +03:00
|
|
|
#include <LibCore/ProcessStatisticsReader.h>
|
2022-01-20 22:54:42 +03:00
|
|
|
#include <LibCore/System.h>
|
|
|
|
#include <LibMain/Main.h>
|
2020-09-27 13:44:03 +03:00
|
|
|
#include <ctype.h>
|
2020-07-30 21:11:17 +03:00
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
struct OpenFile {
|
|
|
|
int fd;
|
|
|
|
int pid;
|
2022-12-04 21:02:33 +03:00
|
|
|
DeprecatedString type;
|
|
|
|
DeprecatedString name;
|
|
|
|
DeprecatedString state;
|
|
|
|
DeprecatedString full_name;
|
2020-07-30 21:11:17 +03:00
|
|
|
};
|
|
|
|
|
2020-09-26 18:58:58 +03:00
|
|
|
static bool parse_name(StringView name, OpenFile& file)
|
|
|
|
{
|
|
|
|
GenericLexer lexer(name);
|
|
|
|
auto component1 = lexer.consume_until(':');
|
2022-01-25 00:47:22 +03:00
|
|
|
lexer.ignore();
|
2020-09-26 18:58:58 +03:00
|
|
|
|
|
|
|
if (lexer.tell_remaining() == 0) {
|
|
|
|
file.name = component1;
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
file.type = component1;
|
2023-03-09 20:37:16 +03:00
|
|
|
auto component2 = lexer.consume_while([](char c) { return is_ascii_printable(c) && c != '('; });
|
|
|
|
lexer.ignore_while(is_ascii_space);
|
2020-09-26 18:58:58 +03:00
|
|
|
file.name = component2;
|
|
|
|
|
|
|
|
if (lexer.tell_remaining() == 0) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
if (!lexer.consume_specific('(')) {
|
2021-01-09 17:09:40 +03:00
|
|
|
dbgln("parse_name: expected (");
|
2020-09-26 18:58:58 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto component3 = lexer.consume_until(')');
|
2022-01-25 00:47:22 +03:00
|
|
|
lexer.ignore();
|
2020-09-26 18:58:58 +03:00
|
|
|
if (lexer.tell_remaining() != 0) {
|
2021-01-09 17:09:40 +03:00
|
|
|
dbgln("parse_name: expected EOF");
|
2020-09-26 18:58:58 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
file.state = component3;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-11 00:48:37 +03:00
|
|
|
static Vector<OpenFile> get_open_files_by_pid(pid_t pid)
|
2020-07-30 21:11:17 +03:00
|
|
|
{
|
2023-02-09 05:02:46 +03:00
|
|
|
auto file = Core::File::open(DeprecatedString::formatted("/proc/{}/fds", pid), Core::File::OpenMode::Read);
|
2020-07-30 21:11:17 +03:00
|
|
|
if (file.is_error()) {
|
2021-05-31 17:43:25 +03:00
|
|
|
outln("lsof: PID {}: {}", pid, file.error());
|
2020-07-30 21:11:17 +03:00
|
|
|
return Vector<OpenFile>();
|
|
|
|
}
|
2022-12-11 19:49:00 +03:00
|
|
|
auto data = file.value()->read_until_eof();
|
2022-09-14 17:49:39 +03:00
|
|
|
if (data.is_error()) {
|
|
|
|
outln("lsof: PID {}: {}", pid, data.error());
|
|
|
|
return {};
|
|
|
|
}
|
2020-07-30 21:11:17 +03:00
|
|
|
|
2022-09-14 17:49:39 +03:00
|
|
|
auto json_or_error = JsonValue::from_string(data.value());
|
2021-12-25 18:13:33 +03:00
|
|
|
if (json_or_error.is_error()) {
|
|
|
|
outln("lsof: {}", json_or_error.error());
|
|
|
|
return Vector<OpenFile>();
|
|
|
|
}
|
|
|
|
auto json = json_or_error.release_value();
|
2020-07-30 21:11:17 +03:00
|
|
|
|
|
|
|
Vector<OpenFile> files;
|
2022-04-01 20:58:27 +03:00
|
|
|
json.as_array().for_each([pid, &files](JsonValue const& object) {
|
2020-07-30 21:11:17 +03:00
|
|
|
OpenFile open_file;
|
|
|
|
open_file.pid = pid;
|
2022-12-22 17:28:18 +03:00
|
|
|
open_file.fd = object.as_object().get_integer<int>("fd"sv).value();
|
2020-09-26 18:58:58 +03:00
|
|
|
|
2022-12-22 17:28:18 +03:00
|
|
|
DeprecatedString name = object.as_object().get_deprecated_string("absolute_path"sv).value_or({});
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(parse_name(name, open_file));
|
2020-09-26 18:58:58 +03:00
|
|
|
open_file.full_name = name;
|
|
|
|
|
2020-07-30 21:11:17 +03:00
|
|
|
files.append(open_file);
|
|
|
|
});
|
|
|
|
return files;
|
|
|
|
}
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
static void display_entry(OpenFile const& file, Core::ProcessStatistics const& statistics)
|
2020-07-30 21:11:17 +03:00
|
|
|
{
|
2021-05-31 17:43:25 +03:00
|
|
|
outln("{:28} {:>4} {:>4} {:10} {:>4} {}", statistics.name, file.pid, statistics.pgid, statistics.username, file.fd, file.full_name);
|
2020-07-30 21:11:17 +03:00
|
|
|
}
|
|
|
|
|
2022-01-20 22:54:42 +03:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
2020-07-30 21:11:17 +03:00
|
|
|
{
|
2022-01-20 22:54:42 +03:00
|
|
|
TRY(Core::System::pledge("stdio rpath proc"));
|
2020-07-30 21:11:17 +03:00
|
|
|
|
2022-01-20 22:54:42 +03:00
|
|
|
TRY(Core::System::unveil("/proc", "r"));
|
2020-07-30 21:11:17 +03:00
|
|
|
// needed by ProcessStatisticsReader::get_all()
|
2022-10-14 21:56:19 +03:00
|
|
|
TRY(Core::System::unveil("/sys/kernel/processes", "r"));
|
2022-01-20 22:54:42 +03:00
|
|
|
TRY(Core::System::unveil("/etc/passwd", "r"));
|
|
|
|
TRY(Core::System::unveil(nullptr, nullptr));
|
2020-08-01 17:30:19 +03:00
|
|
|
|
2020-07-30 21:11:17 +03:00
|
|
|
bool arg_all_processes { false };
|
|
|
|
int arg_fd { -1 };
|
2022-01-30 21:21:08 +03:00
|
|
|
StringView arg_uid;
|
2020-07-30 21:11:17 +03:00
|
|
|
int arg_uid_int = -1;
|
|
|
|
int arg_pgid { -1 };
|
|
|
|
pid_t arg_pid { -1 };
|
2022-01-30 21:21:08 +03:00
|
|
|
StringView arg_filename;
|
2020-07-30 21:11:17 +03:00
|
|
|
|
2022-01-20 22:54:42 +03:00
|
|
|
if (arguments.strings.size() == 1)
|
2020-07-30 21:11:17 +03:00
|
|
|
arg_all_processes = true;
|
|
|
|
else {
|
2020-12-05 18:22:58 +03:00
|
|
|
Core::ArgsParser parser;
|
|
|
|
parser.set_general_help("List open files of a processes. This can mean actual files in the file system, sockets, pipes, etc.");
|
2020-09-26 18:58:58 +03:00
|
|
|
parser.add_option(arg_pid, "Select by PID", nullptr, 'p', "pid");
|
|
|
|
parser.add_option(arg_fd, "Select by file descriptor", nullptr, 'd', "fd");
|
|
|
|
parser.add_option(arg_uid, "Select by login/UID", nullptr, 'u', "login/UID");
|
|
|
|
parser.add_option(arg_pgid, "Select by process group ID", nullptr, 'g', "PGID");
|
2021-04-29 22:46:15 +03:00
|
|
|
parser.add_positional_argument(arg_filename, "Filename", "filename", Core::ArgsParser::Required::No);
|
2022-01-20 22:54:42 +03:00
|
|
|
parser.parse(arguments);
|
2020-07-30 21:11:17 +03:00
|
|
|
}
|
|
|
|
{
|
|
|
|
// try convert UID to int
|
2022-12-04 21:02:33 +03:00
|
|
|
auto arg = DeprecatedString(arg_uid).to_int();
|
2020-07-30 21:11:17 +03:00
|
|
|
if (arg.has_value())
|
|
|
|
arg_uid_int = arg.value();
|
|
|
|
}
|
|
|
|
|
2021-05-31 17:43:25 +03:00
|
|
|
outln("{:28} {:>4} {:>4} {:10} {:>4} {}", "COMMAND", "PID", "PGID", "USER", "FD", "NAME");
|
2022-12-08 16:50:31 +03:00
|
|
|
auto all_processes = TRY(Core::ProcessStatisticsReader::get_all());
|
2020-07-30 21:11:17 +03:00
|
|
|
if (arg_pid == -1) {
|
2022-12-08 16:50:31 +03:00
|
|
|
for (auto& process : all_processes.processes) {
|
2021-05-23 12:08:32 +03:00
|
|
|
if (process.pid == 0)
|
2020-07-30 21:11:17 +03:00
|
|
|
continue;
|
2021-05-23 12:08:32 +03:00
|
|
|
auto open_files = get_open_files_by_pid(process.pid);
|
2020-07-30 21:11:17 +03:00
|
|
|
|
|
|
|
if (open_files.is_empty())
|
|
|
|
continue;
|
|
|
|
|
2021-05-23 12:08:32 +03:00
|
|
|
for (auto& file : open_files) {
|
2020-07-30 21:11:17 +03:00
|
|
|
if ((arg_all_processes)
|
|
|
|
|| (arg_fd != -1 && file.fd == arg_fd)
|
2021-05-23 12:08:32 +03:00
|
|
|
|| (arg_uid_int != -1 && (int)process.uid == arg_uid_int)
|
2022-01-30 21:21:08 +03:00
|
|
|
|| (!arg_uid.is_empty() && process.username == arg_uid)
|
2021-05-23 12:08:32 +03:00
|
|
|
|| (arg_pgid != -1 && (int)process.pgid == arg_pgid)
|
2022-01-30 21:21:08 +03:00
|
|
|
|| (!arg_filename.is_empty() && file.name == arg_filename))
|
2021-05-23 12:08:32 +03:00
|
|
|
display_entry(file, process);
|
2020-07-30 21:11:17 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
auto open_files = get_open_files_by_pid(arg_pid);
|
|
|
|
|
|
|
|
if (open_files.is_empty())
|
|
|
|
return 0;
|
|
|
|
|
2021-05-31 20:03:54 +03:00
|
|
|
for (auto& file : open_files) {
|
2022-12-08 16:50:31 +03:00
|
|
|
display_entry(file, *all_processes.processes.find_if([&](auto& entry) { return entry.pid == arg_pid; }));
|
2020-07-30 21:11:17 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|