blob: 2bcd2ccedd0b1f3c6bee0e8f2140320a1977338d [file] [log] [blame]
Eric Andersen44608e92002-10-22 12:21:15 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
5 * Copyright 1998 by Albert Cahalan; all rights reserved.
6 * Copyright (C) 2002 by Vladimir Oleynik <dzo@simtreas.ru>
Eric Andersen44608e92002-10-22 12:21:15 +00007 *
Bernhard Reutner-Fischerb1629b12006-05-19 19:29:19 +00008 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Eric Andersen44608e92002-10-22 12:21:15 +00009 */
10
Eric Andersen44608e92002-10-22 12:21:15 +000011#include <dirent.h>
12#include <string.h>
13#include <stdlib.h>
Rob Landley7818a422006-04-25 18:42:23 +000014#include <sys/param.h>
Eric Andersen44608e92002-10-22 12:21:15 +000015#include <unistd.h>
Rob Landleyb2804552006-02-13 22:04:27 +000016#include <fcntl.h>
Eric Andersen44608e92002-10-22 12:21:15 +000017
Glenn L McGrath7b1eca22002-11-24 01:32:56 +000018#include "libbb.h"
Eric Andersen44608e92002-10-22 12:21:15 +000019
Rob Landleyb2804552006-02-13 22:04:27 +000020
"Vladimir N. Oleynik"465300c2006-02-14 10:17:09 +000021#define PROCPS_BUFSIZE 1024
22
23static int read_to_buf(const char *filename, void *buf)
Rob Landleyb2804552006-02-13 22:04:27 +000024{
25 int fd;
"Vladimir N. Oleynik"465300c2006-02-14 10:17:09 +000026 ssize_t ret;
Rob Landleyb2804552006-02-13 22:04:27 +000027
28 fd = open(filename, O_RDONLY);
Mike Frysinger58dda842006-07-12 20:04:00 +000029 if (fd < 0)
Rob Landleyb2804552006-02-13 22:04:27 +000030 return -1;
Rob Landley22f383e2006-06-27 18:14:12 +000031 ret = read(fd, buf, PROCPS_BUFSIZE-1);
32 ((char *)buf)[ret > 0 ? ret : 0] = 0;
Rob Landleyb2804552006-02-13 22:04:27 +000033 close(fd);
"Vladimir N. Oleynik"465300c2006-02-14 10:17:09 +000034 return ret;
Rob Landleyb2804552006-02-13 22:04:27 +000035}
36
37
Rob Landleydfba7412006-03-06 20:47:33 +000038procps_status_t * procps_scan(int save_user_arg0)
Eric Andersen44608e92002-10-22 12:21:15 +000039{
40 static DIR *dir;
41 struct dirent *entry;
42 static procps_status_t ret_status;
43 char *name;
44 int n;
45 char status[32];
Rob Landleyb2804552006-02-13 22:04:27 +000046 char *status_tail;
"Vladimir N. Oleynik"465300c2006-02-14 10:17:09 +000047 char buf[PROCPS_BUFSIZE];
Eric Andersen44608e92002-10-22 12:21:15 +000048 procps_status_t curstatus;
49 int pid;
50 long tasknice;
51 struct stat sb;
52
53 if (!dir) {
Rob Landleyd921b2e2006-08-03 15:41:12 +000054 dir = xopendir("/proc");
Eric Andersen44608e92002-10-22 12:21:15 +000055 }
Mike Frysinger58dda842006-07-12 20:04:00 +000056 for (;;) {
Denis Vlasenkoa6dbb082006-10-12 19:29:44 +000057 entry = readdir(dir);
58 if (entry == NULL) {
Eric Andersen44608e92002-10-22 12:21:15 +000059 closedir(dir);
60 dir = 0;
61 return 0;
62 }
63 name = entry->d_name;
64 if (!(*name >= '0' && *name <= '9'))
65 continue;
66
67 memset(&curstatus, 0, sizeof(procps_status_t));
68 pid = atoi(name);
69 curstatus.pid = pid;
70
Rob Landleyb2804552006-02-13 22:04:27 +000071 status_tail = status + sprintf(status, "/proc/%d", pid);
Mike Frysinger58dda842006-07-12 20:04:00 +000072 if (stat(status, &sb))
Eric Andersen242ab832004-01-27 20:17:39 +000073 continue;
Bernhard Reutner-Fischerd5bd1372005-09-20 21:06:17 +000074 bb_getpwuid(curstatus.user, sb.st_uid, sizeof(curstatus.user));
Eric Andersen242ab832004-01-27 20:17:39 +000075
Mike Frysinger301ad672006-06-07 20:24:34 +000076 /* see proc(5) for some details on this */
Rob Landleyb2804552006-02-13 22:04:27 +000077 strcpy(status_tail, "/stat");
"Vladimir N. Oleynik"465300c2006-02-14 10:17:09 +000078 n = read_to_buf(status, buf);
Mike Frysinger58dda842006-07-12 20:04:00 +000079 if (n < 0)
Eric Andersen44608e92002-10-22 12:21:15 +000080 continue;
81 name = strrchr(buf, ')'); /* split into "PID (cmd" and "<rest>" */
Mike Frysinger58dda842006-07-12 20:04:00 +000082 if (name == 0 || name[1] != ' ')
Eric Andersen44608e92002-10-22 12:21:15 +000083 continue;
84 *name = 0;
85 sscanf(buf, "%*s (%15c", curstatus.short_cmd);
86 n = sscanf(name+2,
87 "%c %d "
88 "%*s %*s %*s %*s " /* pgrp, session, tty, tpgid */
89 "%*s %*s %*s %*s %*s " /* flags, min_flt, cmin_flt, maj_flt, cmaj_flt */
"Vladimir N. Oleynik"f246dc72005-09-16 12:55:29 +000090#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
Mike Frysinger373af432006-06-07 21:37:59 +000091 "%lu %lu " /* utime, stime */
Eric Andersen44608e92002-10-22 12:21:15 +000092#else
Mike Frysinger373af432006-06-07 21:37:59 +000093 "%*s %*s " /* utime, stime */
Eric Andersen44608e92002-10-22 12:21:15 +000094#endif
95 "%*s %*s %*s " /* cutime, cstime, priority */
Mike Frysinger373af432006-06-07 21:37:59 +000096 "%ld " /* nice */
Eric Andersen44608e92002-10-22 12:21:15 +000097 "%*s %*s %*s " /* timeout, it_real_value, start_time */
98 "%*s " /* vsize */
Mike Frysinger373af432006-06-07 21:37:59 +000099 "%ld", /* rss */
Eric Andersen44608e92002-10-22 12:21:15 +0000100 curstatus.state, &curstatus.ppid,
"Vladimir N. Oleynik"f246dc72005-09-16 12:55:29 +0000101#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
Eric Andersen44608e92002-10-22 12:21:15 +0000102 &curstatus.utime, &curstatus.stime,
103#endif
104 &tasknice,
105 &curstatus.rss);
"Vladimir N. Oleynik"f246dc72005-09-16 12:55:29 +0000106#ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
Mike Frysinger58dda842006-07-12 20:04:00 +0000107 if (n != 6)
Eric Andersen44608e92002-10-22 12:21:15 +0000108#else
Mike Frysinger58dda842006-07-12 20:04:00 +0000109 if (n != 4)
Eric Andersen44608e92002-10-22 12:21:15 +0000110#endif
111 continue;
112
113 if (curstatus.rss == 0 && curstatus.state[0] != 'Z')
114 curstatus.state[1] = 'W';
115 else
116 curstatus.state[1] = ' ';
117 if (tasknice < 0)
118 curstatus.state[2] = '<';
119 else if (tasknice > 0)
120 curstatus.state[2] = 'N';
121 else
122 curstatus.state[2] = ' ';
123
Glenn L McGratha1e4a0e2004-01-21 11:36:44 +0000124#ifdef PAGE_SHIFT
Eric Andersen44608e92002-10-22 12:21:15 +0000125 curstatus.rss <<= (PAGE_SHIFT - 10); /* 2**10 = 1kb */
Glenn L McGratha1e4a0e2004-01-21 11:36:44 +0000126#else
127 curstatus.rss *= (getpagesize() >> 10); /* 2**10 = 1kb */
128#endif
Eric Andersen44608e92002-10-22 12:21:15 +0000129
Mike Frysinger58dda842006-07-12 20:04:00 +0000130 if (save_user_arg0) {
Rob Landleyb2804552006-02-13 22:04:27 +0000131 strcpy(status_tail, "/cmdline");
"Vladimir N. Oleynik"465300c2006-02-14 10:17:09 +0000132 n = read_to_buf(status, buf);
Mike Frysinger58dda842006-07-12 20:04:00 +0000133 if (n > 0) {
134 if (buf[n-1]=='\n')
Glenn L McGrath393ad1a2002-11-25 22:31:37 +0000135 buf[--n] = 0;
136 name = buf;
Mike Frysinger58dda842006-07-12 20:04:00 +0000137 while (n) {
138 if (((unsigned char)*name) < ' ')
Glenn L McGrath393ad1a2002-11-25 22:31:37 +0000139 *name = ' ';
140 name++;
141 n--;
142 }
143 *name = 0;
Mike Frysinger58dda842006-07-12 20:04:00 +0000144 if (buf[0])
Eric Andersen44608e92002-10-22 12:21:15 +0000145 curstatus.cmd = strdup(buf);
146 /* if NULL it work true also */
147 }
Eric Andersen44608e92002-10-22 12:21:15 +0000148 }
149 return memcpy(&ret_status, &curstatus, sizeof(procps_status_t));
150 }
151}