blob: 9dd0e036e93f5564a72c789b0d81edb4420587d5 [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 *
Bernhard Reutner-Fischer6d6a40c2005-10-04 14:31:18 +00007 * Licensed under the GPL v2, see the file LICENSE in this tarball.
Eric Andersenbdfd0d72001-10-24 05:00:29 +00008 */
9
Erik Andersen246cc6d2000-03-07 07:41:42 +000010#include <stdio.h>
Eric Andersened3ef502001-01-27 08:24:39 +000011#include <stdlib.h>
Eric Andersenef8b6c71999-10-20 08:05:35 +000012#include <unistd.h>
13#include <dirent.h>
Erik Andersen246cc6d2000-03-07 07:41:42 +000014#include <errno.h>
Eric Andersend23f9ba1999-10-20 19:18:15 +000015#include <fcntl.h>
16#include <ctype.h>
Eric Andersened3ef502001-01-27 08:24:39 +000017#include <string.h>
Eric Andersen8d79ce82001-07-22 23:00:15 +000018#include <termios.h>
Erik Andersen2ac2fae2000-03-07 23:32:17 +000019#include <sys/ioctl.h>
Eric Andersencbe31da2001-02-20 06:14:08 +000020#include "busybox.h"
Bernhard Reutner-Fischer6d6a40c2005-10-04 14:31:18 +000021#if ENABLE_SELINUX
Rob Landley60158cb2005-05-03 06:25:50 +000022#include <selinux/selinux.h> /* for is_selinux_enabled() */
Eric Andersen9e480452003-07-03 10:07:04 +000023#endif
Erik Andersen2ac2fae2000-03-07 23:32:17 +000024
Eric Andersenef8b6c71999-10-20 08:05:35 +000025extern int ps_main(int argc, char **argv)
26{
Eric Andersen44608e92002-10-22 12:21:15 +000027 procps_status_t * p;
"Vladimir N. Oleynik"be0ed3d2005-10-04 16:48:26 +000028 int i, len;
"Vladimir N. Oleynik"35939d92005-10-05 10:52:47 +000029
Bernhard Reutner-Fischer6d6a40c2005-10-04 14:31:18 +000030#if ENABLE_SELINUX
Eric Andersen9e480452003-07-03 10:07:04 +000031 int use_selinux = 0;
Rob Landley60158cb2005-05-03 06:25:50 +000032 security_context_t sid=NULL;
Eric Andersen9e480452003-07-03 10:07:04 +000033#endif
"Vladimir N. Oleynik"35939d92005-10-05 10:52:47 +000034
"Vladimir N. Oleynik"be0ed3d2005-10-04 16:48:26 +000035#if ENABLE_FEATURE_PS_WIDE
36 int terminal_width;
37 int w_count = 0;
"Vladimir N. Oleynik"35939d92005-10-05 10:52:47 +000038
39 bb_opt_complementally="-:ww";
"Vladimir N. Oleynik"be0ed3d2005-10-04 16:48:26 +000040#else
"Vladimir N. Oleynik"35939d92005-10-05 10:52:47 +000041# define terminal_width 79
"Vladimir N. Oleynik"be0ed3d2005-10-04 16:48:26 +000042#endif
Bernhard Reutner-Fischer6d6a40c2005-10-04 14:31:18 +000043
44#if ENABLE_FEATURE_PS_WIDE || ENABLE_SELINUX
45 /* handle arguments */
"Vladimir N. Oleynik"be0ed3d2005-10-04 16:48:26 +000046#if ENABLE_FEATURE_PS_WIDE && ENABLE_SELINUX
"Vladimir N. Oleynik"be0ed3d2005-10-04 16:48:26 +000047 i = bb_getopt_ulflags(argc, argv, "wc", &w_count);
48#elif ENABLE_FEATURE_PS_WIDE && !ENABLE_SELINUX
"Vladimir N. Oleynik"35939d92005-10-05 10:52:47 +000049 bb_getopt_ulflags(argc, argv, "w", &w_count);
"Vladimir N. Oleynik"9c44dce2005-10-04 17:09:50 +000050#else /* !ENABLE_FEATURE_PS_WIDE && ENABLE_SELINUX */
"Vladimir N. Oleynik"be0ed3d2005-10-04 16:48:26 +000051 i = bb_getopt_ulflags(argc, argv, "c");
Bernhard Reutner-Fischer6d6a40c2005-10-04 14:31:18 +000052#endif
"Vladimir N. Oleynik"be0ed3d2005-10-04 16:48:26 +000053#if ENABLE_FEATURE_PS_WIDE
54 /* if w is given once, GNU ps sets the width to 132,
55 * if w is given more than once, it is "unlimited"
56 */
"Vladimir N. Oleynik"35939d92005-10-05 10:52:47 +000057 if(w_count) {
"Vladimir N. Oleynik"be0ed3d2005-10-04 16:48:26 +000058 terminal_width = (w_count==1) ? 132 : INT_MAX;
59 } else {
"Vladimir N. Oleynik"35939d92005-10-05 10:52:47 +000060 get_terminal_width_height(1, &terminal_width, NULL);
"Vladimir N. Oleynik"be0ed3d2005-10-04 16:48:26 +000061 /* Go one less... */
62 terminal_width--;
Bernhard Reutner-Fischer6d6a40c2005-10-04 14:31:18 +000063 }
64#endif
"Vladimir N. Oleynik"be0ed3d2005-10-04 16:48:26 +000065#if ENABLE_SELINUX
"Vladimir N. Oleynik"9c44dce2005-10-04 17:09:50 +000066 if ((i & (1+ENABLE_FEATURE_PS_WIDE)) && is_selinux_enabled())
"Vladimir N. Oleynik"be0ed3d2005-10-04 16:48:26 +000067 use_selinux = 1;
68#endif
69#endif /* ENABLE_FEATURE_PS_WIDE || ENABLE_SELINUX */
Bernhard Reutner-Fischer6d6a40c2005-10-04 14:31:18 +000070
Bernhard Reutner-Fischer6d6a40c2005-10-04 14:31:18 +000071#if ENABLE_SELINUX
Rob Landley60158cb2005-05-03 06:25:50 +000072 if (use_selinux)
73 printf(" PID Context Stat Command\n");
Eric Andersen9e480452003-07-03 10:07:04 +000074 else
75#endif
Rob Landley60158cb2005-05-03 06:25:50 +000076 printf(" PID Uid VmSize Stat Command\n");
Erik Andersene49d5ec2000-02-08 19:58:47 +000077
Rob Landley60158cb2005-05-03 06:25:50 +000078 while ((p = procps_scan(1)) != 0) {
79 char *namecmd = p->cmd;
Bernhard Reutner-Fischer6d6a40c2005-10-04 14:31:18 +000080#if ENABLE_SELINUX
"Vladimir N. Oleynik"be0ed3d2005-10-04 16:48:26 +000081 if (use_selinux)
Rob Landley60158cb2005-05-03 06:25:50 +000082 {
Eric Andersen9e480452003-07-03 10:07:04 +000083 char sbuf[128];
84 len = sizeof(sbuf);
Eric Andersen9e480452003-07-03 10:07:04 +000085
Rob Landley60158cb2005-05-03 06:25:50 +000086 if (is_selinux_enabled()) {
87 if (getpidcon(p->pid,&sid)<0)
88 sid=NULL;
89 }
90
91 if (sid) {
92 /* I assume sid initilized with NULL */
93 len = strlen(sid)+1;
94 safe_strncpy(sbuf, sid, len);
95 freecon(sid);
96 sid=NULL;
97 }else {
98 safe_strncpy(sbuf, "unknown",7);
99 }
Eric Andersen9e480452003-07-03 10:07:04 +0000100 len = printf("%5d %-32s %s ", p->pid, sbuf, p->state);
Bernhard Reutner-Fischer6d6a40c2005-10-04 14:31:18 +0000101 }
Eric Andersen9e480452003-07-03 10:07:04 +0000102 else
103#endif
Rob Landley60158cb2005-05-03 06:25:50 +0000104 if(p->rss == 0)
105 len = printf("%5d %-8s %s ", p->pid, p->user, p->state);
106 else
107 len = printf("%5d %-8s %6ld %s ", p->pid, p->user, p->rss, p->state);
Bernhard Reutner-Fischer6d6a40c2005-10-04 14:31:18 +0000108
Eric Andersen44608e92002-10-22 12:21:15 +0000109 i = terminal_width-len;
110
Bernhard Reutner-Fischer6d6a40c2005-10-04 14:31:18 +0000111 if(namecmd && namecmd[0]) {
Eric Andersen44608e92002-10-22 12:21:15 +0000112 if(i < 0)
Bernhard Reutner-Fischer6d6a40c2005-10-04 14:31:18 +0000113 i = 0;
"Vladimir N. Oleynik"2d604582006-01-30 14:02:06 +0000114 if(strlen(namecmd) > (size_t)i)
Eric Andersen44608e92002-10-22 12:21:15 +0000115 namecmd[i] = 0;
116 printf("%s\n", namecmd);
117 } else {
118 namecmd = p->short_cmd;
119 if(i < 2)
120 i = 2;
"Vladimir N. Oleynik"2d604582006-01-30 14:02:06 +0000121 if(strlen(namecmd) > ((size_t)i-2))
Eric Andersen44608e92002-10-22 12:21:15 +0000122 namecmd[i-2] = 0;
123 printf("[%s]\n", namecmd);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000124 }
Bernhard Reutner-Fischer6d6a40c2005-10-04 14:31:18 +0000125 /* no check needed, but to make valgrind happy.. */
126 if (ENABLE_FEATURE_CLEAN_UP && p->cmd)
127 free(p->cmd);
Eric Andersend23f9ba1999-10-20 19:18:15 +0000128 }
Matt Kraai3e856ce2000-12-01 02:55:13 +0000129 return EXIT_SUCCESS;
Eric Andersenef8b6c71999-10-20 08:05:35 +0000130}