find_pid_by_name.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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(
  79. procName[0] == '/' ? p->exe /* support "pidof /path/to/binary" case too */
  80. : bb_basename(p->exe),
  81. procName
  82. ) == 0)
  83. ) {
  84. pidList = xrealloc_vector(pidList, 2, i);
  85. pidList[i++] = p->pid;
  86. }
  87. }
  88. pidList[i] = 0;
  89. return pidList;
  90. }
  91. pid_t* FAST_FUNC pidlist_reverse(pid_t *pidList)
  92. {
  93. int i = 0;
  94. while (pidList[i])
  95. i++;
  96. if (--i >= 0) {
  97. pid_t k;
  98. int j;
  99. for (j = 0; i > j; i--, j++) {
  100. k = pidList[i];
  101. pidList[i] = pidList[j];
  102. pidList[j] = k;
  103. }
  104. }
  105. return pidList;
  106. }