Removed proc dependancies for init and free (which maintaining exactly

the same functionality).  /proc takes up 90k of kernel space, so it is
nice to avoid using it at all costs.  The only places where it is depended
on is for cetain optional mount/umount features, and for ps and lsmod.
 -Erik
This commit is contained in:
Erik Andersen
2000-02-21 21:26:32 +00:00
parent fa4718efcf
commit d07ee46919
5 changed files with 87 additions and 80 deletions

32
free.c
View File

@@ -23,15 +23,33 @@
#include "internal.h" #include "internal.h"
#include <stdio.h> #include <stdio.h>
#include <sys/sysinfo.h>
#define DIVISOR 1024
#if ! defined BB_FEATURE_USE_PROCFS
#error Sorry, I depend on the /proc filesystem right now.
#endif
extern int free_main(int argc, char **argv) extern int free_main(int argc, char **argv)
{ {
char *cmd[] = { "cat", "/proc/meminfo", "\0" }; struct sysinfo info;
sysinfo(&info);
info.totalram/=DIVISOR;
info.freeram/=DIVISOR;
info.totalswap/=DIVISOR;
info.freeswap/=DIVISOR;
info.sharedram/=DIVISOR;
info.bufferram/=DIVISOR;
exit(cat_main(3, cmd));
printf("%6s%13s%13s%13s%13s%13s\n", "", "total", "used", "free",
"shared", "buffers");
printf("%6s%13ld%13ld%13ld%13ld%13ld\n", "Mem:", info.totalram,
info.totalram-info.freeram, info.freeram,
info.sharedram, info.bufferram);
printf("%6s%13ld%13ld%13ld\n", "Swap:", info.totalswap,
info.totalswap-info.freeswap, info.freeswap);
printf("%6s%13ld%13ld%13ld\n", "Total:", info.totalram+info.totalswap,
(info.totalram-info.freeram)+(info.totalswap-info.freeswap),
info.freeram+info.freeswap);
exit(TRUE);
} }

44
init.c
View File

