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 <AK/HashMap.h>
28#include <AK/String.h>
29#include <AK/Vector.h>
30#include <LibCore/ArgsParser.h>
31#include <LibCore/ProcessStatisticsReader.h>
32#include <signal.h>
33#include <stdio.h>
34#include <stdlib.h>
35#include <unistd.h>
36
37static int pid_of(const String& process_name, bool single_shot, bool omit_pid, pid_t pid)
38{
39 bool displayed_at_least_one = false;
40
41 auto processes = Core::ProcessStatisticsReader().get_all();
42
43 for (auto& it : processes) {
44 if (it.value.name == process_name) {
45 if (!omit_pid || it.value.pid != pid) {
46 printf("%d ", it.value.pid);
47 displayed_at_least_one = true;
48
49 if (single_shot)
50 break;
51 }
52 }
53 }
54
55 if (displayed_at_least_one)
56 printf("\n");
57
58 return 0;
59}
60
61int main(int argc, char** argv)
62{
63 bool single_shot = false;
64 const char* omit_pid_value = nullptr;
65 const char* process_name = nullptr;
66
67 Core::ArgsParser args_parser;
68 args_parser.add_option(single_shot, "Only return one pid", nullptr, 's');
69 args_parser.add_option(omit_pid_value, "Omit the given PID, or the parent process if the special value %PPID is passed", nullptr, 'o', "pid");
70 args_parser.add_positional_argument(process_name, "Process name to search for", "process-name");
71
72 args_parser.parse(argc, argv);
73
74 pid_t pid_to_omit = 0;
75 if (omit_pid_value) {
76 bool ok = true;
77 if (!strcmp(omit_pid_value, "%PPID"))
78 pid_to_omit = getppid();
79 else
80 pid_to_omit = StringView(omit_pid_value).to_uint(ok);
81 if (!ok) {
82 fprintf(stderr, "Invalid value for -o\n");
83 args_parser.print_usage(stderr, argv[0]);
84 return 1;
85 }
86 }
87 return pid_of(process_name, single_shot, omit_pid_value != nullptr, pid_to_omit);
88}