patch-procinfo_c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. $OpenBSD: patch-procinfo_c,v 1.3 2011/07/06 13:15:40 jasper Exp $
  2. --- procinfo.c.orig Tue Jun 6 20:36:47 2000
  3. +++ procinfo.c Wed Jul 6 15:14:42 2011
  4. @@ -64,9 +64,9 @@ void get_info(int pid, struct procinfo *p)
  5. #else
  6. int fill_kinfo(struct kinfo_proc *info, int pid)
  7. {
  8. - int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, pid };
  9. - int len = sizeof *info;
  10. - if(sysctl(mib, 4, info, &len, 0, 0) == -1)
  11. + int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, pid, sizeof (struct kinfo_proc), 1 };
  12. + size_t len = sizeof *info;
  13. + if(sysctl(mib, 6, info, &len, 0, 0) == -1)
  14. return -1;
  15. return len?0:-1;
  16. }
  17. @@ -83,12 +83,12 @@ void get_info(int pid, struct procinfo *p)
  18. if(fill_kinfo(&info, pid) == -1) return;
  19. - p->ppid = info.kp_eproc.e_ppid;
  20. - p->tpgid = info.kp_eproc.e_tpgid;
  21. - p->euid = info.kp_eproc.e_pcred.p_svuid;
  22. - p->stat = info.kp_proc.p_stat;
  23. - strncpy(p->exec_file, info.kp_proc.p_comm, EXEC_FILE);
  24. - p->cterm = info.kp_eproc.e_tdev;
  25. + p->ppid = info.p_ppid;
  26. + p->tpgid = info.p_tpgid;
  27. + p->euid = info.p_uid;
  28. + p->stat = info.p_stat;
  29. + strncpy(p->exec_file, info.p_comm, EXEC_FILE);
  30. + p->cterm = info.p_tdev;
  31. p->exec_file[EXEC_FILE] = '\0';
  32. }
  33. #endif
  34. @@ -123,8 +123,9 @@ int get_term(char *tty)
  35. */
  36. int get_login_pid(char *tty)
  37. {
  38. - int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_TTY, 0};
  39. - int len, t, el, i, pid, cndt = -1, l;
  40. + int mib[6] = {CTL_KERN, KERN_PROC, KERN_PROC_TTY, 0, sizeof(struct kinfo_proc), 0};
  41. + int t, el, i, pid, cndt = -1, l;
  42. + size_t len;
  43. struct kinfo_proc *info;
  44. struct procinfo p;
  45. @@ -134,26 +135,27 @@ int get_login_pid(char *tty)
  46. if((t = get_term(tty)) == -1) return -1;
  47. mib[3] = t;
  48. - if(sysctl(mib, 4, 0, &len, 0, 0) == -1)
  49. + if(sysctl(mib, 6, NULL, &len, NULL, 0) == -1)
  50. return -1;
  51. info = calloc(1, len);
  52. if(!info) return -1;
  53. el = len/sizeof(struct kinfo_proc);
  54. - if(sysctl(mib, 4, info, &len, 0, 0) == -1)
  55. + mib[5] = el;
  56. + if(sysctl(mib, 6, info, &len, NULL, 0) == -1)
  57. return -1;
  58. for(i = 0; i < el; i++) {
  59. - if(!(pid = info[i].kp_proc.p_pid)) continue;
  60. + if(!(pid = info[i].p_pid)) continue;
  61. get_info(get_ppid(pid), &p);
  62. if(p.cterm == -1 || p.cterm != t) {
  63. cndt = pid;
  64. - l = strlen(info[i].kp_proc.p_comm);
  65. + l = strlen(info[i].p_comm);
  66. /*
  67. * This is our best match: parent of the process
  68. * doesn't have controlling terminal and process'
  69. * name ends with "sh"
  70. *
  71. */
  72. - if(l > 1 && !strncmp("sh",info[i].kp_proc.p_comm+l-2,2)) {
  73. + if(l > 1 && !strncmp("sh",info[i].p_comm+l-2,2)) {
  74. free(info);
  75. return pid;
  76. }
  77. @@ -168,15 +170,16 @@ int get_login_pid(char *tty)
  78. */
  79. int get_all_info(struct kinfo_proc **info)
  80. {
  81. - int mib[3] = { CTL_KERN, KERN_PROC, KERN_PROC_ALL };
  82. - int len, el;
  83. + int mib[6] = { CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc), 0 };
  84. + int el;
  85. + size_t len;
  86. - if(sysctl(mib, 3, 0, &len, 0, 0) == -1)
  87. + if(sysctl(mib, 6, NULL, &len, 0, 0) == -1)
  88. return 0;
  89. *info = calloc(1, len);
  90. if(!*info) return 0;
  91. - el = len/sizeof(struct kinfo_proc);
  92. - if(sysctl(mib, 3, *info, &len, 0, 0) == -1)
  93. + mib[5] = el = len/sizeof(struct kinfo_proc);
  94. + if(sysctl(mib, 6, *info, &len, 0, 0) == -1)
  95. return 0;
  96. return el;
  97. }
  98. @@ -239,7 +242,7 @@ char *get_cmdline(int pid)
  99. bzero(buf, sizeof buf);
  100. if(fill_kinfo(&info, pid) == -1)
  101. return "-";
  102. - memcpy(buf, info.kp_proc.p_comm, sizeof buf - 1);
  103. + memcpy(buf, info.p_comm, sizeof buf - 1);
  104. if(!full_cmd) return buf;
  105. #ifdef HAVE_LIBKVM
  106. if(!can_use_kvm) return buf;