pgrep.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 <getopt.h>
  10. #include "libbb.h"
  11. #include "xregex.h"
  12. /* Idea taken from kill.c */
  13. #define pgrep (ENABLE_PGREP && applet_name[1] == 'g')
  14. #define pkill (ENABLE_PKILL && applet_name[1] == 'k')
  15. enum {
  16. /* "vlfxon" */
  17. PGREPOPTBIT_V = 0, /* must be first, we need OPT_INVERT = 0/1 */
  18. PGREPOPTBIT_L,
  19. PGREPOPTBIT_F,
  20. PGREPOPTBIT_X,
  21. PGREPOPTBIT_O,
  22. PGREPOPTBIT_N,
  23. };
  24. #define OPT_INVERT (opt & (1 << PGREPOPTBIT_V))
  25. #define OPT_LIST (opt & (1 << PGREPOPTBIT_L))
  26. #define OPT_FULL (opt & (1 << PGREPOPTBIT_F))
  27. #define OPT_ANCHOR (opt & (1 << PGREPOPTBIT_X))
  28. #define OPT_FIRST (opt & (1 << PGREPOPTBIT_O))
  29. #define OPT_LAST (opt & (1 << PGREPOPTBIT_N))
  30. static void act(unsigned pid, char *cmd, int signo, unsigned opt)
  31. {
  32. if (pgrep) {
  33. if (OPT_LIST)
  34. printf("%d %s\n", pid, cmd);
  35. else
  36. printf("%d\n", pid);
  37. } else
  38. kill(pid, signo);
  39. }
  40. int pgrep_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  41. int pgrep_main(int argc ATTRIBUTE_UNUSED, char **argv)
  42. {
  43. unsigned pid = getpid();
  44. int signo = SIGTERM;
  45. unsigned opt;
  46. int scan_mask = PSSCAN_COMM;
  47. char *first_arg;
  48. int first_arg_idx;
  49. int matched_pid;
  50. char *cmd_last;
  51. procps_status_t *proc;
  52. /* These are initialized to 0 */
  53. struct {
  54. regex_t re_buffer;
  55. regmatch_t re_match[1];
  56. } Z;
  57. #define re_buffer (Z.re_buffer)
  58. #define re_match (Z.re_match )
  59. memset(&Z, 0, sizeof(Z));
  60. /* We must avoid interpreting -NUM (signal num) as an option */
  61. first_arg_idx = 1;
  62. while (1) {
  63. first_arg = argv[first_arg_idx];
  64. if (!first_arg)
  65. break;
  66. /* not "-<small_letter>..."? */
  67. if (first_arg[0] != '-' || first_arg[1] < 'a' || first_arg[1] > 'z') {
  68. argv[first_arg_idx] = NULL; /* terminate argv here */
  69. break;
  70. }
  71. first_arg_idx++;
  72. }
  73. opt = getopt32(argv, "vlfxon");
  74. argv[first_arg_idx] = first_arg;
  75. argv += optind;
  76. //argc -= optind; - unused anyway
  77. if (OPT_FULL)
  78. scan_mask |= PSSCAN_ARGVN;
  79. if (pkill) {
  80. if (OPT_LIST) { /* -l: print the whole signal list */
  81. print_signames();
  82. return 0;
  83. }
  84. if (first_arg && first_arg[0] == '-') {
  85. signo = get_signum(&first_arg[1]);
  86. if (signo < 0) /* || signo > MAX_SIGNUM ? */
  87. bb_error_msg_and_die("bad signal name '%s'", &first_arg[1]);
  88. argv++;
  89. }
  90. }
  91. /* One pattern is required */
  92. if (!argv[0] || argv[1])
  93. bb_show_usage();
  94. xregcomp(&re_buffer, argv[0], 0);
  95. matched_pid = 0;
  96. cmd_last = NULL;
  97. proc = NULL;
  98. while ((proc = procps_scan(proc, scan_mask)) != NULL) {
  99. char *cmd;
  100. if (proc->pid == pid)
  101. continue;
  102. cmd = proc->argv0;
  103. if (!cmd)
  104. cmd = proc->comm;
  105. /* NB: OPT_INVERT is always 0 or 1 */
  106. if ((regexec(&re_buffer, cmd, 1, re_match, 0) == 0 /* match found */
  107. && (!OPT_ANCHOR || (re_match[0].rm_so == 0 && re_match[0].rm_eo == strlen(cmd)))) ^ OPT_INVERT
  108. ) {
  109. matched_pid = proc->pid;
  110. if (OPT_LAST) {
  111. free(cmd_last);
  112. cmd_last = xstrdup(cmd);
  113. continue;
  114. }
  115. act(proc->pid, cmd, signo, opt);
  116. if (OPT_FIRST)
  117. break;
  118. }
  119. }
  120. if (cmd_last) {
  121. act(matched_pid, cmd_last, signo, opt);
  122. if (ENABLE_FEATURE_CLEAN_UP)
  123. free(cmd_last);
  124. }
  125. return matched_pid == 0; /* return 1 if no processes listed/signaled */
  126. }