From patchwork Tue May 23 17:23:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonatan Schlag X-Patchwork-Id: 6909 Return-Path: Received: from mail01.ipfire.org (mail01.haj.ipfire.org [172.28.1.202]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) client-signature ECDSA (P-384)) (Client CN "mail01.haj.ipfire.org", Issuer "R3" (verified OK)) by web04.haj.ipfire.org (Postfix) with ESMTPS id 4QQh4d74FPz3wlf for ; Tue, 23 May 2023 17:24:01 +0000 (UTC) Received: from mail02.haj.ipfire.org (mail02.haj.ipfire.org [172.28.1.201]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) client-signature ECDSA (P-384)) (Client CN "mail02.haj.ipfire.org", Issuer "R3" (verified OK)) by mail01.ipfire.org (Postfix) with ESMTPS id 4QQh4c6ByZz9WJ; Tue, 23 May 2023 17:24:00 +0000 (UTC) Received: from mail02.haj.ipfire.org (localhost [127.0.0.1]) by mail02.haj.ipfire.org (Postfix) with ESMTP id 4QQh4c5dXTz30Kd; Tue, 23 May 2023 17:24:00 +0000 (UTC) Received: from mail01.ipfire.org (mail01.haj.ipfire.org [172.28.1.202]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) client-signature ECDSA (P-384)) (Client CN "mail01.haj.ipfire.org", Issuer "R3" (verified OK)) by mail02.haj.ipfire.org (Postfix) with ESMTPS id 4QQh4Z4lV7z2ypP for ; Tue, 23 May 2023 17:23:58 +0000 (UTC) Received: from [127.0.0.1] (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by mail01.ipfire.org (Postfix) with ESMTPSA id 4QQh4Z2ynSzfB; Tue, 23 May 2023 17:23:58 +0000 (UTC) DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=ipfire.org; s=202003ed25519; t=1684862638; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=l0WFUic92WrM648ZHJPvMYebbQv1b0HphHknhjodmiE=; b=d1LWJK4lhhH4A6oLf0zLedjTUd9Tx4KlCKhPlqG7SqLvMmFNZnC3cMqF1DXaBtY6wCK5AD TAk+c5N2kO5Z7dCQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ipfire.org; s=202003rsa; t=1684862638; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=l0WFUic92WrM648ZHJPvMYebbQv1b0HphHknhjodmiE=; b=Qoj+BcAd5yticOQ3h1Vh7y8uFiVbNevfwguxzRnDn7VpzW7l1LaHpiU5X+zRgrE4hqtyxs JNsil/UUqZhBZ20z/c6fW+UXDgkZhZVzjf4iVel9pR63eDZ2ChORiVaaloF/eklmQUbAIK v1vhinjqXZUG1I5KiTK/rYIbZlqqxHoph161mRL4AkEgg78sbNBu0KQeCybeZm+MSODvuF xkFrJmGJAAAUnhcW9fK8a2FfQoHigXYHwhdXbx5WkaHswqPB3MaqJM5u9yvmCufpc0mOiE G9y3KpgkTIs/2Z5XqEgMU1utjSAa56TfMDZ7mf/4iK4M9Qu9iUl4O4pKg6YXaA== From: Jonatan Schlag To: development@lists.ipfire.org Subject: [Patch RFC 10/15] network startup: Refactor how cmd args are processed Date: Tue, 23 May 2023 19:23:10 +0200 Message-Id: <20230523172314.7826-11-jonatan.schlag@ipfire.org> In-Reply-To: <20230523172314.7826-1-jonatan.schlag@ipfire.org> References: <20230523172314.7826-1-jonatan.schlag@ipfire.org> MIME-Version: 1.0 X-BeenThere: development@lists.ipfire.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: IPFire development talk List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: development-bounces@lists.ipfire.org Sender: "Development" This avoids eval and all other sorts of things. We also now exit when we get an invalid zone name. Signed-off-by: Jonatan Schlag --- src/initscripts/system/network | 52 ++++++++++++++++++++-------------- 1 file changed, 31 insertions(+), 21 deletions(-) diff --git a/src/initscripts/system/network b/src/initscripts/system/network index 06240f53c..008fbbe2b 100644 --- a/src/initscripts/system/network +++ b/src/initscripts/system/network @@ -31,41 +31,51 @@ if ! [[ "${DO}" == "start" || "${DO}" == "restart" || "${DO}" == "stop" ]]; the exit 1 fi -if [ -n "${1}" ]; then - ALL=0 - for i in green red blue orange; do - eval "${i}=0" - done -else - ALL=1 - for i in green red blue orange; do - eval "${i}=1" - done +declare -A ZONE_ACTION + +ZONE_ACTION[blue]=false +ZONE_ACTION[green]=false +ZONE_ACTION[orange]=false +ZONE_ACTION[red]=false + +if [ $# -eq 0 ]; then + ZONE_ACTION[blue]=true + ZONE_ACTION[green]=true + ZONE_ACTION[orange]=true + ZONE_ACTION[red]=true fi -while [ ! $# = 0 ]; do +while [ $# -ne 0 ]; do + ZONE_VALID=false for i in green red blue orange; do if [ "${i}" == "${1}" ]; then - eval "${i}=1" + ZONE_ACTION[${i}]=true + ZONE_VALID=true shift + break fi done + + if ! ${ZONE_VALID}; then + echo "'${1}' is not a valid zone. Cannot go on." + exit 1 + fi done case "${DO}" in start) # Starting interfaces... # GREEN - [ "$green" == "1" ] && /etc/rc.d/init.d/networking/green start + ${ZONE_ACTION[green]} && /etc/rc.d/init.d/networking/green start # BLUE - [ "$blue" == "1" ] && /etc/rc.d/init.d/networking/blue start + ${ZONE_ACTION[blue]} && /etc/rc.d/init.d/networking/blue start # ORANGE - [ "$orange" == "1" ] && /etc/rc.d/init.d/networking/orange start + ${ZONE_ACTION[orange]} && /etc/rc.d/init.d/networking/orange start # RED - [ "$red" == "1" ] && /etc/rc.d/init.d/networking/red start + ${ZONE_ACTION[red]} && /etc/rc.d/init.d/networking/red start boot_mesg "Mounting network file systems..." mount -a -O _netdev @@ -79,16 +89,16 @@ case "${DO}" in # Stopping interfaces... # GREEN - [ "$green" == "1" ] && /etc/rc.d/init.d/networking/green stop + ${ZONE_ACTION[green]} && /etc/rc.d/init.d/networking/green stop # BLUE - [ "$blue" == "1" ] && /etc/rc.d/init.d/networking/blue stop + ${ZONE_ACTION[blue]} && /etc/rc.d/init.d/networking/blue stop # ORANGE - [ "$orange" == "1" ] && /etc/rc.d/init.d/networking/orange stop + ${ZONE_ACTION[orange]} && /etc/rc.d/init.d/networking/orange stop # RED - if [ "$red" == "1" ]; then + if ${ZONE_ACTION[red]}; then /etc/rc.d/init.d/networking/red stop fi @@ -97,7 +107,7 @@ case "${DO}" in restart) for i in green red blue orange; do - if [ "${!i}" == "1" ]; then + if {ZONE_ACTION[${i}]}; then ARGS+=" ${i}" fi done