source: MondoRescue/branches/2.2.2/mindi-busybox/libbb/procps.c@ 1247

Last change on this file since 1247 was 821, checked in by Bruno Cornec, 18 years ago

Addition of busybox 1.2.1 as a mindi-busybox new package
This should avoid delivering binary files in mindi not built there (Fedora and Debian are quite serious about that)

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