pidof.cpp 3.3 KB

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