blob: 46e982766d3374b36e17868477caa32a8b9dfe16 [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
Eric Andersen242ab832004-01-27 20:17:39 +000057 sprintf(status, "/proc/%d", pid);
58 if(stat(status, &sb))
59 continue;
60 my_getpwuid(curstatus.user, sb.st_uid);
61
Eric Andersen44608e92002-10-22 12:21:15 +000062 sprintf(status, "/proc/%d/stat", pid);
63 if((fp = fopen(status, "r")) == NULL)
64 continue;
Eric Andersen9e480452003-07-03 10:07:04 +000065#ifdef CONFIG_SELINUX
66 if(use_selinux)
67 {
68 if(fstat_secure(fileno(fp), &sb, sid))
69 continue;
70 }
71 else
72#endif
Eric Andersen44608e92002-10-22 12:21:15 +000073 name = fgets(buf, sizeof(buf), fp);
74 fclose(fp);
75 if(name == NULL)
76 continue;
77 name = strrchr(buf, ')'); /* split into "PID (cmd" and "<rest>" */
78 if(name == 0 || name[1] != ' ')
79 continue;
80 *name = 0;
81 sscanf(buf, "%*s (%15c", curstatus.short_cmd);
82 n = sscanf(name+2,
83 "%c %d "
84 "%*s %*s %*s %*s " /* pgrp, session, tty, tpgid */
85 "%*s %*s %*s %*s %*s " /* flags, min_flt, cmin_flt, maj_flt, cmaj_flt */
86#ifdef FEATURE_CPU_USAGE_PERCENTAGE
87 "%lu %lu "
88#else
89 "%*s %*s "
90#endif
91 "%*s %*s %*s " /* cutime, cstime, priority */
92 "%ld "
93 "%*s %*s %*s " /* timeout, it_real_value, start_time */
94 "%*s " /* vsize */
95 "%ld",
96 curstatus.state, &curstatus.ppid,
97#ifdef FEATURE_CPU_USAGE_PERCENTAGE
98 &curstatus.utime, &curstatus.stime,
99#endif
100 &tasknice,
101 &curstatus.rss);
102#ifdef FEATURE_CPU_USAGE_PERCENTAGE
103 if(n != 6)
104#else
105 if(n != 4)
106#endif
107 continue;
108
109 if (curstatus.rss == 0 && curstatus.state[0] != 'Z')
110 curstatus.state[1] = 'W';
111 else
112 curstatus.state[1] = ' ';
113 if (tasknice < 0)
114 curstatus.state[2] = '<';
115 else if (tasknice > 0)
116 curstatus.state[2] = 'N';
117 else
118 curstatus.state[2] = ' ';
119
Glenn L McGratha1e4a0e2004-01-21 11:36:44 +0000120#ifdef PAGE_SHIFT
Eric Andersen44608e92002-10-22 12:21:15 +0000121 curstatus.rss <<= (PAGE_SHIFT - 10); /* 2**10 = 1kb */
Glenn L McGratha1e4a0e2004-01-21 11:36:44 +0000122#else
123 curstatus.rss *= (getpagesize() >> 10); /* 2**10 = 1kb */
124#endif
Eric Andersen44608e92002-10-22 12:21:15 +0000125
Eric Andersen44608e92002-10-22 12:21:15 +0000126 if(save_user_arg0) {
Eric Andersenfab3e122003-05-26 18:07:30 +0000127 sprintf(status, "/proc/%d/cmdline", pid);
Eric Andersen44608e92002-10-22 12:21:15 +0000128 if((fp = fopen(status, "r")) == NULL)
129 continue;
Glenn L McGrath393ad1a2002-11-25 22:31:37 +0000130 if((n=fread(buf, 1, sizeof(buf)-1, fp)) > 0) {
131 if(buf[n-1]=='\n')
132 buf[--n] = 0;
133 name = buf;
134 while(n) {
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000135 if(((unsigned char)*name) < ' ')
Glenn L McGrath393ad1a2002-11-25 22:31:37 +0000136 *name = ' ';
137 name++;
138 n--;
139 }
140 *name = 0;
Eric Andersen44608e92002-10-22 12:21:15 +0000141 if(buf[0])
142 curstatus.cmd = strdup(buf);
143 /* if NULL it work true also */
144 }
145 fclose(fp);
146 }
147 return memcpy(&ret_status, &curstatus, sizeof(procps_status_t));
148 }
149}
150
Eric Andersen44608e92002-10-22 12:21:15 +0000151/* END CODE */
152/*
153Local Variables:
154c-file-style: "linux"
155c-basic-offset: 4
156tab-width: 4
157End:
158*/