diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2017-10-05 14:06:49 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2017-10-05 14:06:49 +0200 |
commit | 318c8114deb4acd10e74398c5fb4c0068c86bcb1 (patch) | |
tree | 3405d5987b435fcdac87eaf8fa8d74bf5dd0fab8 | |
parent | 6e2beb7dfee1a4542eca1a6e8c0f33486d9d5fb9 (diff) | |
download | busybox-w32-318c8114deb4acd10e74398c5fb4c0068c86bcb1.tar.gz busybox-w32-318c8114deb4acd10e74398c5fb4c0068c86bcb1.tar.bz2 busybox-w32-318c8114deb4acd10e74398c5fb4c0068c86bcb1.zip |
free: make it NOFORK
function old new delta
parse_cached_kb - 85 +85
free_main 580 537 -43
------------------------------------------------------------------------------
(add/remove: 1/0 grow/shrink: 0/1 up/down: 85/-43) Total: 42 bytes
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r-- | NOFORK_NOEXEC.lst | 2 | ||||
-rw-r--r-- | procps/free.c | 51 |
2 files changed, 24 insertions, 29 deletions
diff --git a/NOFORK_NOEXEC.lst b/NOFORK_NOEXEC.lst index 4e53d7204..72c33ddd7 100644 --- a/NOFORK_NOEXEC.lst +++ b/NOFORK_NOEXEC.lst | |||
@@ -144,7 +144,7 @@ flash_unlock - hardware | |||
144 | flashcp - hardware | 144 | flashcp - hardware |
145 | flock - spawner, changes state (file locks), let's play safe and not be noexec | 145 | flock - spawner, changes state (file locks), let's play safe and not be noexec |
146 | fold - noexec. runner | 146 | fold - noexec. runner |
147 | free - noexec. nofork candidate(struct globals, needs to close /proc/meminfo fd) | 147 | free - NOFORK |
148 | freeramdisk - noexec. leaks: open+ioctl_or_perror_and_die | 148 | freeramdisk - noexec. leaks: open+ioctl_or_perror_and_die |
149 | fsck - interactive, longterm | 149 | fsck - interactive, longterm |
150 | fsck.minix - needs ^C | 150 | fsck.minix - needs ^C |
diff --git a/procps/free.c b/procps/free.c index b57e4a322..fa423cfa5 100644 --- a/procps/free.c +++ b/procps/free.c | |||
@@ -15,7 +15,7 @@ | |||
15 | //config: memory in the system, as well as the buffers used by the kernel. | 15 | //config: memory in the system, as well as the buffers used by the kernel. |
16 | //config: The shared memory column should be ignored; it is obsolete. | 16 | //config: The shared memory column should be ignored; it is obsolete. |
17 | 17 | ||
18 | //applet:IF_FREE(APPLET_NOEXEC(free, free, BB_DIR_USR_BIN, BB_SUID_DROP, free)) | 18 | //applet:IF_FREE(APPLET_NOFORK(free, free, BB_DIR_USR_BIN, BB_SUID_DROP, free)) |
19 | 19 | ||
20 | //kbuild:lib-$(CONFIG_FREE) += free.o | 20 | //kbuild:lib-$(CONFIG_FREE) += free.o |
21 | 21 | ||
@@ -40,25 +40,21 @@ | |||
40 | struct globals { | 40 | struct globals { |
41 | unsigned mem_unit; | 41 | unsigned mem_unit; |
42 | #if ENABLE_DESKTOP | 42 | #if ENABLE_DESKTOP |
43 | unsigned unit_steps; | 43 | uint8_t unit_steps; |
44 | # define G_unit_steps G.unit_steps | 44 | # define G_unit_steps g->unit_steps |
45 | #else | 45 | #else |
46 | # define G_unit_steps 10 | 46 | # define G_unit_steps 10 |
47 | #endif | 47 | #endif |
48 | } FIX_ALIASING; | 48 | }; |
49 | #define G (*(struct globals*)bb_common_bufsiz1) | 49 | /* Because of NOFORK, "globals" are not in global data */ |
50 | #define INIT_G() do { \ | ||
51 | setup_common_bufsiz(); \ | ||
52 | /* NB: noexec applet - globals not zeroed */ \ | ||
53 | } while (0) | ||
54 | 50 | ||
55 | 51 | static unsigned long long scale(struct globals *g, unsigned long d) | |
56 | static unsigned long long scale(unsigned long d) | ||
57 | { | 52 | { |
58 | return ((unsigned long long)d * G.mem_unit) >> G_unit_steps; | 53 | return ((unsigned long long)d * g->mem_unit) >> G_unit_steps; |
59 | } | 54 | } |
60 | 55 | ||
61 | static unsigned long parse_cached_kb(void) | 56 | /* NOINLINE reduces main() stack usage, which makes code smaller (on x86 at least) */ |
57 | static NOINLINE unsigned long parse_cached_kb(void) | ||
62 | { | 58 | { |
63 | char buf[60]; /* actual lines we expect are ~30 chars or less */ | 59 | char buf[60]; /* actual lines we expect are ~30 chars or less */ |
64 | FILE *fp; | 60 | FILE *fp; |
@@ -69,8 +65,8 @@ static unsigned long parse_cached_kb(void) | |||
69 | if (sscanf(buf, "Cached: %lu %*s\n", &cached) == 1) | 65 | if (sscanf(buf, "Cached: %lu %*s\n", &cached) == 1) |
70 | break; | 66 | break; |
71 | } | 67 | } |
72 | if (ENABLE_FEATURE_CLEAN_UP) | 68 | /* Have to close because of NOFORK */ |
73 | fclose(fp); | 69 | fclose(fp); |
74 | 70 | ||
75 | return cached; | 71 | return cached; |
76 | } | 72 | } |
@@ -78,11 +74,10 @@ static unsigned long parse_cached_kb(void) | |||
78 | int free_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; | 74 | int free_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
79 | int free_main(int argc UNUSED_PARAM, char **argv IF_NOT_DESKTOP(UNUSED_PARAM)) | 75 | int free_main(int argc UNUSED_PARAM, char **argv IF_NOT_DESKTOP(UNUSED_PARAM)) |
80 | { | 76 | { |
77 | struct globals G; | ||
81 | struct sysinfo info; | 78 | struct sysinfo info; |
82 | unsigned long long cached; | 79 | unsigned long long cached; |
83 | 80 | ||
84 | INIT_G(); | ||
85 | |||
86 | #if ENABLE_DESKTOP | 81 | #if ENABLE_DESKTOP |
87 | G.unit_steps = 10; | 82 | G.unit_steps = 10; |
88 | if (argv[1] && argv[1][0] == '-') { | 83 | if (argv[1] && argv[1][0] == '-') { |
@@ -123,12 +118,12 @@ int free_main(int argc UNUSED_PARAM, char **argv IF_NOT_DESKTOP(UNUSED_PARAM)) | |||
123 | #define FIELDS_2 (FIELDS_6 + 4*6) | 118 | #define FIELDS_2 (FIELDS_6 + 4*6) |
124 | 119 | ||
125 | printf(FIELDS_6, | 120 | printf(FIELDS_6, |
126 | scale(info.totalram), //total | 121 | scale(&G, info.totalram), //total |
127 | scale(info.totalram - info.freeram), //used | 122 | scale(&G, info.totalram - info.freeram), //used |
128 | scale(info.freeram), //free | 123 | scale(&G, info.freeram), //free |
129 | scale(info.sharedram), //shared | 124 | scale(&G, info.sharedram), //shared |
130 | scale(info.bufferram), //buffers | 125 | scale(&G, info.bufferram), //buffers |
131 | scale(cached) //cached | 126 | scale(&G, cached) //cached |
132 | ); | 127 | ); |
133 | /* Show alternate, more meaningful busy/free numbers by counting | 128 | /* Show alternate, more meaningful busy/free numbers by counting |
134 | * buffer cache as free memory. */ | 129 | * buffer cache as free memory. */ |
@@ -136,15 +131,15 @@ int free_main(int argc UNUSED_PARAM, char **argv IF_NOT_DESKTOP(UNUSED_PARAM)) | |||
136 | cached += info.freeram; | 131 | cached += info.freeram; |
137 | cached += info.bufferram; | 132 | cached += info.bufferram; |
138 | printf(FIELDS_2, | 133 | printf(FIELDS_2, |
139 | scale(info.totalram - cached), //used | 134 | scale(&G, info.totalram - cached), //used |
140 | scale(cached) //free | 135 | scale(&G, cached) //free |
141 | ); | 136 | ); |
142 | #if BB_MMU | 137 | #if BB_MMU |
143 | printf("Swap: "); | 138 | printf("Swap: "); |
144 | printf(FIELDS_3, | 139 | printf(FIELDS_3, |
145 | scale(info.totalswap), //total | 140 | scale(&G, info.totalswap), //total |
146 | scale(info.totalswap - info.freeswap), //used | 141 | scale(&G, info.totalswap - info.freeswap), //used |
147 | scale(info.freeswap) //free | 142 | scale(&G, info.freeswap) //free |
148 | ); | 143 | ); |
149 | #endif | 144 | #endif |
150 | return EXIT_SUCCESS; | 145 | return EXIT_SUCCESS; |