ps.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <LibCore/ArgsParser.h>
  27. #include <LibCore/File.h>
  28. #include <LibCore/ProcessStatisticsReader.h>
  29. #include <fcntl.h>
  30. #include <stdio.h>
  31. #include <unistd.h>
  32. int main(int argc, char** argv)
  33. {
  34. if (pledge("stdio rpath tty", nullptr) < 0) {
  35. perror("pledge");
  36. return 1;
  37. }
  38. String this_tty = ttyname(STDIN_FILENO);
  39. if (pledge("stdio rpath", nullptr) < 0) {
  40. perror("pledge");
  41. return 1;
  42. }
  43. if (unveil("/proc/all", "r") < 0) {
  44. perror("unveil");
  45. return 1;
  46. }
  47. if (unveil("/etc/passwd", "r") < 0) {
  48. perror("unveil");
  49. return 1;
  50. }
  51. unveil(nullptr, nullptr);
  52. enum class Alignment {
  53. Left,
  54. Right,
  55. };
  56. struct Column {
  57. String title;
  58. Alignment alignment { Alignment::Left };
  59. int width { 0 };
  60. String buffer;
  61. };
  62. bool every_process_flag = false;
  63. bool full_format_flag = false;
  64. Core::ArgsParser args_parser;
  65. args_parser.add_option(every_process_flag, "Show every process", nullptr, 'e');
  66. args_parser.add_option(full_format_flag, "Full format", nullptr, 'f');
  67. args_parser.parse(argc, argv);
  68. Vector<Column> columns;
  69. int uid_column = -1;
  70. int pid_column = -1;
  71. int ppid_column = -1;
  72. int state_column = -1;
  73. int tty_column = -1;
  74. int cmd_column = -1;
  75. auto add_column = [&](auto title, auto alignment, auto width) {
  76. columns.append({ title, alignment, width, {} });
  77. return columns.size() - 1;
  78. };
  79. if (full_format_flag) {
  80. uid_column = add_column("UID", Alignment::Left, 9);
  81. pid_column = add_column("PID", Alignment::Right, 5);
  82. ppid_column = add_column("PPID", Alignment::Right, 5);
  83. state_column = add_column("STATE", Alignment::Left, 12);
  84. tty_column = add_column("TTY", Alignment::Left, 6);
  85. cmd_column = add_column("CMD", Alignment::Left, 0);
  86. } else {
  87. pid_column = add_column("PID", Alignment::Right, 5);
  88. tty_column = add_column("TTY", Alignment::Left, 6);
  89. cmd_column = add_column("CMD", Alignment::Left, 0);
  90. }
  91. auto print_column = [](auto& column, auto& string) {
  92. if (!column.width) {
  93. printf("%s", string.characters());
  94. return;
  95. }
  96. if (column.alignment == Alignment::Right)
  97. printf("%*s ", column.width, string.characters());
  98. else
  99. printf("%-*s ", column.width, string.characters());
  100. };
  101. for (auto& column : columns)
  102. print_column(column, column.title);
  103. printf("\n");
  104. auto all_processes = Core::ProcessStatisticsReader::get_all();
  105. for (const auto& it : all_processes) {
  106. const auto& proc = it.value;
  107. auto tty = proc.tty;
  108. if (!every_process_flag && tty != this_tty)
  109. continue;
  110. if (tty.starts_with("/dev/"))
  111. tty = tty.characters() + 5;
  112. else
  113. tty = "n/a";
  114. auto* state = proc.threads.is_empty() ? "Zombie" : proc.threads.first().state.characters();
  115. if (uid_column != -1)
  116. columns[uid_column].buffer = proc.username;
  117. if (pid_column != -1)
  118. columns[pid_column].buffer = String::number(proc.pid);
  119. if (ppid_column != -1)
  120. columns[ppid_column].buffer = String::number(proc.ppid);
  121. if (tty_column != -1)
  122. columns[tty_column].buffer = tty;
  123. if (state_column != -1)
  124. columns[state_column].buffer = state;
  125. if (cmd_column != -1)
  126. columns[cmd_column].buffer = proc.name;
  127. for (auto& column : columns)
  128. print_column(column, column.buffer);
  129. printf("\n");
  130. }
  131. return 0;
  132. }