eba90705cc8b7bfbb4448caed79273f28f2f4ba1
[oweals/busybox.git] / libbb / procps.c
1 /* 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>
7  *
8  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
9  */
10
11 #include <dirent.h>
12 #include <string.h>
13 #include <stdlib.h>
14 #include <sys/param.h>
15 #include <unistd.h>
16 #include <fcntl.h>
17
18 #include "libbb.h"
19
20
21 #define PROCPS_BUFSIZE 1024
22
23 static int read_to_buf(const char *filename, void *buf)
24 {
25         ssize_t ret;
26         ret = open_read_close(filename, buf, PROCPS_BUFSIZE-1);
27         ((char *)buf)[ret > 0 ? ret : 0] = '\0';
28         return ret;
29 }
30
31
32 procps_status_t * procps_scan(int save_user_arg0)
33 {
34         static DIR *dir;
35         struct dirent *entry;
36         static procps_status_t ret_status;
37         char *name;
38         int n;
39         char status[32];
40         char *status_tail;
41         char buf[PROCPS_BUFSIZE];
42         procps_status_t curstatus;
43         int pid;
44         long tasknice;
45         struct stat sb;
46
47         if (!dir) {
48                 dir = xopendir("/proc");
49         }
50         for (;;) {
51                 entry = readdir(dir);
52                 if (entry == NULL) {
53                         closedir(dir);
54                         dir = 0;
55                         return 0;
56                 }
57                 name = entry->d_name;
58                 if (!(*name >= '0' && *name <= '9'))
59                         continue;
60
61                 memset(&curstatus, 0, sizeof(procps_status_t));
62                 pid = atoi(name);
63                 curstatus.pid = pid;
64
65                 status_tail = status + sprintf(status, "/proc/%d", pid);
66                 if (stat(status, &sb))
67                         continue;
68                 bb_getpwuid(curstatus.user, sb.st_uid, sizeof(curstatus.user));
69
70                 /* see proc(5) for some details on this */
71                 strcpy(status_tail, "/stat");
72                 n = read_to_buf(status, buf);
73                 if (n < 0)
74                         continue;
75                 name = strrchr(buf, ')'); /* split into "PID (cmd" and "<rest>" */
76                 if (name == 0 || name[1] != ' ')
77                         continue;
78                 *name = 0;
79                 sscanf(buf, "%*s (%15c", curstatus.short_cmd);
80                 n = sscanf(name+2,
81                 "%c %d "
82                 "%*s %*s %*s %*s "     /* pgrp, session, tty, tpgid */
83                 "%*s %*s %*s %*s %*s " /* flags, min_flt, cmin_flt, maj_flt, cmaj_flt */
84 #ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
85                 "%lu %lu "             /* utime, stime */
86 #else
87                 "%*s %*s "             /* utime, stime */
88 #endif
89                 "%*s %*s %*s "         /* cutime, cstime, priority */
90                 "%ld "                 /* nice */
91                 "%*s %*s %*s "         /* timeout, it_real_value, start_time */
92                 "%*s "                 /* vsize */
93                 "%ld",                 /* rss */
94                 curstatus.state, &curstatus.ppid,
95 #ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
96                 &curstatus.utime, &curstatus.stime,
97 #endif
98                 &tasknice,
99                 &curstatus.rss);
100 #ifdef CONFIG_FEATURE_TOP_CPU_USAGE_PERCENTAGE
101                 if (n != 6)
102 #else
103                 if (n != 4)
104 #endif
105                         continue;
106
107                 if (curstatus.rss == 0 && curstatus.state[0] != 'Z')
108                         curstatus.state[1] = 'W';
109                 else
110                         curstatus.state[1] = ' ';
111                 if (tasknice < 0)
112                         curstatus.state[2] = '<';
113                 else if (tasknice > 0)
114                         curstatus.state[2] = 'N';
115                 else
116                         curstatus.state[2] = ' ';
117
118 #ifdef PAGE_SHIFT
119                 curstatus.rss <<= (PAGE_SHIFT - 10);     /* 2**10 = 1kb */
120 #else
121                 curstatus.rss *= (getpagesize() >> 10);     /* 2**10 = 1kb */
122 #endif
123
124                 if (save_user_arg0) {
125                         strcpy(status_tail, "/cmdline");
126                         n = read_to_buf(status, buf);
127                         if (n > 0) {
128                                 if (buf[n-1]=='\n')
129                                         buf[--n] = 0;
130                                 name = buf;
131                                 while (n) {
132                                         if (((unsigned char)*name) < ' ')
133                                                 *name = ' ';
134                                         name++;
135                                         n--;
136                                 }
137                                 *name = 0;
138                                 if (buf[0])
139                                         curstatus.cmd = strdup(buf);
140                                 /* if NULL it work true also */
141                         }
142                 }
143                 return memcpy(&ret_status, &curstatus, sizeof(procps_status_t));
144         }
145 }