blob: 9dc45d35d45940caa68b1545d08794241923d77c [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 Andersencb81e642003-07-14 21:21:08 +00005 * Copyright (C) 1999-2003 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
34#include <fs_secure.h>
35#include <ss.h>
36#include <flask_util.h> /* for is_flask_enabled() */
37#endif
Erik Andersen2ac2fae2000-03-07 23:32:17 +000038
Mark Whitley59ab0252001-01-23 22:30:04 +000039static const int TERMINAL_WIDTH = 79; /* not 80 in case terminal has linefold bug */
Eric Andersen86ab8a32000-06-02 03:21:42 +000040
41
Eric Andersend23f9ba1999-10-20 19:18:15 +000042
Eric Andersenef8b6c71999-10-20 08:05:35 +000043extern int ps_main(int argc, char **argv)
44{
Eric Andersen44608e92002-10-22 12:21:15 +000045 procps_status_t * p;
46 int i, len;
Eric Andersenbdfd0d72001-10-24 05:00:29 +000047#ifdef CONFIG_FEATURE_AUTOWIDTH
Eric Andersenfad04fd2000-07-14 06:49:52 +000048 struct winsize win = { 0, 0, 0, 0 };
Eric Andersen86ab8a32000-06-02 03:21:42 +000049 int terminal_width = TERMINAL_WIDTH;
Erik Andersen2ac2fae2000-03-07 23:32:17 +000050#else
Eric Andersen86ab8a32000-06-02 03:21:42 +000051#define terminal_width TERMINAL_WIDTH
Erik Andersen2ac2fae2000-03-07 23:32:17 +000052#endif
53
Eric Andersen9e480452003-07-03 10:07:04 +000054#ifdef CONFIG_SELINUX
55 int use_selinux = 0;
56 security_id_t sid;
57 if(is_flask_enabled() && argv[1] && !strcmp(argv[1], "-c") )
58 use_selinux = 1;
59#endif
60
Erik Andersen2ac2fae2000-03-07 23:32:17 +000061
Eric Andersenbdfd0d72001-10-24 05:00:29 +000062#ifdef CONFIG_FEATURE_AUTOWIDTH
Erik Andersen2ac2fae2000-03-07 23:32:17 +000063 ioctl(fileno(stdout), TIOCGWINSZ, &win);
64 if (win.ws_col > 0)
65 terminal_width = win.ws_col - 1;
66#endif
67
Eric Andersen9e480452003-07-03 10:07:04 +000068#ifdef CONFIG_SELINUX
69 if(use_selinux)
70 printf(" PID Context Stat Command\n");
71 else
72#endif
Eric Andersen13727802002-04-27 06:06:11 +000073 printf(" PID Uid VmSize Stat Command\n");
Eric Andersen9e480452003-07-03 10:07:04 +000074#ifdef CONFIG_SELINUX
75 while ((p = procps_scan(1, use_selinux, &sid)) != 0) {
76#else
Eric Andersen44608e92002-10-22 12:21:15 +000077 while ((p = procps_scan(1)) != 0) {
Eric Andersen9e480452003-07-03 10:07:04 +000078#endif
Eric Andersen44608e92002-10-22 12:21:15 +000079 char *namecmd = p->cmd;
Erik Andersene49d5ec2000-02-08 19:58:47 +000080
Eric Andersen9e480452003-07-03 10:07:04 +000081#ifdef CONFIG_SELINUX
82 if(use_selinux)
83 {
84 char sbuf[128];
85 len = sizeof(sbuf);
86 if(security_sid_to_context(sid, (security_context_t)&sbuf, &len))
87 strcpy(sbuf, "unknown");
88
89 len = printf("%5d %-32s %s ", p->pid, sbuf, p->state);
90 }
91 else
92#endif
Eric Andersen44608e92002-10-22 12:21:15 +000093 if(p->rss == 0)
94 len = printf("%5d %-8s %s ", p->pid, p->user, p->state);
Eric Andersen13727802002-04-27 06:06:11 +000095 else
Eric Andersen44608e92002-10-22 12:21:15 +000096 len = printf("%5d %-8s %6ld %s ", p->pid, p->user, p->rss, p->state);
97 i = terminal_width-len;
98
99 if(namecmd != 0 && namecmd[0] != 0) {
100 if(i < 0)
101 i = 0;
102 if(strlen(namecmd) > i)
103 namecmd[i] = 0;
104 printf("%s\n", namecmd);
105 } else {
106 namecmd = p->short_cmd;
107 if(i < 2)
108 i = 2;
109 if(strlen(namecmd) > (i-2))
110 namecmd[i-2] = 0;
111 printf("[%s]\n", namecmd);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000112 }
Eric Andersen44608e92002-10-22 12:21:15 +0000113 free(p->cmd);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000114 }
Matt Kraai3e856ce2000-12-01 02:55:13 +0000115 return EXIT_SUCCESS;
Eric Andersenef8b6c71999-10-20 08:05:35 +0000116}
Erik Andersen246cc6d2000-03-07 07:41:42 +0000117