find_pid_by_name.c 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. /* find_pid_by_name()
  35. *
  36. * Modified by Vladimir Oleynik for use with libbb/procps.c
  37. * This finds the pid of the specified process.
  38. * Currently, it's implemented by rummaging through
  39. * the proc filesystem.
  40. *
  41. * Returns a list of all matching PIDs
  42. * It is the caller's duty to free the returned pidlist.
  43. */
  44. pid_t* find_pid_by_name(const char* procName)
  45. {
  46. pid_t* pidList;
  47. int i = 0;
  48. procps_status_t* p = NULL;
  49. pidList = xmalloc(sizeof(*pidList));
  50. while ((p = procps_scan(p, PSSCAN_PID|PSSCAN_COMM|PSSCAN_ARGV0))) {
  51. if (
  52. /* we require comm to match and to not be truncated */
  53. /* in Linux, if comm is 15 chars, it may be a truncated
  54. * name, so we don't allow that to match */
  55. (!p->comm[sizeof(p->comm)-2] && strcmp(p->comm, procName) == 0)
  56. /* or we require argv0 to match (essential for matching reexeced /proc/self/exe)*/
  57. || (p->argv0 && strcmp(bb_basename(p->argv0), procName) == 0)
  58. /* TOOD: we can also try /proc/NUM/exe link, do we want that? */
  59. ) {
  60. pidList = xrealloc(pidList, sizeof(*pidList) * (i+2));
  61. pidList[i++] = p->pid;
  62. }
  63. }
  64. pidList[i] = 0;
  65. return pidList;
  66. }
  67. pid_t *pidlist_reverse(pid_t *pidList)
  68. {
  69. int i = 0;
  70. while (pidList[i])
  71. i++;
  72. if (--i >= 0) {
  73. pid_t k;
  74. int j;
  75. for (j = 0; i > j; i--, j++) {
  76. k = pidList[i];
  77. pidList[i] = pidList[j];
  78. pidList[j] = k;
  79. }
  80. }
  81. return pidList;
  82. }