lsof.cpp 5.4 KB

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