blob: 18a6db36fa14b74e6b993f616b2618f2877168f8 [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersenef8b6c71999-10-20 08:05:35 +00002/*
Erik Andersen246cc6d2000-03-07 07:41:42 +00003 * Mini ps implementation(s) for busybox
4 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +00005 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
Eric Andersenef8b6c71999-10-20 08:05:35 +00006 *
Erik Andersen246cc6d2000-03-07 07:41:42 +00007 * 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 Andersenbdfd0d72001-10-24 05:00:29 +000020 */
21
Erik Andersen246cc6d2000-03-07 07:41:42 +000022#include <stdio.h>
Eric Andersened3ef502001-01-27 08:24:39 +000023#include <stdlib.h>
Eric Andersenef8b6c71999-10-20 08:05:35 +000024#include <unistd.h>
25#include <dirent.h>
Erik Andersen246cc6d2000-03-07 07:41:42 +000026#include <errno.h>
Eric Andersend23f9ba1999-10-20 19:18:15 +000027#include <fcntl.h>
28#include <ctype.h>
Eric Andersened3ef502001-01-27 08:24:39 +000029#include <string.h>
Eric Andersen8d79ce82001-07-22 23:00:15 +000030#include <termios.h>
Erik Andersen2ac2fae2000-03-07 23:32:17 +000031#include <sys/ioctl.h>
Eric Andersencbe31da2001-02-20 06:14:08 +000032#include "busybox.h"
Eric Andersen9e480452003-07-03 10:07:04 +000033#ifdef CONFIG_SELINUX
Rob Landley60158cb2005-05-03 06:25:50 +000034#include <selinux/selinux.h> /* for is_selinux_enabled() */
Eric Andersen9e480452003-07-03 10:07:04 +000035#endif
Erik Andersen2ac2fae2000-03-07 23:32:17 +000036
Mark Whitley59ab0252001-01-23 22:30:04 +000037static const int TERMINAL_WIDTH = 79; /* not 80 in case terminal has linefold bug */
Eric Andersen86ab8a32000-06-02 03:21:42 +000038
39
Eric Andersend23f9ba1999-10-20 19:18:15 +000040
Eric Andersenef8b6c71999-10-20 08:05:35 +000041extern int ps_main(int argc, char **argv)
42{
Eric Andersen44608e92002-10-22 12:21:15 +000043 procps_status_t * p;
44 int i, len;
Eric Andersen86ab8a32000-06-02 03:21:42 +000045 int terminal_width = TERMINAL_WIDTH;
Erik Andersen2ac2fae2000-03-07 23:32:17 +000046
Eric Andersen9e480452003-07-03 10:07:04 +000047#ifdef CONFIG_SELINUX
48 int use_selinux = 0;
Rob Landley60158cb2005-05-03 06:25:50 +000049 security_context_t sid=NULL;
50 if(is_selinux_enabled() && argv[1] && !strcmp(argv[1], "-c") )
Eric Andersen9e480452003-07-03 10:07:04 +000051 use_selinux = 1;
52#endif
53
Eric Andersen8efe9672003-09-15 08:33:45 +000054 get_terminal_width_height(0, &terminal_width, NULL);
55 /* Go one less... */
56 terminal_width--;
Erik Andersen2ac2fae2000-03-07 23:32:17 +000057
Eric Andersen9e480452003-07-03 10:07:04 +000058#ifdef CONFIG_SELINUX
Rob Landley60158cb2005-05-03 06:25:50 +000059 if (use_selinux)
60 printf(" PID Context Stat Command\n");
Eric Andersen9e480452003-07-03 10:07:04 +000061 else
62#endif
Rob Landley60158cb2005-05-03 06:25:50 +000063 printf(" PID Uid VmSize Stat Command\n");
Erik Andersene49d5ec2000-02-08 19:58:47 +000064
Rob Landley60158cb2005-05-03 06:25:50 +000065 while ((p = procps_scan(1)) != 0) {
66 char *namecmd = p->cmd;
Eric Andersen9e480452003-07-03 10:07:04 +000067#ifdef CONFIG_SELINUX
Rob Landley60158cb2005-05-03 06:25:50 +000068 if ( use_selinux )
69 {
Eric Andersen9e480452003-07-03 10:07:04 +000070 char sbuf[128];
71 len = sizeof(sbuf);
Eric Andersen9e480452003-07-03 10:07:04 +000072
Rob Landley60158cb2005-05-03 06:25:50 +000073 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 Andersen9e480452003-07-03 10:07:04 +000087 len = printf("%5d %-32s %s ", p->pid, sbuf, p->state);
Rob Landley60158cb2005-05-03 06:25:50 +000088 }
Eric Andersen9e480452003-07-03 10:07:04 +000089 else
90#endif
Rob Landley60158cb2005-05-03 06:25:50 +000091 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 Andersen44608e92002-10-22 12:21:15 +000095 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 Andersene49d5ec2000-02-08 19:58:47 +0000110 }
Eric Andersen44608e92002-10-22 12:21:15 +0000111 free(p->cmd);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000112 }
Matt Kraai3e856ce2000-12-01 02:55:13 +0000113 return EXIT_SUCCESS;
Eric Andersenef8b6c71999-10-20 08:05:35 +0000114}
Erik Andersen246cc6d2000-03-07 07:41:42 +0000115