ps.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Mini ps implementation(s) for busybox
  4. *
  5. * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the Free
  9. * Software Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful, but WITHOUT
  13. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  15. * more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along with
  18. * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  19. * Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <unistd.h>
  24. #include <dirent.h>
  25. #include <errno.h>
  26. #include <fcntl.h>
  27. #include <ctype.h>
  28. #include <string.h>
  29. #include <termios.h>
  30. #include <sys/ioctl.h>
  31. #include "busybox.h"
  32. #ifdef CONFIG_SELINUX
  33. #include <selinux/selinux.h> /* for is_selinux_enabled() */
  34. #endif
  35. static const int TERMINAL_WIDTH = 79; /* not 80 in case terminal has linefold bug */
  36. extern int ps_main(int argc, char **argv)
  37. {
  38. procps_status_t * p;
  39. int i, len;
  40. int terminal_width = TERMINAL_WIDTH;
  41. #ifdef CONFIG_SELINUX
  42. int use_selinux = 0;
  43. security_context_t sid=NULL;
  44. if(is_selinux_enabled() && argv[1] && !strcmp(argv[1], "-c") )
  45. use_selinux = 1;
  46. #endif
  47. get_terminal_width_height(0, &terminal_width, NULL);
  48. /* Go one less... */
  49. terminal_width--;
  50. #ifdef CONFIG_SELINUX
  51. if (use_selinux)
  52. printf(" PID Context Stat Command\n");
  53. else
  54. #endif
  55. printf(" PID Uid VmSize Stat Command\n");
  56. while ((p = procps_scan(1)) != 0) {
  57. char *namecmd = p->cmd;
  58. #ifdef CONFIG_SELINUX
  59. if ( use_selinux )
  60. {
  61. char sbuf[128];
  62. len = sizeof(sbuf);
  63. if (is_selinux_enabled()) {
  64. if (getpidcon(p->pid,&sid)<0)
  65. sid=NULL;
  66. }
  67. if (sid) {
  68. /* I assume sid initilized with NULL */
  69. len = strlen(sid)+1;
  70. safe_strncpy(sbuf, sid, len);
  71. freecon(sid);
  72. sid=NULL;
  73. }else {
  74. safe_strncpy(sbuf, "unknown",7);
  75. }
  76. len = printf("%5d %-32s %s ", p->pid, sbuf, p->state);
  77. }
  78. else
  79. #endif
  80. if(p->rss == 0)
  81. len = printf("%5d %-8s %s ", p->pid, p->user, p->state);
  82. else
  83. len = printf("%5d %-8s %6ld %s ", p->pid, p->user, p->rss, p->state);
  84. i = terminal_width-len;
  85. if(namecmd != 0 && namecmd[0] != 0) {
  86. if(i < 0)
  87. i = 0;
  88. if(strlen(namecmd) > i)
  89. namecmd[i] = 0;
  90. printf("%s\n", namecmd);
  91. } else {
  92. namecmd = p->short_cmd;
  93. if(i < 2)
  94. i = 2;
  95. if(strlen(namecmd) > (i-2))
  96. namecmd[i-2] = 0;
  97. printf("[%s]\n", namecmd);
  98. }
  99. free(p->cmd);
  100. }
  101. return EXIT_SUCCESS;
  102. }