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