Bug 9786 - [RFC] patch, ash: look for internal applets as last resource to find and execute a command
Summary: [RFC] patch, ash: look for internal applets as last resource to find and exec...
Status: RESOLVED FIXED
Alias: None
Product: buildroot
Classification: Unclassified
Component: Other (show other bugs)
Version: 2015.11
Hardware: All Linux
: P1 normal
Target Milestone: 2015.11
Assignee: unassigned
URL:
Keywords: patch
Depends on:
Blocks:
 
Reported: 2017-03-28 06:59 UTC by Jordi Pujol
Modified: 2018-04-23 05:14 UTC (History)
3 users (show)

See Also:
Host: Apple
Target: iCloud activation
Build: Ipad4th generation


Attachments
look for internal applets as last resource to find and execute a command (4.26 KB, patch)
2017-03-28 06:59 UTC, Jordi Pujol
Details
patch v2-ash.c (5.25 KB, patch)
2017-03-30 14:18 UTC, Jordi Pujol
Details
patch v2-execable.c (2.62 KB, patch)
2017-03-30 14:19 UTC, Jordi Pujol
Details
patch v2-libbb.h (1.97 KB, patch)
2017-03-30 14:19 UTC, Jordi Pujol
Details
patch v2-wish.c (1.18 KB, patch)
2017-03-30 14:20 UTC, Jordi Pujol
Details
V2. Service program to synchronize disks on shutdown (883 bytes, text/plain)
2017-03-30 14:22 UTC, Jordi Pujol
Details
V2. Systemd service to synchronize disks on shutdown (290 bytes, text/plain)
2017-03-30 14:22 UTC, Jordi Pujol
Details
V2. initramfs hook for busybox (1.24 KB, text/plain)
2017-03-30 14:23 UTC, Jordi Pujol
Details
patch v3-ash.c (5.96 KB, patch)
2017-04-11 12:30 UTC, Jordi Pujol
Details
patch v3-execable.c (3.12 KB, patch)
2017-04-11 12:31 UTC, Jordi Pujol
Details
patch v3-libbb.h (2.18 KB, patch)
2017-04-11 12:31 UTC, Jordi Pujol
Details
patch v3-wish.c (1.50 KB, patch)
2017-04-11 12:32 UTC, Jordi Pujol
Details
V3. Systemd service to synchronize disks on shutdow (249 bytes, text/plain)
2017-04-11 12:33 UTC, Jordi Pujol
Details
V3. Service program to synchronize disks on shutdown (823 bytes, text/plain)
2017-04-11 12:34 UTC, Jordi Pujol
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Jordi Pujol 2017-03-28 06:59:59 UTC
Created attachment 6966 [details]
look for internal applets as last resource to find and execute a command

This patch is an enhancement for ash, it will look for internal applets as last resource to find and execute a command.
Even more, lets the user control the order of looking for internal commands by changing the PATH.
The user can specify the preference of internal applets by including a directory named %applet in the environment PATH.
In example, busybox will act like if FEATURE_SH_STANDALONE was enabled when setting PATH to this value:
      export PATH=%applet:/sbin:/usr/sbin:/bin:/usr/bin
Another example, use only internal applets and don't search in PATH:
      export PATH=""

Maybe applying this patch will make unnecessary the FEATURE_SH_STANDALONE and neither the package busybox-static.
Comment 1 Jordi Pujol 2017-03-28 14:58:57 UTC
trying the command:

find -exec cmd ARG;

have found a pending point, to make a reliable patch we should modify also libb/execable.c that depends on FEATURE_SH_STANDALONE
Comment 2 Jordi Pujol 2017-03-30 14:16:20 UTC
I include a new version of this modification, Now the ash shell works properly as desired,

I have tested several commands: from the most common to complex ones like find, which, xargs.

***************************************
~ $ PATH=/usr/local/bin:/usr/bin:/bin
~ $ # executing which command from filesystem
~ $ which -a uuencode df sleep
/usr/bin/uuencode
/bin/df
/bin/sleep
~ $ PATH=%applet:/usr/local/bin:/usr/bin:/bin
~ $ which -a uuencode df sleep
uuencode
/usr/bin/uuencode
df
/bin/df
sleep
/bin/sleep
***************************************

It also boots the system correctly using Debian initramfs, only two links to /bin/busybox are needed to mount /proc, they are mkdir and mount. For this I have modified /usr/share/initramfs-tools/hooks/zz-busybox script, included in this message.

Another interesting script, also included. At the beginning of the process, it sets PATH=%applet:$PATH so that busybox prefers the applets, uses only Busybox commands and runs on a system that is in shutdown process, unmounting the disks. Synchronizes data in memory with the disk and mounts the data partitions in read-only mode.

In short, a single modified Busybox program satisfies two different needs, first need is create and execute a simpler initramfs and second to execute a script that prefers the applets before than other commands in the PATH.

Is it worth improving the development of this alternative?

Comments are welcome.

Note:
Debian repository. Have stored the corresponding packages in:

deb [arch=amd64,i386] http://livenet.ddns.net/ftp/debian/ sid main
deb-src http://livenet.ddns.net/ftp/debian/ sid main
Comment 3 Jordi Pujol 2017-03-30 14:18:22 UTC
Created attachment 6971 [details]
patch v2-ash.c

V2. Changes for the ash.c source
Comment 4 Jordi Pujol 2017-03-30 14:19:05 UTC
Created attachment 6976 [details]
patch v2-execable.c

V2. Changes for the execable.c source
Comment 5 Jordi Pujol 2017-03-30 14:19:54 UTC
Created attachment 6981 [details]
patch v2-libbb.h

V2. Changes for the libbb.h source
Comment 6 Jordi Pujol 2017-03-30 14:20:35 UTC
Created attachment 6986 [details]
patch v2-wish.c

V2. Changes for the which.c source
Comment 7 Jordi Pujol 2017-03-30 14:22:06 UTC
Created attachment 6991 [details]
V2. Service program to synchronize disks on shutdown

V2. Syncronize disks on shutdown
Comment 8 Jordi Pujol 2017-03-30 14:22:54 UTC
Created attachment 6996 [details]
V2. Systemd service to synchronize disks on shutdown

V2. Synchronize disks on shutdown
Comment 9 Jordi Pujol 2017-03-30 14:23:48 UTC
Created attachment 7001 [details]
V2. initramfs hook for busybox

V2. initramfs hook for busybox
Comment 10 Jordi Pujol 2017-04-11 12:26:16 UTC
Version 3.
This version is near to achieve the desired goal,
ash looks for internal applets according to the sequence specified in the PATH, or when %applet is not in PATH, therefore internal applets are the latest to search for.
Also now ash understands %applet/ as the path name to internal applets. We can also use it preceding a command, in example:

ash# %applet/find -name '*old' -exec %applet/rm '{}' \;
Comment 11 Jordi Pujol 2017-04-11 12:29:19 UTC
Version 3:
Also updating the service to synchronize and remount read only disks on shutdown, because before I have uploaded an obsolete version.
Comment 12 Jordi Pujol 2017-04-11 12:30:36 UTC
Created attachment 7011 [details]
patch v3-ash.c
Comment 13 Jordi Pujol 2017-04-11 12:31:04 UTC
Created attachment 7016 [details]
patch v3-execable.c
Comment 14 Jordi Pujol 2017-04-11 12:31:36 UTC
Created attachment 7021 [details]
patch v3-libbb.h
Comment 15 Jordi Pujol 2017-04-11 12:32:20 UTC
Created attachment 7026 [details]
patch v3-wish.c
Comment 16 Jordi Pujol 2017-04-11 12:33:35 UTC
Created attachment 7031 [details]
V3. Systemd service to synchronize disks on shutdow
Comment 17 Jordi Pujol 2017-04-11 12:34:16 UTC
Created attachment 7036 [details]
V3. Service program to synchronize disks on shutdown