Serenity Operating System
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
27#include <LibCore/ArgsParser.h>
28#include <LibCore/File.h>
29#include <LibCore/ProcessStatisticsReader.h>
30#include <fcntl.h>
31#include <stdio.h>
32#include <unistd.h>
33
34int main(int argc, char** argv)
35{
36 if (pledge("stdio rpath tty", nullptr) < 0) {
37 perror("pledge");
38 return 1;
39 }
40
41 String this_tty = ttyname(STDIN_FILENO);
42
43 if (pledge("stdio rpath", nullptr) < 0) {
44 perror("pledge");
45 return 1;
46 }
47
48 if (unveil("/proc/all", "r") < 0) {
49 perror("unveil");
50 return 1;
51 }
52
53 if (unveil("/etc/passwd", "r") < 0) {
54 perror("unveil");
55 return 1;
56 }
57
58 unveil(nullptr, nullptr);
59
60 enum class Alignment {
61 Left,
62 Right,
63 };
64
65 struct Column {
66 String title;
67 Alignment alignment { Alignment::Left };
68 int width { 0 };
69 String buffer;
70 };
71
72 bool every_process_flag = false;
73 bool full_format_flag = false;
74
75 Core::ArgsParser args_parser;
76 args_parser.add_option(every_process_flag, "Show every process", nullptr, 'e');
77 args_parser.add_option(full_format_flag, "Full format", nullptr, 'f');
78 args_parser.parse(argc, argv);
79
80 Vector<Column> columns;
81
82 int uid_column = -1;
83 int pid_column = -1;
84 int ppid_column = -1;
85 int state_column = -1;
86 int tty_column = -1;
87 int cmd_column = -1;
88
89 auto add_column = [&](auto title, auto alignment, auto width) {
90 columns.append({ title, alignment, width, {} });
91 return columns.size() - 1;
92 };
93
94 if (full_format_flag) {
95 uid_column = add_column("UID", Alignment::Left, 8);
96 pid_column = add_column("PID", Alignment::Right, 5);
97 ppid_column = add_column("PPID", Alignment::Right, 5);
98 state_column = add_column("STATE", Alignment::Left, 12);
99 tty_column = add_column("TTY", Alignment::Left, 6);
100 cmd_column = add_column("CMD", Alignment::Left, 0);
101 } else {
102 pid_column = add_column("PID", Alignment::Right, 5);
103 tty_column = add_column("TTY", Alignment::Left, 6);
104 cmd_column = add_column("CMD", Alignment::Left, 0);
105 }
106
107 auto print_column = [](auto& column, auto& string) {
108 if (!column.width) {
109 printf("%s", string.characters());
110 return;
111 }
112 if (column.alignment == Alignment::Right)
113 printf("%*s ", column.width, string.characters());
114 else
115 printf("%-*s ", column.width, string.characters());
116 };
117
118 for (auto& column : columns)
119 print_column(column, column.title);
120 printf("\n");
121
122 auto all_processes = Core::ProcessStatisticsReader::get_all();
123
124 for (const auto& it : all_processes) {
125 const auto& proc = it.value;
126 auto tty = proc.tty;
127
128 if (!every_process_flag && tty != this_tty)
129 continue;
130
131 if (tty.starts_with("/dev/"))
132 tty = tty.characters() + 5;
133 else
134 tty = "n/a";
135
136 auto* state = proc.threads.is_empty() ? "Zombie" : proc.threads.first().state.characters();
137
138 if (uid_column != -1)
139 columns[uid_column].buffer = proc.username;
140 if (pid_column != -1)
141 columns[pid_column].buffer = String::number(proc.pid);
142 if (ppid_column != -1)
143 columns[ppid_column].buffer = String::number(proc.ppid);
144 if (tty_column != -1)
145 columns[tty_column].buffer = tty;
146 if (state_column != -1)
147 columns[state_column].buffer = state;
148 if (cmd_column != -1)
149 columns[cmd_column].buffer = proc.name;
150
151 for (auto& column : columns)
152 print_column(column, column.buffer);
153 printf("\n");
154 }
155
156 return 0;
157}