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/File.h>
28#include <LibCore/ProcessStatisticsReader.h>
29#include <fcntl.h>
30#include <stdio.h>
31#include <unistd.h>
32
33int main(int argc, char** argv)
34{
35 if (pledge("stdio rpath", nullptr) < 0) {
36 perror("pledge");
37 return 1;
38 }
39
40 if (unveil("/proc/all", "r") < 0) {
41 perror("unveil");
42 return 1;
43 }
44
45 if (unveil("/etc/passwd", "r") < 0) {
46 perror("unveil");
47 return 1;
48 }
49
50 unveil(nullptr, nullptr);
51
52 (void)argc;
53 (void)argv;
54
55 printf("PID TPG PGP SID UID STATE PPID NSCHED FDS TTY NAME\n");
56
57 auto all_processes = Core::ProcessStatisticsReader::get_all();
58
59 for (const auto& it : all_processes) {
60 const auto& proc = it.value;
61 auto tty = proc.tty;
62
63 if (tty.starts_with("/dev/"))
64 tty = tty.characters() + 5;
65 else
66 tty = "n/a";
67
68 printf("%-3u %-3u %-3u %-3u %-3u %-11s %-3u %-9u %-3u %-5s %s\n",
69 proc.pid,
70 proc.pgid,
71 proc.pgp,
72 proc.sid,
73 proc.uid,
74 proc.threads.first().state.characters(),
75 proc.ppid,
76 proc.threads.first().times_scheduled,
77 proc.nfds,
78 tty.characters(),
79 proc.name.characters());
80 }
81
82 return 0;
83}