3
0

pidof.c 2.9 KB

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