3
0

find_pid_by_name.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Utility routines.
  4. *
  5. * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
  6. *
  7. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  8. */
  9. #include "libbb.h"
  10. /*
  11. In Linux we have three ways to determine "process name":
  12. 1. /proc/PID/stat has "...(name)...", among other things. It's so-called "comm" field.
  13. 2. /proc/PID/cmdline's first NUL-terminated string. It's argv[0] from exec syscall.
  14. 3. /proc/PID/exe symlink. Points to the running executable file.
  15. kernel threads:
  16. comm: thread name
  17. cmdline: empty
  18. exe: <readlink fails>
  19. executable
  20. comm: first 15 chars of base name
  21. (if executable is a symlink, then first 15 chars of symlink name are used)
  22. cmdline: argv[0] from exec syscall
  23. exe: points to executable (resolves symlink, unlike comm)
  24. script (an executable with #!/path/to/interpreter):
  25. comm: first 15 chars of script's base name (symlinks are not resolved)
  26. cmdline: /path/to/interpreter (symlinks are not resolved)
  27. (script name is in argv[1], args are pushed into argv[2] etc)
  28. exe: points to interpreter's executable (symlinks are resolved)
  29. If FEATURE_PREFER_APPLETS=y (and more so if FEATURE_SH_STANDALONE=y),
  30. some commands started from busybox shell, xargs or find are started by
  31. execXXX("/proc/self/exe", applet_name, params....)
  32. and therefore comm field contains "exe".
  33. */
  34. static int comm_match(procps_status_t *p, const char *procName)
  35. {
  36. int argv1idx;
  37. const char *argv1;
  38. if (strncmp(p->comm, procName, 15) != 0)
  39. return 0; /* comm does not match */
  40. /* In Linux, if comm is 15 chars, it is truncated.
  41. * (or maybe the name was exactly 15 chars, but there is
  42. * no way to know that) */
  43. if (p->comm[14] == '\0')
  44. return 1; /* comm is not truncated - matches */
  45. /* comm is truncated, but first 15 chars match.
  46. * This can be crazily_long_script_name.sh!
  47. * The telltale sign is basename(argv[1]) == procName */
  48. if (!p->argv0)
  49. return 0;
  50. argv1idx = strlen(p->argv0) + 1;
  51. if (argv1idx >= p->argv_len)
  52. return 0;
  53. argv1 = p->argv0 + argv1idx;
  54. if (strcmp(bb_basename(argv1), procName) != 0)
  55. return 0;
  56. return 1;
  57. }
  58. /* This finds the pid of the specified process.
  59. * Currently, it's implemented by rummaging through
  60. * the proc filesystem.
  61. *
  62. * Returns a list of all matching PIDs
  63. * It is the caller's duty to free the returned pidlist.
  64. *
  65. * Modified by Vladimir Oleynik for use with libbb/procps.c
  66. */
  67. pid_t* FAST_FUNC find_pid_by_name(const char *procName)
  68. {
  69. pid_t* pidList;
  70. int i = 0;
  71. procps_status_t* p = NULL;
  72. pidList = xzalloc(sizeof(*pidList));
  73. while ((p = procps_scan(p, PSSCAN_PID|PSSCAN_COMM|PSSCAN_ARGVN|PSSCAN_EXE))) {
  74. if (comm_match(p, procName)
  75. /* or we require argv0 to match (essential for matching reexeced /proc/self/exe)*/
  76. || (p->argv0 && strcmp(bb_basename(p->argv0), procName) == 0)
  77. /* or we require /proc/PID/exe link to match */
  78. || (p->exe && strcmp(bb_basename(p->exe), procName) == 0)
  79. ) {
  80. pidList = xrealloc_vector(pidList, 2, i);
  81. pidList[i++] = p->pid;
  82. }
  83. }
  84. pidList[i] = 0;
  85. return pidList;
  86. }
  87. pid_t* FAST_FUNC pidlist_reverse(pid_t *pidList)
  88. {
  89. int i = 0;
  90. while (pidList[i])
  91. i++;
  92. if (--i >= 0) {
  93. pid_t k;
  94. int j;
  95. for (j = 0; i > j; i--, j++) {
  96. k = pidList[i];
  97. pidList[i] = pidList[j];
  98. pidList[j] = k;
  99. }
  100. }
  101. return pidList;
  102. }