base-files: sysupgrade: fix pseudobridge upgrades
[openwrt/openwrt.git] / package / base-files / files / lib / upgrade / common.sh
index 6f1880fba6c5a458394e7b9bfff5e5f2fcda4d16..ea03f7761f3326f9db3f8b54e561757f8de1dfa6 100644 (file)
@@ -2,8 +2,8 @@
 
 RAM_ROOT=/tmp/root
 
-ldd() { LD_TRACE_LOADED_OBJECTS=1 $*; }
-libs() { ldd $* | awk '{print $3}'; }
+[ -x /usr/bin/ldd ] || ldd() { LD_TRACE_LOADED_OBJECTS=1 $*; }
+libs() { ldd $* 2>/dev/null | sed -r 's/(.* => )?(.*) .*/\2/'; }
 
 install_file() { # <file> [ <file> ... ]
        for file in "$@"; do
@@ -21,9 +21,6 @@ install_bin() { # <file> [ <symlink> ... ]
        files=$1
        [ -x "$src" ] && files="$src $(libs $src)"
        install_file $files
-       [ -e /lib/ld.so.1 ] && {
-               install_file /lib/ld.so.1
-       }
        shift
        for link in "$@"; do {
                dest="$RAM_ROOT/$link"
@@ -51,15 +48,15 @@ supivot() { # <new_root> <old_root>
 
 run_ramfs() { # <command> [...]
        install_bin /bin/busybox /bin/ash /bin/sh /bin/mount /bin/umount        \
-               /sbin/pivot_root /usr/bin/wget /sbin/reboot /bin/sync /bin/dd   \
-               /bin/grep /bin/cp /bin/mv /bin/tar /usr/bin/md5sum "/usr/bin/[" \
-               /bin/dd /bin/vi /bin/ls /bin/cat /usr/bin/awk /usr/bin/hexdump  \
+               /sbin/pivot_root /sbin/reboot /bin/sync /bin/dd /bin/grep       \
+               /bin/cp /bin/mv /bin/tar /usr/bin/md5sum "/usr/bin/[" /bin/dd   \
+               /bin/vi /bin/ls /bin/cat /usr/bin/awk /usr/bin/hexdump          \
                /bin/sleep /bin/zcat /usr/bin/bzcat /usr/bin/printf /usr/bin/wc \
                /bin/cut /usr/bin/printf /bin/sync /bin/mkdir /bin/rmdir        \
-               /bin/rm /usr/bin/basename /bin/kill /bin/chmod
+               /bin/rm /usr/bin/basename /bin/kill /bin/chmod /usr/bin/find
 
+       install_bin /bin/uclient-fetch /bin/wget
        install_bin /sbin/mtd
-       install_bin /sbin/ubi
        install_bin /sbin/mount_root
        install_bin /sbin/snapshot
        install_bin /sbin/snapshot_tool
@@ -71,6 +68,7 @@ run_ramfs() { # <command> [...]
        install_bin /usr/sbin/ubirsvol
        install_bin /usr/sbin/ubirmvol
        install_bin /usr/sbin/ubimkvol
+       install_bin /usr/sbin/partx
        for file in $RAMFS_COPY_BIN; do
                install_bin ${file//:/ }
        done
@@ -99,6 +97,13 @@ kill_remaining() { # [ <signal> ]
        local sig="${1:-TERM}"
        echo -n "Sending $sig to remaining processes ... "
 
+       local my_pid=$$
+       local my_ppid=$(cut -d' ' -f4  /proc/$my_pid/stat)
+       local my_ppisupgraded=
+       grep -q upgraded /proc/$my_ppid/cmdline >/dev/null && {
+               local my_ppisupgraded=1
+       }
+       
        local stat
        for stat in /proc/[0-9]*/stat; do
                [ -f "$stat" ] || continue
@@ -113,18 +118,26 @@ kill_remaining() { # [ <signal> ]
                # Skip kernel threads
                [ -n "$cmdline" ] || continue
 
-               case "$name" in
-                       # Skip essential services
-                       *procd*|*upgraded*|*ash*|*init*|*watchdog*|*ssh*|*dropbear*|*telnet*|*login*|*hostapd*|*wpa_supplicant*|*nas*) : ;;
-
-                       # Killable process
-                       *)
-                               if [ $pid -ne $$ ] && [ $ppid -ne $$ ]; then
-                                       echo -n "$name "
-                                       kill -$sig $pid 2>/dev/null
-                               fi
-                       ;;
-               esac
+               if [ $$ -eq 1 ] || [ $my_ppid -eq 1 ] && [ -n "$my_ppisupgraded" ]; then
+                       # Running as init process, kill everything except me
+                       if [ $pid -ne $$ ] && [ $pid -ne $my_ppid ]; then
+                               echo -n "$name "
+                               kill -$sig $pid 2>/dev/null
+                       fi
+               else 
+                       case "$name" in
+                               # Skip essential services
+                               *procd*|*ash*|*init*|*watchdog*|*ssh*|*dropbear*|*telnet*|*login*|*hostapd*|*wpa_supplicant*|*nas*|*relayd*) : ;;
+
+                               # Killable process
+                               *)
+                                       if [ $pid -ne $$ ] && [ $ppid -ne $$ ]; then
+                                               echo -n "$name "
+                                               kill -$sig $pid 2>/dev/null
+                                       fi
+                               ;;
+                       esac
+               fi
        done
        echo ""
 }
@@ -173,14 +186,14 @@ get_image() { # <source> [ <command> ]
                *) cmd="cat";;
        esac
        if [ -z "$conc" ]; then
-               local magic="$(eval $cmd $from 2>/dev/null | dd bs=2 count=1 2>/dev/null | hexdump -n 2 -e '1/1 "%02x"')"
+               local magic="$(eval $cmd \"$from\" 2>/dev/null | dd bs=2 count=1 2>/dev/null | hexdump -n 2 -e '1/1 "%02x"')"
                case "$magic" in
                        1f8b) conc="zcat";;
                        425a) conc="bzcat";;
                esac
        fi
 
-       eval "$cmd $from 2>/dev/null ${conc:+| $conc}"
+       eval "$cmd \"$from\" 2>/dev/null ${conc:+| $conc}"
 }
 
 get_magic_word() {
@@ -201,12 +214,16 @@ jffs2_copy_config() {
        fi
 }
 
+# Flash firmware to MTD partition
+#
+# $(1): path to image
+# $(2): (optional) pipe command to extract firmware, e.g. dd bs=n skip=m
 default_do_upgrade() {
        sync
        if [ "$SAVE_CONFIG" -eq 1 ]; then
-               get_image "$1" | mtd $MTD_CONFIG_ARGS -j "$CONF_TAR" write - "${PART_NAME:-image}"
+               get_image "$1" "$2" | mtd $MTD_CONFIG_ARGS -j "$CONF_TAR" write - "${PART_NAME:-image}"
        else
-               get_image "$1" | mtd write - "${PART_NAME:-image}"
+               get_image "$1" "$2" | mtd write - "${PART_NAME:-image}"
        fi
 }