summaryrefslogtreecommitdiff
path: root/libbb
diff options
context:
space:
mode:
authorEric Andersen2002-10-22 12:21:15 +0000
committerEric Andersen2002-10-22 12:21:15 +0000
commit44608e9693b03661fbab5e27650bb040c6871d11 (patch)
tree4555230653cdb82d998f076b29130d8fe18a6f7a /libbb
parent1887b0478f2743ce7808e8b37462e18d584611e1 (diff)
downloadbusybox-44608e9693b03661fbab5e27650bb040c6871d11.zip
busybox-44608e9693b03661fbab5e27650bb040c6871d11.tar.gz
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.
Diffstat (limited to 'libbb')
-rw-r--r--libbb/Makefile.in3
-rw-r--r--libbb/find_pid_by_name.c54
-rw-r--r--libbb/procps.c137
3 files changed, 149 insertions, 45 deletions
diff --git a/libbb/Makefile.in b/libbb/Makefile.in
index f4ef0b8..6703cf1 100644
--- a/libbb/Makefile.in
+++ b/libbb/Makefile.in
@@ -43,7 +43,8 @@ LIBBB_SRC:= \
arith.c simplify_path.c inet_common.c inode_hash.c obscure.c \
pwd2spwd.c xfuncs.c correct_password.c change_identity.c \
setup_environment.c run_shell.c pw_encrypt.c restricted_shell.c \
- xgethostbyname2.c create_icmp6_socket.c xconnect.c bb_asprintf.c
+ xgethostbyname2.c create_icmp6_socket.c xconnect.c bb_asprintf.c \
+ procps.c
LIBBB_OBJS=$(patsubst %.c,$(LIBBB_DIR)%.o, $(LIBBB_SRC))
diff --git a/libbb/find_pid_by_name.c b/libbb/find_pid_by_name.c
index 4eaee03..a648137 100644
--- a/libbb/find_pid_by_name.c
+++ b/libbb/find_pid_by_name.c
@@ -22,7 +22,6 @@
#include <stdio.h>
#include <ctype.h>
#include <string.h>
-#include <dirent.h>
#include <stdlib.h>
#include "libbb.h"
@@ -40,7 +39,7 @@
*
* Returns a list of all matching PIDs
*/
-extern long* find_pid_by_name( char* pidName)
+extern long* find_pid_by_name( const char* pidName)
{
int fd, i, j;
char device[] = "/dev/ps";
@@ -112,61 +111,28 @@ extern long* find_pid_by_name( char* pidName)
/* find_pid_by_name()
*
+ * Modified by Vladimir Oleynik for use with libbb/procps.c
* This finds the pid of the specified process.
* Currently, it's implemented by rummaging through
* the proc filesystem.
*
* Returns a list of all matching PIDs
*/
-extern long* find_pid_by_name( char* pidName)
+extern long* find_pid_by_name( const char* pidName)
{
- DIR *dir;
- struct dirent *next;
- long* pidList=NULL;
+ long* pidList;
int i=0;
+ procps_status_t * p;
- dir = opendir("/proc");
- if (!dir)
- perror_msg_and_die("Cannot open /proc");
-
- while ((next = readdir(dir)) != NULL) {
- FILE *status;
- char filename[READ_BUF_SIZE];
- char buffer[READ_BUF_SIZE];
- char name[READ_BUF_SIZE];
-
- /* Must skip ".." since that is outside /proc */
- if (strcmp(next->d_name, "..") == 0)
- continue;
-
- /* If it isn't a number, we don't want it */
- if (!isdigit(*next->d_name))
- continue;
-
- sprintf(filename, "/proc/%s/status", next->d_name);
- if (! (status = fopen(filename, "r")) ) {
- continue;
- }
- if (fgets(buffer, READ_BUF_SIZE-1, status) == NULL) {
- fclose(status);
- continue;
- }
- fclose(status);
-
- /* Buffer should contain a string like "Name: binary_name" */
- sscanf(buffer, "%*s %s", name);
- if (strcmp(name, pidName) == 0) {
+ pidList = xmalloc(sizeof(long));
+ while ((p = procps_scan(0)) != 0) {
+ if (strcmp(p->short_cmd, pidName) == 0) {
pidList=xrealloc( pidList, sizeof(long) * (i+2));
- pidList[i++]=strtol(next->d_name, NULL, 0);
+ pidList[i++]=p->pid;
}
}
- if (pidList) {
- pidList[i]=0;
- } else {
- pidList=xrealloc( pidList, sizeof(long));
- pidList[0]=-1;
- }
+ pidList[i] = i==0 ? -1 : 0;
return pidList;
}
#endif /* CONFIG_FEATURE_USE_DEVPS_PATCH */
diff --git a/libbb/procps.c b/libbb/procps.c
new file mode 100644
index 0000000..9ac5be0
--- /dev/null
+++ b/libbb/procps.c
@@ -0,0 +1,137 @@
+/* vi: set sw=4 ts=4: */
+/*
+ * Utility routines.
+ *
+ * Copyright 1998 by Albert Cahalan; all rights reserved.
+ * Copyright (C) 2002 by Vladimir Oleynik <dzo@simtreas.ru>
+ * GNU Library General Public License Version 2, or any later version
+ *
+ */
+
+#include "libbb.h"
+
+#if ! defined CONFIG_FEATURE_USE_DEVPS_PATCH
+#include <dirent.h>
+#include <string.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <asm/page.h>
+
+
+extern procps_status_t * procps_scan(int save_user_arg0)
+{
+ static DIR *dir;
+ struct dirent *entry;
+ static procps_status_t ret_status;
+ char *name;
+ int n;
+ char status[32];
+ char buf[1024];
+ FILE *fp;
+ procps_status_t curstatus;
+ int pid;
+ long tasknice;
+ struct stat sb;
+
+ if (!dir) {
+ dir = opendir("/proc");
+ if(!dir)
+ error_msg_and_die("Can't open /proc");
+ }
+ for(;;) {
+ if((entry = readdir(dir)) == NULL) {
+ closedir(dir);
+ dir = 0;
+ return 0;
+ }
+ name = entry->d_name;
+ if (!(*name >= '0' && *name <= '9'))
+ continue;
+
+ memset(&curstatus, 0, sizeof(procps_status_t));
+ pid = atoi(name);
+ curstatus.pid = pid;
+
+ sprintf(status, "/proc/%d/stat", pid);
+ if((fp = fopen(status, "r")) == NULL)
+ continue;
+ if(fstat(fileno(fp), &sb))
+ continue;
+ my_getpwuid(curstatus.user, sb.st_uid);
+ name = fgets(buf, sizeof(buf), fp);
+ fclose(fp);
+ if(name == NULL)
+ continue;
+ name = strrchr(buf, ')'); /* split into "PID (cmd" and "<rest>" */
+ if(name == 0 || name[1] != ' ')
+ continue;
+ *name = 0;
+ sscanf(buf, "%*s (%15c", curstatus.short_cmd);
+ n = sscanf(name+2,
+ "%c %d "
+ "%*s %*s %*s %*s " /* pgrp, session, tty, tpgid */
+ "%*s %*s %*s %*s %*s " /* flags, min_flt, cmin_flt, maj_flt, cmaj_flt */
+#ifdef FEATURE_CPU_USAGE_PERCENTAGE
+ "%lu %lu "
+#else
+ "%*s %*s "
+#endif
+ "%*s %*s %*s " /* cutime, cstime, priority */
+ "%ld "
+ "%*s %*s %*s " /* timeout, it_real_value, start_time */
+ "%*s " /* vsize */
+ "%ld",
+ curstatus.state, &curstatus.ppid,
+#ifdef FEATURE_CPU_USAGE_PERCENTAGE
+ &curstatus.utime, &curstatus.stime,
+#endif
+ &tasknice,
+ &curstatus.rss);
+#ifdef FEATURE_CPU_USAGE_PERCENTAGE
+ if(n != 6)
+#else
+ if(n != 4)
+#endif
+ continue;
+
+ if (curstatus.rss == 0 && curstatus.state[0] != 'Z')
+ curstatus.state[1] = 'W';
+ else
+ curstatus.state[1] = ' ';
+ if (tasknice < 0)
+ curstatus.state[2] = '<';
+ else if (tasknice > 0)
+ curstatus.state[2] = 'N';
+ else
+ curstatus.state[2] = ' ';
+
+ curstatus.rss <<= (PAGE_SHIFT - 10); /* 2**10 = 1kb */
+
+ sprintf(status, "/proc/%d/cmdline", pid);
+ if(save_user_arg0) {
+ if((fp = fopen(status, "r")) == NULL)
+ continue;
+ if(fgets(buf, sizeof(buf), fp) != NULL) {
+ name = strchr(buf, '\n');
+ if(name != NULL)
+ *name = 0;
+ if(buf[0])
+ curstatus.cmd = strdup(buf);
+ /* if NULL it work true also */
+ }
+ fclose(fp);
+ }
+ return memcpy(&ret_status, &curstatus, sizeof(procps_status_t));
+ }
+}
+
+#endif /* CONFIG_FEATURE_USE_DEVPS_PATCH. Else this file is empty */
+
+/* END CODE */
+/*
+Local Variables:
+c-file-style: "linux"
+c-basic-offset: 4
+tab-width: 4
+End:
+*/