free: make it NOFORK
[oweals/busybox.git] / procps / free.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Mini free implementation for busybox
4  *
5  * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
6  *
7  * Licensed under GPLv2, see file LICENSE in this source tree.
8  */
9 //config:config FREE
10 //config:       bool "free (2.4 kb)"
11 //config:       default y
12 //config:       select PLATFORM_LINUX #sysinfo()
13 //config:       help
14 //config:       free displays the total amount of free and used physical and swap
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.
17
18 //applet:IF_FREE(APPLET_NOFORK(free, free, BB_DIR_USR_BIN, BB_SUID_DROP, free))
19
20 //kbuild:lib-$(CONFIG_FREE) += free.o
21
22 //usage:#define free_trivial_usage
23 //usage:       "" IF_DESKTOP("[-b/k/m/g]")
24 //usage:#define free_full_usage "\n\n"
25 //usage:       "Display the amount of free and used system memory"
26 //usage:
27 //usage:#define free_example_usage
28 //usage:       "$ free\n"
29 //usage:       "              total         used         free       shared      buffers\n"
30 //usage:       "  Mem:       257628       248724         8904        59644        93124\n"
31 //usage:       " Swap:       128516         8404       120112\n"
32 //usage:       "Total:       386144       257128       129016\n"
33
34 #include "libbb.h"
35 #include "common_bufsiz.h"
36 #ifdef __linux__
37 # include <sys/sysinfo.h>
38 #endif
39
40 struct globals {
41         unsigned mem_unit;
42 #if ENABLE_DESKTOP
43         uint8_t unit_steps;
44 # define G_unit_steps g->unit_steps
45 #else
46 # define G_unit_steps 10
47 #endif
48 };
49 /* Because of NOFORK, "globals" are not in global data */
50
51 static unsigned long long scale(struct globals *g, unsigned long d)
52 {
53         return ((unsigned long long)d * g->mem_unit) >> G_unit_steps;
54 }
55
56 /* NOINLINE reduces main() stack usage, which makes code smaller (on x86 at least) */
57 static NOINLINE unsigned long parse_cached_kb(void)
58 {
59         char buf[60]; /* actual lines we expect are ~30 chars or less */
60         FILE *fp;
61         unsigned long cached = 0;
62
63         fp = xfopen_for_read("/proc/meminfo");
64         while (fgets(buf, sizeof(buf), fp) != NULL) {
65                 if (sscanf(buf, "Cached: %lu %*s\n", &cached) == 1)
66                         break;
67         }
68         /* Have to close because of NOFORK */
69         fclose(fp);
70
71         return cached;
72 }
73
74 int free_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
75 int free_main(int argc UNUSED_PARAM, char **argv IF_NOT_DESKTOP(UNUSED_PARAM))
76 {
77         struct globals G;
78         struct sysinfo info;
79         unsigned long long cached;
80
81 #if ENABLE_DESKTOP
82         G.unit_steps = 10;
83         if (argv[1] && argv[1][0] == '-') {
84                 switch (argv[1][1]) {
85                 case 'b':
86                         G.unit_steps = 0;
87                         break;
88                 case 'k': /* 2^10 */
89                         /* G.unit_steps = 10; - already is */
90                         break;
91                 case 'm': /* 2^(2*10) */
92                         G.unit_steps = 20;
93                         break;
94                 case 'g': /* 2^(3*10) */
95                         G.unit_steps = 30;
96                         break;
97                 default:
98                         bb_show_usage();
99                 }
100         }
101 #endif
102         printf("       %11s%11s%11s%11s%11s%11s\n"
103         "Mem:   ",
104                 "total",
105                 "used",
106                 "free",
107                 "shared", "buffers", "cached" /* swap and total don't have these columns */
108         );
109
110         sysinfo(&info);
111         /* Kernels prior to 2.4.x will return info.mem_unit==0, so cope... */
112         G.mem_unit = (info.mem_unit ? info.mem_unit : 1);
113         /* Extract cached from /proc/meminfo and convert to mem_units */
114         cached = ((unsigned long long) parse_cached_kb() * 1024) / G.mem_unit;
115
116 #define FIELDS_6 "%11llu%11llu%11llu%11llu%11llu%11llu\n"
117 #define FIELDS_3 (FIELDS_6 + 3*6)
118 #define FIELDS_2 (FIELDS_6 + 4*6)
119
120         printf(FIELDS_6,
121                 scale(&G, info.totalram),                //total
122                 scale(&G, info.totalram - info.freeram), //used
123                 scale(&G, info.freeram),                 //free
124                 scale(&G, info.sharedram),               //shared
125                 scale(&G, info.bufferram),               //buffers
126                 scale(&G, cached)                        //cached
127         );
128         /* Show alternate, more meaningful busy/free numbers by counting
129          * buffer cache as free memory. */
130         printf("-/+ buffers/cache:");
131         cached += info.freeram;
132         cached += info.bufferram;
133         printf(FIELDS_2,
134                 scale(&G, info.totalram - cached), //used
135                 scale(&G, cached)                  //free
136         );
137 #if BB_MMU
138         printf("Swap:  ");
139         printf(FIELDS_3,
140                 scale(&G, info.totalswap),                 //total
141                 scale(&G, info.totalswap - info.freeswap), //used
142                 scale(&G, info.freeswap)                   //free
143         );
144 #endif
145         return EXIT_SUCCESS;
146 }