pgrep.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 GPLv2 or later, see file LICENSE in this source tree.
  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 | PSSCAN_ARGV0;
  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 >= 0) {
  105. if ((unsigned char)cmd[i] < ' ')
  106. cmd[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)))
  117. ) ^ OPT_INVERT
  118. ) {
  119. matched_pid = proc->pid;
  120. if (OPT_LAST) {
  121. free(cmd_last);
  122. cmd_last = xstrdup(cmd);
  123. continue;
  124. }
  125. act(proc->pid, cmd, signo);
  126. if (OPT_FIRST)
  127. break;
  128. }
  129. }
  130. if (cmd_last) {
  131. act(matched_pid, cmd_last, signo);
  132. if (ENABLE_FEATURE_CLEAN_UP)
  133. free(cmd_last);
  134. }
  135. return matched_pid == 0; /* return 1 if no processes listed/signaled */
  136. }