pidof.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 the GPL v2, see the file LICENSE in this tarball.
  8. */
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <errno.h>
  12. #include <unistd.h>
  13. #include <signal.h>
  14. #include <ctype.h>
  15. #include <string.h>
  16. #include <sys/types.h>
  17. #include <unistd.h>
  18. #include "busybox.h"
  19. #if ENABLE_FEATURE_PIDOF_SINGLE
  20. #define _SINGLE_COMPL(a) a
  21. #define SINGLE (1<<0)
  22. #else
  23. #define _SINGLE_COMPL(a)
  24. #define SINGLE (0)
  25. #endif
  26. #if ENABLE_FEATURE_PIDOF_OMIT
  27. #define _OMIT_COMPL(a) a
  28. #define _OMIT(a) ,a
  29. #if ENABLE_FEATURE_PIDOF_SINGLE
  30. #define OMIT (1<<1)
  31. #else
  32. #define OMIT (1<<0)
  33. #endif
  34. #else
  35. #define _OMIT_COMPL(a) ""
  36. #define _OMIT(a)
  37. #define OMIT (0)
  38. #define omitted (0)
  39. #endif
  40. int pidof_main(int argc, char **argv)
  41. {
  42. unsigned n = 0;
  43. unsigned fail = 1;
  44. unsigned long int opt;
  45. #if ENABLE_FEATURE_PIDOF_OMIT
  46. llist_t *omits = NULL; /* list of pids to omit */
  47. bb_opt_complementally = _OMIT_COMPL("o::");
  48. #endif
  49. /* do unconditional option parsing */
  50. opt = bb_getopt_ulflags(argc, argv,
  51. _SINGLE_COMPL("s") _OMIT_COMPL("o:")
  52. _OMIT(&omits));
  53. #if ENABLE_FEATURE_PIDOF_OMIT
  54. /* fill omit list. */
  55. {
  56. char getppid_str[32];
  57. llist_t * omits_p = omits;
  58. while (omits_p) {
  59. /* are we asked to exclude the parent's process ID? */
  60. if (!strncmp(omits_p->data, "%PPID", 5)) {
  61. omits_p = llist_free_one(omits_p);
  62. snprintf(getppid_str, sizeof(getppid_str), "%d", getppid());
  63. omits_p = llist_add_to(omits_p, getppid_str);
  64. #if 0
  65. } else {
  66. bb_error_msg_and_die("illegal omit pid value (%s)!\n",
  67. omits_p->data);
  68. #endif
  69. }
  70. omits_p = omits_p->link;
  71. }
  72. }
  73. #endif
  74. /* Looks like everything is set to go. */
  75. while(optind < argc) {
  76. long *pidList;
  77. long *pl;
  78. /* reverse the pidlist like GNU pidof does. */
  79. pidList = pidlist_reverse(find_pid_by_name(argv[optind]));
  80. for(pl = pidList; *pl > 0; pl++) {
  81. #if ENABLE_FEATURE_PIDOF_OMIT
  82. unsigned omitted = 0;
  83. if (opt & OMIT) {
  84. llist_t *omits_p = omits;
  85. while (omits_p)
  86. if (strtol(omits_p->data, NULL, 10) == *pl) {
  87. omitted = 1; break;
  88. } else
  89. omits_p = omits_p->link;
  90. }
  91. #endif
  92. if (!omitted) {
  93. if (n) {
  94. putchar(' ');
  95. } else {
  96. n = 1;
  97. }
  98. printf("%ld", *pl);
  99. }
  100. fail = (!ENABLE_FEATURE_PIDOF_OMIT && omitted);
  101. if (ENABLE_FEATURE_PIDOF_SINGLE && (opt & SINGLE))
  102. break;
  103. }
  104. free(pidList);
  105. optind++;
  106. }
  107. putchar('\n');
  108. #if ENABLE_FEATURE_PIDOF_OMIT
  109. if (ENABLE_FEATURE_CLEAN_UP)
  110. llist_free(omits);
  111. #endif
  112. return fail ? EXIT_FAILURE : EXIT_SUCCESS;
  113. }