diff options
author | andersen <andersen@69ca8d6d-28ef-0310-b511-8ec308f3f277> | 2002-10-22 12:21:15 +0000 |
---|---|---|
committer | andersen <andersen@69ca8d6d-28ef-0310-b511-8ec308f3f277> | 2002-10-22 12:21:15 +0000 |
commit | 0bc1512abd51128e2bf1692bb91915f6fea19384 (patch) | |
tree | 4555230653cdb82d998f076b29130d8fe18a6f7a /libbb | |
parent | fd7f4ba70b04383b2b7b260419b5bf3c1246e109 (diff) | |
download | busybox-w32-0bc1512abd51128e2bf1692bb91915f6fea19384.tar.gz busybox-w32-0bc1512abd51128e2bf1692bb91915f6fea19384.tar.bz2 busybox-w32-0bc1512abd51128e2bf1692bb91915f6fea19384.zip |
Patch last_pach62 from vodz. This patch moves all the /proc parsing
code into libbb so it can be shared by ps, top, etc, saving over 1.5k.
git-svn-id: svn://busybox.net/trunk/busybox@5685 69ca8d6d-28ef-0310-b511-8ec308f3f277
Diffstat (limited to 'libbb')
-rw-r--r-- | libbb/Makefile.in | 3 | ||||
-rw-r--r-- | libbb/find_pid_by_name.c | 54 | ||||
-rw-r--r-- | libbb/procps.c | 137 |
3 files changed, 149 insertions, 45 deletions
diff --git a/libbb/Makefile.in b/libbb/Makefile.in index f4ef0b868..6703cf1f7 100644 --- a/libbb/Makefile.in +++ b/libbb/Makefile.in | |||
@@ -43,7 +43,8 @@ LIBBB_SRC:= \ | |||
43 | arith.c simplify_path.c inet_common.c inode_hash.c obscure.c \ | 43 | arith.c simplify_path.c inet_common.c inode_hash.c obscure.c \ |
44 | pwd2spwd.c xfuncs.c correct_password.c change_identity.c \ | 44 | pwd2spwd.c xfuncs.c correct_password.c change_identity.c \ |
45 | setup_environment.c run_shell.c pw_encrypt.c restricted_shell.c \ | 45 | setup_environment.c run_shell.c pw_encrypt.c restricted_shell.c \ |
46 | xgethostbyname2.c create_icmp6_socket.c xconnect.c bb_asprintf.c | 46 | xgethostbyname2.c create_icmp6_socket.c xconnect.c bb_asprintf.c \ |
47 | procps.c | ||
47 | 48 | ||
48 | LIBBB_OBJS=$(patsubst %.c,$(LIBBB_DIR)%.o, $(LIBBB_SRC)) | 49 | LIBBB_OBJS=$(patsubst %.c,$(LIBBB_DIR)%.o, $(LIBBB_SRC)) |
49 | 50 | ||
diff --git a/libbb/find_pid_by_name.c b/libbb/find_pid_by_name.c index 4eaee03e3..a648137e1 100644 --- a/libbb/find_pid_by_name.c +++ b/libbb/find_pid_by_name.c | |||
@@ -22,7 +22,6 @@ | |||
22 | #include <stdio.h> | 22 | #include <stdio.h> |
23 | #include <ctype.h> | 23 | #include <ctype.h> |
24 | #include <string.h> | 24 | #include <string.h> |
25 | #include <dirent.h> | ||
26 | #include <stdlib.h> | 25 | #include <stdlib.h> |
27 | #include "libbb.h" | 26 | #include "libbb.h" |
28 | 27 | ||
@@ -40,7 +39,7 @@ | |||
40 | * | 39 | * |
41 | * Returns a list of all matching PIDs | 40 | * Returns a list of all matching PIDs |
42 | */ | 41 | */ |
43 | extern long* find_pid_by_name( char* pidName) | 42 | extern long* find_pid_by_name( const char* pidName) |
44 | { | 43 | { |
45 | int fd, i, j; | 44 | int fd, i, j; |
46 | char device[] = "/dev/ps"; | 45 | char device[] = "/dev/ps"; |
@@ -112,61 +111,28 @@ extern long* find_pid_by_name( char* pidName) | |||
112 | 111 | ||
113 | /* find_pid_by_name() | 112 | /* find_pid_by_name() |
114 | * | 113 | * |
114 | * Modified by Vladimir Oleynik for use with libbb/procps.c | ||
115 | * This finds the pid of the specified process. | 115 | * This finds the pid of the specified process. |
116 | * Currently, it's implemented by rummaging through | 116 | * Currently, it's implemented by rummaging through |
117 | * the proc filesystem. | 117 | * the proc filesystem. |
118 | * | 118 | * |
119 | * Returns a list of all matching PIDs | 119 | * Returns a list of all matching PIDs |
120 | */ | 120 | */ |
121 | extern long* find_pid_by_name( char* pidName) | 121 | extern long* find_pid_by_name( const char* pidName) |
122 | { | 122 | { |
123 | DIR *dir; | 123 | long* pidList; |
124 | struct dirent *next; | ||
125 | long* pidList=NULL; | ||
126 | int i=0; | 124 | int i=0; |
125 | procps_status_t * p; | ||
127 | 126 | ||
128 | dir = opendir("/proc"); | 127 | pidList = xmalloc(sizeof(long)); |
129 | if (!dir) | 128 | while ((p = procps_scan(0)) != 0) { |
130 | perror_msg_and_die("Cannot open /proc"); | 129 | if (strcmp(p->short_cmd, pidName) == 0) { |
131 | |||
132 | while ((next = readdir(dir)) != NULL) { | ||
133 | FILE *status; | ||
134 | char filename[READ_BUF_SIZE]; | ||
135 | char buffer[READ_BUF_SIZE]; | ||
136 | char name[READ_BUF_SIZE]; | ||
137 | |||
138 | /* Must skip ".." since that is outside /proc */ | ||
139 | if (strcmp(next->d_name, "..") == 0) | ||
140 | continue; | ||
141 | |||
142 | /* If it isn't a number, we don't want it */ | ||
143 | if (!isdigit(*next->d_name)) | ||
144 | continue; | ||
145 | |||
146 | sprintf(filename, "/proc/%s/status", next->d_name); | ||
147 | if (! (status = fopen(filename, "r")) ) { | ||
148 | continue; | ||
149 | } | ||
150 | if (fgets(buffer, READ_BUF_SIZE-1, status) == NULL) { | ||
151 | fclose(status); | ||
152 | continue; | ||
153 | } | ||
154 | fclose(status); | ||
155 | |||
156 | /* Buffer should contain a string like "Name: binary_name" */ | ||
157 | sscanf(buffer, "%*s %s", name); | ||
158 | if (strcmp(name, pidName) == 0) { | ||
159 | pidList=xrealloc( pidList, sizeof(long) * (i+2)); | 130 | pidList=xrealloc( pidList, sizeof(long) * (i+2)); |
160 | pidList[i++]=strtol(next->d_name, NULL, 0); | 131 | pidList[i++]=p->pid; |
161 | } | 132 | } |
162 | } | 133 | } |
163 | 134 | ||
164 | if (pidList) { | 135 | pidList[i] = i==0 ? -1 : 0; |
165 | pidList[i]=0; | ||
166 | } else { | ||
167 | pidList=xrealloc( pidList, sizeof(long)); | ||
168 | pidList[0]=-1; | ||
169 | } | ||
170 | return pidList; | 136 | return pidList; |
171 | } | 137 | } |
172 | #endif /* CONFIG_FEATURE_USE_DEVPS_PATCH */ | 138 | #endif /* CONFIG_FEATURE_USE_DEVPS_PATCH */ |
diff --git a/libbb/procps.c b/libbb/procps.c new file mode 100644 index 000000000..9ac5be092 --- /dev/null +++ b/libbb/procps.c | |||
@@ -0,0 +1,137 @@ | |||
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 | * GNU Library General Public License Version 2, or any later version | ||
8 | * | ||
9 | */ | ||
10 | |||
11 | #include "libbb.h" | ||
12 | |||
13 | #if ! defined CONFIG_FEATURE_USE_DEVPS_PATCH | ||
14 | #include <dirent.h> | ||
15 | #include <string.h> | ||
16 | #include <stdlib.h> | ||
17 | #include <unistd.h> | ||
18 | #include <asm/page.h> | ||
19 | |||
20 | |||
21 | extern procps_status_t * procps_scan(int save_user_arg0) | ||
22 | { | ||
23 | static DIR *dir; | ||
24 | struct dirent *entry; | ||
25 | static procps_status_t ret_status; | ||
26 | char *name; | ||
27 | int n; | ||
28 | char status[32]; | ||
29 | char buf[1024]; | ||
30 | FILE *fp; | ||
31 | procps_status_t curstatus; | ||
32 | int pid; | ||
33 | long tasknice; | ||
34 | struct stat sb; | ||
35 | |||
36 | if (!dir) { | ||
37 | dir = opendir("/proc"); | ||
38 | if(!dir) | ||
39 | error_msg_and_die("Can't open /proc"); | ||
40 | } | ||
41 | for(;;) { | ||
42 | if((entry = readdir(dir)) == NULL) { | ||
43 | closedir(dir); | ||
44 | dir = 0; | ||
45 | return 0; | ||
46 | } | ||
47 | name = entry->d_name; | ||
48 | if (!(*name >= '0' && *name <= '9')) | ||
49 | continue; | ||
50 | |||
51 | memset(&curstatus, 0, sizeof(procps_status_t)); | ||
52 | pid = atoi(name); | ||
53 | curstatus.pid = pid; | ||
54 | |||
55 | sprintf(status, "/proc/%d/stat", pid); | ||
56 | if((fp = fopen(status, "r")) == NULL) | ||
57 | continue; | ||
58 | if(fstat(fileno(fp), &sb)) | ||
59 | continue; | ||
60 | my_getpwuid(curstatus.user, sb.st_uid); | ||
61 | name = fgets(buf, sizeof(buf), fp); | ||
62 | fclose(fp); | ||
63 | if(name == NULL) | ||
64 | continue; | ||
65 | name = strrchr(buf, ')'); /* split into "PID (cmd" and "<rest>" */ | ||
66 | if(name == 0 || name[1] != ' ') | ||
67 | continue; | ||
68 | *name = 0; | ||
69 | sscanf(buf, "%*s (%15c", curstatus.short_cmd); | ||
70 | n = sscanf(name+2, | ||
71 | "%c %d " | ||
72 | "%*s %*s %*s %*s " /* pgrp, session, tty, tpgid */ | ||
73 | "%*s %*s %*s %*s %*s " /* flags, min_flt, cmin_flt, maj_flt, cmaj_flt */ | ||
74 | #ifdef FEATURE_CPU_USAGE_PERCENTAGE | ||
75 | "%lu %lu " | ||
76 | #else | ||
77 | "%*s %*s " | ||
78 | #endif | ||
79 | "%*s %*s %*s " /* cutime, cstime, priority */ | ||
80 | "%ld " | ||
81 | "%*s %*s %*s " /* timeout, it_real_value, start_time */ | ||
82 | "%*s " /* vsize */ | ||
83 | "%ld", | ||
84 | curstatus.state, &curstatus.ppid, | ||
85 | #ifdef FEATURE_CPU_USAGE_PERCENTAGE | ||
86 | &curstatus.utime, &curstatus.stime, | ||
87 | #endif | ||
88 | &tasknice, | ||
89 | &curstatus.rss); | ||
90 | #ifdef FEATURE_CPU_USAGE_PERCENTAGE | ||
91 | if(n != 6) | ||
92 | #else | ||
93 | if(n != 4) | ||
94 | #endif | ||
95 | continue; | ||
96 | |||
97 | if (curstatus.rss == 0 && curstatus.state[0] != 'Z') | ||
98 | curstatus.state[1] = 'W'; | ||
99 | else | ||
100 | curstatus.state[1] = ' '; | ||
101 | if (tasknice < 0) | ||
102 | curstatus.state[2] = '<'; | ||
103 | else if (tasknice > 0) | ||
104 | curstatus.state[2] = 'N'; | ||
105 | else | ||
106 | curstatus.state[2] = ' '; | ||
107 | |||
108 | curstatus.rss <<= (PAGE_SHIFT - 10); /* 2**10 = 1kb */ | ||
109 | |||
110 | sprintf(status, "/proc/%d/cmdline", pid); | ||
111 | if(save_user_arg0) { | ||
112 | if((fp = fopen(status, "r")) == NULL) | ||
113 | continue; | ||
114 | if(fgets(buf, sizeof(buf), fp) != NULL) { | ||
115 | name = strchr(buf, '\n'); | ||
116 | if(name != NULL) | ||
117 | *name = 0; | ||
118 | if(buf[0]) | ||
119 | curstatus.cmd = strdup(buf); | ||
120 | /* if NULL it work true also */ | ||
121 | } | ||
122 | fclose(fp); | ||
123 | } | ||
124 | return memcpy(&ret_status, &curstatus, sizeof(procps_status_t)); | ||
125 | } | ||
126 | } | ||
127 | |||
128 | #endif /* CONFIG_FEATURE_USE_DEVPS_PATCH. Else this file is empty */ | ||
129 | |||
130 | /* END CODE */ | ||
131 | /* | ||
132 | Local Variables: | ||
133 | c-file-style: "linux" | ||
134 | c-basic-offset: 4 | ||
135 | tab-width: 4 | ||
136 | End: | ||
137 | */ | ||