pgrep.c 3.3 KB

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