Serenity Operating System
1/*
2 * Copyright (c) 2020, Maciej Zygmanowski <sppmacd@pm.me>
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
7#include <AK/DeprecatedString.h>
8#include <AK/GenericLexer.h>
9#include <AK/JsonArray.h>
10#include <AK/JsonObject.h>
11#include <AK/JsonValue.h>
12#include <AK/Vector.h>
13#include <LibCore/ArgsParser.h>
14#include <LibCore/File.h>
15#include <LibCore/ProcessStatisticsReader.h>
16#include <LibCore/System.h>
17#include <LibMain/Main.h>
18#include <ctype.h>
19#include <stdio.h>
20
21struct OpenFile {
22 int fd;
23 int pid;
24 DeprecatedString type;
25 DeprecatedString name;
26 DeprecatedString state;
27 DeprecatedString full_name;
28};
29
30static bool parse_name(StringView name, OpenFile& file)
31{
32 GenericLexer lexer(name);
33 auto component1 = lexer.consume_until(':');
34 lexer.ignore();
35
36 if (lexer.tell_remaining() == 0) {
37 file.name = component1;
38 return true;
39 } else {
40 file.type = component1;
41 auto component2 = lexer.consume_while([](char c) { return isprint(c) && c != '('; });
42 lexer.ignore_while(isspace);
43 file.name = component2;
44
45 if (lexer.tell_remaining() == 0) {
46 return true;
47 } else {
48 if (!lexer.consume_specific('(')) {
49 dbgln("parse_name: expected (");
50 return false;
51 }
52
53 auto component3 = lexer.consume_until(')');
54 lexer.ignore();
55 if (lexer.tell_remaining() != 0) {
56 dbgln("parse_name: expected EOF");
57 return false;
58 }
59
60 file.state = component3;
61 return true;
62 }
63 }
64}
65
66static Vector<OpenFile> get_open_files_by_pid(pid_t pid)
67{
68 auto file = Core::File::open(DeprecatedString::formatted("/proc/{}/fds", pid), Core::File::OpenMode::Read);
69 if (file.is_error()) {
70 outln("lsof: PID {}: {}", pid, file.error());
71 return Vector<OpenFile>();
72 }
73 auto data = file.value()->read_until_eof();
74 if (data.is_error()) {
75 outln("lsof: PID {}: {}", pid, data.error());
76 return {};
77 }
78
79 auto json_or_error = JsonValue::from_string(data.value());
80 if (json_or_error.is_error()) {
81 outln("lsof: {}", json_or_error.error());
82 return Vector<OpenFile>();
83 }
84 auto json = json_or_error.release_value();
85
86 Vector<OpenFile> files;
87 json.as_array().for_each([pid, &files](JsonValue const& object) {
88 OpenFile open_file;
89 open_file.pid = pid;
90 open_file.fd = object.as_object().get_integer<int>("fd"sv).value();
91
92 DeprecatedString name = object.as_object().get_deprecated_string("absolute_path"sv).value_or({});
93 VERIFY(parse_name(name, open_file));
94 open_file.full_name = name;
95
96 files.append(open_file);
97 });
98 return files;
99}
100
101static void display_entry(OpenFile const& file, Core::ProcessStatistics const& statistics)
102{
103 outln("{:28} {:>4} {:>4} {:10} {:>4} {}", statistics.name, file.pid, statistics.pgid, statistics.username, file.fd, file.full_name);
104}
105
106ErrorOr<int> serenity_main(Main::Arguments arguments)
107{
108 TRY(Core::System::pledge("stdio rpath proc"));
109
110 TRY(Core::System::unveil("/proc", "r"));
111 // needed by ProcessStatisticsReader::get_all()
112 TRY(Core::System::unveil("/sys/kernel/processes", "r"));
113 TRY(Core::System::unveil("/etc/passwd", "r"));
114 TRY(Core::System::unveil(nullptr, nullptr));
115
116 bool arg_all_processes { false };
117 int arg_fd { -1 };
118 StringView arg_uid;
119 int arg_uid_int = -1;
120 int arg_pgid { -1 };
121 pid_t arg_pid { -1 };
122 StringView arg_filename;
123
124 if (arguments.strings.size() == 1)
125 arg_all_processes = true;
126 else {
127 Core::ArgsParser parser;
128 parser.set_general_help("List open files of a processes. This can mean actual files in the file system, sockets, pipes, etc.");
129 parser.add_option(arg_pid, "Select by PID", nullptr, 'p', "pid");
130 parser.add_option(arg_fd, "Select by file descriptor", nullptr, 'd', "fd");
131 parser.add_option(arg_uid, "Select by login/UID", nullptr, 'u', "login/UID");
132 parser.add_option(arg_pgid, "Select by process group ID", nullptr, 'g', "PGID");
133 parser.add_positional_argument(arg_filename, "Filename", "filename", Core::ArgsParser::Required::No);
134 parser.parse(arguments);
135 }
136 {
137 // try convert UID to int
138 auto arg = DeprecatedString(arg_uid).to_int();
139 if (arg.has_value())
140 arg_uid_int = arg.value();
141 }
142
143 outln("{:28} {:>4} {:>4} {:10} {:>4} {}", "COMMAND", "PID", "PGID", "USER", "FD", "NAME");
144 auto all_processes = TRY(Core::ProcessStatisticsReader::get_all());
145 if (arg_pid == -1) {
146 for (auto& process : all_processes.processes) {
147 if (process.pid == 0)
148 continue;
149 auto open_files = get_open_files_by_pid(process.pid);
150
151 if (open_files.is_empty())
152 continue;
153
154 for (auto& file : open_files) {
155 if ((arg_all_processes)
156 || (arg_fd != -1 && file.fd == arg_fd)
157 || (arg_uid_int != -1 && (int)process.uid == arg_uid_int)
158 || (!arg_uid.is_empty() && process.username == arg_uid)
159 || (arg_pgid != -1 && (int)process.pgid == arg_pgid)
160 || (!arg_filename.is_empty() && file.name == arg_filename))
161 display_entry(file, process);
162 }
163 }
164 } else {
165 auto open_files = get_open_files_by_pid(arg_pid);
166
167 if (open_files.is_empty())
168 return 0;
169
170 for (auto& file : open_files) {
171 display_entry(file, *all_processes.processes.find_if([&](auto& entry) { return entry.pid == arg_pid; }));
172 }
173 }
174
175 return 0;
176}