Mark Whitley | eac2636 | 2000-12-19 17:35:24 +0000 | [diff] [blame] | 1 | How to Add a New Applet to BusyBox |
| 2 | ================================== |
| 3 | |
| 4 | This document details the steps you must take to add a new applet to BusyBox. |
| 5 | |
| 6 | Credits: |
| 7 | Matt Kraai - initial writeup |
| 8 | Mark Whitley - the remix |
Denys Vlasenko | f332617 | 2014-03-17 15:06:29 +0100 | [diff] [blame] | 9 | Thomas Lundquist - trying to keep it updated |
Denis Vlasenko | 7e84e53 | 2007-05-08 17:52:17 +0000 | [diff] [blame] | 10 | |
Dan Fandrich | 9669363 | 2010-08-10 23:45:27 -0700 | [diff] [blame] | 11 | When doing this you should consider using the latest git HEAD. |
Denys Vlasenko | 5370bfb | 2009-09-06 02:58:59 +0200 | [diff] [blame] | 12 | This is a good thing if you plan to getting it committed into mainline. |
Mark Whitley | eac2636 | 2000-12-19 17:35:24 +0000 | [diff] [blame] | 13 | |
| 14 | Initial Write |
| 15 | ------------- |
| 16 | |
Mike Frysinger | 9754b91 | 2005-09-02 23:06:30 +0000 | [diff] [blame] | 17 | First, write your applet. Be sure to include copyright information at the top, |
| 18 | such as who you stole the code from and so forth. Also include the mini-GPL |
Bartosz Golaszewski | 9dbe4d0 | 2014-03-12 22:43:50 +0100 | [diff] [blame] | 19 | boilerplate and Config.in/Kbuild/usage/applet.h snippets (more on that below |
| 20 | in this document). Be sure to name the main function <applet>_main instead |
| 21 | of main. And be sure to put it in <applet>.c. Make sure to #include "libbb.h" |
| 22 | as the first include file in your applet. |
Glenn L McGrath | 9c91e41 | 2003-10-01 11:33:46 +0000 | [diff] [blame] | 23 | |
| 24 | For a new applet mu, here is the code that would go in mu.c: |
Mark Whitley | eac2636 | 2000-12-19 17:35:24 +0000 | [diff] [blame] | 25 | |
Denys Vlasenko | f332617 | 2014-03-17 15:06:29 +0100 | [diff] [blame] | 26 | (libbb.h already includes most usual header files. You do not need |
Denis Vlasenko | 7e84e53 | 2007-05-08 17:52:17 +0000 | [diff] [blame] | 27 | #include <stdio.h> etc...) |
| 28 | |
| 29 | |
Mark Whitley | eac2636 | 2000-12-19 17:35:24 +0000 | [diff] [blame] | 30 | ----begin example code------ |
| 31 | |
| 32 | /* vi: set sw=4 ts=4: */ |
| 33 | /* |
| 34 | * Mini mu implementation for busybox |
| 35 | * |
Mark Whitley | eac2636 | 2000-12-19 17:35:24 +0000 | [diff] [blame] | 36 | * Copyright (C) [YEAR] by [YOUR NAME] <YOUR EMAIL> |
| 37 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 38 | * Licensed under GPLv2, see file LICENSE in this source tree. |
Mark Whitley | eac2636 | 2000-12-19 17:35:24 +0000 | [diff] [blame] | 39 | */ |
| 40 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 41 | #include "libbb.h" |
Denis Vlasenko | 7e84e53 | 2007-05-08 17:52:17 +0000 | [diff] [blame] | 42 | #include "other.h" |
Mark Whitley | eac2636 | 2000-12-19 17:35:24 +0000 | [diff] [blame] | 43 | |
Bartosz Golaszewski | 9dbe4d0 | 2014-03-12 22:43:50 +0100 | [diff] [blame] | 44 | //config:config MU |
| 45 | //config: bool "MU" |
Denys Vlasenko | f332617 | 2014-03-17 15:06:29 +0100 | [diff] [blame] | 46 | //config: default y |
Bartosz Golaszewski | 9dbe4d0 | 2014-03-12 22:43:50 +0100 | [diff] [blame] | 47 | //config: help |
| 48 | //config: Returns an indeterminate value. |
| 49 | |
| 50 | //kbuild:lib-$(CONFIG_MU) += mu.o |
| 51 | //applet:IF_MU(APPLET(mu, BB_DIR_USR_BIN, BB_SUID_DROP)) |
| 52 | |
| 53 | //usage:#define mu_trivial_usage |
Denys Vlasenko | f332617 | 2014-03-17 15:06:29 +0100 | [diff] [blame] | 54 | //usage: "[-abcde] FILE..." |
Bartosz Golaszewski | 9dbe4d0 | 2014-03-12 22:43:50 +0100 | [diff] [blame] | 55 | //usage:#define mu_full_usage |
Denys Vlasenko | f332617 | 2014-03-17 15:06:29 +0100 | [diff] [blame] | 56 | //usage: "Returns an indeterminate value\n" |
| 57 | //usage: "\n -a First function" |
| 58 | //usage: "\n -b Second function" |
Bartosz Golaszewski | 9dbe4d0 | 2014-03-12 22:43:50 +0100 | [diff] [blame] | 59 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 60 | int mu_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Mark Whitley | eac2636 | 2000-12-19 17:35:24 +0000 | [diff] [blame] | 61 | int mu_main(int argc, char **argv) |
| 62 | { |
| 63 | int fd; |
Denis Vlasenko | 360362d | 2007-11-04 04:46:46 +0000 | [diff] [blame] | 64 | ssize_t n; |
Mark Whitley | eac2636 | 2000-12-19 17:35:24 +0000 | [diff] [blame] | 65 | char mu; |
| 66 | |
Bernhard Reutner-Fischer | 8c1eda5 | 2006-08-28 23:39:36 +0000 | [diff] [blame] | 67 | fd = xopen("/dev/random", O_RDONLY); |
Mark Whitley | eac2636 | 2000-12-19 17:35:24 +0000 | [diff] [blame] | 68 | |
| 69 | if ((n = safe_read(fd, &mu, 1)) < 1) |
Mike Frysinger | 0ea3a6f | 2005-04-23 01:43:45 +0000 | [diff] [blame] | 70 | bb_perror_msg_and_die("/dev/random"); |
Mark Whitley | eac2636 | 2000-12-19 17:35:24 +0000 | [diff] [blame] | 71 | |
| 72 | return mu; |
| 73 | } |
| 74 | |
| 75 | ----end example code------ |
| 76 | |
Mark Whitley | 8a6b619 | 2000-12-19 17:54:38 +0000 | [diff] [blame] | 77 | |
| 78 | Coding Style |
| 79 | ------------ |
| 80 | |
| 81 | Before you submit your applet for inclusion in BusyBox, (or better yet, before |
| 82 | you _write_ your applet) please read through the style guide in the docs |
| 83 | directory and make your program compliant. |
| 84 | |
| 85 | |
Glenn L McGrath | 2e6d3cf | 2001-06-24 12:36:54 +0000 | [diff] [blame] | 86 | Some Words on libbb |
| 87 | ------------------- |
Mark Whitley | 8a6b619 | 2000-12-19 17:54:38 +0000 | [diff] [blame] | 88 | |
Mark Whitley | eac2636 | 2000-12-19 17:35:24 +0000 | [diff] [blame] | 89 | As you are writing your applet, please be aware of the body of pre-existing |
Glenn L McGrath | 2e6d3cf | 2001-06-24 12:36:54 +0000 | [diff] [blame] | 90 | useful functions in libbb. Use these instead of reinventing the wheel. |
Mark Whitley | 8a6b619 | 2000-12-19 17:54:38 +0000 | [diff] [blame] | 91 | |
Mark Whitley | eac2636 | 2000-12-19 17:35:24 +0000 | [diff] [blame] | 92 | Additionally, if you have any useful, general-purpose functions in your |
Mike Frysinger | 9754b91 | 2005-09-02 23:06:30 +0000 | [diff] [blame] | 93 | applet that could be useful in other applets, consider putting them in libbb. |
Mark Whitley | eac2636 | 2000-12-19 17:35:24 +0000 | [diff] [blame] | 94 | |
Denis Vlasenko | 7e84e53 | 2007-05-08 17:52:17 +0000 | [diff] [blame] | 95 | And it may be possible that some of the other applets uses functions you |
| 96 | could use. If so, you have to rip the function out of the applet and make |
| 97 | a libbb function out of it. |
| 98 | |
| 99 | Adding a libbb function: |
| 100 | ------------------------ |
| 101 | |
| 102 | Make a new file named <function_name>.c |
| 103 | |
| 104 | ----start example code------ |
| 105 | |
| 106 | #include "libbb.h" |
| 107 | #include "other.h" |
| 108 | |
Bartosz Golaszewski | 9dbe4d0 | 2014-03-12 22:43:50 +0100 | [diff] [blame] | 109 | //kbuild:lib-y += function.o |
| 110 | |
Denis Vlasenko | 7e84e53 | 2007-05-08 17:52:17 +0000 | [diff] [blame] | 111 | int function(char *a) |
| 112 | { |
| 113 | return *a; |
| 114 | } |
| 115 | |
| 116 | ----end example code------ |
| 117 | |
Bartosz Golaszewski | 9dbe4d0 | 2014-03-12 22:43:50 +0100 | [diff] [blame] | 118 | Remember about the kbuild snippet. |
Denis Vlasenko | 7e84e53 | 2007-05-08 17:52:17 +0000 | [diff] [blame] | 119 | |
Denis Vlasenko | 4b924f3 | 2007-05-30 00:29:55 +0000 | [diff] [blame] | 120 | You should also try to find a suitable place in include/libbb.h for |
Denis Vlasenko | 7e84e53 | 2007-05-08 17:52:17 +0000 | [diff] [blame] | 121 | the function declaration. If not, add it somewhere anyway, with or without |
| 122 | ifdefs to include or not. |
| 123 | |
Holger Blasum | b27d62a | 2010-10-28 20:37:05 +0200 | [diff] [blame] | 124 | You can look at libbb/Config.src and try to find out if the function is |
Denys Vlasenko | 5370bfb | 2009-09-06 02:58:59 +0200 | [diff] [blame] | 125 | tunable and add it there if it is. |
Denis Vlasenko | 7e84e53 | 2007-05-08 17:52:17 +0000 | [diff] [blame] | 126 | |
Mark Whitley | eac2636 | 2000-12-19 17:35:24 +0000 | [diff] [blame] | 127 | |
Bartosz Golaszewski | 9dbe4d0 | 2014-03-12 22:43:50 +0100 | [diff] [blame] | 128 | Kbuild/Config.in/usage/applets.h snippets in .c files |
| 129 | ----------------------------------------------------- |
| 130 | |
| 131 | The old way of adding new applets was to put all the information needed by the |
| 132 | configuration and build system into appropriate files (namely: Kbuild.src and |
| 133 | Config.src in new applet's directory) and to add the applet declaration and |
| 134 | usage info text to include/applets.src.h and include/usage.src.h respectively. |
| 135 | |
| 136 | Since the scripts/gen_build_files.sh script had been introduced, the preferred |
| 137 | way is to have all these declarations contained within the applet .c files. |
| 138 | |
| 139 | Every line intended to be processed by gen_build_files.sh should start as a |
| 140 | comment without any preceding whitespaces and be followed by an appropriate |
| 141 | keyword - kbuild, config, usage or applet - and a colon, just like shown in the |
| 142 | first example above. |
| 143 | |
| 144 | |
Matt Kraai | 3b1cbd7 | 2002-03-18 16:03:00 +0000 | [diff] [blame] | 145 | Placement / Directory |
| 146 | --------------------- |
| 147 | |
| 148 | Find the appropriate directory for your new applet. |
| 149 | |
Bartosz Golaszewski | 9dbe4d0 | 2014-03-12 22:43:50 +0100 | [diff] [blame] | 150 | Add the config snippet to the .c file: |
Matt Kraai | 3b1cbd7 | 2002-03-18 16:03:00 +0000 | [diff] [blame] | 151 | |
Bartosz Golaszewski | 9dbe4d0 | 2014-03-12 22:43:50 +0100 | [diff] [blame] | 152 | //config:config MU |
| 153 | //config: bool "MU" |
Denys Vlasenko | f332617 | 2014-03-17 15:06:29 +0100 | [diff] [blame] | 154 | //config: default y |
Bartosz Golaszewski | 9dbe4d0 | 2014-03-12 22:43:50 +0100 | [diff] [blame] | 155 | //config: help |
Denys Vlasenko | fbecca1 | 2017-08-06 14:03:27 +0200 | [diff] [blame] | 156 | //config: Returns an indeterminate value. |
| 157 | |
| 158 | Add the kbuild snippet to the .c file: |
| 159 | |
| 160 | //kbuild:lib-$(CONFIG_MU) += mu.o |
Matt Kraai | 3b1cbd7 | 2002-03-18 16:03:00 +0000 | [diff] [blame] | 161 | |
| 162 | |
Mark Whitley | eac2636 | 2000-12-19 17:35:24 +0000 | [diff] [blame] | 163 | Usage String(s) |
| 164 | --------------- |
| 165 | |
Bartosz Golaszewski | 9dbe4d0 | 2014-03-12 22:43:50 +0100 | [diff] [blame] | 166 | Next, add usage information for your applet to the .c file. |
Matt Kraai | 3b1cbd7 | 2002-03-18 16:03:00 +0000 | [diff] [blame] | 167 | This should look like the following: |
Mark Whitley | eac2636 | 2000-12-19 17:35:24 +0000 | [diff] [blame] | 168 | |
Bartosz Golaszewski | 9dbe4d0 | 2014-03-12 22:43:50 +0100 | [diff] [blame] | 169 | //usage:#define mu_trivial_usage |
Denys Vlasenko | f332617 | 2014-03-17 15:06:29 +0100 | [diff] [blame] | 170 | //usage: "[-abcde] FILE..." |
Denys Vlasenko | fbecca1 | 2017-08-06 14:03:27 +0200 | [diff] [blame] | 171 | //usage:#define mu_full_usage "\n\n" |
| 172 | //usage: "Returns an indeterminate value" |
| 173 | //usage: "\n" |
Denys Vlasenko | f332617 | 2014-03-17 15:06:29 +0100 | [diff] [blame] | 174 | //usage: "\n -a First function" |
| 175 | //usage: "\n -b Second function" |
Bartosz Golaszewski | 9dbe4d0 | 2014-03-12 22:43:50 +0100 | [diff] [blame] | 176 | //usage: ... |
Mark Whitley | eac2636 | 2000-12-19 17:35:24 +0000 | [diff] [blame] | 177 | |
| 178 | If your program supports flags, the flags should be mentioned on the first |
Denys Vlasenko | f332617 | 2014-03-17 15:06:29 +0100 | [diff] [blame] | 179 | line ([-abcde]) and a detailed description of each flag should go in the |
| 180 | mu_full_usage section, one flag per line. |
Mark Whitley | eac2636 | 2000-12-19 17:35:24 +0000 | [diff] [blame] | 181 | |
| 182 | |
| 183 | Header Files |
| 184 | ------------ |
| 185 | |
Bartosz Golaszewski | 9dbe4d0 | 2014-03-12 22:43:50 +0100 | [diff] [blame] | 186 | Finally add the applet declaration snippet. Be sure to read the top of |
| 187 | applets.src.h before adding your applet - it contains important info |
| 188 | on applet macros and conventions. |
Mark Whitley | eac2636 | 2000-12-19 17:35:24 +0000 | [diff] [blame] | 189 | |
Bartosz Golaszewski | 9dbe4d0 | 2014-03-12 22:43:50 +0100 | [diff] [blame] | 190 | //applet:IF_MU(APPLET(mu, BB_DIR_USR_BIN, BB_SUID_DROP)) |
Mark Whitley | eac2636 | 2000-12-19 17:35:24 +0000 | [diff] [blame] | 191 | |
| 192 | |
Mark Whitley | eac2636 | 2000-12-19 17:35:24 +0000 | [diff] [blame] | 193 | The Grand Announcement |
| 194 | ---------------------- |
| 195 | |
Dan Fandrich | 9669363 | 2010-08-10 23:45:27 -0700 | [diff] [blame] | 196 | Then create a diff by adding the new files to git (remember your libbb files) |
| 197 | git add <where you put it>/mu.c |
Denis Vlasenko | 4b924f3 | 2007-05-30 00:29:55 +0000 | [diff] [blame] | 198 | eventually also: |
Dan Fandrich | 9669363 | 2010-08-10 23:45:27 -0700 | [diff] [blame] | 199 | git add libbb/function.c |
Denis Vlasenko | 7e84e53 | 2007-05-08 17:52:17 +0000 | [diff] [blame] | 200 | then |
Dan Fandrich | 9669363 | 2010-08-10 23:45:27 -0700 | [diff] [blame] | 201 | git commit |
| 202 | git format-patch HEAD^ |
Glenn L McGrath | 9c91e41 | 2003-10-01 11:33:46 +0000 | [diff] [blame] | 203 | and send it to the mailing list: |
Mike Frysinger | 9754b91 | 2005-09-02 23:06:30 +0000 | [diff] [blame] | 204 | busybox@busybox.net |
| 205 | http://busybox.net/mailman/listinfo/busybox |
Mark Whitley | eac2636 | 2000-12-19 17:35:24 +0000 | [diff] [blame] | 206 | |
Glenn L McGrath | 9c91e41 | 2003-10-01 11:33:46 +0000 | [diff] [blame] | 207 | Sending patches as attachments is preferred, but not required. |