pgrep.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Mini pgrep/pkill implementation for busybox
  4. *
  5. * Copyright (C) 2007 Loic Grenie <loic.grenie@gmail.com>
  6. *
  7. * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
  8. */
  9. #include "libbb.h"
  10. #include "xregex.h"
  11. /* Idea taken from kill.c */
  12. #define pgrep (ENABLE_PGREP && applet_name[1] == 'g')
  13. #define pkill (ENABLE_PKILL && applet_name[1] == 'k')
  14. enum {
  15. /* "vlfxons:P:" */
  16. OPTBIT_V = 0, /* must be first, we need OPT_INVERT = 0/1 */
  17. OPTBIT_L,
  18. OPTBIT_F,
  19. OPTBIT_X,
  20. OPTBIT_O,
  21. OPTBIT_N,
  22. OPTBIT_S,
  23. OPTBIT_P,
  24. };
  25. #define OPT_INVERT (opt & (1 << OPTBIT_V))
  26. #define OPT_LIST (opt & (1 << OPTBIT_L))
  27. #define OPT_FULL (opt & (1 << OPTBIT_F))
  28. #define OPT_ANCHOR (opt & (1 << OPTBIT_X))
  29. #define OPT_FIRST (opt & (1 << OPTBIT_O))
  30. #define OPT_LAST (opt & (1 << OPTBIT_N))
  31. #define OPT_SID (opt & (1 << OPTBIT_S))
  32. #define OPT_PPID (opt & (1 << OPTBIT_P))
  33. static void act(unsigned pid, char *cmd, int signo)
  34. {
  35. if (pgrep) {
  36. if (option_mask32 & (1 << OPTBIT_L)) /* OPT_LIST */
  37. printf("%d %s\n", pid, cmd);
  38. else
  39. printf("%d\n", pid);
  40. } else
  41. kill(pid, signo);
  42. }
  43. int pgrep_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  44. int pgrep_main(int argc UNUSED_PARAM, char **argv)
  45. {
  46. unsigned pid;
  47. int signo;
  48. unsigned opt;
  49. int scan_mask;
  50. int matched_pid;
  51. int sid2match, ppid2match;
  52. char *cmd_last;
  53. procps_status_t *proc;
  54. /* These are initialized to 0 */
  55. struct {
  56. regex_t re_buffer;
  57. regmatch_t re_match[1];
  58. } Z;
  59. #define re_buffer (Z.re_buffer)
  60. #define re_match (Z.re_match )
  61. memset(&Z, 0, sizeof(Z));
  62. /* Parse -SIGNAL for pkill. Must be first option, if present */
  63. signo = SIGTERM;
  64. if (pkill && argv[1] && argv[1][0] == '-') {
  65. int temp = get_signum(argv[1]+1);
  66. if (temp != -1) {
  67. signo = temp;
  68. argv++;
  69. }
  70. }
  71. /* Parse remaining options */
  72. ppid2match = -1;
  73. sid2match = -1;
  74. opt_complementary = "s+:P+"; /* numeric opts */
  75. opt = getopt32(argv, "vlfxons:P:", &sid2match, &ppid2match);
  76. argv += optind;
  77. if (pkill && OPT_LIST) { /* -l: print the whole signal list */
  78. print_signames();
  79. return 0;
  80. }
  81. pid = getpid();
  82. if (sid2match == 0)
  83. sid2match = getsid(pid);
  84. scan_mask = PSSCAN_COMM;
  85. if (OPT_FULL)
  86. scan_mask |= PSSCAN_ARGVN;
  87. /* One pattern is required, if no -s and no -P */
  88. if ((sid2match & ppid2match) < 0 && (!argv[0] || argv[1]))
  89. bb_show_usage();
  90. if (argv[0])
  91. xregcomp(&re_buffer, argv[0], 0);
  92. matched_pid = 0;
  93. cmd_last = NULL;
  94. proc = NULL;
  95. while ((proc = procps_scan(proc, scan_mask)) != NULL) {
  96. char *cmd;
  97. if (proc->pid == pid)
  98. continue;
  99. cmd = proc->argv0;
  100. if (!cmd) {
  101. cmd = proc->comm;
  102. } else {
  103. int i = proc->argv_len;
  104. while (i) {
  105. if (!cmd[i]) cmd[i] = ' ';
  106. i--;
  107. }
  108. }
  109. if (ppid2match >= 0 && ppid2match != proc->ppid)
  110. continue;
  111. if (sid2match >= 0 && sid2match != proc->sid)
  112. continue;
  113. /* NB: OPT_INVERT is always 0 or 1 */
  114. if (!argv[0] ||
  115. (regexec(&re_buffer, cmd, 1, re_match, 0) == 0 /* match found */
  116. && (!OPT_ANCHOR || (re_match[0].rm_so == 0 && re_match[0].rm_eo == (regoff_t)strlen(cmd)))) ^ OPT_INVERT
  117. ) {
  118. matched_pid = proc->pid;
  119. if (OPT_LAST) {
  120. free(cmd_last);
  121. cmd_last = xstrdup(cmd);
  122. continue;
  123. }
  124. act(proc->pid, cmd, signo);
  125. if (OPT_FIRST)
  126. break;
  127. }
  128. }
  129. if (cmd_last) {
  130. act(matched_pid, cmd_last, signo);
  131. if (ENABLE_FEATURE_CLEAN_UP)
  132. free(cmd_last);
  133. }
  134. return matched_pid == 0; /* return 1 if no processes listed/signaled */
  135. }