be0d61bd3d4cf4e61ca3099bd1edf9125ab284ba
[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  * SELinux support: (c) 2007 by Yuichi Nakamura <ynakam@hitachisoft.jp>
8  * 
9  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
10  */
11
12 #include "libbb.h"
13
14
15 typedef struct unsigned_to_name_map_t {
16         unsigned id;
17         char name[USERNAME_MAX_SIZE];
18 } unsigned_to_name_map_t;
19
20 typedef struct cache_t {
21         unsigned_to_name_map_t *cache;
22         int size;
23 } cache_t;
24
25 static cache_t username, groupname;
26
27 static void clear_cache(cache_t *cp)
28 {
29         free(cp->cache);
30         cp->cache = NULL;
31         cp->size = 0;
32 }
33 void clear_username_cache(void)
34 {
35         clear_cache(&username);
36         clear_cache(&groupname);
37 }
38
39 #if 0 /* more generic, but we don't need that yet */
40 /* Returns -N-1 if not found. */
41 /* cp->cache[N] is allocated and must be filled in this case */
42 static int get_cached(cache_t *cp, unsigned id)
43 {
44         int i;
45         for (i = 0; i < cp->size; i++)
46                 if (cp->cache[i].id == id)
47                         return i;
48         i = cp->size++;
49         cp->cache = xrealloc(cp->cache, cp->size * sizeof(*cp->cache));
50         cp->cache[i++].id = id;
51         return -i;
52 }
53 #endif
54
55 typedef char* ug_func(char *name, long uid, int bufsize);
56 static char* get_cached(cache_t *cp, unsigned id, ug_func* fp)
57 {
58         int i;
59         for (i = 0; i < cp->size; i++)
60                 if (cp->cache[i].id == id)
61                         return cp->cache[i].name;
62         i = cp->size++;
63         cp->cache = xrealloc(cp->cache, cp->size * sizeof(*cp->cache));
64         cp->cache[i].id = id;
65         fp(cp->cache[i].name, id, sizeof(cp->cache[i].name));
66         return cp->cache[i].name;
67 }
68 const char* get_cached_username(uid_t uid)
69 {
70         return get_cached(&username, uid, bb_getpwuid);
71 }
72 const char* get_cached_groupname(gid_t gid)
73 {
74         return get_cached(&groupname, gid, bb_getgrgid);
75 }
76
77
78 #define PROCPS_BUFSIZE 1024
79
80 static int read_to_buf(const char *filename, void *buf)
81 {
82         ssize_t ret;
83         ret = open_read_close(filename, buf, PROCPS_BUFSIZE-1);
84         ((char *)buf)[ret > 0 ? ret : 0] = '\0';
85         return ret;
86 }
87
88 procps_status_t* alloc_procps_scan(int flags)
89 {
90         procps_status_t* sp = xzalloc(sizeof(procps_status_t));
91         sp->dir = xopendir("/proc");
92         return sp;
93 }
94
95 void free_procps_scan(procps_status_t* sp)
96 {
97         closedir(sp->dir);
98         free(sp->cmd);
99         USE_SELINUX(free(sp->context);)
100         free(sp);
101 }
102
103 void BUG_comm_size(void);
104 procps_status_t* procps_scan(procps_status_t* sp, int flags)
105 {
106         struct dirent *entry;
107         char buf[PROCPS_BUFSIZE];
108         char filename[sizeof("/proc//cmdline") + sizeof(int)*3];
109         char *filename_tail;
110         long tasknice;
111         unsigned pid;
112         int n;
113         struct stat sb;
114
115         if (!sp)
116                 sp = alloc_procps_scan(flags);
117
118         for (;;) {
119                 entry = readdir(sp->dir);
120                 if (entry == NULL) {
121                         free_procps_scan(sp);
122                         return NULL;
123                 }
124                 pid = bb_strtou(entry->d_name, NULL, 10);
125                 if (errno)
126                         continue;
127
128                 /* After this point we have to break, not continue
129                  * ("continue" would mean that current /proc/NNN
130                  * is not a valid process info) */
131
132                 memset(&sp->vsz, 0, sizeof(*sp) - offsetof(procps_status_t, vsz));
133
134                 sp->pid = pid;
135                 if (!(flags & ~PSSCAN_PID)) break;
136
137 #if ENABLE_SELINUX
138                 if (flags & PSSCAN_CONTEXT) {
139                         if (getpidcon(sp->pid, &sp->context) < 0)
140                                 sp->context = NULL;
141                 }       
142 #endif  
143
144                 filename_tail = filename + sprintf(filename, "/proc/%d", pid);
145
146                 if (flags & PSSCAN_UIDGID) {
147                         if (stat(filename, &sb))
148                                 break;
149                         /* Need comment - is this effective or real UID/GID? */
150                         sp->uid = sb.st_uid;
151                         sp->gid = sb.st_gid;
152                 }
153
154                 if (flags & PSSCAN_STAT) {
155                         char *cp;
156                         unsigned long vsz, rss;
157                         int tty;
158
159                         /* see proc(5) for some details on this */
160                         strcpy(filename_tail, "/stat");
161                         n = read_to_buf(filename, buf);
162                         if (n < 0)
163                                 break;
164                         cp = strrchr(buf, ')'); /* split into "PID (cmd" and "<rest>" */
165                         if (!cp || cp[1] != ' ')
166                                 break;
167                         cp[0] = '\0';
168                         if (sizeof(sp->comm) < 16)
169                                 BUG_comm_size();
170                         sscanf(buf, "%*s (%15c", sp->comm);
171                         n = sscanf(cp+2,
172                                 "%c %u "               /* state, ppid */
173                                 "%u %u %d %*s "        /* pgid, sid, tty, tpgid */
174                                 "%*s %*s %*s %*s %*s " /* flags, min_flt, cmin_flt, maj_flt, cmaj_flt */
175                                 "%lu %lu "             /* utime, stime */
176                                 "%*s %*s %*s "         /* cutime, cstime, priority */
177                                 "%ld "                 /* nice */
178                                 "%*s %*s %*s "         /* timeout, it_real_value, start_time */
179                                 "%lu "                 /* vsize */
180                                 "%lu "                 /* rss */
181                         /*      "%lu %lu %lu %lu %lu %lu " rss_rlim, start_code, end_code, start_stack, kstk_esp, kstk_eip */
182                         /*      "%u %u %u %u "         signal, blocked, sigignore, sigcatch */
183                         /*      "%lu %lu %lu"          wchan, nswap, cnswap */
184                                 ,
185                                 sp->state, &sp->ppid,
186                                 &sp->pgid, &sp->sid, &tty,
187                                 &sp->utime, &sp->stime,
188                                 &tasknice,
189                                 &vsz,
190                                 &rss);
191                         if (n != 10)
192                                 break;
193
194                         sp->tty_str[0] = '?';
195                         /* sp->tty_str[1] = '\0'; - done by memset */
196                         if (tty) /* tty field of "0" means "no tty" */
197                                 snprintf(sp->tty_str, sizeof(sp->tty_str), "%u,%u",
198                                         (tty >> 8) & 0xfff, /* major */
199                                         (tty & 0xff) | ((tty >> 12) & 0xfff00));
200                         if (sp->vsz == 0 && sp->state[0] != 'Z')
201                                 sp->state[1] = 'W';
202                         else
203                                 sp->state[1] = ' ';
204                         if (tasknice < 0)
205                                 sp->state[2] = '<';
206                         else if (tasknice) /* > 0 */
207                                 sp->state[2] = 'N';
208                         else
209                                 sp->state[2] = ' ';
210
211                         sp->vsz = vsz >> 10; /* vsize is in bytes and we want kb */
212                         sp->rss = rss >> 10;
213                 }
214
215                 if (flags & PSSCAN_CMD) {
216                         free(sp->cmd);
217                         sp->cmd = NULL;
218                         strcpy(filename_tail, "/cmdline");
219                         n = read_to_buf(filename, buf);
220                         if (n <= 0)
221                                 break;
222                         if (buf[n-1] == '\n') {
223                                 if (!--n)
224                                         break;
225                                 buf[n] = '\0';
226                         }
227                         do {
228                                 n--;
229                                 if ((unsigned char)(buf[n]) < ' ')
230                                         buf[n] = ' ';
231                         } while (n);
232                         sp->cmd = xstrdup(buf);
233                 }
234                 break;
235         }
236         return sp;
237 }
238 /* from kernel:
239         //             pid comm S ppid pgid sid tty_nr tty_pgrp flg
240         sprintf(buffer,"%d (%s) %c %d  %d   %d  %d     %d       %lu %lu \
241 %lu %lu %lu %lu %lu %ld %ld %ld %ld %d 0 %llu %lu %ld %lu %lu %lu %lu %lu \
242 %lu %lu %lu %lu %lu %lu %lu %lu %d %d %lu %lu %llu\n",
243                 task->pid,
244                 tcomm,
245                 state,
246                 ppid,
247                 pgid,
248                 sid,
249                 tty_nr,
250                 tty_pgrp,
251                 task->flags,
252                 min_flt,
253                 cmin_flt,
254                 maj_flt,
255                 cmaj_flt,
256                 cputime_to_clock_t(utime),
257                 cputime_to_clock_t(stime),
258                 cputime_to_clock_t(cutime),
259                 cputime_to_clock_t(cstime),
260                 priority,
261                 nice,
262                 num_threads,
263                 // 0,
264                 start_time,
265                 vsize,
266                 mm ? get_mm_rss(mm) : 0,
267                 rsslim,
268                 mm ? mm->start_code : 0,
269                 mm ? mm->end_code : 0,
270                 mm ? mm->start_stack : 0,
271                 esp,
272                 eip,
273 the rest is some obsolete cruft
274 */