From nobody Wed Nov 25 21:51:32 2026 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4jZYKd0gtlz72HJ6 for ; Wed, 25 Nov 2026 21:51:33 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mx1.freebsd.org (Postfix) with ESMTPS id 4jZYKc4sKyz4lfd for ; Wed, 25 Nov 2026 21:51:32 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1795643492; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=C+Ir5xRdtaKc5VK7U0Kq6Z+PwpnJKxmz5pR2nOT3Ry8=; b=ed4vAziQ9gu/wED2hdt9JddQFNuFFSdWGQYxgyoB62zXrcGwtBXw3RdepDy1hDWiabhi8u FCgfj3l7JOHWWCL2ViWJN84hqVZuFDJMJ5wyc3XC82hLykg+oaLnH1pievNxqws8fRZ6hn OlkHe2SD34LnQ2koWMuUZbGkfmFE0filGJkgwbPZDp7h0jrAsFlQrCKkZCTMGabAbLVSgH WzSDEfBAG+56QbcZL3RA+a+UoaHNTc//jhUR97k0eFoRXYhvXiRLDF94yOzuimxlXl8gRW p2Un2VYgO5umTNCuPoWYrjf95WKVdh/D2invHWpv6IyQErx+appIgY79r9dR5w== ARC-Seal: i=1; a=rsa-sha256; d=freebsd.org; s=dkim; cv=none; t=1795643492; b=pgfJfIYb/ce2CBYkoztKm65TruSBgPc9m2TM6qbuyiNagypF/IM3eLdzHzvUr3Y5kbT7bH rdIpq5kSHU2zvRLEKwmuf2IuuocXhX8Gk0pddjBvJBwRKN1dLROgBF4bgKH4F3dCmmpwe7 KR7z/biYBiEU8e2E1XLZ28tLP8RseCrpfpS5atPOh7nVO8P9HFyFnJJIA4mUSIbG6zgX/3 EB0NiW4Exg/GZuxuwaVyMozVCcPmTPelcOqKl3NgvDUXngluSX0Jmqu2ko89o6yFXn8KoZ Zn1hnA9y8vnH63a5Fz72SYvbUqNMPLH2YBGxgtQj1PUdBJqAGtiomb5LoTCcxw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1795643492; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=C+Ir5xRdtaKc5VK7U0Kq6Z+PwpnJKxmz5pR2nOT3Ry8=; b=jK3/muaR6VdPPQuetlyW4/64biacffEXeZRcGuep82oM51k4bhRSn989jcKmqDWFa9l/72 gp5B8rLAw01yhLV0iBbpXOC1+Wp5tZC0yD16e19cmhda+TtXNUCick61bvb6wOFJYmQLnD bWBS6yK5FwRCX3dZ4sa+87eMOepy6a8JnxypJhCfJ9T+HtJcIauMVT/vCqSZSpP2+wHeKa glJZ6HxGlHA1sFd+l5MLt8G3h8MoOwA5ecOGiQ23ULnstliA5bQLg2QmP6huDzEEIjs0DG Jj7nuBGGuq2GwfmNhGdLVx/SYQ0wd6iGs2QUX5Snj9MtMBtS0qHiv03wgx9CKw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) by mxrelay.nyi.freebsd.org (Postfix) with ESMTP id 4jZYKc4XdwzlFx for ; Wed, 25 Nov 2026 21:51:32 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from git (uid 1279) (envelope-from git@FreeBSD.org) id 139f9 by gitrepo.freebsd.org (DragonFly Mail Agent v0.13+ on gitrepo.freebsd.org); Wed, 25 Nov 2026 21:51:32 +0000 To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Devin Teske Subject: git: 0b1de275ebc4 - main - sysrc: Accept values that contain a newline List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all X-BeenThere: dev-commits-src-all@freebsd.org List-Id: Commit messages for all branches of the src repository List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: dteske X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 0b1de275ebc43cdb51f2c9c477339de810635fff Auto-Submitted: auto-generated Date: Wed, 25 Nov 2026 21:51:32 +0000 Message-Id: <6b075864.139f9.4f44e428@gitrepo.freebsd.org> The branch main has been updated by dteske: URL: https://cgit.FreeBSD.org/src/commit/?id=0b1de275ebc43cdb51f2c9c477339de810635fff commit 0b1de275ebc43cdb51f2c9c477339de810635fff Author: Devin Teske AuthorDate: 2026-11-25 21:47:08 +0000 Commit: Devin Teske CommitDate: 2026-11-25 21:47:38 +0000 sysrc: Accept values that contain a newline awk -v rejects a newline in the value, and the old writer treated that failure as an empty edit. The temporary file then held only the new assignment, so the rest of the configuration was lost. Pass the value in the environment and replace the last assignment by joining that line through end of file. A quoted newline stays with the value, and the text after it stays after the closer. If awk exits above 1, leave the file alone. sh -n still refuses a value that would break the quotes. PR: 283729 MFC after: 2 weeks Reviewed by: kfv Differential Revision: https://reviews.freebsd.org/D61743 --- usr.sbin/sysrc/sysrc | 2 +- usr.sbin/sysrc/sysrc.8 | 5 +-- usr.sbin/sysrc/sysrc.subr | 81 +++++++++++++++++++++++++------------- usr.sbin/sysrc/tests/sysrc_test.sh | 67 +++++++++++++++++++++++++++++++ 4 files changed, 123 insertions(+), 32 deletions(-) diff --git a/usr.sbin/sysrc/sysrc b/usr.sbin/sysrc/sysrc index 986f726a7435..d79bb4952c40 100644 --- a/usr.sbin/sysrc/sysrc +++ b/usr.sbin/sysrc/sysrc @@ -16,7 +16,7 @@ pgm="${0##*/}" # Program basename # # Version information # -SYSRC_VERSION="9.2 Nov-18,2026" +SYSRC_VERSION="9.3 Nov-18,2026" # # Options diff --git a/usr.sbin/sysrc/sysrc.8 b/usr.sbin/sysrc/sysrc.8 index 40f60c6ff077..9067ec55a6a2 100644 --- a/usr.sbin/sysrc/sysrc.8 +++ b/usr.sbin/sysrc/sysrc.8 @@ -475,9 +475,8 @@ The following standard commands are required by .Xr mv 1 , .Xr rm 1 , .Xr sh 1 , -.Xr stat 1 , and -.Xr tail 1 . +.Xr stat 1 . .Sh FILES .Bl -tag -width ".Pa /etc/defaults/rc.conf" -compact .It Pa /etc/defaults/rc.conf @@ -542,11 +541,11 @@ Return if $cloned_interfaces is set: .Dl sysrc cloned_interfaces+"alternate" .Sh SEE ALSO +.Xr rc.conf 5 , .Xr jail 8 , .Xr jexec 8 , .Xr jls 8 , .Xr rc 8 , -.Xr rc.conf 5 , .Xr rc.subr 8 , .Xr sysctl 8 .Sh HISTORY diff --git a/usr.sbin/sysrc/sysrc.subr b/usr.sbin/sysrc/sysrc.subr index 9ee396f96086..c8c47b2a1e85 100644 --- a/usr.sbin/sysrc/sysrc.subr +++ b/usr.sbin/sysrc/sysrc.subr @@ -30,6 +30,7 @@ FAILURE=1 msg_cannot_create_permission_denied="%s: cannot create %s: Permission denied" msg_permission_denied="%s: %s: Permission denied" msg_previous_syntax_errors="%s: Not overwriting \`%s' due to previous syntax errors" +msg_awk_failure="%s: Not overwriting \`%s' due to awk(1) failure" # # Valid characters that can appear in an sh(1) variable name @@ -1093,61 +1094,80 @@ f_sysrc_desc() f_sysrc_set_awk=' # Variables that should be defined on the invocation line: # -v varname="varname" -# -v new_value="new_value" +# The new value is ENVIRON["SYSRC_NV"], not -v. awk -v rejects a newline. # BEGIN { regex = "^[[:space:]]*"varname"=" - found = retval = 0 + new_value = ENVIRON["SYSRC_NV"] + retval = 0 } -{ - # If already found... just spew - if ( found ) { print; next } +{ lines[NR] = $0 } +END { + last = 0 + for (i = 1; i <= NR; i++) + if ( match(lines[i], regex) ) last = i + if ( !last ) { + for (i = 1; i <= NR; i++) print lines[i] + exit 0 + } + for (i = 1; i < last; i++) print lines[i] - # Does this line match an assignment to our variable? - if ( ! match($0, regex) ) { print; next } + # From the last assignment through EOF, so a quoted newline stays + # with the value and the remainder of the file stays after it. + text = lines[last] + for (i = last + 1; i <= NR; i++) + text = text "\n" lines[i] - # Save important match information - found = 1 + match(text, regex) matchlen = RSTART + RLENGTH - 1 - - # Store the value text for later munging - value = substr($0, matchlen + 1, length($0) - matchlen) - - # Store the first character of the value + value = substr(text, matchlen + 1, length(text) - matchlen) t1 = t2 = substr(value, 0, 1) # Assignment w/ back-ticks, expression, or misc. # We ignore these since we did not generate them # - if ( t1 ~ /[`$\\]/ ) { retval = 1; print; next } + if ( t1 ~ /[`$\\]/ ) { retval = 1; print text } # Assignment w/ single-quoted value else if ( t1 == "'\''" ) { sub(/^'\''[^'\'']*/, "", value) if ( length(value) == 0 ) t2 = "" sub(/^'\''/, "", value) + printf "%s%c%s%c%s\n", substr(text, 0, matchlen), \ + t1, new_value, t2, value } # Assignment w/ double-quoted value + # NB: \\ does not escape the closing quote else if ( t1 == "\"" ) { - sub(/^"(.*\\\\+")*[^"]*/, "", value) + sub(/^"([^"\\]|\\.)*/, "", value) if ( length(value) == 0 ) t2 = "" sub(/^"/, "", value) + printf "%s%c%s%c%s\n", substr(text, 0, matchlen), \ + t1, new_value, t2, value } # Assignment w/ non-quoted value else if ( t1 ~ /[^[:space:];]/ ) { t1 = t2 = "\"" sub(/^[^[:space:]]*/, "", value) + printf "%s%c%s%c%s\n", substr(text, 0, matchlen), \ + t1, new_value, t2, value } # Null-assignment - else if ( t1 ~ /[[:space:];]/ ) { t1 = t2 = "\"" } + else if ( t1 ~ /[[:space:];]/ ) { + t1 = t2 = "\"" + printf "%s%c%s%c%s\n", substr(text, 0, matchlen), \ + t1, new_value, t2, value + } + + else + printf "%s%c%s%c%s\n", substr(text, 0, matchlen), \ + t1, new_value, t2, value - printf "%s%c%s%c%s\n", substr($0, 0, matchlen), \ - t1, new_value, t2, value + exit retval } -END { exit retval } ' f_sysrc_set() { @@ -1248,21 +1268,26 @@ f_sysrc_set() # # Operate on the matching file, replacing only the last occurrence. # - # Use awk to ensure LF at end of each line, else files without ending - # LF will trigger a bug in `tail -r' where last two lines are joined. + # A newline in the value is data. awk -v cannot carry one, so the + # value is passed in the environment. Physical lines are not reversed; + # a newline inside quotes belongs to the value. # local new_contents retval - new_contents=$( awk 1 "$file" 2> /dev/null | tail -r ) - new_contents=$( echo "$new_contents" | awk -v varname="$varname" \ - -v new_value="$new_value" "$f_sysrc_set_awk" ) + new_contents=$( SYSRC_NV="$new_value" awk -v varname="$varname" \ + "$f_sysrc_set_awk" "$file" ) retval=$? + if [ "$retval" -gt 1 ]; then + f_err "$msg_awk_failure\n" "$pgm" "$file" + rm -f "$tmpfile" + return $FAILURE + fi # # Write the temporary file contents. # - echo "$new_contents" | tail -r > "$tmpfile" || return $FAILURE - if [ $retval -ne $SUCCESS ]; then - echo "$varname=\"$new_value\"" >> "$tmpfile" + printf '%s\n' "$new_contents" > "$tmpfile" || return $FAILURE + if [ "$retval" -ne "$SUCCESS" ]; then + printf '%s="%s"\n' "$varname" "$new_value" >> "$tmpfile" fi # diff --git a/usr.sbin/sysrc/tests/sysrc_test.sh b/usr.sbin/sysrc/tests/sysrc_test.sh index 3e051be9ed80..7a5a6f90b85a 100644 --- a/usr.sbin/sysrc/tests/sysrc_test.sh +++ b/usr.sbin/sysrc/tests/sysrc_test.sh @@ -506,10 +506,77 @@ strappend_body() sysrc -f "${SYSRC_CONF}" 'cron_flags+=-J 15' } +atf_test_case set_variable_newline +set_variable_newline_head() +{ + atf_set "descr" "Assignment values may contain a newline" +} +set_variable_newline_body() +{ + local val + + printf '%s\n' 'test_enable="NO"' 'others_enable="YES"' > "${SYSRC_CONF}" + val=$(printf 'YES\nNOX') + val="${val%X}" + + atf_check -o inline:"test_enable: NO -> YES +NO +" sysrc -f "${SYSRC_CONF}" "test_enable=$val" + + printf '%s\n' 'test_enable="YES' 'NO"' 'others_enable="YES"' > expected + atf_check -o file:expected cat "${SYSRC_CONF}" + + atf_check -o inline:"YES +NO +" sysrc -f "${SYSRC_CONF}" -nr test_enable + + # A new variable is appended. The neighbor stays. + printf '%s\n' 'keep=1' > "${SYSRC_CONF}" + atf_check -o inline:"fresh: -> YES +NO +" sysrc -f "${SYSRC_CONF}" "fresh=$val" + printf '%s\n' 'keep=1' 'fresh="YES' 'NO"' > expected + atf_check -o file:expected cat "${SYSRC_CONF}" + + # A quote that would break the string does not replace the file + printf '%s\n' 'test_enable="YES' 'NO"' 'others_enable="YES"' \ + > "${SYSRC_CONF}" + cp "${SYSRC_CONF}" saved + val=$(printf 'YE"S\nNOX') + val="${val%X}" + atf_check -s exit:1 -e match:"syntax errors" \ + sysrc -f "${SYSRC_CONF}" "test_enable=$val" + atf_check -o file:saved cat "${SYSRC_CONF}" +} + +atf_test_case set_variable_backslash +set_variable_backslash_head() +{ + atf_set "descr" "A value ending in a backslash can be replaced" +} +set_variable_backslash_body() +{ + # "C:\\" stores the value C:\ and the closer is not escaped + printf '%s\n' 'bar="C:\\"' 'keep=1' > "${SYSRC_CONF}" + atf_check -o inline:'bar: C:\ -> NEW\n' \ + sysrc -f "${SYSRC_CONF}" bar=NEW + printf '%s\n' 'bar="NEW"' 'keep=1' > expected + atf_check -o file:expected cat "${SYSRC_CONF}" + + # A later value ending in a backslash belongs to that later name + printf '%s\n' 'aa="old"' 'bb="C:\\"' > "${SYSRC_CONF}" + atf_check -o inline:'aa: old -> NEW\n' \ + sysrc -f "${SYSRC_CONF}" aa=NEW + printf '%s\n' 'aa="NEW"' 'bb="C:\\"' > expected + atf_check -o file:expected cat "${SYSRC_CONF}" +} + atf_init_test_cases() { atf_add_test_case check_variable atf_add_test_case set_variable + atf_add_test_case set_variable_newline + atf_add_test_case set_variable_backslash atf_add_test_case remove_variable_x_flag atf_add_test_case append_list atf_add_test_case strappend