3
0

pgrep.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. //usage:#define pgrep_trivial_usage
  10. //usage: "[-flnovx] [-s SID|-P PPID|PATTERN]"
  11. //usage:#define pgrep_full_usage "\n\n"
  12. //usage: "Display process(es) selected by regex PATTERN\n"
  13. //usage: "\n -l Show command name too"
  14. //usage: "\n -f Match against entire command line"
  15. //usage: "\n -n Show the newest process only"
  16. //usage: "\n -o Show the oldest process only"
  17. //usage: "\n -v Negate the match"
  18. //usage: "\n -x Match whole name (not substring)"
  19. //usage: "\n -s Match session ID (0 for current)"
  20. //usage: "\n -P Match parent process ID"
  21. //usage:
  22. //usage:#define pkill_trivial_usage
  23. //usage: "[-l|-SIGNAL] [-fnovx] [-s SID|-P PPID|PATTERN]"
  24. //usage:#define pkill_full_usage "\n\n"
  25. //usage: "Send a signal to process(es) selected by regex PATTERN\n"
  26. //usage: "\n -l List all signals"
  27. //usage: "\n -f Match against entire command line"
  28. //usage: "\n -n Signal the newest process only"
  29. //usage: "\n -o Signal the oldest process only"
  30. //usage: "\n -v Negate the match"
  31. //usage: "\n -x Match whole name (not substring)"
  32. //usage: "\n -s Match session ID (0 for current)"
  33. //usage: "\n -P Match parent process ID"
  34. #include "libbb.h"
  35. #include "xregex.h"
  36. /* Idea taken from kill.c */
  37. #define pgrep (ENABLE_PGREP && applet_name[1] == 'g')
  38. #define pkill (ENABLE_PKILL && applet_name[1] == 'k')
  39. enum {
  40. /* "vlfxons:P:" */
  41. OPTBIT_V = 0, /* must be first, we need OPT_INVERT = 0/1 */
  42. OPTBIT_L,
  43. OPTBIT_F,
  44. OPTBIT_X,
  45. OPTBIT_O,
  46. OPTBIT_N,
  47. OPTBIT_S,
  48. OPTBIT_P,
  49. };
  50. #define OPT_INVERT (opt & (1 << OPTBIT_V))
  51. #define OPT_LIST (opt & (1 << OPTBIT_L))
  52. #define OPT_FULL (opt & (1 << OPTBIT_F))
  53. #define OPT_ANCHOR (opt & (1 << OPTBIT_X))
  54. #define OPT_FIRST (opt & (1 << OPTBIT_O))
  55. #define OPT_LAST (opt & (1 << OPTBIT_N))
  56. #define OPT_SID (opt & (1 << OPTBIT_S))
  57. #define OPT_PPID (opt & (1 << OPTBIT_P))
  58. static void act(unsigned pid, char *cmd, int signo)
  59. {
  60. if (pgrep) {
  61. if (option_mask32 & (1 << OPTBIT_L)) /* OPT_LIST */
  62. printf("%u %s\n", pid, cmd);
  63. else
  64. printf("%u\n", pid);
  65. } else
  66. kill(pid, signo);
  67. }
  68. int pgrep_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  69. int pgrep_main(int argc UNUSED_PARAM, char **argv)
  70. {
  71. unsigned pid;
  72. int signo;
  73. unsigned opt;
  74. int scan_mask;
  75. int matched_pid;
  76. int sid2match, ppid2match;
  77. char *cmd_last;
  78. procps_status_t *proc;
  79. /* These are initialized to 0 */
  80. struct {
  81. regex_t re_buffer;
  82. regmatch_t re_match[1];
  83. } Z;
  84. #define re_buffer (Z.re_buffer)
  85. #define re_match (Z.re_match )
  86. memset(&Z, 0, sizeof(Z));
  87. /* Parse -SIGNAL for pkill. Must be first option, if present */
  88. signo = SIGTERM;
  89. if (pkill && argv[1] && argv[1][0] == '-') {
  90. int temp = get_signum(argv[1]+1);
  91. if (temp != -1) {
  92. signo = temp;
  93. argv++;
  94. }
  95. }
  96. /* Parse remaining options */
  97. ppid2match = -1;
  98. sid2match = -1;
  99. opt_complementary = "s+:P+"; /* numeric opts */
  100. opt = getopt32(argv, "vlfxons:P:", &sid2match, &ppid2match);
  101. argv += optind;
  102. if (pkill && OPT_LIST) { /* -l: print the whole signal list */
  103. print_signames();
  104. return 0;
  105. }
  106. pid = getpid();
  107. if (sid2match == 0)
  108. sid2match = getsid(pid);
  109. scan_mask = PSSCAN_COMM | PSSCAN_ARGV0;
  110. if (OPT_FULL)
  111. scan_mask |= PSSCAN_ARGVN;
  112. /* One pattern is required, if no -s and no -P */
  113. if ((sid2match & ppid2match) < 0 && (!argv[0] || argv[1]))
  114. bb_show_usage();
  115. if (argv[0])
  116. xregcomp(&re_buffer, argv[0], OPT_ANCHOR ? REG_EXTENDED : (REG_EXTENDED|REG_NOSUB));
  117. matched_pid = 0;
  118. cmd_last = NULL;
  119. proc = NULL;
  120. while ((proc = procps_scan(proc, scan_mask)) != NULL) {
  121. char *cmd;
  122. if (proc->pid == pid)
  123. continue;
  124. cmd = proc->argv0;
  125. if (!cmd) {
  126. cmd = proc->comm;
  127. } else {
  128. int i = proc->argv_len;
  129. while (--i >= 0) {
  130. if ((unsigned char)cmd[i] < ' ')
  131. cmd[i] = ' ';
  132. }
  133. }
  134. if (ppid2match >= 0 && ppid2match != proc->ppid)
  135. continue;
  136. if (sid2match >= 0 && sid2match != proc->sid)
  137. continue;
  138. /* NB: OPT_INVERT is always 0 or 1 */
  139. if (!argv[0]
  140. || (regexec(&re_buffer, cmd, 1, re_match, 0) == 0 /* match found */
  141. && (!OPT_ANCHOR || (re_match[0].rm_so == 0 && re_match[0].rm_eo == (regoff_t)strlen(cmd)))
  142. ) ^ OPT_INVERT
  143. ) {
  144. matched_pid = proc->pid;
  145. if (OPT_LAST) {
  146. free(cmd_last);
  147. cmd_last = xstrdup(cmd);
  148. continue;
  149. }
  150. act(proc->pid, cmd, signo);
  151. if (OPT_FIRST)
  152. break;
  153. }
  154. }
  155. if (cmd_last) {
  156. act(matched_pid, cmd_last, signo);
  157. if (ENABLE_FEATURE_CLEAN_UP)
  158. free(cmd_last);
  159. }
  160. return matched_pid == 0; /* return 1 if no processes listed/signaled */
  161. }