pidof.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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"
  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. //kbuild:lib-$(CONFIG_PIDOF) += pidof.o
  33. //usage:#if (ENABLE_FEATURE_PIDOF_SINGLE || ENABLE_FEATURE_PIDOF_OMIT)
  34. //usage:#define pidof_trivial_usage
  35. //usage: "[OPTIONS] [NAME]..."
  36. //usage:#define USAGE_PIDOF "\n"
  37. //usage:#else
  38. //usage:#define pidof_trivial_usage
  39. //usage: "[NAME]..."
  40. //usage:#define USAGE_PIDOF /* none */
  41. //usage:#endif
  42. //usage:#define pidof_full_usage "\n\n"
  43. //usage: "List PIDs of all processes with names that match NAMEs"
  44. //usage: USAGE_PIDOF
  45. //usage: IF_FEATURE_PIDOF_SINGLE(
  46. //usage: "\n -s Show only one PID"
  47. //usage: )
  48. //usage: IF_FEATURE_PIDOF_OMIT(
  49. //usage: "\n -o PID Omit given pid"
  50. //usage: "\n Use %PPID to omit pid of pidof's parent"
  51. //usage: )
  52. //usage:
  53. //usage:#define pidof_example_usage
  54. //usage: "$ pidof init\n"
  55. //usage: "1\n"
  56. //usage: IF_FEATURE_PIDOF_OMIT(
  57. //usage: "$ pidof /bin/sh\n20351 5973 5950\n")
  58. //usage: IF_FEATURE_PIDOF_OMIT(
  59. //usage: "$ pidof /bin/sh -o %PPID\n20351 5950")
  60. #include "libbb.h"
  61. enum {
  62. IF_FEATURE_PIDOF_SINGLE(OPTBIT_SINGLE,)
  63. IF_FEATURE_PIDOF_OMIT( OPTBIT_OMIT ,)
  64. OPT_SINGLE = IF_FEATURE_PIDOF_SINGLE((1<<OPTBIT_SINGLE)) + 0,
  65. OPT_OMIT = IF_FEATURE_PIDOF_OMIT( (1<<OPTBIT_OMIT )) + 0,
  66. };
  67. int pidof_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  68. int pidof_main(int argc UNUSED_PARAM, char **argv)
  69. {
  70. unsigned first = 1;
  71. unsigned opt;
  72. #if ENABLE_FEATURE_PIDOF_OMIT
  73. llist_t *omits = NULL; /* list of pids to omit */
  74. #endif
  75. /* do unconditional option parsing */
  76. opt = getopt32(argv, ""
  77. IF_FEATURE_PIDOF_SINGLE ("s")
  78. IF_FEATURE_PIDOF_OMIT("o:*", &omits));
  79. #if ENABLE_FEATURE_PIDOF_OMIT
  80. /* fill omit list. */
  81. {
  82. llist_t *omits_p = omits;
  83. while (1) {
  84. omits_p = llist_find_str(omits_p, "%PPID");
  85. if (!omits_p)
  86. break;
  87. /* are we asked to exclude the parent's process ID? */
  88. omits_p->data = utoa((unsigned)getppid());
  89. }
  90. }
  91. #endif
  92. /* Looks like everything is set to go. */
  93. argv += optind;
  94. while (*argv) {
  95. pid_t *pidList;
  96. pid_t *pl;
  97. /* reverse the pidlist like GNU pidof does. */
  98. pidList = pidlist_reverse(find_pid_by_name(*argv));
  99. for (pl = pidList; *pl; pl++) {
  100. #if ENABLE_FEATURE_PIDOF_OMIT
  101. if (opt & OPT_OMIT) {
  102. llist_t *omits_p = omits;
  103. while (omits_p) {
  104. if (xatoul(omits_p->data) == (unsigned long)(*pl)) {
  105. goto omitting;
  106. }
  107. omits_p = omits_p->link;
  108. }
  109. }
  110. #endif
  111. printf(" %u" + first, (unsigned)*pl);
  112. first = 0;
  113. if (ENABLE_FEATURE_PIDOF_SINGLE && (opt & OPT_SINGLE))
  114. break;
  115. #if ENABLE_FEATURE_PIDOF_OMIT
  116. omitting: ;
  117. #endif
  118. }
  119. free(pidList);
  120. argv++;
  121. }
  122. if (!first)
  123. bb_putchar('\n');
  124. #if ENABLE_FEATURE_PIDOF_OMIT
  125. if (ENABLE_FEATURE_CLEAN_UP)
  126. llist_free(omits, NULL);
  127. #endif
  128. return first; /* 1 (failure) - no processes found */
  129. }