From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: passt.top; dmarc=none (p=none dis=none) header.from=gibson.dropbear.id.au Authentication-Results: passt.top; dkim=pass (2048-bit key; secure) header.d=gibson.dropbear.id.au header.i=@gibson.dropbear.id.au header.a=rsa-sha256 header.s=202410 header.b=dhIbO1Ki; dkim-atps=neutral Received: from mail.ozlabs.org (mail.ozlabs.org [IPv6:2404:9400:2221:ea00::3]) by passt.top (Postfix) with ESMTPS id 988965A004E for ; Fri, 25 Oct 2024 03:04:23 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202410; t=1729818252; bh=yhlVhPmwkS/HfX4YCWxhTCPkG5NjE193GrdEf1aF87Q=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=dhIbO1Kif//iIOEs56vmYbYDfbllGLNvoVvYn8dlpr0IhF1IJ/Rx/Oy1sJY7e3KPr UxHoTtMcVkkbs0TrCKv2R+tYS6ocR2R0BffnxZFUndwSPcu6Als5c8VLIVJ4q96noq M5jmTmW77qP0G5rizs4UVdNsA9fDc+N4Av4JtCtL/ZAdmy5vjwZxAzOlrqb5VU1dXY 2/YJOcVSSOXrYx895W5Kc7gh6LyY+o2RONjRRkdtowgkxHQTwBeMnAW2Lm/uHzdZ+6 rUcJ/vLcXGk2bCdMxLJR83qUA4GHo8YdqRqVcsrRqwBnetmXoLfDsa0LoJ8GlAgqN9 o7iMoS2tRbDGg== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4XZPgc0psXz4wb1; Fri, 25 Oct 2024 12:04:12 +1100 (AEDT) Date: Fri, 25 Oct 2024 12:04:01 +1100 From: David Gibson To: Stefano Brivio Subject: Re: [PATCH 8/8] util: Don't use errno after a successful call in __daemon() Message-ID: References: <20241024230438.3192725-1-sbrivio@redhat.com> <20241024230438.3192725-9-sbrivio@redhat.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="itzVoVgbvoRextpq" Content-Disposition: inline In-Reply-To: <20241024230438.3192725-9-sbrivio@redhat.com> Message-ID-Hash: G73E3TD5WTFC4OIOUAGUJ2UFYNK45V5A X-Message-ID-Hash: G73E3TD5WTFC4OIOUAGUJ2UFYNK45V5A X-MailFrom: dgibson@gandalf.ozlabs.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: passt-dev@passt.top X-Mailman-Version: 3.3.8 Precedence: list List-Id: Development discussion and patches for passt Archived-At: Archived-At: List-Archive: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: --itzVoVgbvoRextpq Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Fri, Oct 25, 2024 at 01:04:38AM +0200, Stefano Brivio wrote: > I thought we could just set errno to 0, do a bunch of stuff, and check > that errno didn't change to infer we succeeded. But clang-tidy, > starting with LLVM 19, reports: >=20 > /home/sbrivio/passt/util.c:465:6: error: An undefined value may be read f= rom 'errno' [clang-analyzer-unix.Errno,-warnings-as-errors] > 465 | if (errno) > | ^ > /usr/include/errno.h:38:16: note: expanded from macro 'errno' > 38 | # define errno (*__errno_location ()) > | ^~~~~~~~~~~~~~~~~~~~~~ > /home/sbrivio/passt/util.c:446:6: note: Assuming the condition is false > 446 | if (pid =3D=3D -1) { > | ^~~~~~~~~ > /home/sbrivio/passt/util.c:446:2: note: Taking false branch > 446 | if (pid =3D=3D -1) { > | ^ > /home/sbrivio/passt/util.c:451:6: note: Assuming 'pid' is 0 > 451 | if (pid) { > | ^~~ > /home/sbrivio/passt/util.c:451:2: note: Taking false branch > 451 | if (pid) { > | ^ > /home/sbrivio/passt/util.c:463:2: note: Assuming that 'close' is successf= ul; 'errno' becomes undefined after the call > 463 | close(devnull_fd); > | ^~~~~~~~~~~~~~~~~ > /home/sbrivio/passt/util.c:465:6: note: An undefined value may be read fr= om 'errno' > 465 | if (errno) > | ^ > /usr/include/errno.h:38:16: note: expanded from macro 'errno' > 38 | # define errno (*__errno_location ()) > | ^~~~~~~~~~~~~~~~~~~~~~ >=20 > And the LLVM documentation for the unix.Errno checker, 1.1.8.3 > unix.Errno (C), mentions, at: >=20 > https://clang.llvm.org/docs/analyzer/checkers.html#unix-errno >=20 > that: >=20 > The C and POSIX standards often do not define if a standard library > function may change value of errno if the call does not fail. > Therefore, errno should only be used if it is known from the return > value of a function that the call has failed. >=20 > which is, somewhat surprisingly, the case for close(). Ah, yeah. > Instead of using errno, check the actual return values of the calls > we issue here. >=20 > Signed-off-by: Stefano Brivio Reviewed-by: David Gibson > --- > util.c | 15 +++++---------- > 1 file changed, 5 insertions(+), 10 deletions(-) >=20 > diff --git a/util.c b/util.c > index b719f9a..02e18fb 100644 > --- a/util.c > +++ b/util.c > @@ -453,16 +453,11 @@ int __daemon(int pidfile_fd, int devnull_fd) > exit(EXIT_SUCCESS); > } > =20 > - errno =3D 0; > - > - setsid(); > - > - dup2(devnull_fd, STDIN_FILENO); > - dup2(devnull_fd, STDOUT_FILENO); > - dup2(devnull_fd, STDERR_FILENO); > - close(devnull_fd); > - > - if (errno) > + if (setsid() < 0 || > + dup2(devnull_fd, STDIN_FILENO) < 0 || > + dup2(devnull_fd, STDOUT_FILENO) < 0 || > + dup2(devnull_fd, STDERR_FILENO) < 0 || > + close(devnull_fd)) > exit(EXIT_FAILURE); > =20 > return 0; --=20 David Gibson (he or they) | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you, not the other way | around. http://www.ozlabs.org/~dgibson --itzVoVgbvoRextpq Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAEBCAAdFiEEO+dNsU4E3yXUXRK2zQJF27ox2GcFAmca7oEACgkQzQJF27ox 2Ge2WA//RcLxCJ1a+mmoi+SDYL12Fr7cZD7q02Wj/ue5vz5v3gJJ7MfGrERyQwYv R30Jj+clb4qioSDXT7oWLQP657VSKUg0VYJjAVdshZt2i8WCFY5a5zIkSBS8/3GM CYX9Wz/0hcifkaK6Xq2epCqlE+8Z548AqzvSW4kquTnCwcDFAW/wPcmjYx6LFx9f 2DcRdGrHyEeODoUKLs4iKb5C6oSsc/M/Rerj6Fq460ReBBHlqwcN7OCxFOWSmSMa 8PLzxO5UUx6BoeEzLvcwzHS7XC8vGkEZNlIV22TgHIw07AgueqrM9ta9471xHfz0 sKu8SqEl7A1094k4YmP2LR4C28u8/cGHbqJBpyqkh1VJqCQibnxQrANPHhTGreOd KNu7px/hkz4OfNHc4yzOpRYpaDKIUIpw/YZ2gJzuxOmIMDgVutxGE7YZtO8H3+eE yEc4hyzZyy3RImOUJY/bUrmKz8Wo9m2urI9gEeE7Mrzcrf6cGVFRNzpmkPNYfySF t22+8QZE7NjHRO20NTZim9sXaM2wdk2f3MxN4XZOM7+KdnxRghkMhxlv/GdelJrD ADkEe4TlJqQ8UmzjYVl9v2CZ/IraR8/MZF7SgYqA6lR4UW7qusPoxy3/Eh/PBHf9 uNaPRbLRBIKLNZNwsX/gZfBKHpIhcsuME3C1SqYBs5kbCHTE5Ms= =aDUd -----END PGP SIGNATURE----- --itzVoVgbvoRextpq--