Skip to content

Instantly share code, notes, and snippets.

@xnox
Last active January 23, 2025 01:25
Show Gist options
  • Save xnox/5d522f187b817ca8f76f126c5c39ef96 to your computer and use it in GitHub Desktop.
Save xnox/5d522f187b817ca8f76f126c5c39ef96 to your computer and use it in GitHub Desktop.
diff --git a/Config.in b/Config.in
index ad0cd1e26..6a0727086 100644
--- a/Config.in
+++ b/Config.in
@@ -212,6 +212,15 @@ config INSTALL_NO_USR
will install applets only to /bin and /sbin,
never to /usr/bin or /usr/sbin.
+config INSTALL_ALWAYS_USR_BIN
+ bool "Always use /usr/bin"
+ default n
+ help
+ Disable use of /bin /sbin /usr/sbin and install everything into
+ /usr/bin. "busybox --install" and "make install" will install applets
+ only to /usr/bin. This is intenteded to be used with usr-merge and
+ sbin-merge systems.
+
config FEATURE_SUID
bool "Drop SUID state for most applets"
default y
diff --git a/Makefile.custom b/Makefile.custom
index 6f679c4e1..00c90743d 100644
--- a/Makefile.custom
+++ b/Makefile.custom
@@ -35,6 +35,9 @@ ifeq ($(CONFIG_FEATURE_INDIVIDUAL),y)
INSTALL_OPTS:= --binaries
LIBBUSYBOX_SONAME:= 0_lib/libbusybox.so.$(BB_VER)
endif
+ifeq ($(CONFIG_INSTALL_ALWAYS_USR_BIN),y)
+INSTALL_OPTS+= --always-usr-bin
+endif
install: $(srctree)/applets/install.sh busybox busybox.links
$(Q)DO_INSTALL_LIBS="$(strip $(LIBBUSYBOX_SONAME) $(DO_INSTALL_LIBS))" \
$(SHELL) $< $(CONFIG_PREFIX) $(INSTALL_OPTS)
diff --git a/applets/install.sh b/applets/install.sh
index 415896893..2d5f6e148 100755
--- a/applets/install.sh
+++ b/applets/install.sh
@@ -24,11 +24,14 @@ scriptwrapper="n"
binaries="n"
cleanup="0"
noclobber="0"
+usrbin="n"
+bindir="bin"
while [ ${#} -gt 0 ]; do
case "$1" in
--hardlinks) linkopts="-f";;
--symlinks) linkopts="-fs";;
--binaries) binaries="y";;
+ --always-usr-bin)usrbin="y"; bindir="usr/bin";;
--scriptwrapper) scriptwrapper="y"; swrapall="y";;
--sw-sh-hard) scriptwrapper="y"; linkopts="-f";;
--sw-sh-sym) scriptwrapper="y"; linkopts="-fs";;
@@ -60,8 +63,8 @@ if [ -n "$DO_INSTALL_LIBS" ] && [ x"$DO_INSTALL_LIBS" != x"n" ]; then
done
fi
-if [ x"$cleanup" = x"1" ] && [ -e "$prefix/bin/busybox" ]; then
- inode=`ls -i "$prefix/bin/busybox" | awk '{print $1}'`
+if [ x"$cleanup" = x"1" ] && [ -e "$prefix/$bindir/busybox" ]; then
+ inode=`ls -i "$prefix/$bindir/busybox" | awk '{print $1}'`
sub_shell_it=`
cd "$prefix"
for d in usr/sbin usr/bin sbin bin; do
@@ -76,9 +79,9 @@ if [ x"$cleanup" = x"1" ] && [ -e "$prefix/bin/busybox" ]; then
exit 0
fi
-rm -f "$prefix/bin/busybox" || exit 1
-mkdir -p "$prefix/bin" || exit 1
-install -m 755 busybox "$prefix/bin/busybox" || exit 1
+rm -f "$prefix/$bindir/busybox" || exit 1
+mkdir -p "$prefix/$bindir" || exit 1
+install -m 755 busybox "$prefix/$bindir/busybox" || exit 1
for i in $h; do
appdir=`dirname "$i"`
@@ -93,7 +96,7 @@ for i in $h; do
ln $linkopts busybox "$prefix/$i" || exit 1
else
rm -f "$prefix/$i"
- echo "#!/bin/busybox" >"$prefix/$i"
+ echo "#!/$bindir/busybox" >"$prefix/$i"
chmod +x "$prefix/$i"
fi
echo " $prefix/$i"
@@ -108,11 +111,11 @@ for i in $h; do
fi
else
if [ x"$linkopts" = x"-f" ]; then
- bb_path="$prefix/bin/busybox"
+ bb_path="$prefix/$bindir/busybox"
else
case "$appdir" in
/)
- bb_path="bin/busybox"
+ bb_path="$bindir/busybox"
;;
/bin)
bb_path="busybox"
@@ -121,7 +124,7 @@ for i in $h; do
bb_path="../bin/busybox"
;;
/usr/bin | /usr/sbin)
- bb_path="../../bin/busybox"
+ [ "$usrbin" = "y" ] && bb_path="busybox" || bb_path="../../bin/busybox"
;;
*)
echo "Unknown installation directory: $appdir"
diff --git a/include/applet_metadata.h b/include/applet_metadata.h
index 566ef3517..09e391fd5 100644
--- a/include/applet_metadata.h
+++ b/include/applet_metadata.h
@@ -10,14 +10,21 @@
/* order matters: used as index into "install_dir[]" in appletlib.c */
typedef enum bb_install_loc_t {
BB_DIR_ROOT = 0,
+#if ENABLE_INSTALL_ALWAYS_USR_BIN
+ BB_DIR_USR_BIN,
+ BB_DIR_BIN = BB_DIR_USR_BIN,
+ BB_DIR_SBIN = BB_DIR_USR_BIN,
+ BB_DIR_USR_SBIN = BB_DIR_USR_BIN,
+#else
BB_DIR_BIN,
BB_DIR_SBIN,
-#if ENABLE_INSTALL_NO_USR
+# if ENABLE_INSTALL_NO_USR
BB_DIR_USR_BIN = BB_DIR_BIN,
BB_DIR_USR_SBIN = BB_DIR_SBIN,
-#else
+# else
BB_DIR_USR_BIN,
BB_DIR_USR_SBIN,
+# endif
#endif
} bb_install_loc_t;
diff --git a/include/applets.src.h b/include/applets.src.h
index 60968cec7..3d495cd72 100644
--- a/include/applets.src.h
+++ b/include/applets.src.h
@@ -85,6 +85,11 @@ s - suid type:
# define BB_DIR_USR_SBIN BB_DIR_SBIN
#endif
+#if ENABLE_INSTALL_ALWAYS_USR_BIN
+# define BB_DIR_BIN BB_DIR_USR_BIN
+# define BB_DIR_SBIN BB_DIR_USR_BIN
+# define BB_DIR_USR_SBIN BB_DIR_USR_BIN
+#endif
INSERT
diff --git a/libbb/appletlib.c b/libbb/appletlib.c
index d2e5900b5..cf2a354a1 100644
--- a/libbb/appletlib.c
+++ b/libbb/appletlib.c
@@ -653,15 +653,24 @@ static void check_suid(int applet_no)
# if ENABLE_FEATURE_INSTALLER
static const char usr_bin [] ALIGN1 = "/usr/bin/";
+# if ENABLE_INSTALL_ALWAYS_USR_BIN
+static const char *const install_dir[] ALIGN_PTR = {
+ &usr_bin [8], /* "/" */
+ usr_bin,
+ usr_bin,
+ usr_bin,
+ usr_bin
+# else
static const char usr_sbin[] ALIGN1 = "/usr/sbin/";
static const char *const install_dir[] ALIGN_PTR = {
&usr_bin [8], /* "/" */
&usr_bin [4], /* "/bin/" */
&usr_sbin[4] /* "/sbin/" */
-# if !ENABLE_INSTALL_NO_USR
+# if !ENABLE_INSTALL_NO_USR
,usr_bin
,usr_sbin
-# endif
+# endif
+# endif
};
/* create (sym)links for each applet */
@xnox
Copy link
Author

xnox commented Jan 23, 2025

This is all bad:

$ ls -latr _install/usr/bin/egrep
lrwxrwxrwx 1 xnox xnox 17 Jan 23 01:00 _install/usr/bin/egrep -> ../../bin/busybox

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment