Enable more tools in config.maemo
[busybox4maemo] / debian / patches / applets-fallback.patch
1 --- busybox.orig/scripts/defconfig
2 +++ busybox/scripts/defconfig
3 @@ -30,8 +30,8 @@
4  CONFIG_FEATURE_SUID_CONFIG=y
5  CONFIG_FEATURE_SUID_CONFIG_QUIET=y
6  # CONFIG_SELINUX is not set
7 -# CONFIG_FEATURE_PREFER_APPLETS is not set
8 -CONFIG_BUSYBOX_EXEC_PATH="/proc/self/exe"
9 +# CONFIG_FEATURE_APPLETS_FALLBACK is not set
10 +CONFIG_BUSYBOX_EXEC_PATH="/bin/busybox"
11  CONFIG_FEATURE_SYSLOG=y
12  CONFIG_FEATURE_HAVE_RPC=y
13  
14 --- busybox.orig/shell/Config.in
15 +++ busybox/shell/Config.in
16 @@ -261,35 +261,7 @@
17  config FEATURE_SH_STANDALONE
18         bool "Standalone shell"
19         default n
20 -       depends on (MSH || LASH || HUSH || ASH) && FEATURE_PREFER_APPLETS
21 -       help
22 -         This option causes busybox shells to use busybox applets
23 -         in preference to executables in the PATH whenever possible.  For
24 -         example, entering the command 'ifconfig' into the shell would cause
25 -         busybox to use the ifconfig busybox applet.  Specifying the fully
26 -         qualified executable name, such as '/sbin/ifconfig' will still
27 -         execute the /sbin/ifconfig executable on the filesystem.  This option
28 -         is generally used when creating a statically linked version of busybox
29 -         for use as a rescue shell, in the event that you screw up your system.
30 -
31 -         This is implemented by re-execing /proc/self/exe (typically)
32 -         with right parameters. Some selected applets ("NOFORK" applets)
33 -         can even be executed without creating new process.
34 -         Instead, busybox will call <applet>_main() internally.
35 -
36 -         However, this causes problems in chroot jails without mounted /proc
37 -         and with ps/top (command name can be shown as 'exe' for applets
38 -         started this way).
39 -# untrue?
40 -#        Note that this will *also* cause applets to take precedence
41 -#        over shell builtins of the same name.  So turning this on will
42 -#        eliminate any performance gained by turning on the builtin "echo"
43 -#        and "test" commands in ash.
44 -# untrue?
45 -#        Note that when using this option, the shell will attempt to directly
46 -#        run '/bin/busybox'.  If you do not have the busybox binary sitting in
47 -#        that exact location with that exact name, this option will not work at
48 -#        all.
49 +       depends on (MSH || LASH || HUSH || ASH) && FEATURE_APPLETS_FALLBACK
50  
51  config CTTYHACK
52         bool "cttyhack"
53 --- busybox.orig/shell/ash.c
54 +++ busybox/shell/ash.c
55 @@ -6629,20 +6629,6 @@
56  {
57         int repeated = 0;
58  
59 -#if ENABLE_FEATURE_SH_STANDALONE
60 -       if (strchr(cmd, '/') == NULL) {
61 -               int a = find_applet_by_name(cmd);
62 -               if (a >= 0) {
63 -                       if (APPLET_IS_NOEXEC(a))
64 -                               run_applet_no_and_exit(a, argv);
65 -                       /* re-exec ourselves with the new arguments */
66 -                       execve(bb_busybox_exec_path, argv, envp);
67 -                       /* If they called chroot or otherwise made the binary no longer
68 -                        * executable, fall through */
69 -               }
70 -       }
71 -#endif
72 -
73   repeat:
74  #ifdef SYSV
75         do {
76 @@ -6687,11 +6673,7 @@
77  
78         clearredir(1);
79         envp = environment();
80 -       if (strchr(argv[0], '/')
81 -#if ENABLE_FEATURE_SH_STANDALONE
82 -        || find_applet_by_name(argv[0]) >= 0
83 -#endif
84 -       ) {
85 +       if (strchr(argv[0], '/')) {
86                 tryexec(argv[0], argv, envp);
87                 e = errno;
88         } else {
89 @@ -6706,6 +6688,11 @@
90                 }
91         }
92  
93 +#if ENABLE_FEATURE_SH_STANDALONE
94 +       if (e == ENOENT)
95 +               bb_execv_applet(argv[0], argv, envp);
96 +#endif
97 +
98         /* Map to POSIX errors */
99         switch (e) {
100         case EACCES:
101 --- busybox.orig/libbb/vfork_daemon_rexec.c
102 +++ busybox/libbb/vfork_daemon_rexec.c
103 @@ -100,7 +100,7 @@
104         return 0;
105  }
106  
107 -#if ENABLE_FEATURE_PREFER_APPLETS
108 +#if ENABLE_FEATURE_APPLETS_FALLBACK
109  void save_nofork_data(struct nofork_save_area *save)
110  {
111         memcpy(&save->die_jmp, &die_jmp, sizeof(die_jmp));
112 @@ -170,7 +170,7 @@
113  int spawn_and_wait(char **argv)
114  {
115         int rc;
116 -#if ENABLE_FEATURE_PREFER_APPLETS
117 +#if ENABLE_FEATURE_APPLETS_FALLBACK
118         int a = find_applet_by_name(argv[0]);
119  
120         if (a >= 0 && (APPLET_IS_NOFORK(a)
121 --- busybox.orig/libbb/execable.c
122 +++ busybox/libbb/execable.c
123 @@ -9,6 +9,9 @@
124  
125  #include "libbb.h"
126  
127 +#include <alloca.h>
128 +#include <stdarg.h>
129 +
130  /* check if path points to an executable file;
131   * return 1 if found;
132   * return 0 otherwise;
133 @@ -60,12 +63,60 @@
134         return 0;
135  }
136  
137 -#if ENABLE_FEATURE_PREFER_APPLETS
138 -/* just like the real execvp, but try to launch an applet named 'file' first
139 +#if ENABLE_FEATURE_APPLETS_FALLBACK
140 +int bb_execv_applet(const char *name, char *const argv[], char *const envp[])
141 +{
142 +       const char **path = bb_busybox_exec_paths;
143 +
144 +       errno = ENOENT;
145 +
146 +       if (find_applet_by_name(name) < 0)
147 +               return -1;
148 +
149 +       for (; *path; ++path)
150 +               execve(*path, argv, envp);
151 +
152 +       return -1;
153 +}
154 +
155 +/* just like the real execvp, but try to launch applet if exec failes
156   */
157  int bb_execvp(const char *file, char *const argv[])
158  {
159 -       return execvp(find_applet_by_name(file) >= 0 ? bb_busybox_exec_path : file,
160 -                                       argv);
161 +       int ret = execvp(file, argv);
162 +       if (errno != ENOENT)
163 +               return ret;
164 +
165 +       return bb_execv_applet(file, argv, environ);
166 +}
167 +
168 +int bb_execlp(const char *file, const char *arg, ...)
169 +{
170 +#define INITIAL_ARGV_MAX 16
171 +       size_t argv_max = INITIAL_ARGV_MAX;
172 +       const char **argv = malloc(argv_max * sizeof (const char *));
173 +       va_list args;
174 +       unsigned int i = 0;
175 +       int ret;
176 +
177 +       va_start (args, arg);
178 +       while (argv[i++] != NULL) {
179 +               if (i == argv_max) { 
180 +                       const char **nptr;
181 +                       argv_max *= 2;
182 +                       nptr = realloc (argv, argv_max * sizeof (const char *));
183 +                       if (nptr == NULL)
184 +                               return -1;
185 +                       argv = nptr;
186 +               }
187 +
188 +               argv[i] = va_arg (args, const char *);
189 +       }
190 +       va_end (args);
191 +
192 +       ret = bb_execvp(file, (char *const *)argv);
193 +       free(argv);
194 +
195 +       return ret;
196  }
197  #endif
198 --- busybox.orig/libbb/error_msg_and_die.c
199 +++ busybox/libbb/error_msg_and_die.c
200 @@ -10,14 +10,14 @@
201  #include "libbb.h"
202  
203  int die_sleep;
204 -#if ENABLE_FEATURE_PREFER_APPLETS || ENABLE_HUSH
205 +#if ENABLE_FEATURE_APPLETS_FALLBACK || ENABLE_HUSH
206  jmp_buf die_jmp;
207  #endif
208  
209  void xfunc_die(void)
210  {
211         if (die_sleep) {
212 -               if ((ENABLE_FEATURE_PREFER_APPLETS || ENABLE_HUSH)
213 +               if ((ENABLE_FEATURE_APPLETS_FALLBACK || ENABLE_HUSH)
214                  && die_sleep < 0
215                 ) {
216                         /* Special case. We arrive here if NOFORK applet
217 --- busybox.orig/libbb/fflush_stdout_and_exit.c
218 +++ busybox/libbb/fflush_stdout_and_exit.c
219 @@ -18,7 +18,7 @@
220         if (fflush(stdout))
221                 bb_perror_msg_and_die(bb_msg_standard_output);
222  
223 -       if (ENABLE_FEATURE_PREFER_APPLETS && die_sleep < 0) {
224 +       if (ENABLE_FEATURE_APPLETS_FALLBACK && die_sleep < 0) {
225                 /* We are in NOFORK applet. Do not exit() directly,
226                  * but use xfunc_die() */
227                 xfunc_error_retval = retval;
228 --- busybox.orig/libbb/messages.c
229 +++ busybox/libbb/messages.c
230 @@ -43,7 +43,15 @@
231  const char bb_path_gshadow_file[] ALIGN1 = "/etc/gshadow";
232  const char bb_path_motd_file[] ALIGN1 = "/etc/motd";
233  const char bb_dev_null[] ALIGN1 = "/dev/null";
234 -const char bb_busybox_exec_path[] ALIGN1 = CONFIG_BUSYBOX_EXEC_PATH;
235 +const char *bb_busybox_exec_paths[] ALIGN1 = {
236 +#ifdef __linux__
237 +       "/proc/self/exe",
238 +#endif
239 +#ifdef CONFIG_BUSYBOX_EXEC_PATH
240 +       CONFIG_BUSYBOX_EXEC_PATH,
241 +#endif
242 +       NULL
243 +};
244  const char bb_default_login_shell[] ALIGN1 = LIBBB_DEFAULT_LOGIN_SHELL;
245  /* util-linux manpage says /sbin:/bin:/usr/sbin:/usr/bin,
246   * but I want to save a few bytes here. Check libbb.h before changing! */
247 --- busybox.orig/libbb/appletlib.c
248 +++ busybox/libbb/appletlib.c
249 @@ -607,9 +607,9 @@
250  
251         if (ENABLE_FEATURE_INSTALLER && strcmp(argv[1], "--install") == 0) {
252                 const char *busybox;
253 -               busybox = xmalloc_readlink(bb_busybox_exec_path);
254 +               busybox = xmalloc_readlink(bb_busybox_exec_paths[0]);
255                 if (!busybox)
256 -                       busybox = bb_busybox_exec_path;
257 +                       busybox = bb_busybox_exec_paths[0];
258                 /* -s makes symlinks */
259                 install_links(busybox, argv[2] && strcmp(argv[2], "-s") == 0);
260                 return 0;
261 --- busybox.orig/include/libbb.h
262 +++ busybox/include/libbb.h
263 @@ -650,12 +650,12 @@
264  /* BB_EXECxx always execs (it's not doing NOFORK/NOEXEC stuff),
265   * but it may exec busybox and call applet instead of searching PATH.
266   */
267 -#if ENABLE_FEATURE_PREFER_APPLETS
268 +#if ENABLE_FEATURE_APPLETS_FALLBACK
269 +int bb_execv_applet(const char *name, char *const argv[], char *const envp[]);
270  int bb_execvp(const char *file, char *const argv[]);
271 -#define BB_EXECVP(prog,cmd) bb_execvp(prog,cmd)
272 -#define BB_EXECLP(prog,cmd,...) \
273 -       execlp((find_applet_by_name(prog) >= 0) ? CONFIG_BUSYBOX_EXEC_PATH : prog, \
274 -               cmd, __VA_ARGS__)
275 +int bb_execlp(const char *file, const char *arg, ...);
276 +#define BB_EXECVP(prog,cmd)     bb_execvp(prog,cmd)
277 +#define BB_EXECLP(prog,cmd,...) bb_execlp(prog,cmd, __VA_ARGS__)
278  #else
279  #define BB_EXECVP(prog,cmd)     execvp(prog,cmd)
280  #define BB_EXECLP(prog,cmd,...) execlp(prog,cmd, __VA_ARGS__)
281 @@ -1211,7 +1211,7 @@
282  extern const char bb_path_motd_file[];
283  extern const char bb_path_wtmp_file[];
284  extern const char bb_dev_null[];
285 -extern const char bb_busybox_exec_path[];
286 +extern const char *bb_busybox_exec_paths[];
287  /* util-linux manpage says /sbin:/bin:/usr/sbin:/usr/bin,
288   * but I want to save a few bytes here */
289  extern const char bb_PATH_root_path[]; /* "PATH=/sbin:/usr/sbin:/bin:/usr/bin" */
290 --- busybox.orig/include/busybox.h
291 +++ busybox/include/busybox.h
292 @@ -32,13 +32,13 @@
293  extern const uint16_t applet_nameofs[];
294  extern const uint8_t applet_install_loc[];
295  
296 -#if ENABLE_FEATURE_SUID || ENABLE_FEATURE_PREFER_APPLETS
297 +#if ENABLE_FEATURE_SUID || ENABLE_FEATURE_APPLETS_FALLBACK
298  #define APPLET_NAME(i) (applet_names + (applet_nameofs[i] & 0x0fff))
299  #else
300  #define APPLET_NAME(i) (applet_names + applet_nameofs[i])
301  #endif
302  
303 -#if ENABLE_FEATURE_PREFER_APPLETS
304 +#if ENABLE_FEATURE_APPLETS_FALLBACK
305  #define APPLET_IS_NOFORK(i) (applet_nameofs[i] & (1 << 12))
306  #define APPLET_IS_NOEXEC(i) (applet_nameofs[i] & (1 << 13))
307  #endif
308 --- busybox.orig/applets/applet_tables.c
309 +++ busybox/applets/applet_tables.c
310 @@ -87,7 +87,7 @@
311         for (i = 0; i < NUM_APPLETS; i++) {
312                 printf("0x%04x,\n",
313                         offset[i]
314 -#if ENABLE_FEATURE_PREFER_APPLETS
315 +#if ENABLE_FEATURE_APPLETS_FALLBACK
316                         + (applets[i].nofork << 12)
317                         + (applets[i].noexec << 13)
318  #endif
319 --- busybox.orig/Config.in
320 +++ busybox/Config.in
321 @@ -222,29 +222,16 @@
322  
323           Most people will leave this set to 'N'.
324  
325 -config FEATURE_PREFER_APPLETS
326 -       bool "exec prefers applets"
327 +config FEATURE_APPLETS_FALLBACK
328 +       bool "exec uses applets as fallback"
329         default n
330 -       help
331 -         This is an experimental option which directs applets about to
332 -         call 'exec' to try and find an applicable busybox applet before
333 -         searching the PATH. This is typically done by exec'ing
334 -         /proc/self/exe.
335 -         This may affect shell, find -exec, xargs and similar applets.
336 -         They will use applets even if /bin/<applet> -> busybox link
337 -         is missing (or is not a link to busybox). However, this causes
338 -         problems in chroot jails without mounted /proc and with ps/top
339 -         (command name can be shown as 'exe' for applets started this way).
340  
341  config BUSYBOX_EXEC_PATH
342         string "Path to BusyBox executable"
343 -       default "/proc/self/exe"
344 +       default "/bin/busybox"
345         help
346           When Busybox applets need to run other busybox applets, BusyBox
347 -         sometimes needs to exec() itself.  When the /proc filesystem is
348 -         mounted, /proc/self/exe always points to the currently running
349 -         executable.  If you haven't got /proc, set this to wherever you
350 -         want to run BusyBox from.
351 +         sometimes needs to exec() itself.
352  
353  # These are auto-selected by other options
354  
355 @@ -294,7 +281,7 @@
356  config BUILD_LIBBUSYBOX
357         bool "Build shared libbusybox"
358         default n
359 -       depends on !FEATURE_PREFER_APPLETS
360 +       depends on !FEATURE_APPLETS_FALLBACK
361         help
362           Build a shared library libbusybox.so.N.N.N which contains all
363           busybox code.
364 @@ -503,7 +490,7 @@
365  
366  config INSTALL_APPLET_DONT
367         bool "not installed"
368 -       depends on FEATURE_INSTALLER || FEATURE_SH_STANDALONE || FEATURE_PREFER_APPLETS
369 +       depends on FEATURE_INSTALLER || FEATURE_SH_STANDALONE || FEATURE_APPLETS_FALLBACK
370         help
371           Do not install applet links. Useful when using the -install feature
372           or a standalone shell for rescue purposes.