pidof.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * pidof implementation for busybox
  4. *
  5. * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
  6. *
  7. * Licensed under GPLv2, see file LICENSE in this source tree.
  8. */
  9. //config:config PIDOF
  10. //config: bool "pidof (6.6 kb)"
  11. //config: default y
  12. //config: help
  13. //config: Pidof finds the process id's (pids) of the named programs. It prints
  14. //config: those id's on the standard output.
  15. //config:
  16. //config:config FEATURE_PIDOF_SINGLE
  17. //config: bool "Enable single shot (-s)"
  18. //config: default y
  19. //config: depends on PIDOF
  20. //config: help
  21. //config: Support '-s' for returning only the first pid found.
  22. //config:
  23. //config:config FEATURE_PIDOF_OMIT
  24. //config: bool "Enable omitting pids (-o PID)"
  25. //config: default y
  26. //config: depends on PIDOF
  27. //config: help
  28. //config: Support '-o PID' for omitting the given pid(s) in output.
  29. //config: The special pid %PPID can be used to name the parent process
  30. //config: of the pidof, in other words the calling shell or shell script.
  31. //applet:IF_PIDOF(APPLET(pidof, BB_DIR_BIN, BB_SUID_DROP))
  32. /* can't be noexec: can find _itself_ under wrong name, since after fork only,
  33. * /proc/PID/cmdline and comm are wrong! Can fix comm (prctl(PR_SET_NAME)),
  34. * but cmdline?
  35. */
  36. //kbuild:lib-$(CONFIG_PIDOF) += pidof.o
  37. //usage:#if (ENABLE_FEATURE_PIDOF_SINGLE || ENABLE_FEATURE_PIDOF_OMIT)
  38. //usage:#define pidof_trivial_usage
  39. //usage: "[OPTIONS] [NAME]..."
  40. //usage:#define USAGE_PIDOF "\n"
  41. //usage:#else
  42. //usage:#define pidof_trivial_usage
  43. //usage: "[NAME]..."
  44. //usage:#define USAGE_PIDOF /* none */
  45. //usage:#endif
  46. //usage:#define pidof_full_usage "\n\n"
  47. //usage: "List PIDs of all processes with names that match NAMEs"
  48. //usage: USAGE_PIDOF
  49. //usage: IF_FEATURE_PIDOF_SINGLE(
  50. //usage: "\n -s Show only one PID"
  51. //usage: )
  52. //usage: IF_FEATURE_PIDOF_OMIT(
  53. //usage: "\n -o PID Omit given pid"
  54. //usage: "\n Use %PPID to omit pid of pidof's parent"
  55. //usage: )
  56. //usage:
  57. //usage:#define pidof_example_usage
  58. //usage: "$ pidof init\n"
  59. //usage: "1\n"
  60. //usage: IF_FEATURE_PIDOF_OMIT(
  61. //usage: "$ pidof /bin/sh\n20351 5973 5950\n")
  62. //usage: IF_FEATURE_PIDOF_OMIT(
  63. //usage: "$ pidof /bin/sh -o %PPID\n20351 5950")
  64. #include "libbb.h"
  65. enum {
  66. IF_FEATURE_PIDOF_SINGLE(OPTBIT_SINGLE,)
  67. IF_FEATURE_PIDOF_OMIT( OPTBIT_OMIT ,)
  68. OPT_SINGLE = IF_FEATURE_PIDOF_SINGLE((1<<OPTBIT_SINGLE)) + 0,
  69. OPT_OMIT = IF_FEATURE_PIDOF_OMIT( (1<<OPTBIT_OMIT )) + 0,
  70. };
  71. int pidof_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  72. int pidof_main(int argc UNUSED_PARAM, char **argv)
  73. {
  74. unsigned first = 1;
  75. unsigned opt;
  76. #if ENABLE_FEATURE_PIDOF_OMIT
  77. llist_t *omits = NULL; /* list of pids to omit */
  78. #endif
  79. /* do unconditional option parsing */
  80. opt = getopt32(argv, ""
  81. IF_FEATURE_PIDOF_SINGLE ("s")
  82. IF_FEATURE_PIDOF_OMIT("o:*", &omits));
  83. #if ENABLE_FEATURE_PIDOF_OMIT
  84. /* fill omit list. */
  85. {
  86. llist_t *omits_p = omits;
  87. while (1) {
  88. omits_p = llist_find_str(omits_p, "%PPID");
  89. if (!omits_p)
  90. break;
  91. /* are we asked to exclude the parent's process ID? */
  92. omits_p->data = utoa((unsigned)getppid());
  93. }
  94. }
  95. #endif
  96. /* Looks like everything is set to go. */
  97. argv += optind;
  98. while (*argv) {
  99. pid_t *pidList;
  100. pid_t *pl;
  101. /* reverse the pidlist like GNU pidof does. */
  102. pidList = pidlist_reverse(find_pid_by_name(*argv));
  103. for (pl = pidList; *pl; pl++) {
  104. #if ENABLE_FEATURE_PIDOF_OMIT
  105. if (opt & OPT_OMIT) {
  106. llist_t *omits_p = omits;
  107. while (omits_p) {
  108. if (xatoul(omits_p->data) == (unsigned long)(*pl)) {
  109. goto omitting;
  110. }
  111. omits_p = omits_p->link;
  112. }
  113. }
  114. #endif
  115. printf(" %u" + first, (unsigned)*pl);
  116. first = 0;
  117. if (ENABLE_FEATURE_PIDOF_SINGLE && (opt & OPT_SINGLE))
  118. break;
  119. #if ENABLE_FEATURE_PIDOF_OMIT
  120. omitting: ;
  121. #endif
  122. }
  123. free(pidList);
  124. argv++;
  125. }
  126. if (!first)
  127. bb_putchar('\n');
  128. #if ENABLE_FEATURE_PIDOF_OMIT
  129. if (ENABLE_FEATURE_CLEAN_UP)
  130. llist_free(omits, NULL);
  131. #endif
  132. return first; /* 1 (failure) - no processes found */
  133. }