blob: 44103fae8992d9b877967e3c577914c2b07d8708 [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>
7 * GNU Library General Public License Version 2, or any later version
8 *
9 */
10
Eric Andersen44608e92002-10-22 12:21:15 +000011#include <dirent.h>
12#include <string.h>
13#include <stdlib.h>
14#include <unistd.h>
15#include <asm/page.h>
16
Glenn L McGrath7b1eca22002-11-24 01:32:56 +000017#include "libbb.h"
Eric Andersen44608e92002-10-22 12:21:15 +000018
Eric Andersen9e480452003-07-03 10:07:04 +000019extern procps_status_t * procps_scan(int save_user_arg0
20#ifdef CONFIG_SELINUX
21 , int use_selinux , security_id_t *sid
22#endif
23 )
Eric Andersen44608e92002-10-22 12:21:15 +000024{
25 static DIR *dir;
26 struct dirent *entry;
27 static procps_status_t ret_status;
28 char *name;
29 int n;
30 char status[32];
31 char buf[1024];
32 FILE *fp;
33 procps_status_t curstatus;
34 int pid;
35 long tasknice;
36 struct stat sb;
37
38 if (!dir) {
39 dir = opendir("/proc");
40 if(!dir)
Manuel Novoa III cad53642003-03-19 09:13:01 +000041 bb_error_msg_and_die("Can't open /proc");
Eric Andersen44608e92002-10-22 12:21:15 +000042 }
43 for(;;) {
44 if((entry = readdir(dir)) == NULL) {
45 closedir(dir);
46 dir = 0;
47 return 0;
48 }
49 name = entry->d_name;
50 if (!(*name >= '0' && *name <= '9'))
51 continue;
52
53 memset(&curstatus, 0, sizeof(procps_status_t));
54 pid = atoi(name);
55 curstatus.pid = pid;
56
57 sprintf(status, "/proc/%d/stat", pid);
58 if((fp = fopen(status, "r")) == NULL)
59 continue;
Eric Andersen9e480452003-07-03 10:07:04 +000060#ifdef CONFIG_SELINUX
61 if(use_selinux)
62 {
63 if(fstat_secure(fileno(fp), &sb, sid))
64 continue;
65 }
66 else
67#endif
Eric Andersen44608e92002-10-22 12:21:15 +000068 if(fstat(fileno(fp), &sb))
69 continue;
70 my_getpwuid(curstatus.user, sb.st_uid);
71 name = fgets(buf, sizeof(buf), fp);
72 fclose(fp);
73 if(name == NULL)
74 continue;
75 name = strrchr(buf, ')'); /* split into "PID (cmd" and "<rest>" */
76 if(name == 0 || name[1] != ' ')
77 continue;
78 *name = 0;
79 sscanf(buf, "%*s (%15c", curstatus.short_cmd);
80 n = sscanf(name+2,
81 "%c %d "
82 "%*s %*s %*s %*s " /* pgrp, session, tty, tpgid */
83 "%*s %*s %*s %*s %*s " /* flags, min_flt, cmin_flt, maj_flt, cmaj_flt */
84#ifdef FEATURE_CPU_USAGE_PERCENTAGE
85 "%lu %lu "
86#else
87 "%*s %*s "
88#endif
89 "%*s %*s %*s " /* cutime, cstime, priority */
90 "%ld "
91 "%*s %*s %*s " /* timeout, it_real_value, start_time */
92 "%*s " /* vsize */
93 "%ld",
94 curstatus.state, &curstatus.ppid,
95#ifdef FEATURE_CPU_USAGE_PERCENTAGE
96 &curstatus.utime, &curstatus.stime,
97#endif
98 &tasknice,
99 &curstatus.rss);
100#ifdef FEATURE_CPU_USAGE_PERCENTAGE
101 if(n != 6)
102#else
103 if(n != 4)
104#endif
105 continue;
106
107 if (curstatus.rss == 0 && curstatus.state[0] != 'Z')
108 curstatus.state[1] = 'W';
109 else
110 curstatus.state[1] = ' ';
111 if (tasknice < 0)
112 curstatus.state[2] = '<';
113 else if (tasknice > 0)
114 curstatus.state[2] = 'N';
115 else
116 curstatus.state[2] = ' ';
117
118 curstatus.rss <<= (PAGE_SHIFT - 10); /* 2**10 = 1kb */
119
Eric Andersen44608e92002-10-22 12:21:15 +0000120 if(save_user_arg0) {
Eric Andersenfab3e122003-05-26 18:07:30 +0000121 sprintf(status, "/proc/%d/cmdline", pid);
Eric Andersen44608e92002-10-22 12:21:15 +0000122 if((fp = fopen(status, "r")) == NULL)
123 continue;
Glenn L McGrath393ad1a2002-11-25 22:31:37 +0000124 if((n=fread(buf, 1, sizeof(buf)-1, fp)) > 0) {
125 if(buf[n-1]=='\n')
126 buf[--n] = 0;
127 name = buf;
128 while(n) {
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000129 if(((unsigned char)*name) < ' ')
Glenn L McGrath393ad1a2002-11-25 22:31:37 +0000130 *name = ' ';
131 name++;
132 n--;
133 }
134 *name = 0;
Eric Andersen44608e92002-10-22 12:21:15 +0000135 if(buf[0])
136 curstatus.cmd = strdup(buf);
137 /* if NULL it work true also */
138 }
139 fclose(fp);
140 }
141 return memcpy(&ret_status, &curstatus, sizeof(procps_status_t));
142 }
143}
144
Eric Andersen44608e92002-10-22 12:21:15 +0000145/* END CODE */
146/*
147Local Variables:
148c-file-style: "linux"
149c-basic-offset: 4
150tab-width: 4
151End:
152*/