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