2020-07-30 18:11:17 +00:00
|
|
|
/*
|
2020-08-01 14:30:19 +00:00
|
|
|
* Copyright (c) 2020, Maciej Zygmanowski <sppmacd@pm.me>
|
2020-07-30 18:11:17 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-07-30 18:11:17 +00:00
|
|
|
*/
|
|
|
|
|
2020-11-15 12:11:21 +00:00
|
|
|
#include <AK/ByteBuffer.h>
|
2020-09-26 15:58:58 +00:00
|
|
|
#include <AK/GenericLexer.h>
|
2020-07-30 18:11:17 +00:00
|
|
|
#include <AK/HashMap.h>
|
|
|
|
#include <AK/JsonArray.h>
|
|
|
|
#include <AK/JsonObject.h>
|
|
|
|
#include <AK/JsonParser.h>
|
|
|
|
#include <AK/JsonValue.h>
|
|
|
|
#include <AK/String.h>
|
|
|
|
#include <AK/Vector.h>
|
|
|
|
#include <LibCore/ArgsParser.h>
|
|
|
|
#include <LibCore/File.h>
|
|
|
|
#include <LibCore/ProcessStatisticsReader.h>
|
2020-09-27 10:44:03 +00:00
|
|
|
#include <ctype.h>
|
2020-07-30 18:11:17 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
struct OpenFile {
|
|
|
|
int fd;
|
|
|
|
int pid;
|
2020-09-26 15:58:58 +00:00
|
|
|
String type;
|
2020-07-30 18:11:17 +00:00
|
|
|
String name;
|
2020-09-26 15:58:58 +00:00
|
|
|
String state;
|
|
|
|
String full_name;
|
2020-07-30 18:11:17 +00:00
|
|
|
};
|
|
|
|
|
2020-09-26 15:58:58 +00:00
|
|
|
static bool parse_name(StringView name, OpenFile& file)
|
|
|
|
{
|
|
|
|
GenericLexer lexer(name);
|
|
|
|
auto component1 = lexer.consume_until(':');
|
|
|
|
|
|
|
|
if (lexer.tell_remaining() == 0) {
|
|
|
|
file.name = component1;
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
file.type = component1;
|
2021-05-19 02:24:37 +00:00
|
|
|
auto component2 = lexer.consume_while([](char c) { return isprint(c) && c != '('; });
|
2020-09-27 10:44:03 +00:00
|
|
|
lexer.ignore_while(isspace);
|
2020-09-26 15:58:58 +00:00
|
|
|
file.name = component2;
|
|
|
|
|
|
|
|
if (lexer.tell_remaining() == 0) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
if (!lexer.consume_specific('(')) {
|
2021-01-09 14:09:40 +00:00
|
|
|
dbgln("parse_name: expected (");
|
2020-09-26 15:58:58 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto component3 = lexer.consume_until(')');
|
|
|
|
if (lexer.tell_remaining() != 0) {
|
2021-01-09 14:09:40 +00:00
|
|
|
dbgln("parse_name: expected EOF");
|
2020-09-26 15:58:58 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
file.state = component3;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-10 21:48:37 +00:00
|
|
|
static Vector<OpenFile> get_open_files_by_pid(pid_t pid)
|
2020-07-30 18:11:17 +00:00
|
|
|
{
|
2021-05-12 09:26:43 +00:00
|
|
|
auto file = Core::File::open(String::formatted("/proc/{}/fds", pid), Core::OpenMode::ReadOnly);
|
2020-07-30 18:11:17 +00:00
|
|
|
if (file.is_error()) {
|
2021-05-31 14:43:25 +00:00
|
|
|
outln("lsof: PID {}: {}", pid, file.error());
|
2020-07-30 18:11:17 +00:00
|
|
|
return Vector<OpenFile>();
|
|
|
|
}
|
|
|
|
auto data = file.value()->read_all();
|
|
|
|
|
|
|
|
JsonParser parser(data);
|
|
|
|
auto result = parser.parse();
|
|
|
|
|
|
|
|
if (!result.has_value()) {
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY_NOT_REACHED();
|
2020-07-30 18:11:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Vector<OpenFile> files;
|
|
|
|
result.value().as_array().for_each([pid, &files](const JsonValue& object) {
|
|
|
|
OpenFile open_file;
|
|
|
|
open_file.pid = pid;
|
|
|
|
open_file.fd = object.as_object().get("fd").to_int();
|
2020-09-26 15:58:58 +00:00
|
|
|
|
|
|
|
String name = object.as_object().get("absolute_path").to_string();
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(parse_name(name, open_file));
|
2020-09-26 15:58:58 +00:00
|
|
|
open_file.full_name = name;
|
|
|
|
|
2020-07-30 18:11:17 +00:00
|
|
|
files.append(open_file);
|
|
|
|
});
|
|
|
|
return files;
|
|
|
|
}
|
|
|
|
|
2020-08-10 21:48:37 +00:00
|
|
|
static void display_entry(const OpenFile& file, const Core::ProcessStatistics& statistics)
|
2020-07-30 18:11:17 +00:00
|
|
|
{
|
2021-05-31 14:43:25 +00:00
|
|
|
outln("{:28} {:>4} {:>4} {:10} {:>4} {}", statistics.name, file.pid, statistics.pgid, statistics.username, file.fd, file.full_name);
|
2020-07-30 18:11:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char* argv[])
|
|
|
|
{
|
|
|
|
if (pledge("stdio rpath proc", nullptr) < 0) {
|
|
|
|
perror("pledge");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (unveil("/proc", "r") < 0) {
|
|
|
|
perror("unveil /proc");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// needed by ProcessStatisticsReader::get_all()
|
|
|
|
if (unveil("/etc/passwd", "r") < 0) {
|
2020-08-01 14:30:19 +00:00
|
|
|
perror("unveil /etc/passwd");
|
2020-07-30 18:11:17 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-08-01 14:30:19 +00:00
|
|
|
unveil(nullptr, nullptr);
|
|
|
|
|
2020-07-30 18:11:17 +00:00
|
|
|
bool arg_all_processes { false };
|
|
|
|
int arg_fd { -1 };
|
|
|
|
const char* arg_uid { nullptr };
|
|
|
|
int arg_uid_int = -1;
|
|
|
|
int arg_pgid { -1 };
|
|
|
|
pid_t arg_pid { -1 };
|
2021-04-29 19:46:15 +00:00
|
|
|
const char* arg_filename { nullptr };
|
2020-07-30 18:11:17 +00:00
|
|
|
|
|
|
|
if (argc == 1)
|
|
|
|
arg_all_processes = true;
|
|
|
|
else {
|
2020-12-05 15:22:58 +00: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 15:58:58 +00: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 19:46:15 +00:00
|
|
|
parser.add_positional_argument(arg_filename, "Filename", "filename", Core::ArgsParser::Required::No);
|
2020-07-30 18:11:17 +00:00
|
|
|
parser.parse(argc, argv);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
// try convert UID to int
|
|
|
|
auto arg = String(arg_uid).to_int();
|
|
|
|
if (arg.has_value())
|
|
|
|
arg_uid_int = arg.value();
|
|
|
|
}
|
|
|
|
|
2021-05-31 14:43:25 +00:00
|
|
|
outln("{:28} {:>4} {:>4} {:10} {:>4} {}", "COMMAND", "PID", "PGID", "USER", "FD", "NAME");
|
2021-01-02 03:58:47 +00:00
|
|
|
auto processes = Core::ProcessStatisticsReader::get_all();
|
|
|
|
if (!processes.has_value())
|
|
|
|
return 1;
|
2020-07-30 18:11:17 +00:00
|
|
|
if (arg_pid == -1) {
|
2021-05-23 09:08:32 +00:00
|
|
|
for (auto& process : processes.value()) {
|
|
|
|
if (process.pid == 0)
|
2020-07-30 18:11:17 +00:00
|
|
|
continue;
|
2021-05-23 09:08:32 +00:00
|
|
|
auto open_files = get_open_files_by_pid(process.pid);
|
2020-07-30 18:11:17 +00:00
|
|
|
|
|
|
|
if (open_files.is_empty())
|
|
|
|
continue;
|
|
|
|
|
2021-05-23 09:08:32 +00:00
|
|
|
for (auto& file : open_files) {
|
2020-07-30 18:11:17 +00:00
|
|
|
if ((arg_all_processes)
|
|
|
|
|| (arg_fd != -1 && file.fd == arg_fd)
|
2021-05-23 09:08:32 +00:00
|
|
|
|| (arg_uid_int != -1 && (int)process.uid == arg_uid_int)
|
|
|
|
|| (arg_uid != nullptr && process.username == arg_uid)
|
|
|
|
|| (arg_pgid != -1 && (int)process.pgid == arg_pgid)
|
2021-04-29 19:46:15 +00:00
|
|
|
|| (arg_filename != nullptr && file.name == arg_filename))
|
2021-05-23 09:08:32 +00:00
|
|
|
display_entry(file, process);
|
2020-07-30 18:11:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
auto open_files = get_open_files_by_pid(arg_pid);
|
|
|
|
|
|
|
|
if (open_files.is_empty())
|
|
|
|
return 0;
|
|
|
|
|
2021-05-31 17:03:54 +00:00
|
|
|
for (auto& file : open_files) {
|
2021-05-23 09:08:32 +00:00
|
|
|
display_entry(file, *processes->find_if([&](auto& entry) { return entry.pid == arg_pid; }));
|
2020-07-30 18:11:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|