blob: 214c1905e89a313abe3e7e9d4b51e995875a58e0 [file] [log] [blame]
Eric Andersen6c4a6b12004-10-08 10:50:08 +00001<!--#include file="header.html" -->
2
Eric Andersen6c4a6b12004-10-08 10:50:08 +00003<h3>Frequently Asked Questions</h3>
4
5This is a collection of some of the more frequently asked questions
6about BusyBox. Some of the questions even have answers. If you
7have additions to this FAQ document, we would love to add them,
8
Rob Landleyb73d2bf2006-05-11 15:00:32 +00009<h2>General questions</h2>
Eric Andersen6c4a6b12004-10-08 10:50:08 +000010<ol>
Rob Landleyac4c92d2006-05-11 17:21:13 +000011<li><a href="#getting_started">How can I get started using BusyBox?</a></li>
12<li><a href="#configure">How do I configure busybox?</a></li>
13<li><a href="#build_system">How do I build a BusyBox-based system?</a></li>
14<li><a href="#kernel">Which Linux kernel versions are supported?</a></li>
15<li><a href="#arch">Which architectures does BusyBox run on?</a></li>
16<li><a href="#libc">Which C libraries are supported?</a></li>
17<li><a href="#commercial">Can I include BusyBox as part of the software on my device?</a></li>
18<li><a href="#external">Where can I find other small utilities since busybox does not include the features I want?</a></li></li>
19<li><a href="#demanding">I demand that you to add &lt;favorite feature&gt; right now! How come you don't answer all my questions on the mailing list instantly? I demand that you help me with all of my problems <em>Right Now</em>!</a></li>
20<li><a href="#helpme">I need help with BusyBox! What should I do?</a></li>
21<li><a href="#contracts">I need you to add &lt;favorite feature&gt;! Are the BusyBox developers willing to be paid in order to fix bugs or add in &lt;favorite feature&gt;? Are you willing to provide support contracts?</a></li>
Rob Landleyb73d2bf2006-05-11 15:00:32 +000022</ol>
Rob Landleyde7f9b72005-07-31 04:27:19 +000023
Rob Landleyb73d2bf2006-05-11 15:00:32 +000024<h2>Troubleshooting</h2>
25<ol>
26<li><a href="#bugs">I think I found a bug in BusyBox! What should I do?!</a></li>
Rob Landley95718b32006-08-16 22:13:56 +000027<li><a href="#backporting">I'm using an ancient version from the dawn of time and something's broken. Can you backport fixes for free?</a></li>
Rob Landleyb73d2bf2006-05-11 15:00:32 +000028<li><a href="#init">Busybox init isn't working!</a></li>
29<li><a href="#sed">I can't configure busybox on my system.</a></li>
30<li><a href="#job_control">Why do I keep getting "sh: can't access tty; job control turned off" errors? Why doesn't Control-C work within my shell?</a></li>
31</ol>
32
33<h2>Programming questions</h2>
34<ol>
35 <li><a href="#goals">What are the goals of busybox?</a></li>
36 <li><a href="#design">What is the design of busybox?</a></li>
37 <li><a href="#source">How is the source code organized?</a></li>
38 <ul>
39 <li><a href="#source_applets">The applet directories.</a></li>
40 <li><a href="#source_libbb">The busybox shared library (libbb)</a></li>
41 </ul>
42 <li><a href="#optimize">I want to make busybox even smaller, how do I go about it?</a></li>
43 <li><a href="#adding">Adding an applet to busybox</a></li>
44 <li><a href="#standards">What standards does busybox adhere to?</a></li>
45 <li><a href="#portability">Portability.</a></li>
46 <li><a href="#tips">Tips and tricks.</a></li>
47 <ul>
48 <li><a href="#tips_encrypted_passwords">Encrypted Passwords</a></li>
49 <li><a href="#tips_vfork">Fork and vfork</a></li>
50 <li><a href="#tips_short_read">Short reads and writes</a></li>
51 <li><a href="#tips_memory">Memory used by relocatable code, PIC, and static linking.</a></li>
52 <li><a href="#tips_kernel_headers">Including Linux kernel headers.</a></li>
53 </ul>
54 <li><a href="#who">Who are the BusyBox developers?</a></li>
55</ul>
Eric Andersen6c4a6b12004-10-08 10:50:08 +000056
57
58</ol>
59
Rob Landleyb73d2bf2006-05-11 15:00:32 +000060<h1>General questions</h1>
61
Rob Landley380ad122005-09-23 16:52:09 +000062<hr />
63<p>
64<h2><a name="getting_started">How can I get started using BusyBox?</a></h2>
65<p> If you just want to try out busybox without installing it, download the
Rob Landleyac4c92d2006-05-11 17:21:13 +000066 tarball, extract it, run "make defconfig", and then run "make".
Rob Landley380ad122005-09-23 16:52:09 +000067</p>
68<p>
Rob Landleyac4c92d2006-05-11 17:21:13 +000069 This will create a busybox binary with almost all features enabled. To try
Rob Landley380ad122005-09-23 16:52:09 +000070 out a busybox applet, type "./busybox [appletname] [options]", for
71 example "./busybox ls -l" or "./busybox cat LICENSE". Type "./busybox"
72 to see a command list, and "busybox appletname --help" to see a brief
73 usage message for a given applet.
74</p>
75<p>
76 BusyBox uses the name it was invoked under to determine which applet is
77 being invoked. (Try "mv busybox ls" and then "./ls -l".) Installing
78 busybox consists of creating symlinks (or hardlinks) to the busybox
Rob Landley3d283dd2005-11-03 22:11:00 +000079 binary for each applet in busybox, and making sure these links are in
80 the shell's command $PATH. The special applet name "busybox" (or with
81 any optional suffix, such as "busybox-static") uses the first argument
82 to determine which applet to run, as shown above.
Rob Landley380ad122005-09-23 16:52:09 +000083</p>
84<p>
Denis Vlasenkof7996f32007-01-11 17:20:00 +000085 BusyBox also has a feature called the
Bernhard Reutner-Fischer32f3ebf2006-12-10 13:40:16 +000086 <a name="standalone_shell">"standalone shell"</a>, where the busybox
Rob Landley380ad122005-09-23 16:52:09 +000087 shell runs any built-in applets before checking the command path. This
88 feature is also enabled by "make allyesconfig", and to try it out run
89 the command line "PATH= ./busybox ash". This will blank your command path
90 and run busybox as your command shell, so the only commands it can find
91 (without an explicit path such as /bin/ls) are the built-in busybox ones.
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +000092 This is another good way to see what's built into busybox.
93 Note that the standalone shell requires CONFIG_BUSYBOX_EXEC_PATH
94 to be set appropriately, depending on whether or not /proc/self/exe is
95 available or not. If you do not have /proc, then point that config option
96 to the location of your busybox binary, usually /bin/busybox.
97 (So if you set it to /proc/self/exe, and happen to be able to chroot into
98 your rootfs, you must mount /proc beforehand.)
Rob Landley380ad122005-09-23 16:52:09 +000099</p>
Bernhard Reutner-Fischer32f3ebf2006-12-10 13:40:16 +0000100<p>
101 A typical indication that you set CONFIG_BUSYBOX_EXEC_PATH to proc but
102 forgot to mount proc is:
103<pre>
104$ /bin/echo $PATH
105/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/bin/X11
106$ echo $PATH
107/bin/sh: echo: not found
108</pre>
Rob Landleyac4c92d2006-05-11 17:21:13 +0000109<hr />
110<p>
111<h2><a name="configure">How do I configure busybox?</a></h2>
112<p> Busybox is configured similarly to the linux kernel. Create a default
113 configuration and then run "make menuconfig" to modify it. The end
114 result is a .config file that tells the busybox build process what features
115 to include. So instead of "./configure; make; make install" the equivalent
116 busybox build would be "make defconfig; make; make install".
117</p>
118
119<p> Busybox configured with all features enabled is a little under a megabyte
120 dynamically linked on x86. To create a smaller busybox, configure it with
121 fewer features. Individual busybox applets cost anywhere from a few
122 hundred bytes to tens of kilobytes. Disable unneeded applets to save,
123 space, using menuconfig.
124</p>
125
126<p>The most important busybox configurators are:</p>
127
128<ul>
129<li><p>make <b>defconfig</b> - Create the maximum "sane" configuration. This
130enables almost all features, minus things like debugging options and features
131that require changes to the rest of the system to work (such as selinux or
132devfs device names). Use this if you want to start from a full-featured
133busybox and remove features until it's small enough.</p></li>
134<li><p>make <b>allnoconfig</b> - Disable everything. This creates a tiny version
135of busybox that doesn't do anything. Start here if you know exactly what
136you want and would like to select only those features.</p></li>
137<li><p>make <b>menuconfig</b> - Interactively modify a .config file through a
138multi-level menu interface. Use this after one of the previous two.</p></li>
139</ul>
140
141<p>Some other configuration options are:</p>
142<ul>
143<li><p>make <b>oldconfig</b> - Update an old .config file for a newer version
144of busybox.</p></li>
145<li><p>make <b>allyesconfig</b> - Select absolutely everything. This creates
146a statically linked version of busybox full of debug code, with dependencies on
147selinux, using devfs names... This makes sure everything compiles. Whether
148or not the result would do anything useful is an open question.</p></li>
149<li><p>make <b>allbareconfig</b> - Select all applets but disable all sub-features
150within each applet. More build coverage testing.</p></li>
151<li><p>make <b>randconfig</b> - Create a random configuration for test purposes.</p></li>
152</ul>
153
154<p> Menuconfig modifies your .config file through an interactive menu where you can enable or disable
155 busybox features, and get help about each feature.
156
157
158
Rob Landley380ad122005-09-23 16:52:09 +0000159<p>
160 To build a smaller busybox binary, run "make menuconfig" and disable the
161 features you don't need. (Or run "make allnoconfig" and then use
162 menuconfig to add just the features you need. Don't forget to recompile
163 with "make" once you've finished configuring.)
164</p>
165<hr/>
166<p/>
167<h2><a name="build_system">How do I build a BusyBox-based system?</a></h2>
168<p>
169 BusyBox is a package that replaces a dozen standard packages, but it is
170 not by itself a complete bootable system. Building an entire Linux
171 distribution from source is a bit beyond the scope of this FAQ, but it
172 understandably keeps cropping up on the mailing list, so here are some
173 pointers.
174</p>
175<p>
176 Start by learning how to strip a working system down to the bare essentials
177 needed to run one or two commands, so you know what it is you actually
178 need. An excellent practical place to do
179 this is the <a href="http://www.tldp.org/HOWTO/Bootdisk-HOWTO/">Linux
180 BootDisk Howto</a>, or for a more theoretical approach try
181 <a href="http://www.tldp.org/HOWTO/From-PowerUp-To-Bash-Prompt-HOWTO.html">From
182 PowerUp to Bash Prompt</a>.
183</p>
184<p>
185 To learn how to build a working Linux system entirely from source code,
186 the place to go is the <a href="http://www.linuxfromscratch.org">Linux
187 From Scratch</a> project. They have an entire book of step-by-step
188 instructions you can
189 <a href="http://www.linuxfromscratch.org/lfs/view/stable/">read online</a>
190 or
191 <a href="http://www.linuxfromscratch.org/lfs/downloads/stable/">download</a>.
192 Be sure to check out the other sections of their main page, including
193 Beyond Linux From Scratch, Hardened Linux From Scratch, their Hints
194 directory, and their LiveCD project. (They also have mailing lists which
195 are better sources of answers to Linux-system building questions than
196 the busybox list.)
197</p>
198<p>
199 If you want an automated yet customizable system builder which produces
200 a BusyBox and uClibc based system, try
201 <a href="http://buildroot.uclibc.org">buildroot</a>, which is
Rob Landleya253e732006-02-14 08:29:48 +0000202 another project by the maintainer of the uClibc (Erik Andersen).
203 Download the tarball, extract it, unset CC, make.
Rob Landley380ad122005-09-23 16:52:09 +0000204 For more instructions, see the website.
205</p>
206
Rob Landleyd48633f2006-03-09 18:03:21 +0000207<hr />
208<p>
Eric Andersen6c4a6b12004-10-08 10:50:08 +0000209<h2><a name="kernel">Which Linux kernel versions are supported?</a></h2>
210<p>
Rob Landley380ad122005-09-23 16:52:09 +0000211 Full functionality requires Linux 2.4.x or better. (Earlier versions may
212 still work, but are no longer regularly tested.) A large fraction of the
Eric Andersen6c4a6b12004-10-08 10:50:08 +0000213 code should run on just about anything. While the current code is fairly
214 Linux specific, it should be fairly easy to port the majority of the code
215 to support, say, FreeBSD or Solaris, or Mac OS X, or even Windows (if you
216 are into that sort of thing).
Rob Landley380ad122005-09-23 16:52:09 +0000217</p>
Eric Andersen6c4a6b12004-10-08 10:50:08 +0000218<hr />
219<p>
220<h2><a name="arch">Which architectures does BusyBox run on?</a></h2>
221<p>
Eric Andersen6c4a6b12004-10-08 10:50:08 +0000222 BusyBox in general will build on any architecture supported by gcc.
Rob Landley380ad122005-09-23 16:52:09 +0000223 Kernel module loading for 2.4 Linux kernels is currently
Eric Andersen6c4a6b12004-10-08 10:50:08 +0000224 limited to ARM, CRIS, H8/300, x86, ia64, x86_64, m68k, MIPS, PowerPC,
225 S390, SH3/4/5, Sparc, v850e, and x86_64 for 2.4.x kernels.
Rob Landley380ad122005-09-23 16:52:09 +0000226</p>
227<p>
Eric Andersen6c4a6b12004-10-08 10:50:08 +0000228 With 2.6.x kernels, module loading support should work on all architectures.
Rob Landley380ad122005-09-23 16:52:09 +0000229</p>
Eric Andersen6c4a6b12004-10-08 10:50:08 +0000230<hr />
231<p>
232<h2><a name="libc">Which C libraries are supported?</a></h2>
233<p>
Rob Landley380ad122005-09-23 16:52:09 +0000234 On Linux, BusyBox releases are tested against uClibc (0.9.27 or later) and
235 glibc (2.2 or later). Both should provide full functionality with busybox,
236 and if you find a bug we want to hear about it.
237</p>
Mike Frysingerd505e3e2005-10-29 08:03:54 +0000238<p>
Rob Landley380ad122005-09-23 16:52:09 +0000239 Linux-libc5 is no longer maintained (and has no known advantages over
240 uClibc), dietlibc is known to have numerous unfixed bugs, and klibc is
241 missing too many features to build BusyBox. If you require a small C
242 library for Linux, the busybox developers recommend uClibc.
243</p>
244<p>
245 Some BusyBox applets have been built and run under a combination
246 of newlib and libgloss (see
247 <a href="http://www.busybox.net/lists/busybox/2005-March/013759.html">this thread</a>).
248 This is still experimental, but may be supported in a future release.
249</p>
Rob Landleyb73d2bf2006-05-11 15:00:32 +0000250
Eric Andersen6c4a6b12004-10-08 10:50:08 +0000251<hr />
252<p>
Mike Frysinger86097b32005-09-15 01:37:36 +0000253<h2><a name="commercial">Can I include BusyBox as part of the software on my device?</a></h2>
Rob Landleyb73d2bf2006-05-11 15:00:32 +0000254<p>
Eric Andersen6c4a6b12004-10-08 10:50:08 +0000255
Rob Landleyb73d2bf2006-05-11 15:00:32 +0000256<p>
Eric Andersen6c4a6b12004-10-08 10:50:08 +0000257 Yes. As long as you <a href="http://busybox.net/license.html">fully comply
258 with the generous terms of the GPL BusyBox license</a> you can ship BusyBox
259 as part of the software on your device.
Rob Landleyb73d2bf2006-05-11 15:00:32 +0000260</p>
Eric Andersen6c4a6b12004-10-08 10:50:08 +0000261
262<hr />
263<p>
Denis Vlasenkof7996f32007-01-11 17:20:00 +0000264<h2><a name="external">where can i find other small utilities since busybox
Rob Landleyb73d2bf2006-05-11 15:00:32 +0000265 does not include the features i want?</a></h2>
Eric Andersen6c4a6b12004-10-08 10:50:08 +0000266<p>
Rob Landleyb73d2bf2006-05-11 15:00:32 +0000267 we maintain such a <a href="tinyutils.html">list</a> on this site!
268</p>
Eric Andersen6c4a6b12004-10-08 10:50:08 +0000269
270<hr />
271<p>
Rob Landleyb73d2bf2006-05-11 15:00:32 +0000272<h2><a name="demanding">I demand that you to add &lt;favorite feature&gt; right now! How come you don't answer all my questions on the mailing list instantly? I demand that you help me with all of my problems <em>Right Now</em>!</a></h2>
Eric Andersen6c4a6b12004-10-08 10:50:08 +0000273<p>
274
275 You have not paid us a single cent and yet you still have the product of
276 many years of our work. We are not your slaves! We work on BusyBox
277 because we find it useful and interesting. If you go off flaming us, we
278 will ignore you.
279
280
281<hr />
282<p>
283<h2><a name="helpme">I need help with BusyBox! What should I do?</a></h2>
284<p>
285
286 If you find that you need help with BusyBox, you can ask for help on the
Rob Landleyb73d2bf2006-05-11 15:00:32 +0000287 BusyBox mailing list at busybox@busybox.net.</p>
Eric Andersen6c4a6b12004-10-08 10:50:08 +0000288
Rob Landleyb73d2bf2006-05-11 15:00:32 +0000289<p> In addition to the mailing list, Erik Andersen (andersee), Manuel Nova
290 (mjn3), Rob Landley (landley), Mike Frysinger (SpanKY), Bernhard Fischer
291 (blindvt), and other long-time BusyBox developers are known to hang out
292 on the uClibc IRC channel: #uclibc on irc.freenode.net. There is a
293 <a href="http://ibot.Rikers.org/%23uclibc/">web archive of
294 daily logs of the #uclibc IRC channel</a> going back to 2002.
295</p>
Eric Andersen6c4a6b12004-10-08 10:50:08 +0000296
Rob Landleyb73d2bf2006-05-11 15:00:32 +0000297<p>
Rob Landleya253e732006-02-14 08:29:48 +0000298 <b>Please do not send private email to Rob, Erik, Manuel, or the other
299 BusyBox contributors asking for private help unless you are planning on
300 paying for consulting services.</b>
Rob Landleyb73d2bf2006-05-11 15:00:32 +0000301</p>
Eric Andersen6c4a6b12004-10-08 10:50:08 +0000302
Rob Landleyb73d2bf2006-05-11 15:00:32 +0000303<p>
Eric Andersen6c4a6b12004-10-08 10:50:08 +0000304 When we answer questions on the BusyBox mailing list, it helps everyone
305 since people with similar problems in the future will be able to get help
306 by searching the mailing list archives. Private help is reserved as a paid
307 service. If you need to use private communication, or if you are serious
308 about getting timely assistance with BusyBox, you should seriously consider
309 paying for consulting services.
Rob Landleyb73d2bf2006-05-11 15:00:32 +0000310</p>
Eric Andersen6c4a6b12004-10-08 10:50:08 +0000311
312<hr />
313<p>
Rob Landleyb73d2bf2006-05-11 15:00:32 +0000314<h2><a name="contracts">I need you to add &lt;favorite feature&gt;! Are the BusyBox developers willing to be paid in order to fix bugs or add in &lt;favorite feature&gt;? Are you willing to provide support contracts?</a></h2>
315</p>
316
Eric Andersen6c4a6b12004-10-08 10:50:08 +0000317<p>
Rob Landleyb73d2bf2006-05-11 15:00:32 +0000318 Yes we are. The easy way to sponsor a new feature is to post an offer on
319 the mailing list to see who's interested. You can also email the project's
320 maintainer and ask them to recommend someone.
321</p>
Eric Andersen6c4a6b12004-10-08 10:50:08 +0000322
Rob Landleyb73d2bf2006-05-11 15:00:32 +0000323<p> If you prefer to deal with an organization rather than an individual, Rob
324 Landley (the current BusyBox maintainer) works for
325 <a http://www.timesys.com>TimeSys</a>, and Eric Andersen (the previous
326 busybox maintainer and current uClibc maintainer) owns
327 <a href="http://codepoet-consulting.com/">CodePoet Consulting</a>. Both
328 companies offer support contracts and handle new development, and there
329 are plenty of other companies that do the same.
330</p>
Eric Andersen6c4a6b12004-10-08 10:50:08 +0000331
332
Rob Landleyb73d2bf2006-05-11 15:00:32 +0000333
334
335<h1>Troubleshooting</h1>
336
337<hr />
338<p></p>
339<h2><a name="bugs">I think I found a bug in BusyBox! What should I do?</a></h2>
340<p></p>
341
342<p>
343 If you simply need help with using or configuring BusyBox, please submit a
344 detailed description of your problem to the BusyBox mailing list at <a
345 href="mailto:busybox@busybox.net"> busybox@busybox.net</a>.
346 Please do not send email to individual developers asking
347 for private help unless you are planning on paying for consulting services.
348 When we answer questions on the BusyBox mailing list, it helps everyone,
349 while private answers help only you...
350</p>
351
352<p>
Rob Landley95718b32006-08-16 22:13:56 +0000353 Bug reports and new feature patches sometimes get lost when posted to the
354 mailing list, because the developers of BusyBox are busy people and have
355 only so much they can keep in their brains at a time. You can post a
356 polite reminder after 2-3 days without offending anybody. If that doesn't
357 result in a solution, please use the
358 <a href="http://bugs.busybox.net/">BusyBox Bug
359 and Patch Tracking System</a> to submit a detailed explanation and we'll
360 get to it as soon as we can.
Rob Landleyb73d2bf2006-05-11 15:00:32 +0000361</p>
362
Rob Landley95718b32006-08-16 22:13:56 +0000363<p>
364 Note that bugs entered into the bug system without being mentioned on the
365 mailing list first may languish there for months before anyone even notices
366 them. We generally go through the bug system when preparing for new
367 development releases, to see what fell through the cracks while we were
368 off writing new features. (It's a fast/unreliable vs slow/reliable thing.
369 Saves retransits, but the latency sucks.)
370</p>
371
372<hr />
373<p></p>
374<h2><a name="backporting">I'm using an ancient version from the dawn of time and something's broken. Can you backport fixes for free?</h2>
375
376<p>Variants of this one get asked a lot.</p>
377
378<p>The purpose of the BusyBox mailing list is to develop and improve BusyBox,
379and we're happy to respond to our users' needs. But if you're coming to the
380list for free tech support we're going to ask you to upgrade to a current
381version before we try to diagnose your problem.</p>
382
383<p>If you're building BusyBox 0.50 with uClibc 0.9.19 and gcc 0.9.26 there's a
384fairly large chance that whatever problem you're seeing has already been fixed.
385To get that fix, all you have to do is upgrade to a newer version. If you
386don't at least _try_ that, you're wasting our time.</p>
387
388<p>The volunteers are happy to fix any bugs you point out in the current
389versions because doing so helps everybody and makes the project better. We
390want to make the current version work for you. But diagnosing, debugging, and
391backporting fixes to old versions isn't something we do for free, because it
392doesn't help anybody but you. The cost of volunteer tech support is using a
393reasonably current version of the project.</p>
394
395<p>If you don't want to upgrade, you have the complete source code and thus
396the ability to fix it yourself, or hire a consultant to do it for you. If you
397got your version from a vendor who still supports the older version, they can
398help you. But there are limits as to what the volunteers will feel obliged to
399do for you.</p>
400
401<p>As a rule of thumb, volunteers will generally answer polite questions about
402a given version for about three years after its release before it's so old
403we don't remember the answer off the top of our head. And if you want us to
404put any _effort_ into tracking it down, we want you to put in a little effort
405of your own by confirming it's still a problem with the current version. It's
406also hard for us to fix a problem of yours if we can't reproduce it because
407we don't have any systems running an environment that old.</p>
408
409<p>A consultant will happily set up a special environment just to reproduce
410your problem, and you can always ask on the list if any of the developers
411have consulting rates.</p>
412
Eric Andersen6c4a6b12004-10-08 10:50:08 +0000413<hr />
414<p>
Rob Landleyb73d2bf2006-05-11 15:00:32 +0000415<h2><a name="init">Busybox init isn't working!</a></h2>
Mike Frysinger77dbe732005-04-17 04:32:22 +0000416<p>
Rob Landleyc5e4ab72006-06-29 04:59:11 +0000417 Init is the first program that runs, so it might be that no programs are
418 working on your new system because of a problem with your cross-compiler,
419 kernel, console settings, shared libraries, root filesystem... To rule all
420 that out, first build a statically linked version of the following "hello
421 world" program with your cross compiler toolchain:
Rob Landleyb73d2bf2006-05-11 15:00:32 +0000422</p>
423<pre>
424#include &lt;stdio.h&gt;
Mike Frysinger77dbe732005-04-17 04:32:22 +0000425
Rob Landleyb73d2bf2006-05-11 15:00:32 +0000426int main(int argc, char *argv)
427{
428 printf("Hello world!\n");
429 sleep(999999999);
430}
431</pre>
432
433<p>
434 Now try to boot your device with an "init=" argument pointing to your
435 hello world program. Did you see the hello world message? Until you
436 do, don't bother messing with busybox init.
437</p>
438
439<p>
440 Once you've got it working statically linked, try getting it to work
441 dynamically linked. Then read the FAQ entry <a href="#build_system">How
Rob Landleyc5e4ab72006-06-29 04:59:11 +0000442 do I build a BusyBox-based system?</a>, and the
443 <a href="/downloads/BusyBox.html#item_init">documentation for BusyBox
444 init</a>.
Rob Landleyb73d2bf2006-05-11 15:00:32 +0000445</p>
Mike Frysinger77dbe732005-04-17 04:32:22 +0000446
447<hr />
448<p>
Rob Landleyb73d2bf2006-05-11 15:00:32 +0000449<h2><a name="sed">I can't configure busybox on my system.</a></h2>
450<p>
451 Configuring Busybox depends on a recent version of sed. Older
452 distributions (Red Hat 7.2, Debian 3.0) may not come with a
453 usable version. Luckily BusyBox can use its own sed to configure itself,
454 although this leads to a bit of a chicken and egg problem.
455 You can work around this by hand-configuring busybox to build with just
456 sed, then putting that sed in your path to configure the rest of busybox
457 with, like so:
458</p>
459
460<pre>
461 tar xvjf sources/busybox-x.x.x.tar.bz2
462 cd busybox-x.x.x
463 make allnoconfig
464 make include/bb_config.h
465 echo "CONFIG_SED=y" >> .config
466 echo "#undef ENABLE_SED" >> include/bb_config.h
467 echo "#define ENABLE_SED 1" >> include/bb_config.h
468 make
469 mv busybox sed
470 export PATH=`pwd`:"$PATH"
471</pre>
472
473<p>Then you can run "make defconfig" or "make menuconfig" normally.</p>
474
475<hr />
476<p>
477<h2><a name="job_control">Why do I keep getting "sh: can't access tty; job control turned off" errors? Why doesn't Control-C work within my shell?</a></h2>
Eric Andersen6c4a6b12004-10-08 10:50:08 +0000478<p>
479
Rob Landleyb73d2bf2006-05-11 15:00:32 +0000480 Job control will be turned off since your shell can not obtain a controlling
481 terminal. This typically happens when you run your shell on /dev/console.
482 The kernel will not provide a controlling terminal on the /dev/console
483 device. Your should run your shell on a normal tty such as tty1 or ttyS0
484 and everything will work perfectly. If you <em>REALLY</em> want your shell
485 to run on /dev/console, then you can hack your kernel (if you are into that
486 sortof thing) by changing drivers/char/tty_io.c to change the lines where
487 it sets "noctty = 1;" to instead set it to "0". I recommend you instead
488 run your shell on a real console...
489</p>
Eric Andersen6c4a6b12004-10-08 10:50:08 +0000490
Rob Landleyb73d2bf2006-05-11 15:00:32 +0000491<h1>Development</h1>
Eric Andersen6c4a6b12004-10-08 10:50:08 +0000492
Rob Landleyb73d2bf2006-05-11 15:00:32 +0000493<h2><b><a name="goals">What are the goals of busybox?</a></b></h2>
494
495<p>Busybox aims to be the smallest and simplest correct implementation of the
496standard Linux command line tools. First and foremost, this means the
497smallest executable size we can manage. We also want to have the simplest
498and cleanest implementation we can manage, be <a href="#standards">standards
499compliant</a>, minimize run-time memory usage (heap and stack), run fast, and
500take over the world.</p>
501
502<h2><b><a name="design">What is the design of busybox?</a></b></h2>
503
504<p>Busybox is like a swiss army knife: one thing with many functions.
505The busybox executable can act like many different programs depending on
506the name used to invoke it. Normal practice is to create a bunch of symlinks
507pointing to the busybox binary, each of which triggers a different busybox
508function. (See <a href="FAQ.html#getting_started">getting started</a> in the
509FAQ for more information on usage, and <a href="BusyBox.html">the
510busybox documentation</a> for a list of symlink names and what they do.)
511
512<p>The "one binary to rule them all" approach is primarily for size reasons: a
513single multi-purpose executable is smaller then many small files could be.
514This way busybox only has one set of ELF headers, it can easily share code
515between different apps even when statically linked, it has better packing
516efficiency by avoding gaps between files or compression dictionary resets,
517and so on.</p>
518
519<p>Work is underway on new options such as "make standalone" to build separate
520binaries for each applet, and a "libbb.so" to make the busybox common code
521available as a shared library. Neither is ready yet at the time of this
522writing.</p>
523
524<a name="source"></a>
525
526<h2><a name="source_applets"><b>The applet directories</b></a></h2>
527
528<p>The directory "applets" contains the busybox startup code (applets.c and
529busybox.c), and several subdirectories containing the code for the individual
530applets.</p>
531
532<p>Busybox execution starts with the main() function in applets/busybox.c,
Denis Vlasenko8f8f2682006-10-03 21:00:43 +0000533which sets the global variable applet_name to argv[0] and calls
Denis Vlasenkoe4f2d062007-04-11 17:03:19 +0000534run_applet_and_exit() in applets/applets.c. That uses the applets[] array
Rob Landleyb73d2bf2006-05-11 15:00:32 +0000535(defined in include/busybox.h and filled out in include/applets.h) to
536transfer control to the appropriate APPLET_main() function (such as
537cat_main() or sed_main()). The individual applet takes it from there.</p>
538
539<p>This is why calling busybox under a different name triggers different
540functionality: main() looks up argv[0] in applets[] to get a function pointer
541to APPLET_main().</p>
542
543<p>Busybox applets may also be invoked through the multiplexor applet
544"busybox" (see busybox_main() in applets/busybox.c), and through the
545standalone shell (grep for STANDALONE_SHELL in applets/shell/*.c).
546See <a href="FAQ.html#getting_started">getting started</a> in the
547FAQ for more information on these alternate usage mechanisms, which are
548just different ways to reach the relevant APPLET_main() function.</p>
549
550<p>The applet subdirectories (archival, console-tools, coreutils,
551debianutils, e2fsprogs, editors, findutils, init, loginutils, miscutils,
552modutils, networking, procps, shell, sysklogd, and util-linux) correspond
553to the configuration sub-menus in menuconfig. Each subdirectory contains the
554code to implement the applets in that sub-menu, as well as a Config.in
555file defining that configuration sub-menu (with dependencies and help text
556for each applet), and the makefile segment (Makefile.in) for that
557subdirectory.</p>
558
559<p>The run-time --help is stored in usage_messages[], which is initialized at
560the start of applets/applets.c and gets its help text from usage.h. During the
561build this help text is also used to generate the BusyBox documentation (in
562html, txt, and man page formats) in the docs directory. See
563<a href="#adding">adding an applet to busybox</a> for more
564information.</p>
565
566<h2><a name="source_libbb"><b>libbb</b></a></h2>
567
568<p>Most non-setup code shared between busybox applets lives in the libbb
569directory. It's a mess that evolved over the years without much auditing
570or cleanup. For anybody looking for a great project to break into busybox
571development with, documenting libbb would be both incredibly useful and good
572experience.</p>
573
574<p>Common themes in libbb include allocation functions that test
575for failure and abort the program with an error message so the caller doesn't
576have to test the return value (xmalloc(), xstrdup(), etc), wrapped versions
577of open(), close(), read(), and write() that test for their own failures
578and/or retry automatically, linked list management functions (llist.c),
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000579command line argument parsing (getopt32.c), and a whole lot more.</p>
Eric Andersen6c4a6b12004-10-08 10:50:08 +0000580
581<hr />
Rob Landleyc7a3e1b2005-07-31 04:25:00 +0000582<p>
583<h2><a name="optimize">I want to make busybox even smaller, how do I go about it?</a></h2>
584<p>
585 To conserve bytes it's good to know where they're being used, and the
586 size of the final executable isn't always a reliable indicator of
587 the size of the components (since various structures are rounded up,
588 so a small change may not even be visible by itself, but many small
589 savings add up).
590</p>
Rob Landleyb73d2bf2006-05-11 15:00:32 +0000591
592<p> The busybox Makefile builds two versions of busybox, one of which
593 (busybox_unstripped) has extra information that various analysis tools
594 can use. (This has nothing to do with CONFIG_DEBUG, leave that off
595 when trying to optimize for size.)
596</p>
597
598<p> The <b>"make bloatcheck"</b> option uses Matt Mackall's bloat-o-meter
599 script to compare two versions of busybox (busybox_unstripped vs
600 busybox_old), and report which symbols changed size and by how much.
Rob Landleyd244bc12006-05-27 21:30:34 +0000601 To use it, first build a base version with <b>"make baseline"</b>.
602 (This creates busybox_old, which should have the original sizes for
603 comparison purposes.) Then build the new version with your changes
604 and run "make bloatcheck" to see the size differences from the old
605 version.
Rob Landleyb73d2bf2006-05-11 15:00:32 +0000606</p>
Rob Landleyc7a3e1b2005-07-31 04:25:00 +0000607<p>
Rob Landleyb73d2bf2006-05-11 15:00:32 +0000608 The first line of output has totals: how many symbols were added or
609 removed, how many symbols grew or shrank, the number of bytes added
610 and number of bytes removed by these changes, and finally the total
611 number of bytes difference between the two files. The remaining
612 lines show each individual symbol, the old and new sizes, and the
613 increase or decrease in size (which results are sorted by).
614</p>
615<p>
616 The <b>"make sizes"</b> option produces raw symbol size information for
617 busybox_unstripped. This is the output from the "nm --size-sort"
618 command (see "man nm" for more information), and is the information
619 bloat-o-meter parses to produce the comparison report above. For
620 defconfig, this is a good way to find the largest symbols in the tree
621 (which is a good place to start when trying to shrink the code). To
622 take a closer look at individual applets, configure busybox with just
623 one applet (run "make allnoconfig" and then switch on a single applet
624 with menuconfig), and then use "make sizes" to see the size of that
625 applet's components.
626</p>
627<p>
628 The "showasm" command (in the scripts directory) produces an assembly
629 dump of a function, providing a closer look at what changed. Try
630 "scripts/showasm busybox_unstripped" to list available symbols, and
631 "scripts/showasm busybox_unstripped symbolname" to see the assembly
632 for a sepecific symbol.
Rob Landleyc7a3e1b2005-07-31 04:25:00 +0000633</p>
634<hr />
Eric Andersen6c4a6b12004-10-08 10:50:08 +0000635
Rob Landleyc7a3e1b2005-07-31 04:25:00 +0000636
637
Rob Landleyb73d2bf2006-05-11 15:00:32 +0000638<h2><a name="adding"><b>Adding an applet to busybox</b></a></h2>
639
640<p>To add a new applet to busybox, first pick a name for the applet and
641a corresponding CONFIG_NAME. Then do this:</p>
642
643<ul>
644<li>Figure out where in the busybox source tree your applet best fits,
645and put your source code there. Be sure to use APPLET_main() instead
646of main(), where APPLET is the name of your applet.</li>
647
648<li>Add your applet to the relevant Config.in file (which file you add
649it to determines where it shows up in "make menuconfig"). This uses
650the same general format as the linux kernel's configuration system.</li>
651
652<li>Add your applet to the relevant Makefile.in file (in the same
653directory as the Config.in you chose), using the existing entries as a
654template and the same CONFIG symbol as you used for Config.in. (Don't
655forget "needlibm" or "needcrypt" if your applet needs libm or
656libcrypt.)</li>
657
658<li>Add your applet to "include/applets.h", using one of the existing
659entries as a template. (Note: this is in alphabetical order. Applets
660are found via binary search, and if you add an applet out of order it
661won't work.)</li>
662
663<li>Add your applet's runtime help text to "include/usage.h". You need
664at least appname_trivial_usage (the minimal help text, always included
665in the busybox binary when this applet is enabled) and appname_full_usage
666(extra help text included in the busybox binary with
667CONFIG_FEATURE_VERBOSE_USAGE is enabled), or it won't compile.
668The other two help entry types (appname_example_usage and
669appname_notes_usage) are optional. They don't take up space in the binary,
670but instead show up in the generated documentation (BusyBox.html,
671BusyBox.txt, and the man page BusyBox.1).</li>
672
673<li>Run menuconfig, switch your applet on, compile, test, and fix the
674bugs. Be sure to try both "allyesconfig" and "allnoconfig" (and
675"allbareconfig" if relevant).</li>
676
677</ul>
678
679<h2><a name="standards">What standards does busybox adhere to?</a></h2>
680
681<p>The standard we're paying attention to is the "Shell and Utilities"
682portion of the <a href="http://www.opengroup.org/onlinepubs/009695399/">Open
683Group Base Standards</a> (also known as the Single Unix Specification version
6843 or SUSv3). Note that paying attention isn't necessarily the same thing as
685following it.</p>
686
687<p>SUSv3 doesn't even mention things like init, mount, tar, or losetup, nor
688commonly used options like echo's '-e' and '-n', or sed's '-i'. Busybox is
689driven by what real users actually need, not the fact the standard believes
690we should implement ed or sccs. For size reasons, we're unlikely to include
691much internationalization support beyond UTF-8, and on top of all that, our
692configuration menu lets developers chop out features to produce smaller but
693very non-standard utilities.</p>
694
695<p>Also, Busybox is aimed primarily at Linux. Unix standards are interesting
696because Linux tries to adhere to them, but portability to dozens of platforms
697is only interesting in terms of offering a restricted feature set that works
698everywhere, not growing dozens of platform-specific extensions. Busybox
699should be portable to all hardware platforms Linux supports, and any other
700similar operating systems that are easy to do and won't require much
701maintenance.</p>
702
703<p>In practice, standards compliance tends to be a clean-up step once an
704applet is otherwise finished. When polishing and testing a busybox applet,
705we ensure we have at least the option of full standards compliance, or else
706document where we (intentionally) fall short.</p>
707
708<h2><a name="portability">Portability.</a></h2>
709
710<p>Busybox is a Linux project, but that doesn't mean we don't have to worry
711about portability. First of all, there are different hardware platforms,
712different C library implementations, different versions of the kernel and
713build toolchain... The file "include/platform.h" exists to centralize and
714encapsulate various platform-specific things in one place, so most busybox
715code doesn't have to care where it's running.</p>
716
717<p>To start with, Linux runs on dozens of hardware platforms. We try to test
718each release on x86, x86-64, arm, power pc, and mips. (Since qemu can handle
719all of these, this isn't that hard.) This means we have to care about a number
720of portability issues like endianness, word size, and alignment, all of which
721belong in platform.h. That header handles conditional #includes and gives
722us macros we can use in the rest of our code. At some point in the future
723we might grow a platform.c, possibly even a platform subdirectory. As long
724as the applets themselves don't have to care.</p>
725
726<p>On a related note, we made the "default signedness of char varies" problem
727go away by feeding the compiler -funsigned-char. This gives us consistent
728behavior on all platforms, and defaults to 8-bit clean text processing (which
729gets us halfway to UTF-8 support). NOMMU support is less easily separated
730(see the tips section later in this document), but we're working on it.</p>
731
732<p>Another type of portability is build environments: we unapologetically use
733a number of gcc and glibc extensions (as does the Linux kernel), but these have
734been picked up by packages like uClibc, TCC, and Intel's C Compiler. As for
735gcc, we take advantage of newer compiler optimizations to get the smallest
736possible size, but we also regression test against an older build environment
737using the Red Hat 9 image at "http://busybox.net/downloads/qemu". This has a
7382.4 kernel, gcc 3.2, make 3.79.1, and glibc 2.3, and is the oldest
739build/deployment environment we still put any effort into maintaining. (If
740anyone takes an interest in older kernels you're welcome to submit patches,
741but the effort would probably be better spent
742<a href="http://www.selenic.com/linux-tiny/">trimming
743down the 2.6 kernel</a>.) Older gcc versions than that are uninteresting since
744we now use c99 features, although
745<a href="http://fabrice.bellard.free.fr/tcc/">tcc</a> might be worth a
746look.</p>
747
748<p>We also test busybox against the current release of uClibc. Older versions
749of uClibc aren't very interesting (they were buggy, and uClibc wasn't really
750usable as a general-purpose C library before version 0.9.26 anyway).</p>
751
752<p>Other unix implementations are mostly uninteresting, since Linux binaries
753have become the new standard for portable Unix programs. Specifically,
754the ubiquity of Linux was cited as the main reason the Intel Binary
755Compatability Standard 2 died, by the standards group organized to name a
756successor to ibcs2: <a href="http://www.telly.org/86open/">the 86open
757project</a>. That project disbanded in 1999 with the endorsement of an
758existing standard: Linux ELF binaries. Since then, the major players at the
759time (such as <a
760href=http://www-03.ibm.com/servers/aix/products/aixos/linux/index.html>AIX</a>, <a
761href=http://www.sun.com/software/solaris/ds/linux_interop.jsp#3>Solaris</a>, and
762<a href=http://www.onlamp.com/pub/a/bsd/2000/03/17/linuxapps.html>FreeBSD</a>)
763have all either grown Linux support or folded.</p>
764
765<p>The major exceptions are newcomer MacOS X, some embedded environments
766(such as newlib+libgloss) which provide a posix environment but not a full
767Linux environment, and environments like Cygwin that provide only partial Linux
768emulation. Also, some embedded Linux systems run a Linux kernel but amputate
769things like the /proc directory to save space.</p>
770
771<p>Supporting these systems is largely a question of providing a clean subset
772of BusyBox's functionality -- whichever applets can easily be made to
773work in that environment. Annotating the configuration system to
774indicate which applets require which prerequisites (such as procfs) is
775also welcome. Other efforts to support these systems (swapping #include
776files to build in different environments, adding adapter code to platform.h,
777adding more extensive special-case supporting infrastructure such as mount's
778legacy mtab support) are handled on a case-by-case basis. Support that can be
779cleanly hidden in platform.h is reasonably attractive, and failing that
780support that can be cleanly separated into a separate conditionally compiled
781file is at least worth a look. Special-case code in the body of an applet is
782something we're trying to avoid.</p>
783
784<h2><a name="tips" />Programming tips and tricks.</a></h2>
785
786<p>Various things busybox uses that aren't particularly well documented
787elsewhere.</p>
788
789<h2><a name="tips_encrypted_passwords">Encrypted Passwords</a></h2>
790
791<p>Password fields in /etc/passwd and /etc/shadow are in a special format.
792If the first character isn't '$', then it's an old DES style password. If
793the first character is '$' then the password is actually three fields
794separated by '$' characters:</p>
795<pre>
796 <b>$type$salt$encrypted_password</b>
797</pre>
798
799<p>The "type" indicates which encryption algorithm to use: 1 for MD5 and 2 for SHA1.</p>
800
801<p>The "salt" is a bunch of ramdom characters (generally 8) the encryption
802algorithm uses to perturb the password in a known and reproducible way (such
803as by appending the random data to the unencrypted password, or combining
804them with exclusive or). Salt is randomly generated when setting a password,
805and then the same salt value is re-used when checking the password. (Salt is
806thus stored unencrypted.)</p>
807
808<p>The advantage of using salt is that the same cleartext password encrypted
809with a different salt value produces a different encrypted value.
810If each encrypted password uses a different salt value, an attacker is forced
811to do the cryptographic math all over again for each password they want to
812check. Without salt, they could simply produce a big dictionary of commonly
813used passwords ahead of time, and look up each password in a stolen password
814file to see if it's a known value. (Even if there are billions of possible
815passwords in the dictionary, checking each one is just a binary search against
816a file only a few gigabytes long.) With salt they can't even tell if two
817different users share the same password without guessing what that password
818is and decrypting it. They also can't precompute the attack dictionary for
819a specific password until they know what the salt value is.</p>
820
821<p>The third field is the encrypted password (plus the salt). For md5 this
822is 22 bytes.</p>
823
824<p>The busybox function to handle all this is pw_encrypt(clear, salt) in
825"libbb/pw_encrypt.c". The first argument is the clear text password to be
826encrypted, and the second is a string in "$type$salt$password" format, from
827which the "type" and "salt" fields will be extracted to produce an encrypted
828value. (Only the first two fields are needed, the third $ is equivalent to
829the end of the string.) The return value is an encrypted password in
830/etc/passwd format, with all three $ separated fields. It's stored in
831a static buffer, 128 bytes long.</p>
832
833<p>So when checking an existing password, if pw_encrypt(text,
834old_encrypted_password) returns a string that compares identical to
835old_encrypted_password, you've got the right password. When setting a new
836password, generate a random 8 character salt string, put it in the right
837format with sprintf(buffer, "$%c$%s", type, salt), and feed buffer as the
838second argument to pw_encrypt(text,buffer).</p>
839
840<h2><a name="tips_vfork">Fork and vfork</a></h2>
841
842<p>On systems that haven't got a Memory Management Unit, fork() is unreasonably
843expensive to implement (and sometimes even impossible), so a less capable
844function called vfork() is used instead. (Using vfork() on a system with an
845MMU is like pounding a nail with a wrench. Not the best tool for the job, but
846it works.)</p>
847
848<p>Busybox hides the difference between fork() and vfork() in
849libbb/bb_fork_exec.c. If you ever want to fork and exec, use bb_fork_exec()
850(which returns a pid and takes the same arguments as execve(), although in
851this case envp can be NULL) and don't worry about it. This description is
852here in case you want to know why that does what it does.</p>
853
854<p>Implementing fork() depends on having a Memory Management Unit. With an
855MMU then you can simply set up a second set of page tables and share the
856physical memory via copy-on-write. So a fork() followed quickly by exec()
857only copies a few pages of the parent's memory, just the ones it changes
858before freeing them.</p>
859
860<p>With a very primitive MMU (using a base pointer plus length instead of page
861tables, which can provide virtual addresses and protect processes from each
862other, but no copy on write) you can still implement fork. But it's
863unreasonably expensive, because you have to copy all the parent process'
864memory into the new process (which could easily be several megabytes per fork).
865And you have to do this even though that memory gets freed again as soon as the
866exec happens. (This is not just slow and a waste of space but causes memory
867usage spikes that can easily cause the system to run out of memory.)</p>
868
869<p>Without even a primitive MMU, you have no virtual addresses. Every process
870can reach out and touch any other process' memory, because all pointers are to
871physical addresses with no protection. Even if you copy a process' memory to
872new physical addresses, all of its pointers point to the old objects in the
873old process. (Searching through the new copy's memory for pointers and
874redirect them to the new locations is not an easy problem.)</p>
875
876<p>So with a primitive or missing MMU, fork() is just not a good idea.</p>
877
878<p>In theory, vfork() is just a fork() that writeably shares the heap and stack
879rather than copying it (so what one process writes the other one sees). In
880practice, vfork() has to suspend the parent process until the child does exec,
881at which point the parent wakes up and resumes by returning from the call to
882vfork(). All modern kernel/libc combinations implement vfork() to put the
883parent to sleep until the child does its exec. There's just no other way to
884make it work: the parent has to know the child has done its exec() or exit()
885before it's safe to return from the function it's in, so it has to block
886until that happens. In fact without suspending the parent there's no way to
887even store separate copies of the return value (the pid) from the vfork() call
888itself: both assignments write into the same memory location.</p>
889
890<p>One way to understand (and in fact implement) vfork() is this: imagine
891the parent does a setjmp and then continues on (pretending to be the child)
892until the exec() comes around, then the _exec_ does the actual fork, and the
893parent does a longjmp back to the original vfork call and continues on from
894there. (It thus becomes obvious why the child can't return, or modify
895local variables it doesn't want the parent to see changed when it resumes.)
896
897<p>Note a common mistake: the need for vfork doesn't mean you can't have two
898processes running at the same time. It means you can't have two processes
899sharing the same memory without stomping all over each other. As soon as
900the child calls exec(), the parent resumes.</p>
901
902<p>If the child's attempt to call exec() fails, the child should call _exit()
903rather than a normal exit(). This avoids any atexit() code that might confuse
904the parent. (The parent should never call _exit(), only a vforked child that
905failed to exec.)</p>
906
907<p>(Now in theory, a nommu system could just copy the _stack_ when it forks
908(which presumably is much shorter than the heap), and leave the heap shared.
909Even with no MMU at all
910In practice, you've just wound up in a multi-threaded situation and you can't
911do a malloc() or free() on your heap without freeing the other process' memory
912(and if you don't have the proper locking for being threaded, corrupting the
913heap if both of you try to do it at the same time and wind up stomping on
914each other while traversing the free memory lists). The thing about vfork is
915that it's a big red flag warning "there be dragons here" rather than
916something subtle and thus even more dangerous.)</p>
917
918<h2><a name="tips_sort_read">Short reads and writes</a></h2>
919
920<p>Busybox has special functions, bb_full_read() and bb_full_write(), to
921check that all the data we asked for got read or written. Is this a real
922world consideration? Try the following:</p>
923
924<pre>while true; do echo hello; sleep 1; done | tee out.txt</pre>
925
926<p>If tee is implemented with bb_full_read(), tee doesn't display output
927in real time but blocks until its entire input buffer (generally a couple
928kilobytes) is read, then displays it all at once. In that case, we _want_
929the short read, for user interface reasons. (Note that read() should never
930return 0 unless it has hit the end of input, and an attempt to write 0
931bytes should be ignored by the OS.)</p>
932
933<p>As for short writes, play around with two processes piping data to each
934other on the command line (cat bigfile | gzip &gt; out.gz) and suspend and
935resume a few times (ctrl-z to suspend, "fg" to resume). The writer can
936experience short writes, which are especially dangerous because if you don't
937notice them you'll discard data. They can also happen when a system is under
938load and a fast process is piping to a slower one. (Such as an xterm waiting
939on x11 when the scheduler decides X is being a CPU hog with all that
940text console scrolling...)</p>
941
942<p>So will data always be read from the far end of a pipe at the
943same chunk sizes it was written in? Nope. Don't rely on that. For one
Denis Vlasenkof7996f32007-01-11 17:20:00 +0000944counterexample, see <a href="http://www.faqs.org/rfcs/rfc896.html">rfc 896
Rob Landleyb73d2bf2006-05-11 15:00:32 +0000945for Nagle's algorithm</a>, which waits a fraction of a second or so before
946sending out small amounts of data through a TCP/IP connection in case more
947data comes in that can be merged into the same packet. (In case you were
948wondering why action games that use TCP/IP set TCP_NODELAY to lower the latency
949on their their sockets, now you know.)</p>
950
951<h2><a name="tips_memory">Memory used by relocatable code, PIC, and static linking.</a></h2>
952
953<p>The downside of standard dynamic linking is that it results in self-modifying
954code. Although each executable's pages are mmaped() into a process' address
955space from the executable file and are thus naturally shared between processes
956out of the page cache, the library loader (ld-linux.so.2 or ld-uClibc.so.0)
957writes to these pages to supply addresses for relocatable symbols. This
958dirties the pages, triggering copy-on-write allocation of new memory for each
959processes' dirtied pages.</p>
960
961<p>One solution to this is Position Independent Code (PIC), a way of linking
962a file so all the relocations are grouped together. This dirties fewer
963pages (often just a single page) for each process' relocations. The down
964side is this results in larger executables, which take up more space on disk
965(and a correspondingly larger space in memory). But when many copies of the
966same program are running, PIC dynamic linking trades a larger disk footprint
967for a smaller memory footprint, by sharing more pages.</p>
968
969<p>A third solution is static linking. A statically linked program has no
970relocations, and thus the entire executable is shared between all running
971instances. This tends to have a significantly larger disk footprint, but
972on a system with only one or two executables, shared libraries aren't much
973of a win anyway.</p>
974
975<p>You can tell the glibc linker to display debugging information about its
976relocations with the environment variable "LD_DEBUG". Try
977"LD_DEBUG=help /bin/true" for a list of commands. Learning to interpret
978"LD_DEBUG=statistics cat /proc/self/statm" could be interesting.</p>
979
980<p>For more on this topic, here's Rich Felker:</p>
981<blockquote>
982<p>Dynamic linking (without fixed load addresses) fundamentally requires
983at least one dirty page per dso that uses symbols. Making calls (but
984never taking the address explicitly) to functions within the same dso
985does not require a dirty page by itself, but will with ELF unless you
986use -Bsymbolic or hidden symbols when linking.</p>
987
988<p>ELF uses significant additional stack space for the kernel to pass all
989the ELF data structures to the newly created process image. These are
990located above the argument list and environment. This normally adds 1
991dirty page to the process size.</p>
992
993<p>The ELF dynamic linker has its own data segment, adding one or more
994dirty pages. I believe it also performs relocations on itself.</p>
995
996<p>The ELF dynamic linker makes significant dynamic allocations to manage
997the global symbol table and the loaded dso's. This data is never
998freed. It will be needed again if libdl is used, so unconditionally
999freeing it is not possible, but normal programs do not use libdl. Of
1000course with glibc all programs use libdl (due to nsswitch) so the
1001issue was never addressed.</p>
1002
1003<p>ELF also has the issue that segments are not page-aligned on disk.
1004This saves up to 4k on disk, but at the expense of using an additional
1005dirty page in most cases, due to a large portion of the first data
1006page being filled with a duplicate copy of the last text page.</p>
1007
1008<p>The above is just a partial list of the tiny memory penalties of ELF
1009dynamic linking, which eventually add up to quite a bit. The smallest
1010I've been able to get a process down to is 8 dirty pages, and the
1011above factors seem to mostly account for it (but some were difficult
1012to measure).</p>
1013</blockquote>
1014
1015<h2><a name="tips_kernel_headers"></a>Including kernel headers</h2>
1016
1017<p>The "linux" or "asm" directories of /usr/include contain Linux kernel
1018headers, so that the C library can talk directly to the Linux kernel. In
1019a perfect world, applications shouldn't include these headers directly, but
1020we don't live in a perfect world.</p>
1021
1022<p>For example, Busybox's losetup code wants linux/loop.c because nothing else
1023#defines the structures to call the kernel's loopback device setup ioctls.
1024Attempts to cut and paste the information into a local busybox header file
1025proved incredibly painful, because portions of the loop_info structure vary by
1026architecture, namely the type __kernel_dev_t has different sizes on alpha,
1027arm, x86, and so on. Meaning we either #include <linux/posix_types.h> or
1028we hardwire #ifdefs to check what platform we're building on and define this
1029type appropriately for every single hardware architecture supported by
1030Linux, which is simply unworkable.</p>
1031
1032<p>This is aside from the fact that the relevant type defined in
1033posix_types.h was renamed to __kernel_old_dev_t during the 2.5 series, so
1034to cut and paste the structure into our header we have to #include
1035<linux/version.h> to figure out which name to use. (What we actually do is
1036check if we're building on 2.6, and if so just use the new 64 bit structure
1037instead to avoid the rename entirely.) But we still need the version
1038check, since 2.4 didn't have the 64 bit structure.</p>
1039
1040<p>The BusyBox developers spent <u>two years</u> trying to figure
1041out a clean way to do all this. There isn't one. The losetup in the
1042util-linux package from kernel.org isn't doing it cleanly either, they just
1043hide the ugliness by nesting #include files. Their mount/loop.h
1044#includes "my_dev_t.h", which #includes <linux/posix_types.h> and
1045<linux/version.h> just like we do. There simply is no alternative.</p>
1046
1047<p>Just because directly #including kernel headers is sometimes
1048unavoidable doesn't me we should include them when there's a better
1049way to do it. However, block copying information out of the kernel headers
1050is not a better way.</p>
1051
1052<h2><a name="who">Who are the BusyBox developers?</a></h2>
1053
1054<p>The following login accounts currently exist on busybox.net. (I.E. these
1055people can commit <a href="http://busybox.net/downloads/patches">patches</a>
1056into subversion for the BusyBox, uClibc, and buildroot projects.)</p>
1057
1058<pre>
1059aldot :Bernhard Fischer
Denis Vlasenkoabfc4cf2006-11-18 16:30:04 +00001060andersen :Erik Andersen - uClibc and BuildRoot maintainer.
Rob Landleyb73d2bf2006-05-11 15:00:32 +00001061bug1 :Glenn McGrath
1062davidm :David McCullough
Denis Vlasenkoabfc4cf2006-11-18 16:30:04 +00001063gkajmowi :Garrett Kajmowicz - uClibc++ maintainer
Rob Landleyb73d2bf2006-05-11 15:00:32 +00001064jbglaw :Jan-Benedict Glaw
1065jocke :Joakim Tjernlund
Denis Vlasenkoabfc4cf2006-11-18 16:30:04 +00001066landley :Rob Landley - BusyBox maintainer
Rob Landleyb73d2bf2006-05-11 15:00:32 +00001067lethal :Paul Mundt
1068mjn3 :Manuel Novoa III
1069osuadmin :osuadmin
1070pgf :Paul Fox
1071pkj :Peter Kjellerstedt
1072prpplague :David Anders
1073psm :Peter S. Mazinger
1074russ :Russ Dill
1075sandman :Robert Griebl
1076sjhill :Steven J. Hill
1077solar :Ned Ludd
1078timr :Tim Riker
1079tobiasa :Tobias Anderberg
1080vapier :Mike Frysinger
1081</pre>
1082
1083<p>The following accounts used to exist on busybox.net, but don't anymore so
Denis Vlasenkoabfc4cf2006-11-18 16:30:04 +00001084I can't ask /etc/passwd for their names. Rob Wentworth <robwen@gmail.com>
1085asked Google and recovered the names:</p>
Rob Landleyb73d2bf2006-05-11 15:00:32 +00001086
1087<pre>
Denis Vlasenkoabfc4cf2006-11-18 16:30:04 +00001088aaronl :Aaron Lehmann
1089beppu :John Beppu
1090dwhedon :David Whedon
1091erik :Erik Andersen
Denis Vlasenkof7996f32007-01-11 17:20:00 +00001092gfeldman :Gennady Feldman
Denis Vlasenkoabfc4cf2006-11-18 16:30:04 +00001093jimg :Jim Gleason
1094kraai :Matt Kraai
1095markw :Mark Whitley
1096miles :Miles Bader
1097proski :Pavel Roskin
1098rjune :Richard June
1099tausq :Randolph Chung
1100vodz :Vladimir N. Oleynik
Rob Landleyb73d2bf2006-05-11 15:00:32 +00001101</pre>
1102
1103
Eric Andersen6c4a6b12004-10-08 10:50:08 +00001104<br>
1105<br>
1106<br>
1107
1108<!--#include file="footer.html" -->