From b972651a062da141915a760787115d611f99a481 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beat=20K=C3=BCng?= Date: Thu, 27 Sep 2018 11:25:23 +0200 Subject: [PATCH] posix shell: use /bin/sh instead of bash This uses the systems default shell: - Ubuntu: dash - Fedora: bash Since bash is invoked via /bin/sh, it operates in POSIX mode: https://tiswww.case.edu/php/chet/bash/POSIX - remove '# Ignore the expand_aliases command in zshell.' Not needed because the shell operates in POSIX mode - [[ is bashism -> use [ - autostart_files=( $autostart_file_match ) is not supported in dash, so use 'ls' - shellcheck runs the dash flavor, since dash is a minimalistic shell. Tested on dash & bash. --- ROMFS/px4fmu_common/init.d-posix/rcS | 7 ++----- Tools/run-shellcheck.sh | 1 + platforms/posix/CMakeLists.txt | 8 ++++---- platforms/posix/src/main.cpp | 27 +++++++++++---------------- platforms/posix/src/px4-alias.sh_in | 20 +++++++++++--------- 5 files changed, 29 insertions(+), 34 deletions(-) diff --git a/ROMFS/px4fmu_common/init.d-posix/rcS b/ROMFS/px4fmu_common/init.d-posix/rcS index 62c05d7a8f..31d5179c00 100644 --- a/ROMFS/px4fmu_common/init.d-posix/rcS +++ b/ROMFS/px4fmu_common/init.d-posix/rcS @@ -5,7 +5,7 @@ # shellcheck disable=SC1091 . px4-alias.sh -SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +SCRIPT_DIR="$(CDPATH='' cd -- "$(dirname -- "$0")" && pwd)" # # Main SITL startup script @@ -149,10 +149,7 @@ then fi # Autostart ID -autostart_file_match="etc/init.d-posix/$(param show -q SYS_AUTOSTART)_*" -# shellcheck disable=SC2206 -autostart_files=( $autostart_file_match ) -autostart_file="${autostart_files[0]}" # use first match, but there should really only be one +autostart_file=$( ls -- etc/init.d-posix/"$(param show -q SYS_AUTOSTART)"_* ) if [ ! -e "$autostart_file" ]; then echo "Error: no autostart file found" exit -1 diff --git a/Tools/run-shellcheck.sh b/Tools/run-shellcheck.sh index bf890f6f77..3354bbc30c 100755 --- a/Tools/run-shellcheck.sh +++ b/Tools/run-shellcheck.sh @@ -32,6 +32,7 @@ shellcheck -x \ -e SC2148 \ -e SC2166 \ -e SC2039 \ + --shell=dash \ $scripts ret=$? if [ $ret -ne 0 ]; then diff --git a/platforms/posix/CMakeLists.txt b/platforms/posix/CMakeLists.txt index c4e2f10210..ef2dbfe39f 100644 --- a/platforms/posix/CMakeLists.txt +++ b/platforms/posix/CMakeLists.txt @@ -7,9 +7,9 @@ get_property(module_libraries GLOBAL PROPERTY PX4_MODULE_LIBRARIES) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) -set(PX4_BASH_PREFIX "px4-") +set(PX4_SHELL_COMMAND_PREFIX "px4-") -add_definitions("-DPX4_BASH_PREFIX=\"${PX4_BASH_PREFIX}\"") +add_definitions("-DPX4_SHELL_COMMAND_PREFIX=\"${PX4_SHELL_COMMAND_PREFIX}\"") px4_posix_generate_builtin_commands( OUT apps @@ -18,7 +18,7 @@ px4_posix_generate_builtin_commands( px4_posix_generate_alias( OUT ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/px4-alias.sh MODULE_LIST ${module_libraries} - PREFIX ${PX4_BASH_PREFIX} + PREFIX ${PX4_SHELL_COMMAND_PREFIX} ) if (("${BOARD}" STREQUAL "eagle") OR ("${BOARD}" STREQUAL "excelsior")) @@ -97,7 +97,7 @@ endif() # Module Symlinks px4_posix_generate_symlinks( MODULE_LIST ${module_libraries} - PREFIX ${PX4_BASH_PREFIX} + PREFIX ${PX4_SHELL_COMMAND_PREFIX} TARGET px4 ) diff --git a/platforms/posix/src/main.cpp b/platforms/posix/src/main.cpp index 46d6b92833..f7d301392b 100644 --- a/platforms/posix/src/main.cpp +++ b/platforms/posix/src/main.cpp @@ -99,8 +99,7 @@ static void register_sig_handler(); static void set_cpu_scaling(); static int create_symlinks_if_needed(std::string &data_path); static int create_dirs(); -static int run_startup_bash_script(const std::string &commands_file, const std::string &absolute_binary_path, - int instance); +static int run_startup_script(const std::string &commands_file, const std::string &absolute_binary_path, int instance); static std::string get_absolute_binary_path(const std::string &argv0); static void wait_to_exit(); static bool is_already_running(int instance); @@ -123,7 +122,7 @@ int main(int argc, char **argv) bool pxh_off = false; /* Symlinks point to all commands that can be used as a client with a prefix. */ - const char prefix[] = PX4_BASH_PREFIX; + const char prefix[] = PX4_SHELL_COMMAND_PREFIX; int path_length = 0; std::string absolute_binary_path; // full path to the px4 binary being executed @@ -275,7 +274,7 @@ int main(int argc, char **argv) px4::init_once(); px4::init(argc, argv, "px4"); - ret = run_startup_bash_script(commands_file, absolute_binary_path, instance); + ret = run_startup_script(commands_file, absolute_binary_path, instance); // We now block here until we need to exit. if (pxh_off) { @@ -479,16 +478,12 @@ std::string get_absolute_binary_path(const std::string &argv0) return pwd() + "/" + base; } -int run_startup_bash_script(const std::string &commands_file, const std::string &absolute_binary_path, - int instance) +int run_startup_script(const std::string &commands_file, const std::string &absolute_binary_path, + int instance) { -#ifdef __DF_BEBOP - std::string bash_command("sh "); -#else - std::string bash_command("bash "); -#endif + std::string shell_command("/bin/sh "); - bash_command += commands_file + ' ' + std::to_string(instance); + shell_command += commands_file + ' ' + std::to_string(instance); // Update the PATH variable to include the absolute_binary_path // (required for the px4-alias.sh script and px4-* commands). @@ -526,12 +521,12 @@ int run_startup_bash_script(const std::string &commands_file, const std::string } - PX4_INFO("Calling startup script: %s", bash_command.c_str()); + PX4_INFO("Calling startup script: %s", shell_command.c_str()); int ret = 0; - if (!bash_command.empty()) { - ret = system(bash_command.c_str()); + if (!shell_command.empty()) { + ret = system(shell_command.c_str()); if (ret == 0) { PX4_INFO("Startup script returned successfully"); @@ -560,7 +555,7 @@ void print_usage() printf("\n"); printf(" px4 [-h|-d] [-s ] [-t ] [] [-i ]\n"); printf("\n"); - printf(" -s bash start script to be used as startup (default=etc/init.d/rcS)\n"); + printf(" -s shell script to be used as startup (default=etc/init.d/rcS)\n"); printf(" directory where startup files and mixers are located,\n"); printf(" (if not given, CWD is used)\n"); printf(" -i px4 instance id to run multiple instances [0...N], default=0\n"); diff --git a/platforms/posix/src/px4-alias.sh_in b/platforms/posix/src/px4-alias.sh_in index 351f8a91db..6480c41027 100644 --- a/platforms/posix/src/px4-alias.sh_in +++ b/platforms/posix/src/px4-alias.sh_in @@ -2,14 +2,9 @@ # File is auto-generated by cmake compilation, do not edit. -# Ignore the expand_aliases command in zshell. -if [ ! -n "$ZSH_VERSION" ]; then - shopt -s expand_aliases -fi - # Map the NuttX-style variable definition 'set ' to something that # bash and alternatives understand -# define _set first because sh does not like overwriting set directly +# define _set first because sh (POSIX shell) does not like overwriting set directly _set() { eval $1=$2 } @@ -22,8 +17,15 @@ alias set=_set # scripts) sh() { script="$1" - [[ "$script" != /* ]] && script="/$script" - source "$(pwd)$script" + case "$script" in + "/"*) + script="$script" + ;; + *) + script="/$script" + ;; + esac + . "$(pwd)$script" } # Don't stop on errors. @@ -32,6 +34,6 @@ sh() { # Arguments passed to this script: # $1: optional instance id px4_instance=0 -[[ -n "$1" ]] && px4_instance=$1 +[ -n "$1" ] && px4_instance=$1 ${alias_string}