From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by finch.gentoo.org (Postfix) with ESMTPS id 1E55B158009 for ; Sat, 24 Jun 2023 17:01:46 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id 4FCF3E086D; Sat, 24 Jun 2023 17:01:42 +0000 (UTC) Received: from smtp.gentoo.org (mail.gentoo.org [IPv6:2001:470:ea4a:1:5054:ff:fec7:86e4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by pigeon.gentoo.org (Postfix) with ESMTPS id F4187E0866 for ; Sat, 24 Jun 2023 17:01:41 +0000 (UTC) From: Mike Gilbert To: gentoo-dev@lists.gentoo.org Subject: [gentoo-dev] [PATCH] acct-user.eclass: improve error message when usermod fails Date: Sat, 24 Jun 2023 13:01:38 -0400 Message-ID: <20230624170138.4118012-1-floppym@gentoo.org> X-Mailer: git-send-email 2.41.0 Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-dev@lists.gentoo.org Reply-to: gentoo-dev@lists.gentoo.org X-Auto-Response-Suppress: DR, RN, NRN, OOF, AutoReply MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Archives-Salt: 30f5de63-fd26-459a-810c-6a2ab3eaab37 X-Archives-Hash: cc21a9400c0ed79b586e77cf106db459 usermod refuses to update the home directory for a user with running processes. Output a more helpful message and avoid calling die for this. For other usermod failures, output stderr as an eerror message and die. Example output: * Failed to update user portage * This user currently has one or more running processes. * Please update this user manually with the following command: * usermod '--comment' 'System user; portage' '--home' '/var/lib/portage/home' '--shell' '/bin/bash' '--gid' 'portage' '--groups' '' portage Bug: https://bugs.gentoo.org/888189 Signed-off-by: Mike Gilbert --- eclass/acct-user.eclass | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/eclass/acct-user.eclass b/eclass/acct-user.eclass index f8a51ebf9c6b..88d7d354c8e6 100644 --- a/eclass/acct-user.eclass +++ b/eclass/acct-user.eclass @@ -432,12 +432,30 @@ acct-user_pkg_postinst() { fi elog "Updating user ${ACCT_USER_NAME}" - if ! usermod "${opts[@]}" "${ACCT_USER_NAME}" 2>"${T}/usermod-error.log"; then - # usermod outputs a warning if unlocking the account would result in an - # empty password. Hide stderr in a text file and display it if usermod - # fails. + # usermod outputs a warning if unlocking the account would result in an + # empty password. Hide stderr in a text file and display it if usermod fails. + usermod "${opts[@]}" "${ACCT_USER_NAME}" 2>"${T}/usermod-error.log" + local status=$? + if [[ ${status} -ne 0 ]]; then cat "${T}/usermod-error.log" >&2 - die "usermod failed" + if [[ ${status} -eq 8 ]]; then + # usermod refused to update the home directory + # for a uid with active processes. + eerror "Failed to update user ${ACCT_USER_NAME}" + eerror "This user currently has one or more running processes." + eerror "Please update this user manually with the following command:" + + # Surround opts with quotes. + # With bash-5 (EAPI 8), we can use "${opts[@]@Q}" instead. + local q="'" + local optsq=( "${opts[@]/#/$q}" ) + optsq=( "${optsq[@]/%/$q}" ) + + eerror " usermod ${optsq[@]} ${ACCT_USER_NAME}" + else + eerror "$(<"${T}/usermod-error.log")" + die "usermod failed" + fi fi } -- 2.41.0