aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEric Andersen <andersen@codepoet.org>1999-10-20 19:18:15 +0000
committerEric Andersen <andersen@codepoet.org>1999-10-20 19:18:15 +0000
commitd23f9ba0f6d82e6bab8a3ec1b804865f4b22cfb7 (patch)
tree0ab285d58cd8cf530ad8acac823545df892c90e9
parentef8b6c757de9684f5d88eff4b014527e87121137 (diff)
downloadbusybox-w32-d23f9ba0f6d82e6bab8a3ec1b804865f4b22cfb7.tar.gz
busybox-w32-d23f9ba0f6d82e6bab8a3ec1b804865f4b22cfb7.tar.bz2
busybox-w32-d23f9ba0f6d82e6bab8a3ec1b804865f4b22cfb7.zip
Made ps work. Fixed some stuff.
-rw-r--r--Makefile2
-rw-r--r--busybox.def.h2
-rw-r--r--chmod_chown_chgrp.c50
-rw-r--r--init.c13
-rw-r--r--init/init.c13
-rw-r--r--internal.h6
-rw-r--r--procps/ps.c118
-rw-r--r--ps.c118
-rw-r--r--utility.c101
9 files changed, 309 insertions, 114 deletions
diff --git a/Makefile b/Makefile
index 5f8970669..ee3930880 100644
--- a/Makefile
+++ b/Makefile
@@ -5,7 +5,7 @@ BUILDTIME=$(shell date "+%Y%m%d-%H%M")
5 5
6# Comment out the following to make a debuggable build 6# Comment out the following to make a debuggable build
7# Leave this off for production use. 7# Leave this off for production use.
8DODEBUG=true 8#DODEBUG=true
9 9
10#This will choke on a non-debian system 10#This will choke on a non-debian system
11ARCH=`uname -m | sed -e 's/i.86/i386/' | sed -e 's/sparc.*/sparc/'` 11ARCH=`uname -m | sed -e 's/i.86/i386/' | sed -e 's/sparc.*/sparc/'`
diff --git a/busybox.def.h b/busybox.def.h
index eec662193..09379965c 100644
--- a/busybox.def.h
+++ b/busybox.def.h
@@ -21,7 +21,7 @@
21#define BB_GREP 21#define BB_GREP
22#define BB_HALT 22#define BB_HALT
23#define BB_INIT 23#define BB_INIT
24//#define BB_KILL // Supplied by ash 24#define BB_KILL
25//#define BB_LENGTH 25//#define BB_LENGTH
26#define BB_LN 26#define BB_LN
27#define BB_LOADFONT 27#define BB_LOADFONT
diff --git a/chmod_chown_chgrp.c b/chmod_chown_chgrp.c
index faebbbe10..85ba247b8 100644
--- a/chmod_chown_chgrp.c
+++ b/chmod_chown_chgrp.c
@@ -48,56 +48,6 @@ static const char chmod_usage[] = "[-R] MODE[,MODE]... FILE...\n"
48 "\t-R\tchange files and directories recursively.\n"; 48 "\t-R\tchange files and directories recursively.\n";
49 49
50 50
51uid_t my_getid(const char *filename, const char *name)
52{
53 FILE *stream;
54 char *rname, *start, *end, buf[128];
55 uid_t rid;
56
57 stream=fopen(filename,"r");
58
59 while (fgets (buf, 128, stream) != NULL) {
60 if (buf[0] == '#')
61 continue;
62
63 start = buf;
64 end = strchr (start, ':');
65 if (end == NULL)
66 continue;
67 *end = '\0';
68 rname = start;
69
70 start = end + 1;
71 end = strchr (start, ':');
72 if (end == NULL)
73 continue;
74
75 start = end + 1;
76 rid = (uid_t) strtol (start, &end, 10);
77 if (end == start)
78 continue;
79
80 if (name) {
81 if (0 == strcmp(rname, name))
82 return( rid);
83 }
84 }
85 fclose(stream);
86 return (-1);
87}
88
89uid_t
90my_getpwnam(char *name)
91{
92 return my_getid("/etc/passwd", name);
93}
94
95gid_t
96my_getgrnam(char *name)
97{
98 return my_getid("/etc/group", name);
99}
100
101static int fileAction(const char *fileName, struct stat* statbuf) 51static int fileAction(const char *fileName, struct stat* statbuf)
102{ 52{
103 switch (whichApp) { 53 switch (whichApp) {
diff --git a/init.c b/init.c
index 6c44cce15..62865451a 100644
--- a/init.c
+++ b/init.c
@@ -194,19 +194,6 @@ set_free_pages()
194 fclose(f); 194 fclose(f);
195} 195}
196 196
197static int
198get_kernel_revision()
199{
200 FILE *f;
201 int major=0, minor=0, patch=0;
202
203 f = fopen("/proc/sys/kernel/osrelease","r");
204 fscanf(f,"%d.%d.%d",&major,&minor,&patch);
205 fclose(f);
206 return major*65536 + minor*256 + patch;
207}
208
209
210static void 197static void
211shutdown_system(void) 198shutdown_system(void)
212{ 199{
diff --git a/init/init.c b/init/init.c
index 6c44cce15..62865451a 100644
--- a/init/init.c
+++ b/init/init.c
@@ -194,19 +194,6 @@ set_free_pages()
194 fclose(f); 194 fclose(f);
195} 195}
196 196
197static int
198get_kernel_revision()
199{
200 FILE *f;
201 int major=0, minor=0, patch=0;
202
203 f = fopen("/proc/sys/kernel/osrelease","r");
204 fscanf(f,"%d.%d.%d",&major,&minor,&patch);
205 fclose(f);
206 return major*65536 + minor*256 + patch;
207}
208
209
210static void 197static void
211shutdown_system(void) 198shutdown_system(void)
212{ 199{
diff --git a/internal.h b/internal.h
index 9b230a335..f2c2d8a7d 100644
--- a/internal.h
+++ b/internal.h
@@ -138,6 +138,12 @@ extern void createPath (const char *name, int mode);
138extern int parse_mode( const char* s, mode_t* theMode); 138extern int parse_mode( const char* s, mode_t* theMode);
139extern volatile void usage(const char *usage); 139extern volatile void usage(const char *usage);
140 140
141extern uid_t my_getpwnam(char *name);
142extern gid_t my_getgrnam(char *name);
143extern void my_getpwuid(char* name, uid_t uid);
144extern void my_getgrgid(char* group, gid_t gid);
145extern int get_kernel_revision();
146
141 147
142 148
143#if defined (BB_FSCK_MINIX) || defined (BB_MKFS_MINIX) 149#if defined (BB_FSCK_MINIX) || defined (BB_MKFS_MINIX)
diff --git a/procps/ps.c b/procps/ps.c
index b8e4cd3a0..97a5d6ba2 100644
--- a/procps/ps.c
+++ b/procps/ps.c
@@ -23,16 +23,82 @@
23#include <unistd.h> 23#include <unistd.h>
24#include <dirent.h> 24#include <dirent.h>
25#include <stdio.h> 25#include <stdio.h>
26#include <fcntl.h>
27#include <ctype.h>
28
29
30typedef struct proc_s {
31 char
32 cmd[16]; /* basename of executable file in call to exec(2) */
33 int
34 ruid, rgid, /* real only (sorry) */
35 pid, /* process id */
36 ppid; /* pid of parent process */
37 char
38 state; /* single-char code for process state (S=sleeping) */
39} proc_t;
40
41
42
43static int file2str(char *filename, char *ret, int cap)
44{
45 int fd, num_read;
46
47 if ( (fd = open(filename, O_RDONLY, 0)) == -1 ) return -1;
48 if ( (num_read = read(fd, ret, cap - 1)) <= 0 ) return -1;
49 ret[num_read] = 0;
50 close(fd);
51 return num_read;
52}
53
54
55static void parse_proc_status(char* S, proc_t* P)
56{
57 char* tmp;
58 memset(P->cmd, 0, sizeof P->cmd);
59 sscanf (S, "Name:\t%15c", P->cmd);
60 tmp = strchr(P->cmd,'\n');
61 if (tmp)
62 *tmp='\0';
63 tmp = strstr (S,"State");
64 sscanf (tmp, "State:\t%c", &P->state);
65
66 tmp = strstr (S,"Pid:");
67 if(tmp) sscanf (tmp,
68 "Pid:\t%d\n"
69 "PPid:\t%d\n",
70 &P->pid,
71 &P->ppid
72 );
73 else fprintf(stderr, "Internal error!\n");
74
75 /* For busybox, ignoring effecting, saved, etc */
76 tmp = strstr (S,"Uid:");
77 if(tmp) sscanf (tmp,
78 "Uid:\t%d", &P->ruid);
79 else fprintf(stderr, "Internal error!\n");
80
81 tmp = strstr (S,"Gid:");
82 if(tmp) sscanf (tmp,
83 "Gid:\t%d", &P->rgid);
84 else fprintf(stderr, "Internal error!\n");
85
86}
26 87
27 88
28extern int ps_main(int argc, char **argv) 89extern int ps_main(int argc, char **argv)
29{ 90{
91 proc_t p;
30 DIR *dir; 92 DIR *dir;
31 FILE *file; 93 FILE *file;
32 struct dirent *entry; 94 struct dirent *entry;
95 char path[32], sbuf[512];
96 char uidName[10]="";
97 char groupName[10]="";
98 int i, c;
33 99
34 if ( argc>1 && **(argv+1) == '-' ) { 100 if ( argc>1 && **(argv+1) == '-' ) {
35 usage ("ps\n"); 101 usage ("ps - report process status\nThis version of ps accepts no options.\n");
36 } 102 }
37 103
38 dir = opendir("/proc"); 104 dir = opendir("/proc");
@@ -41,28 +107,44 @@ extern int ps_main(int argc, char **argv)
41 exit(FALSE); 107 exit(FALSE);
42 } 108 }
43 109
44 fprintf(stdout, "PID\tUid\tGid\tState\tName\n"); 110 fprintf(stdout, "%5s %-8s %-3s %5s %s\n", "PID", "Uid", "Gid", "State", "Command");
45 while ((entry = readdir(dir)) != NULL) { 111 while ((entry = readdir(dir)) != NULL) {
46 char psStatus[NAME_MAX]; 112 uidName[0]='\0';
47 char psName[NAME_MAX]=""; 113 groupName[0]='\0';
48 char psState[NAME_MAX]=""; 114
49 int psPID=0, psPPID=0, psUid=0, psGid=0; 115 if (! isdigit(*entry->d_name))
50 //if (match(entry->d_name, "[0-9]") == FALSE)
51 // continue;
52 sprintf(psStatus, "/proc/%s/status", entry->d_name);
53 file = fopen( psStatus, "r");
54 if (file == NULL) {
55 continue; 116 continue;
56 //perror(psStatus); 117 sprintf(path, "/proc/%s/status", entry->d_name);
57 //exit( FALSE); 118 if ((file2str(path, sbuf, sizeof sbuf)) != -1 ) {
119 parse_proc_status(sbuf, &p);
58 } 120 }
59 fscanf(file, "Name:\t%s\nState:\t%s\nPid:\t%d\nPPid:\t%d\nUid:\t%d\nGid:\t%d",
60 psName, psState, &psPID, &psPPID, &psUid, &psGid);
61 fclose(file);
62 121
63 fprintf(stdout, "%d\t%d\t%d\t%s\t%s\n", psPID, psUid, psGid, psState, psName); 122 /* Make some adjustments as needed */
123 my_getpwuid( uidName, p.ruid);
124 my_getgrgid( groupName, p.rgid);
125 if (*uidName == '\0')
126 sprintf( uidName, "%d", p.ruid);
127 if (*groupName == '\0')
128 sprintf( groupName, "%d", p.rgid);
129
130 fprintf(stdout, "%5d %-8s %-8s %c ", p.pid, uidName, groupName, p.state);
131 sprintf(path, "/proc/%s/cmdline", entry->d_name);
132 file = fopen(path, "r");
133 if (file == NULL) {
134 perror(path);
135 exit(FALSE);
136 }
137 i=0;
138 while (((c = getc(file)) != EOF) && (i < 53)) {
139 i++;
140 if (c == '\0')
141 c = ' ';
142 putc(c, stdout);
143 }
144 if (i==0)
145 fprintf(stdout, "%s", p.cmd);
146 fprintf(stdout, "\n");
64 } 147 }
65 closedir(dir); 148 closedir(dir);
66 exit(TRUE); 149 exit(TRUE);
67} 150}
68
diff --git a/ps.c b/ps.c
index b8e4cd3a0..97a5d6ba2 100644
--- a/ps.c
+++ b/ps.c
@@ -23,16 +23,82 @@
23#include <unistd.h> 23#include <unistd.h>
24#include <dirent.h> 24#include <dirent.h>
25#include <stdio.h> 25#include <stdio.h>
26#include <fcntl.h>
27#include <ctype.h>
28
29
30typedef struct proc_s {
31 char
32 cmd[16]; /* basename of executable file in call to exec(2) */
33 int
34 ruid, rgid, /* real only (sorry) */
35 pid, /* process id */
36 ppid; /* pid of parent process */
37 char
38 state; /* single-char code for process state (S=sleeping) */
39} proc_t;
40
41
42
43static int file2str(char *filename, char *ret, int cap)
44{
45 int fd, num_read;
46
47 if ( (fd = open(filename, O_RDONLY, 0)) == -1 ) return -1;
48 if ( (num_read = read(fd, ret, cap - 1)) <= 0 ) return -1;
49 ret[num_read] = 0;
50 close(fd);
51 return num_read;
52}
53
54
55static void parse_proc_status(char* S, proc_t* P)
56{
57 char* tmp;
58 memset(P->cmd, 0, sizeof P->cmd);
59 sscanf (S, "Name:\t%15c", P->cmd);
60 tmp = strchr(P->cmd,'\n');
61 if (tmp)
62 *tmp='\0';
63 tmp = strstr (S,"State");
64 sscanf (tmp, "State:\t%c", &P->state);
65
66 tmp = strstr (S,"Pid:");
67 if(tmp) sscanf (tmp,
68 "Pid:\t%d\n"
69 "PPid:\t%d\n",
70 &P->pid,
71 &P->ppid
72 );
73 else fprintf(stderr, "Internal error!\n");
74
75 /* For busybox, ignoring effecting, saved, etc */
76 tmp = strstr (S,"Uid:");
77 if(tmp) sscanf (tmp,
78 "Uid:\t%d", &P->ruid);
79 else fprintf(stderr, "Internal error!\n");
80
81 tmp = strstr (S,"Gid:");
82 if(tmp) sscanf (tmp,
83 "Gid:\t%d", &P->rgid);
84 else fprintf(stderr, "Internal error!\n");
85
86}
26 87
27 88
28extern int ps_main(int argc, char **argv) 89extern int ps_main(int argc, char **argv)
29{ 90{
91 proc_t p;
30 DIR *dir; 92 DIR *dir;
31 FILE *file; 93 FILE *file;
32 struct dirent *entry; 94 struct dirent *entry;
95 char path[32], sbuf[512];
96 char uidName[10]="";
97 char groupName[10]="";
98 int i, c;
33 99
34 if ( argc>1 && **(argv+1) == '-' ) { 100 if ( argc>1 && **(argv+1) == '-' ) {
35 usage ("ps\n"); 101 usage ("ps - report process status\nThis version of ps accepts no options.\n");
36 } 102 }
37 103
38 dir = opendir("/proc"); 104 dir = opendir("/proc");
@@ -41,28 +107,44 @@ extern int ps_main(int argc, char **argv)
41 exit(FALSE); 107 exit(FALSE);
42 } 108 }
43 109
44 fprintf(stdout, "PID\tUid\tGid\tState\tName\n"); 110 fprintf(stdout, "%5s %-8s %-3s %5s %s\n", "PID", "Uid", "Gid", "State", "Command");
45 while ((entry = readdir(dir)) != NULL) { 111 while ((entry = readdir(dir)) != NULL) {
46 char psStatus[NAME_MAX]; 112 uidName[0]='\0';
47 char psName[NAME_MAX]=""; 113 groupName[0]='\0';
48 char psState[NAME_MAX]=""; 114
49 int psPID=0, psPPID=0, psUid=0, psGid=0; 115 if (! isdigit(*entry->d_name))
50 //if (match(entry->d_name, "[0-9]") == FALSE)
51 // continue;
52 sprintf(psStatus, "/proc/%s/status", entry->d_name);
53 file = fopen( psStatus, "r");
54 if (file == NULL) {
55 continue; 116 continue;
56 //perror(psStatus); 117 sprintf(path, "/proc/%s/status", entry->d_name);
57 //exit( FALSE); 118 if ((file2str(path, sbuf, sizeof sbuf)) != -1 ) {
119 parse_proc_status(sbuf, &p);
58 } 120 }
59 fscanf(file, "Name:\t%s\nState:\t%s\nPid:\t%d\nPPid:\t%d\nUid:\t%d\nGid:\t%d",
60 psName, psState, &psPID, &psPPID, &psUid, &psGid);
61 fclose(file);
62 121
63 fprintf(stdout, "%d\t%d\t%d\t%s\t%s\n", psPID, psUid, psGid, psState, psName); 122 /* Make some adjustments as needed */
123 my_getpwuid( uidName, p.ruid);
124 my_getgrgid( groupName, p.rgid);
125 if (*uidName == '\0')
126 sprintf( uidName, "%d", p.ruid);
127 if (*groupName == '\0')
128 sprintf( groupName, "%d", p.rgid);
129
130 fprintf(stdout, "%5d %-8s %-8s %c ", p.pid, uidName, groupName, p.state);
131 sprintf(path, "/proc/%s/cmdline", entry->d_name);
132 file = fopen(path, "r");
133 if (file == NULL) {
134 perror(path);
135 exit(FALSE);
136 }
137 i=0;
138 while (((c = getc(file)) != EOF) && (i < 53)) {
139 i++;
140 if (c == '\0')
141 c = ' ';
142 putc(c, stdout);
143 }
144 if (i==0)
145 fprintf(stdout, "%s", p.cmd);
146 fprintf(stdout, "\n");
64 } 147 }
65 closedir(dir); 148 closedir(dir);
66 exit(TRUE); 149 exit(TRUE);
67} 150}
68
diff --git a/utility.c b/utility.c
index fa9849160..981769f92 100644
--- a/utility.c
+++ b/utility.c
@@ -42,6 +42,27 @@ volatile void usage(const char *usage)
42} 42}
43 43
44 44
45#if defined (BB_INIT) || defined (BB_PS)
46
47/* Returns kernel version encoded as major*65536 + minor*256 + patch,
48 * so, for example, to check if the kernel is greater than 2.2.11:
49 * if (get_kernel_revision() <= 2*65536+2*256+11) { <stuff> }
50 */
51int
52get_kernel_revision()
53{
54 FILE *f;
55 int major=0, minor=0, patch=0;
56
57 f = fopen("/proc/sys/kernel/osrelease","r");
58 fscanf(f,"%d.%d.%d",&major,&minor,&patch);
59 fclose(f);
60 return major*65536 + minor*256 + patch;
61}
62
63#endif
64
65
45 66
46#if defined (BB_CP) || defined (BB_MV) 67#if defined (BB_CP) || defined (BB_MV)
47/* 68/*
@@ -659,4 +680,84 @@ parse_mode( const char* s, mode_t* theMode)
659 680
660 681
661 682
683
684
685
686
687#if defined (BB_CHMOD_CHOWN_CHGRP) || defined (BB_PS)
688
689/* Use this to avoid needing the glibc NSS stuff
690 * This uses storage buf to hold things.
691 * */
692uid_t
693my_getid(const char *filename, char *name, uid_t id)
694{
695 FILE *stream;
696 char *rname, *start, *end, buf[128];
697 uid_t rid;
698
699 stream=fopen(filename,"r");
700
701 while (fgets (buf, 128, stream) != NULL) {
702 if (buf[0] == '#')
703 continue;
704
705 start = buf;
706 end = strchr (start, ':');
707 if (end == NULL)
708 continue;
709 *end = '\0';
710 rname = start;
711
712 start = end + 1;
713 end = strchr (start, ':');
714 if (end == NULL)
715 continue;
716
717 start = end + 1;
718 rid = (uid_t) strtol (start, &end, 10);
719 if (end == start)
720 continue;
721
722 if (name) {
723 if (0 == strcmp(rname, name))
724 return( rid);
725 }
726 if ( id != -1 && id == rid ) {
727 strncpy(name, rname, 8);
728 return( TRUE);
729 }
730 }
731 fclose(stream);
732 return (-1);
733}
734
735uid_t
736my_getpwnam(char *name)
737{
738 return my_getid("/etc/passwd", name, -1);
739}
740
741gid_t
742my_getgrnam(char *name)
743{
744 return my_getid("/etc/group", name, -1);
745}
746
747void
748my_getpwuid(char* name, uid_t uid)
749{
750 my_getid("/etc/passwd", name, uid);
751}
752
753void
754my_getgrgid(char* group, gid_t gid)
755{
756 my_getid("/etc/group", group, gid);
757}
758
759
760#endif
761
762
662/* END CODE */ 763/* END CODE */