find_pid_by_name.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 the GPL v2 or later, see the file LICENSE in this tarball.
  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. /* comm does not match */
  38. if (strncmp(p->comm, procName, 15) != 0)
  39. return 0;
  40. /* in Linux, if comm is 15 chars, it may be a truncated */
  41. if (p->comm[14] == '\0') /* comm is not truncated - match */
  42. return 1;
  43. /* comm is truncated, but first 15 chars match.
  44. * This can be crazily_long_script_name.sh!
  45. * The telltale sign is basename(argv[1]) == procName. */
  46. if (!p->argv0)
  47. return 0;
  48. argv1idx = strlen(p->argv0) + 1;
  49. if (argv1idx >= p->argv_len)
  50. return 0;
  51. if (strcmp(bb_basename(p->argv0 + argv1idx), procName) != 0)
  52. return 0;
  53. return 1;
  54. }
  55. /* find_pid_by_name()
  56. *
  57. * Modified by Vladimir Oleynik for use with libbb/procps.c
  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. pid_t* FAST_FUNC find_pid_by_name(const char *procName)
  66. {
  67. pid_t* pidList;
  68. int i = 0;
  69. procps_status_t* p = NULL;
  70. pidList = xzalloc(sizeof(*pidList));
  71. while ((p = procps_scan(p, PSSCAN_PID|PSSCAN_COMM|PSSCAN_ARGVN))) {
  72. if (comm_match(p, procName)
  73. /* or we require argv0 to match (essential for matching reexeced /proc/self/exe)*/
  74. || (p->argv0 && strcmp(bb_basename(p->argv0), procName) == 0)
  75. /* TODO: we can also try /proc/NUM/exe link, do we want that? */
  76. ) {
  77. pidList = xrealloc_vector(pidList, 2, i);
  78. pidList[i++] = p->pid;
  79. }
  80. }
  81. pidList[i] = 0;
  82. return pidList;
  83. }
  84. pid_t* FAST_FUNC pidlist_reverse(pid_t *pidList)
  85. {
  86. int i = 0;
  87. while (pidList[i])
  88. i++;
  89. if (--i >= 0) {
  90. pid_t k;
  91. int j;
  92. for (j = 0; i > j; i--, j++) {
  93. k = pidList[i];
  94. pidList[i] = pidList[j];
  95. pidList[j] = k;
  96. }
  97. }
  98. return pidList;
  99. }