@@ -49,15 +49,12 @@
#include <linux/serial.h> /* for serial_struct */ #include <linux/serial.h> /* for serial_struct */
#include <sys/vt.h> /* for vt_stat */ #include <sys/vt.h> /* for vt_stat */
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <sys/sysinfo.h> /* For check_free_memory() */
#include <linux/version.h> #include <linux/version.h>
#ifdef BB_SYSLOGD #ifdef BB_SYSLOGD
#include <sys/syslog.h> #include <sys/syslog.h>
#endif #endif
#if ! defined BB_FEATURE_USE_PROCFS
#error Sorry, I depend on the /proc filesystem right now.
#endif
#ifndef KERNEL_VERSION #ifndef KERNEL_VERSION
#define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c)) #define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))
#endif #endif
@@ -226,25 +223,18 @@ void set_term(int fd)
} }
/* How much memory does this machine have? */ /* How much memory does this machine have? */
static int mem_total() static int check_free_memory()
{ {
char s[80]; struct sysinfo info;
char *p = "/proc/meminfo";
FILE *f;
const char pattern[] = "MemTotal:";
if ((f = fopen(p, "r")) < 0) { sysinfo(&info);
message(LOG, "Error opening %s: %s\n", p, strerror(errno)); if (sysinfo(&info) != 0) {
message(LOG, "Error checking free memory: %s\n", strerror(errno));
return -1; return -1;
} }
while (NULL != fgets(s, 79, f)) {
p = strstr(s, pattern); return(info.freeram/1024);
if (NULL != p) {
fclose(f);
return (atoi(p + strlen(pattern)));
}
}
return -1;
} }
static void console_init() static void console_init()
@@ -454,13 +444,13 @@ static void check_memory()
{ {
struct stat statBuf; struct stat statBuf;
if (mem_total() > 3500) if (check_free_memory() > 1000)
return; return;
if (stat("/etc/fstab", &statBuf) == 0) { if (stat("/etc/fstab", &statBuf) == 0) {
/* Try to turn on swap */ /* Try to turn on swap */
system("/sbin/swapon -a"); system("/sbin/swapon -a");
if (mem_total() < 3500) if (check_free_memory() < 1000)
goto goodnight; goto goodnight;
} else } else
goto goodnight; goto goodnight;
@@ -555,6 +545,11 @@ static void reboot_signal(int sig)
} }
#if defined BB_FEATURE_INIT_CHROOT #if defined BB_FEATURE_INIT_CHROOT
#if ! defined BB_FEATURE_USE_PROCFS
#error Sorry, I depend on the /proc filesystem right now.
#endif
static void check_chroot(int sig) static void check_chroot(int sig)
{ {
char *argv_init[2] = { "init", NULL, }; char *argv_init[2] = { "init", NULL, };
@@ -853,13 +848,6 @@ extern int init_main(int argc, char **argv)
#endif #endif
/* Mount /proc */
if (mount("proc", "/proc", "proc", 0, 0) == 0) {
message(LOG, "Mounting /proc: done.\n");
kernelVersion = get_kernel_revision();
} else
message(LOG | CONSOLE, "Mounting /proc: failed!\n");
/* Make sure there is enough memory to do something useful. */ /* Make sure there is enough memory to do something useful. */
check_memory(); check_memory();

View File

@@ -49,15 +49,12 @@
#include <linux/serial.h> /* for serial_struct */ #include <linux/serial.h> /* for serial_struct */
#include <sys/vt.h> /* for vt_stat */ #include <sys/vt.h> /* for vt_stat */
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <sys/sysinfo.h> /* For check_free_memory() */
#include <linux/version.h> #include <linux/version.h>
#ifdef BB_SYSLOGD #ifdef BB_SYSLOGD
#include <sys/syslog.h> #include <sys/syslog.h>
#endif #endif
#if ! defined BB_FEATURE_USE_PROCFS
#error Sorry, I depend on the /proc filesystem right now.
#endif
#ifndef KERNEL_VERSION #ifndef KERNEL_VERSION
#define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c)) #define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))
#endif #endif
@@ -226,25 +223,18 @@ void set_term(int fd)
} }
/* How much memory does this machine have? */ /* How much memory does this machine have? */
static int mem_total() static int check_free_memory()
{ {
char s[80]; struct sysinfo info;
char *p = "/proc/meminfo";
FILE *f;
const char pattern[] = "MemTotal:";
if ((f = fopen(p, "r")) < 0) { sysinfo(&info);
message(LOG, "Error opening %s: %s\n", p, strerror(errno)); if (sysinfo(&info) != 0) {
message(LOG, "Error checking free memory: %s\n", strerror(errno));
return -1; return -1;
} }
while (NULL != fgets(s, 79, f)) {
p = strstr(s, pattern); return(info.freeram/1024);
if (NULL != p) {
fclose(f);
return (atoi(p + strlen(pattern)));
}
}
return -1;
} }
static void console_init() static void console_init()
@@ -454,13 +444,13 @@ static void check_memory()
{ {
struct stat statBuf; struct stat statBuf;
if (mem_total() > 3500) if (check_free_memory() > 1000)
return; return;
if (stat("/etc/fstab", &statBuf) == 0) { if (stat("/etc/fstab", &statBuf) == 0) {
/* Try to turn on swap */ /* Try to turn on swap */
system("/sbin/swapon -a"); system("/sbin/swapon -a");
if (mem_total() < 3500) if (check_free_memory() < 1000)
goto goodnight; goto goodnight;
} else } else
goto goodnight; goto goodnight;
@@ -555,6 +545,11 @@ static void reboot_signal(int sig)
} }
#if defined BB_FEATURE_INIT_CHROOT #if defined BB_FEATURE_INIT_CHROOT
#if ! defined BB_FEATURE_USE_PROCFS
#error Sorry, I depend on the /proc filesystem right now.
#endif
static void check_chroot(int sig) static void check_chroot(int sig)
{ {
char *argv_init[2] = { "init", NULL, }; char *argv_init[2] = { "init", NULL, };
@@ -853,13 +848,6 @@ extern int init_main(int argc, char **argv)
#endif #endif
/* Mount /proc */
if (mount("proc", "/proc", "proc", 0, 0) == 0) {
message(LOG, "Mounting /proc: done.\n");
kernelVersion = get_kernel_revision();
} else
message(LOG | CONSOLE, "Mounting /proc: failed!\n");
/* Make sure there is enough memory to do something useful. */ /* Make sure there is enough memory to do something useful. */
check_memory(); check_memory();

View File

@@ -23,15 +23,33 @@
#include "internal.h" #include "internal.h"
#include <stdio.h> #include <stdio.h>
#include <sys/sysinfo.h>
#define DIVISOR 1024
#if ! defined BB_FEATURE_USE_PROCFS
#error Sorry, I depend on the /proc filesystem right now.
#endif
extern int free_main(int argc, char **argv) extern int free_main(int argc, char **argv)
{ {
char *cmd[] = { "cat", "/proc/meminfo", "\0" }; struct sysinfo info;
sysinfo(&info);
info.totalram/=DIVISOR;
info.freeram/=DIVISOR;
info.totalswap/=DIVISOR;
info.freeswap/=DIVISOR;
info.sharedram/=DIVISOR;
info.bufferram/=DIVISOR;
exit(cat_main(3, cmd));
printf("%6s%13s%13s%13s%13s%13s\n", "", "total", "used", "free",
"shared", "buffers");
printf("%6s%13ld%13ld%13ld%13ld%13ld\n", "Mem:", info.totalram,
info.totalram-info.freeram, info.freeram,
info.sharedram, info.bufferram);
printf("%6s%13ld%13ld%13ld\n", "Swap:", info.totalswap,
info.totalswap-info.freeswap, info.freeswap);
printf("%6s%13ld%13ld%13ld\n", "Total:", info.totalram+info.totalswap,
(info.totalram-info.freeram)+(info.totalswap-info.freeswap),
info.freeram+info.freeswap);
exit(TRUE);
} }

View File

@@ -48,6 +48,7 @@
#include <unistd.h> #include <unistd.h>
#include <ctype.h> #include <ctype.h>
#include <sys/param.h> /* for PATH_MAX */ #include <sys/param.h> /* for PATH_MAX */
#include <sys/utsname.h> /* for uname(2) */
#if defined BB_FEATURE_MOUNT_LOOP #if defined BB_FEATURE_MOUNT_LOOP
#include <fcntl.h> #include <fcntl.h>
@@ -103,26 +104,20 @@ extern void fatalError(char *s, ...)
} }
#if defined (BB_INIT) || defined (BB_PS) #if defined (BB_INIT) || defined (BB_PS)
#if ! defined BB_FEATURE_USE_PROCFS
#error Sorry, I depend on the /proc filesystem right now.
#endif
/* Returns kernel version encoded as major*65536 + minor*256 + patch, /* Returns kernel version encoded as major*65536 + minor*256 + patch,
* so, for example, to check if the kernel is greater than 2.2.11: * so, for example, to check if the kernel is greater than 2.2.11:
* if (get_kernel_revision() <= 2*65536+2*256+11) { <stuff> } * if (get_kernel_revision() <= 2*65536+2*256+11) { <stuff> }
*/ */
int get_kernel_revision() int get_kernel_revision()
{ {
FILE *file; struct utsname name;
int major = 0, minor = 0, patch = 0; int major = 0, minor = 0, patch = 0;
file = fopen("/proc/sys/kernel/osrelease", "r"); if (uname(&name) == -1) {
if (file == NULL) { perror("cannot get system information");
/* bummer, /proc must not be mounted... */
return (0); return (0);
} }
fscanf(file, "%d.%d.%d", &major, &minor, &patch); sscanf(name.version, "%d.%d.%d", &major, &minor, &patch);
fclose(file);
return major * 65536 + minor * 256 + patch; return major * 65536 + minor * 256 + patch;
} }
#endif /* BB_INIT || BB_PS */ #endif /* BB_INIT || BB_PS */