aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--free.c32
-rw-r--r--init.c44
-rw-r--r--init/init.c44
-rw-r--r--procps/free.c32
-rw-r--r--utility.c15
5 files changed, 87 insertions, 80 deletions
diff --git a/free.c b/free.c
index b07135430..78a36fe9a 100644
--- a/free.c
+++ b/free.c
@@ -23,15 +23,33 @@
23 23
24#include "internal.h" 24#include "internal.h"
25#include <stdio.h> 25#include <stdio.h>
26#include <sys/sysinfo.h>
26 27
27 28#define DIVISOR 1024
28#if ! defined BB_FEATURE_USE_PROCFS
29#error Sorry, I depend on the /proc filesystem right now.
30#endif
31
32extern int free_main(int argc, char **argv) 29extern int free_main(int argc, char **argv)
33{ 30{
34 char *cmd[] = { "cat", "/proc/meminfo", "\0" }; 31 struct sysinfo info;
32 sysinfo(&info);
33 info.totalram/=DIVISOR;
34 info.freeram/=DIVISOR;
35 info.totalswap/=DIVISOR;
36 info.freeswap/=DIVISOR;
37 info.sharedram/=DIVISOR;
38 info.bufferram/=DIVISOR;
39
40
41 printf("%6s%13s%13s%13s%13s%13s\n", "", "total", "used", "free",
42 "shared", "buffers");
43
44 printf("%6s%13ld%13ld%13ld%13ld%13ld\n", "Mem:", info.totalram,
45 info.totalram-info.freeram, info.freeram,
46 info.sharedram, info.bufferram);
47
48 printf("%6s%13ld%13ld%13ld\n", "Swap:", info.totalswap,
49 info.totalswap-info.freeswap, info.freeswap);
35 50
36 exit(cat_main(3, cmd)); 51 printf("%6s%13ld%13ld%13ld\n", "Total:", info.totalram+info.totalswap,
52 (info.totalram-info.freeram)+(info.totalswap-info.freeswap),
53 info.freeram+info.freeswap);
54 exit(TRUE);
37} 55}
diff --git a/init.c b/init.c
index 4a19822ae..6ec811599 100644
--- a/init.c
+++ b/init.c
@@ -49,15 +49,12 @@
49#include <linux/serial.h> /* for serial_struct */ 49#include <linux/serial.h> /* for serial_struct */
50#include <sys/vt.h> /* for vt_stat */ 50#include <sys/vt.h> /* for vt_stat */
51#include <sys/ioctl.h> 51#include <sys/ioctl.h>
52#include <sys/sysinfo.h> /* For check_free_memory() */
52#include <linux/version.h> 53#include <linux/version.h>
53#ifdef BB_SYSLOGD 54#ifdef BB_SYSLOGD
54#include <sys/syslog.h> 55#include <sys/syslog.h>
55#endif 56#endif
56 57
57#if ! defined BB_FEATURE_USE_PROCFS
58#error Sorry, I depend on the /proc filesystem right now.
59#endif
60
61#ifndef KERNEL_VERSION 58#ifndef KERNEL_VERSION
62#define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c)) 59#define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))
63#endif 60#endif
@@ -226,25 +223,18 @@ void set_term(int fd)
226} 223}
227 224
228/* How much memory does this machine have? */ 225/* How much memory does this machine have? */
229static int mem_total() 226static int check_free_memory()
230{ 227{
231 char s[80]; 228 struct sysinfo info;
232 char *p = "/proc/meminfo";
233 FILE *f;
234 const char pattern[] = "MemTotal:";
235 229
236 if ((f = fopen(p, "r")) < 0) { 230 sysinfo(&info);
237 message(LOG, "Error opening %s: %s\n", p, strerror(errno)); 231 if (sysinfo(&info) != 0) {
232 message(LOG, "Error checking free memory: %s\n", strerror(errno));
238 return -1; 233 return -1;
239 } 234 }
240 while (NULL != fgets(s, 79, f)) { 235
241 p = strstr(s, pattern); 236 return(info.freeram/1024);
242 if (NULL != p) { 237
243 fclose(f);
244 return (atoi(p + strlen(pattern)));
245 }
246 }
247 return -1;
248} 238}
249 239
250static void console_init() 240static void console_init()
@@ -454,13 +444,13 @@ static void check_memory()
454{ 444{
455 struct stat statBuf; 445 struct stat statBuf;
456 446
457 if (mem_total() > 3500) 447 if (check_free_memory() > 1000)
458 return; 448 return;
459 449
460 if (stat("/etc/fstab", &statBuf) == 0) { 450 if (stat("/etc/fstab", &statBuf) == 0) {
461 /* Try to turn on swap */ 451 /* Try to turn on swap */
462 system("/sbin/swapon -a"); 452 system("/sbin/swapon -a");
463 if (mem_total() < 3500) 453 if (check_free_memory() < 1000)
464 goto goodnight; 454 goto goodnight;
465 } else 455 } else
466 goto goodnight; 456 goto goodnight;
@@ -555,6 +545,11 @@ static void reboot_signal(int sig)
555} 545}
556 546
557#if defined BB_FEATURE_INIT_CHROOT 547#if defined BB_FEATURE_INIT_CHROOT
548
549#if ! defined BB_FEATURE_USE_PROCFS
550#error Sorry, I depend on the /proc filesystem right now.
551#endif
552
558static void check_chroot(int sig) 553static void check_chroot(int sig)
559{ 554{
560 char *argv_init[2] = { "init", NULL, }; 555 char *argv_init[2] = { "init", NULL, };
@@ -853,13 +848,6 @@ extern int init_main(int argc, char **argv)
853#endif 848#endif
854 849
855 850
856 /* Mount /proc */
857 if (mount("proc", "/proc", "proc", 0, 0) == 0) {
858 message(LOG, "Mounting /proc: done.\n");
859 kernelVersion = get_kernel_revision();
860 } else
861 message(LOG | CONSOLE, "Mounting /proc: failed!\n");
862
863 /* Make sure there is enough memory to do something useful. */ 851 /* Make sure there is enough memory to do something useful. */
864 check_memory(); 852 check_memory();
865 853
diff --git a/init/init.c b/init/init.c
index 4a19822ae..6ec811599 100644
--- a/init/init.c
+++ b/init/init.c
@@ -49,15 +49,12 @@
49#include <linux/serial.h> /* for serial_struct */ 49#include <linux/serial.h> /* for serial_struct */
50#include <sys/vt.h> /* for vt_stat */ 50#include <sys/vt.h> /* for vt_stat */
51#include <sys/ioctl.h> 51#include <sys/ioctl.h>
52#include <sys/sysinfo.h> /* For check_free_memory() */
52#include <linux/version.h> 53#include <linux/version.h>
53#ifdef BB_SYSLOGD 54#ifdef BB_SYSLOGD
54#include <sys/syslog.h> 55#include <sys/syslog.h>
55#endif 56#endif
56 57
57#if ! defined BB_FEATURE_USE_PROCFS
58#error Sorry, I depend on the /proc filesystem right now.
59#endif
60
61#ifndef KERNEL_VERSION 58#ifndef KERNEL_VERSION
62#define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c)) 59#define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))
63#endif 60#endif
@@ -226,25 +223,18 @@ void set_term(int fd)
226} 223}
227 224
228/* How much memory does this machine have? */ 225/* How much memory does this machine have? */
229static int mem_total() 226static int check_free_memory()
230{ 227{
231 char s[80]; 228 struct sysinfo info;
232 char *p = "/proc/meminfo";
233 FILE *f;
234 const char pattern[] = "MemTotal:";
235 229
236 if ((f = fopen(p, "r")) < 0) { 230 sysinfo(&info);
237 message(LOG, "Error opening %s: %s\n", p, strerror(errno)); 231 if (sysinfo(&info) != 0) {
232 message(LOG, "Error checking free memory: %s\n", strerror(errno));
238 return -1; 233 return -1;
239 } 234 }
240 while (NULL != fgets(s, 79, f)) { 235
241 p = strstr(s, pattern); 236 return(info.freeram/1024);
242 if (NULL != p) { 237
243 fclose(f);
244 return (atoi(p + strlen(pattern)));
245 }
246 }
247 return -1;
248} 238}
249 239
250static void console_init() 240static void console_init()
@@ -454,13 +444,13 @@ static void check_memory()
454{ 444{
455 struct stat statBuf; 445 struct stat statBuf;
456 446
457 if (mem_total() > 3500) 447 if (check_free_memory() > 1000)
458 return; 448 return;
459 449
460 if (stat("/etc/fstab", &statBuf) == 0) { 450 if (stat("/etc/fstab", &statBuf) == 0) {
461 /* Try to turn on swap */ 451 /* Try to turn on swap */
462 system("/sbin/swapon -a"); 452 system("/sbin/swapon -a");
463 if (mem_total() < 3500) 453 if (check_free_memory() < 1000)
464 goto goodnight; 454 goto goodnight;
465 } else 455 } else
466 goto goodnight; 456 goto goodnight;
@@ -555,6 +545,11 @@ static void reboot_signal(int sig)
555} 545}
556 546
557#if defined BB_FEATURE_INIT_CHROOT 547#if defined BB_FEATURE_INIT_CHROOT
548
549#if ! defined BB_FEATURE_USE_PROCFS
550#error Sorry, I depend on the /proc filesystem right now.
551#endif
552
558static void check_chroot(int sig) 553static void check_chroot(int sig)
559{ 554{
560 char *argv_init[2] = { "init", NULL, }; 555 char *argv_init[2] = { "init", NULL, };
@@ -853,13 +848,6 @@ extern int init_main(int argc, char **argv)
853#endif 848#endif
854 849
855 850
856 /* Mount /proc */
857 if (mount("proc", "/proc", "proc", 0, 0) == 0) {
858 message(LOG, "Mounting /proc: done.\n");
859 kernelVersion = get_kernel_revision();
860 } else
861 message(LOG | CONSOLE, "Mounting /proc: failed!\n");
862
863 /* Make sure there is enough memory to do something useful. */ 851 /* Make sure there is enough memory to do something useful. */
864 check_memory(); 852 check_memory();
865 853
diff --git a/procps/free.c b/procps/free.c
index b07135430..78a36fe9a 100644
--- a/procps/free.c
+++ b/procps/free.c
@@ -23,15 +23,33 @@
23 23
24#include "internal.h" 24#include "internal.h"
25#include <stdio.h> 25#include <stdio.h>
26#include <sys/sysinfo.h>
26 27
27 28#define DIVISOR 1024
28#if ! defined BB_FEATURE_USE_PROCFS
29#error Sorry, I depend on the /proc filesystem right now.
30#endif
31
32extern int free_main(int argc, char **argv) 29extern int free_main(int argc, char **argv)
33{ 30{
34 char *cmd[] = { "cat", "/proc/meminfo", "\0" }; 31 struct sysinfo info;
32 sysinfo(&info);
33 info.totalram/=DIVISOR;
34 info.freeram/=DIVISOR;
35 info.totalswap/=DIVISOR;
36 info.freeswap/=DIVISOR;
37 info.sharedram/=DIVISOR;
38 info.bufferram/=DIVISOR;
39
40
41 printf("%6s%13s%13s%13s%13s%13s\n", "", "total", "used", "free",
42 "shared", "buffers");
43
44 printf("%6s%13ld%13ld%13ld%13ld%13ld\n", "Mem:", info.totalram,
45 info.totalram-info.freeram, info.freeram,
46 info.sharedram, info.bufferram);
47
48 printf("%6s%13ld%13ld%13ld\n", "Swap:", info.totalswap,
49 info.totalswap-info.freeswap, info.freeswap);
35 50
36 exit(cat_main(3, cmd)); 51 printf("%6s%13ld%13ld%13ld\n", "Total:", info.totalram+info.totalswap,
52 (info.totalram-info.freeram)+(info.totalswap-info.freeswap),
53 info.freeram+info.freeswap);
54 exit(TRUE);
37} 55}
diff --git a/utility.c b/utility.c
index 5bfed81d7..64598fab6 100644
--- a/utility.c
+++ b/utility.c
@@ -48,6 +48,7 @@
48#include <unistd.h> 48#include <unistd.h>
49#include <ctype.h> 49#include <ctype.h>
50#include <sys/param.h> /* for PATH_MAX */ 50#include <sys/param.h> /* for PATH_MAX */
51#include <sys/utsname.h> /* for uname(2) */
51 52
52#if defined BB_FEATURE_MOUNT_LOOP 53#if defined BB_FEATURE_MOUNT_LOOP
53#include <fcntl.h> 54#include <fcntl.h>
@@ -103,26 +104,20 @@ extern void fatalError(char *s, ...)
103} 104}
104 105
105#if defined (BB_INIT) || defined (BB_PS) 106#if defined (BB_INIT) || defined (BB_PS)
106
107#if ! defined BB_FEATURE_USE_PROCFS
108#error Sorry, I depend on the /proc filesystem right now.
109#endif
110/* Returns kernel version encoded as major*65536 + minor*256 + patch, 107/* Returns kernel version encoded as major*65536 + minor*256 + patch,
111 * so, for example, to check if the kernel is greater than 2.2.11: 108 * so, for example, to check if the kernel is greater than 2.2.11:
112 * if (get_kernel_revision() <= 2*65536+2*256+11) { <stuff> } 109 * if (get_kernel_revision() <= 2*65536+2*256+11) { <stuff> }
113 */ 110 */
114int get_kernel_revision() 111int get_kernel_revision()
115{ 112{
116 FILE *file; 113 struct utsname name;
117 int major = 0, minor = 0, patch = 0; 114 int major = 0, minor = 0, patch = 0;
118 115
119 file = fopen("/proc/sys/kernel/osrelease", "r"); 116 if (uname(&name) == -1) {
120 if (file == NULL) { 117 perror("cannot get system information");
121 /* bummer, /proc must not be mounted... */
122 return (0); 118 return (0);
123 } 119 }
124 fscanf(file, "%d.%d.%d", &major, &minor, &patch); 120 sscanf(name.version, "%d.%d.%d", &major, &minor, &patch);
125 fclose(file);
126 return major * 65536 + minor * 256 + patch; 121 return major * 65536 + minor * 256 + patch;
127} 122}
128#endif /* BB_INIT || BB_PS */ 123#endif /* BB_INIT || BB_PS */