blob: c9dcfde0c0f4327248efd412aa72e859af136f19 [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
Glenn L McGrath7b1eca22002-11-24 01:32:56 +000011#include "libbb.h"
Eric Andersen44608e92002-10-22 12:21:15 +000012
Rob Landleyb2804552006-02-13 22:04:27 +000013
Denis Vlasenkoc50f3702006-11-24 14:57:31 +000014typedef struct unsigned_to_name_map_t {
15 unsigned id;
Denis Vlasenko2405ad62007-01-19 21:24:17 +000016 char name[USERNAME_MAX_SIZE];
Denis Vlasenkoc50f3702006-11-24 14:57:31 +000017} unsigned_to_name_map_t;
Denis Vlasenko459e4d62006-11-05 00:43:51 +000018
Denis Vlasenkoc50f3702006-11-24 14:57:31 +000019typedef struct cache_t {
20 unsigned_to_name_map_t *cache;
21 int size;
22} cache_t;
Denis Vlasenko459e4d62006-11-05 00:43:51 +000023
Denis Vlasenkoc50f3702006-11-24 14:57:31 +000024static cache_t username, groupname;
25
26static void clear_cache(cache_t *cp)
27{
28 free(cp->cache);
29 cp->cache = NULL;
30 cp->size = 0;
31}
Denis Vlasenko459e4d62006-11-05 00:43:51 +000032void clear_username_cache(void)
33{
Denis Vlasenkoc50f3702006-11-24 14:57:31 +000034 clear_cache(&username);
35 clear_cache(&groupname);
Denis Vlasenko459e4d62006-11-05 00:43:51 +000036}
37
Denis Vlasenkob8a8e602006-11-24 14:59:45 +000038#if 0 /* more generic, but we don't need that yet */
Denis Vlasenkoc50f3702006-11-24 14:57:31 +000039/* Returns -N-1 if not found. */
40/* cp->cache[N] is allocated and must be filled in this case */
41static int get_cached(cache_t *cp, unsigned id)
Denis Vlasenko459e4d62006-11-05 00:43:51 +000042{
43 int i;
Denis Vlasenkoc50f3702006-11-24 14:57:31 +000044 for (i = 0; i < cp->size; i++)
45 if (cp->cache[i].id == id)
46 return i;
47 i = cp->size++;
48 cp->cache = xrealloc(cp->cache, cp->size * sizeof(*cp->cache));
49 cp->cache[i++].id = id;
50 return -i;
51}
Denis Vlasenkob8a8e602006-11-24 14:59:45 +000052#endif
53
54typedef char* ug_func(char *name, long uid, int bufsize);
55static char* get_cached(cache_t *cp, unsigned id, ug_func* fp)
56{
57 int i;
58 for (i = 0; i < cp->size; i++)
59 if (cp->cache[i].id == id)
60 return cp->cache[i].name;
61 i = cp->size++;
62 cp->cache = xrealloc(cp->cache, cp->size * sizeof(*cp->cache));
63 cp->cache[i].id = id;
64 fp(cp->cache[i].name, id, sizeof(cp->cache[i].name));
65 return cp->cache[i].name;
66}
Denis Vlasenkoc50f3702006-11-24 14:57:31 +000067const char* get_cached_username(uid_t uid)
68{
Denis Vlasenkob8a8e602006-11-24 14:59:45 +000069 return get_cached(&username, uid, bb_getpwuid);
Denis Vlasenkoc50f3702006-11-24 14:57:31 +000070}
Denis Vlasenkob8a8e602006-11-24 14:59:45 +000071const char* get_cached_groupname(gid_t gid)
Denis Vlasenkoc50f3702006-11-24 14:57:31 +000072{
Denis Vlasenkob8a8e602006-11-24 14:59:45 +000073 return get_cached(&groupname, gid, bb_getgrgid);
Denis Vlasenko459e4d62006-11-05 00:43:51 +000074}
75
76
"Vladimir N. Oleynik"465300c2006-02-14 10:17:09 +000077#define PROCPS_BUFSIZE 1024
78
79static int read_to_buf(const char *filename, void *buf)
Rob Landleyb2804552006-02-13 22:04:27 +000080{
"Vladimir N. Oleynik"465300c2006-02-14 10:17:09 +000081 ssize_t ret;
Denis Vlasenkoea620772006-10-14 02:23:43 +000082 ret = open_read_close(filename, buf, PROCPS_BUFSIZE-1);
83 ((char *)buf)[ret > 0 ? ret : 0] = '\0';
"Vladimir N. Oleynik"465300c2006-02-14 10:17:09 +000084 return ret;
Rob Landleyb2804552006-02-13 22:04:27 +000085}
86
Denis Vlasenko459e4d62006-11-05 00:43:51 +000087procps_status_t* alloc_procps_scan(int flags)
Eric Andersen44608e92002-10-22 12:21:15 +000088{
Denis Vlasenko459e4d62006-11-05 00:43:51 +000089 procps_status_t* sp = xzalloc(sizeof(procps_status_t));
90 sp->dir = xopendir("/proc");
91 return sp;
92}
Denis Vlasenko35fb5122006-11-01 09:16:49 +000093
Denis Vlasenko459e4d62006-11-05 00:43:51 +000094void free_procps_scan(procps_status_t* sp)
95{
96 closedir(sp->dir);
97 free(sp->cmd);
98 free(sp);
99}
100
101void BUG_comm_size(void);
102procps_status_t* procps_scan(procps_status_t* sp, int flags)
103{
Denis Vlasenko35fb5122006-11-01 09:16:49 +0000104 struct dirent *entry;
"Vladimir N. Oleynik"465300c2006-02-14 10:17:09 +0000105 char buf[PROCPS_BUFSIZE];
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000106 char filename[sizeof("/proc//cmdline") + sizeof(int)*3];
107 char *filename_tail;
Eric Andersen44608e92002-10-22 12:21:15 +0000108 long tasknice;
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000109 unsigned pid;
Denis Vlasenkodd539f72006-11-01 20:20:37 +0000110 int n;
Eric Andersen44608e92002-10-22 12:21:15 +0000111 struct stat sb;
112
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000113 if (!sp)
114 sp = alloc_procps_scan(flags);
115
Mike Frysinger58dda842006-07-12 20:04:00 +0000116 for (;;) {
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000117 entry = readdir(sp->dir);
Denis Vlasenkoa6dbb082006-10-12 19:29:44 +0000118 if (entry == NULL) {
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000119 free_procps_scan(sp);
120 return NULL;
Eric Andersen44608e92002-10-22 12:21:15 +0000121 }
Denis Vlasenkod686a042006-11-27 14:43:21 +0000122 pid = bb_strtou(entry->d_name, NULL, 10);
123 if (errno)
Eric Andersen44608e92002-10-22 12:21:15 +0000124 continue;
125
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000126 /* After this point we have to break, not continue
127 * ("continue" would mean that current /proc/NNN
128 * is not a valid process info) */
Eric Andersen44608e92002-10-22 12:21:15 +0000129
Mike Frysinger0aa6ba52007-02-08 08:21:58 +0000130 memset(&sp->vsz, 0, sizeof(*sp) - offsetof(procps_status_t, vsz));
Eric Andersen242ab832004-01-27 20:17:39 +0000131
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000132 sp->pid = pid;
133 if (!(flags & ~PSSCAN_PID)) break;
Eric Andersen44608e92002-10-22 12:21:15 +0000134
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000135 filename_tail = filename + sprintf(filename, "/proc/%d", pid);
136
137 if (flags & PSSCAN_UIDGID) {
138 if (stat(filename, &sb))
139 break;
Denis Vlasenko2405ad62007-01-19 21:24:17 +0000140 /* Need comment - is this effective or real UID/GID? */
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000141 sp->uid = sb.st_uid;
142 sp->gid = sb.st_gid;
143 }
Denis Vlasenkof7996f32007-01-11 17:20:00 +0000144
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000145 if (flags & PSSCAN_STAT) {
146 char *cp;
147 /* see proc(5) for some details on this */
148 strcpy(filename_tail, "/stat");
149 n = read_to_buf(filename, buf);
150 if (n < 0)
151 break;
152 cp = strrchr(buf, ')'); /* split into "PID (cmd" and "<rest>" */
153 if (!cp || cp[1] != ' ')
154 break;
155 cp[0] = '\0';
156 if (sizeof(sp->comm) < 16)
157 BUG_comm_size();
158 sscanf(buf, "%*s (%15c", sp->comm);
159 n = sscanf(cp+2,
160 "%c %u " /* state, ppid */
161 "%u %u %*s %*s " /* pgid, sid, tty, tpgid */
162 "%*s %*s %*s %*s %*s " /* flags, min_flt, cmin_flt, maj_flt, cmaj_flt */
163 "%lu %lu " /* utime, stime */
164 "%*s %*s %*s " /* cutime, cstime, priority */
165 "%ld " /* nice */
166 "%*s %*s %*s " /* timeout, it_real_value, start_time */
Mike Frysinger0aa6ba52007-02-08 08:21:58 +0000167 "%lu ", /* vsize */
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000168 sp->state, &sp->ppid,
169 &sp->pgid, &sp->sid,
170 &sp->utime, &sp->stime,
171 &tasknice,
Mike Frysinger0aa6ba52007-02-08 08:21:58 +0000172 &sp->vsz);
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000173 if (n != 8)
174 break;
175
Mike Frysinger0aa6ba52007-02-08 08:21:58 +0000176 if (sp->vsz == 0 && sp->state[0] != 'Z')
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000177 sp->state[1] = 'W';
178 else
179 sp->state[1] = ' ';
180 if (tasknice < 0)
181 sp->state[2] = '<';
182 else if (tasknice > 0)
183 sp->state[2] = 'N';
184 else
185 sp->state[2] = ' ';
Eric Andersen44608e92002-10-22 12:21:15 +0000186
Mike Frysinger0aa6ba52007-02-08 08:21:58 +0000187 sp->vsz >>= 10; /* vsize is in bytes and we want kb */
Eric Andersen44608e92002-10-22 12:21:15 +0000188 }
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000189
190 if (flags & PSSCAN_CMD) {
191 free(sp->cmd);
192 sp->cmd = NULL;
193 strcpy(filename_tail, "/cmdline");
194 n = read_to_buf(filename, buf);
195 if (n <= 0)
196 break;
197 if (buf[n-1] == '\n') {
198 if (!--n)
199 break;
200 buf[n] = '\0';
201 }
202 do {
203 n--;
204 if ((unsigned char)(buf[n]) < ' ')
205 buf[n] = ' ';
206 } while (n);
Denis Vlasenko2405ad62007-01-19 21:24:17 +0000207 sp->cmd = xstrdup(buf);
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000208 }
209 break;
Eric Andersen44608e92002-10-22 12:21:15 +0000210 }
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000211 return sp;
Eric Andersen44608e92002-10-22 12:21:15 +0000212}
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000213/* from kernel:
214 // pid comm S ppid pgid sid tty_nr tty_pgrp flg
Denis Vlasenkod031ffa2006-11-24 21:54:44 +0000215 sprintf(buffer,"%d (%s) %c %d %d %d %d %d %lu %lu \
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000216%lu %lu %lu %lu %lu %ld %ld %ld %ld %d 0 %llu %lu %ld %lu %lu %lu %lu %lu \
217%lu %lu %lu %lu %lu %lu %lu %lu %d %d %lu %lu %llu\n",
Denis Vlasenkod031ffa2006-11-24 21:54:44 +0000218 task->pid,
219 tcomm,
220 state,
221 ppid,
222 pgid,
223 sid,
224 tty_nr,
225 tty_pgrp,
226 task->flags,
227 min_flt,
Denis Vlasenkod031ffa2006-11-24 21:54:44 +0000228 cmin_flt,
229 maj_flt,
230 cmaj_flt,
231 cputime_to_clock_t(utime),
232 cputime_to_clock_t(stime),
233 cputime_to_clock_t(cutime),
234 cputime_to_clock_t(cstime),
235 priority,
236 nice,
237 num_threads,
238 // 0,
239 start_time,
240 vsize,
241 mm ? get_mm_rss(mm) : 0,
242 rsslim,
243 mm ? mm->start_code : 0,
244 mm ? mm->end_code : 0,
245 mm ? mm->start_stack : 0,
246 esp,
247 eip,
Denis Vlasenko459e4d62006-11-05 00:43:51 +0000248the rest is some obsolete cruft
249*/