Serenity Operating System
at hosted 89 lines 3.3 kB view raw
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 <string.h> 36#include <unistd.h> 37 38static int pid_of(const String& process_name, bool single_shot, bool omit_pid, pid_t pid) 39{ 40 bool displayed_at_least_one = false; 41 42 auto processes = Core::ProcessStatisticsReader().get_all(); 43 44 for (auto& it : processes) { 45 if (it.value.name == process_name) { 46 if (!omit_pid || it.value.pid != pid) { 47 printf("%d ", it.value.pid); 48 displayed_at_least_one = true; 49 50 if (single_shot) 51 break; 52 } 53 } 54 } 55 56 if (displayed_at_least_one) 57 printf("\n"); 58 59 return 0; 60} 61 62int main(int argc, char** argv) 63{ 64 bool single_shot = false; 65 const char* omit_pid_value = nullptr; 66 const char* process_name = nullptr; 67 68 Core::ArgsParser args_parser; 69 args_parser.add_option(single_shot, "Only return one pid", nullptr, 's'); 70 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"); 71 args_parser.add_positional_argument(process_name, "Process name to search for", "process-name"); 72 73 args_parser.parse(argc, argv); 74 75 pid_t pid_to_omit = 0; 76 if (omit_pid_value) { 77 bool ok = true; 78 if (!strcmp(omit_pid_value, "%PPID")) 79 pid_to_omit = getppid(); 80 else 81 pid_to_omit = StringView(omit_pid_value).to_uint(ok); 82 if (!ok) { 83 fprintf(stderr, "Invalid value for -o\n"); 84 args_parser.print_usage(stderr, argv[0]); 85 return 1; 86 } 87 } 88 return pid_of(process_name, single_shot, omit_pid_value != nullptr, pid_to_omit); 89}