Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Eric Andersen | ef8b6c7 | 1999-10-20 08:05:35 +0000 | [diff] [blame] | 2 | /* |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 3 | * Mini ps implementation(s) for busybox |
| 4 | * |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> |
Eric Andersen | ef8b6c7 | 1999-10-20 08:05:35 +0000 | [diff] [blame] | 6 | * |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 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 |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 20 | */ |
| 21 | |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 22 | #include <stdio.h> |
Eric Andersen | ed3ef50 | 2001-01-27 08:24:39 +0000 | [diff] [blame] | 23 | #include <stdlib.h> |
Eric Andersen | ef8b6c7 | 1999-10-20 08:05:35 +0000 | [diff] [blame] | 24 | #include <unistd.h> |
| 25 | #include <dirent.h> |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 26 | #include <errno.h> |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 27 | #include <fcntl.h> |
| 28 | #include <ctype.h> |
Eric Andersen | ed3ef50 | 2001-01-27 08:24:39 +0000 | [diff] [blame] | 29 | #include <string.h> |
Eric Andersen | 8d79ce8 | 2001-07-22 23:00:15 +0000 | [diff] [blame] | 30 | #include <termios.h> |
Erik Andersen | 2ac2fae | 2000-03-07 23:32:17 +0000 | [diff] [blame] | 31 | #include <sys/ioctl.h> |
Eric Andersen | cbe31da | 2001-02-20 06:14:08 +0000 | [diff] [blame] | 32 | #include "busybox.h" |
Eric Andersen | 9e48045 | 2003-07-03 10:07:04 +0000 | [diff] [blame] | 33 | #ifdef CONFIG_SELINUX |
Rob Landley | 60158cb | 2005-05-03 06:25:50 +0000 | [diff] [blame] | 34 | #include <selinux/selinux.h> /* for is_selinux_enabled() */ |
Eric Andersen | 9e48045 | 2003-07-03 10:07:04 +0000 | [diff] [blame] | 35 | #endif |
Erik Andersen | 2ac2fae | 2000-03-07 23:32:17 +0000 | [diff] [blame] | 36 | |
Mark Whitley | 59ab025 | 2001-01-23 22:30:04 +0000 | [diff] [blame] | 37 | static const int TERMINAL_WIDTH = 79; /* not 80 in case terminal has linefold bug */ |
Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 38 | |
| 39 | |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 40 | |
Eric Andersen | ef8b6c7 | 1999-10-20 08:05:35 +0000 | [diff] [blame] | 41 | extern int ps_main(int argc, char **argv) |
| 42 | { |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 43 | procps_status_t * p; |
| 44 | int i, len; |
Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 45 | int terminal_width = TERMINAL_WIDTH; |
Erik Andersen | 2ac2fae | 2000-03-07 23:32:17 +0000 | [diff] [blame] | 46 | |
Eric Andersen | 9e48045 | 2003-07-03 10:07:04 +0000 | [diff] [blame] | 47 | #ifdef CONFIG_SELINUX |
| 48 | int use_selinux = 0; |
Rob Landley | 60158cb | 2005-05-03 06:25:50 +0000 | [diff] [blame] | 49 | security_context_t sid=NULL; |
| 50 | if(is_selinux_enabled() && argv[1] && !strcmp(argv[1], "-c") ) |
Eric Andersen | 9e48045 | 2003-07-03 10:07:04 +0000 | [diff] [blame] | 51 | use_selinux = 1; |
| 52 | #endif |
| 53 | |
Eric Andersen | 8efe967 | 2003-09-15 08:33:45 +0000 | [diff] [blame] | 54 | get_terminal_width_height(0, &terminal_width, NULL); |
| 55 | /* Go one less... */ |
| 56 | terminal_width--; |
Erik Andersen | 2ac2fae | 2000-03-07 23:32:17 +0000 | [diff] [blame] | 57 | |
Eric Andersen | 9e48045 | 2003-07-03 10:07:04 +0000 | [diff] [blame] | 58 | #ifdef CONFIG_SELINUX |
Rob Landley | 60158cb | 2005-05-03 06:25:50 +0000 | [diff] [blame] | 59 | if (use_selinux) |
| 60 | printf(" PID Context Stat Command\n"); |
Eric Andersen | 9e48045 | 2003-07-03 10:07:04 +0000 | [diff] [blame] | 61 | else |
| 62 | #endif |
Rob Landley | 60158cb | 2005-05-03 06:25:50 +0000 | [diff] [blame] | 63 | printf(" PID Uid VmSize Stat Command\n"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 64 | |
Rob Landley | 60158cb | 2005-05-03 06:25:50 +0000 | [diff] [blame] | 65 | while ((p = procps_scan(1)) != 0) { |
| 66 | char *namecmd = p->cmd; |
Eric Andersen | 9e48045 | 2003-07-03 10:07:04 +0000 | [diff] [blame] | 67 | #ifdef CONFIG_SELINUX |
Rob Landley | 60158cb | 2005-05-03 06:25:50 +0000 | [diff] [blame] | 68 | if ( use_selinux ) |
| 69 | { |
Eric Andersen | 9e48045 | 2003-07-03 10:07:04 +0000 | [diff] [blame] | 70 | char sbuf[128]; |
| 71 | len = sizeof(sbuf); |
Eric Andersen | 9e48045 | 2003-07-03 10:07:04 +0000 | [diff] [blame] | 72 | |
Rob Landley | 60158cb | 2005-05-03 06:25:50 +0000 | [diff] [blame] | 73 | if (is_selinux_enabled()) { |
| 74 | if (getpidcon(p->pid,&sid)<0) |
| 75 | sid=NULL; |
| 76 | } |
| 77 | |
| 78 | if (sid) { |
| 79 | /* I assume sid initilized with NULL */ |
| 80 | len = strlen(sid)+1; |
| 81 | safe_strncpy(sbuf, sid, len); |
| 82 | freecon(sid); |
| 83 | sid=NULL; |
| 84 | }else { |
| 85 | safe_strncpy(sbuf, "unknown",7); |
| 86 | } |
Eric Andersen | 9e48045 | 2003-07-03 10:07:04 +0000 | [diff] [blame] | 87 | len = printf("%5d %-32s %s ", p->pid, sbuf, p->state); |
Rob Landley | 60158cb | 2005-05-03 06:25:50 +0000 | [diff] [blame] | 88 | } |
Eric Andersen | 9e48045 | 2003-07-03 10:07:04 +0000 | [diff] [blame] | 89 | else |
| 90 | #endif |
Rob Landley | 60158cb | 2005-05-03 06:25:50 +0000 | [diff] [blame] | 91 | if(p->rss == 0) |
| 92 | len = printf("%5d %-8s %s ", p->pid, p->user, p->state); |
| 93 | else |
| 94 | len = printf("%5d %-8s %6ld %s ", p->pid, p->user, p->rss, p->state); |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 95 | i = terminal_width-len; |
| 96 | |
| 97 | if(namecmd != 0 && namecmd[0] != 0) { |
| 98 | if(i < 0) |
| 99 | i = 0; |
| 100 | if(strlen(namecmd) > i) |
| 101 | namecmd[i] = 0; |
| 102 | printf("%s\n", namecmd); |
| 103 | } else { |
| 104 | namecmd = p->short_cmd; |
| 105 | if(i < 2) |
| 106 | i = 2; |
| 107 | if(strlen(namecmd) > (i-2)) |
| 108 | namecmd[i-2] = 0; |
| 109 | printf("[%s]\n", namecmd); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 110 | } |
Eric Andersen | 44608e9 | 2002-10-22 12:21:15 +0000 | [diff] [blame] | 111 | free(p->cmd); |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 112 | } |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 113 | return EXIT_SUCCESS; |
Eric Andersen | ef8b6c7 | 1999-10-20 08:05:35 +0000 | [diff] [blame] | 114 | } |
Erik Andersen | 246cc6d | 2000-03-07 07:41:42 +0000 | [diff] [blame] | 115 | |