From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail.ozlabs.org (gandalf.ozlabs.org [150.107.74.76]) by passt.top (Postfix) with ESMTPS id 85E445A0307 for ; Tue, 28 May 2024 09:24:41 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202312; t=1716881069; bh=AcPyfEf5LipYnlc4JZmi+MsAwLfHD0iEc/whwDSgkXo=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=mQBxKPmdMqvIuQlBn8nvRxBXSdeTQwJ6Y80HuoxJJ3OhHHmsvkjc3wA4TcKSTqgZa AmYB37ChB1i0wYhXg+OP9VpE05YUm9Z+BxoT8x4K8OtszoUV5iLB+bZPgUIyZrEhkI 8/hkEApHh4Ml5wErqSEYzva7lKgVKNydPNRqB8DU1rEuTruVRfbavqE3x9nYRcxFGw BHpiotNn52YHWGl3yL8/+Em9NHBm3d463iV4PYl6l1NZGCLXps+GzijRQiqHPVe/tt wY6OmsfFwjjgDoVtwarBrlZtkEXiu+KtuX5PvPdpb62vPVST9QJEwqIQzjZEYM4fRm JBu6EKJ12jUCQ== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4VpPCd6BlMz4x2d; Tue, 28 May 2024 17:24:29 +1000 (AEST) Date: Tue, 28 May 2024 17:04:51 +1000 From: David Gibson To: Stefano Brivio Subject: Re: [PATCH 6/8] passt, util: Move opening of PID file to its own function Message-ID: References: <20240522205911.261325-1-sbrivio@redhat.com> <20240522205911.261325-7-sbrivio@redhat.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="YRPGBtwSFAOhy69R" Content-Disposition: inline In-Reply-To: <20240522205911.261325-7-sbrivio@redhat.com> Message-ID-Hash: JS4ITFDWJNIYDLEMGBR3JYTPWHAQUVX4 X-Message-ID-Hash: JS4ITFDWJNIYDLEMGBR3JYTPWHAQUVX4 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, "'Richard W . M . Jones'" , Minxi Hou 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: --YRPGBtwSFAOhy69R Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Wed, May 22, 2024 at 10:59:09PM +0200, Stefano Brivio wrote: > We won't call it from main() any longer: move it. >=20 > Signed-off-by: Stefano Brivio Reviewed-by: David Gibson > --- > passt.c | 11 ++--------- > util.c | 22 ++++++++++++++++++++++ > util.h | 1 + > 3 files changed, 25 insertions(+), 9 deletions(-) >=20 > diff --git a/passt.c b/passt.c > index fb9773d..e2446fc 100644 > --- a/passt.c > +++ b/passt.c > @@ -199,7 +199,7 @@ void exit_handler(int signal) > */ > int main(int argc, char **argv) > { > - int nfds, i, devnull_fd =3D -1, pidfile_fd =3D -1; > + int nfds, i, devnull_fd =3D -1, pidfile_fd; > struct epoll_event events[EPOLL_EVENTS]; > char *log_name, argv0[PATH_MAX], *name; > struct ctx c =3D { 0 }; > @@ -299,14 +299,7 @@ int main(int argc, char **argv) > } > } > =20 > - if (*c.pid_file) { > - if ((pidfile_fd =3D open(c.pid_file, > - O_CREAT | O_TRUNC | O_WRONLY | O_CLOEXEC, > - S_IRUSR | S_IWUSR)) < 0) { > - perror("PID file open"); > - exit(EXIT_FAILURE); > - } > - } > + pidfile_fd =3D pidfile_open(c.pid_file); > =20 > if (isolate_prefork(&c)) > die("Failed to sandbox process, exiting"); > diff --git a/util.c b/util.c > index 18c04ba..cf5a62b 100644 > --- a/util.c > +++ b/util.c > @@ -402,6 +402,28 @@ void pidfile_write(int fd, pid_t pid) > close(fd); > } > =20 > +/** > + * pidfile_open() - Open PID file if needed > + * @path: Path for PID file, empty string if no PID file is requested > + * > + * Return: descriptor for PID file, -1 if path is NULL, won't return on = failure > + */ > +int pidfile_open(const char *path) > +{ > + int fd; > + > + if (!*path) > + return -1; > + > + if ((fd =3D open(path, O_CREAT | O_TRUNC | O_WRONLY | O_CLOEXEC, > + S_IRUSR | S_IWUSR)) < 0) { > + perror("PID file open"); > + exit(EXIT_FAILURE); > + } > + > + return fd; > +} > + > /** > * __daemon() - daemon()-like function writing PID file before parent ex= its > * @pidfile_fd: Open PID file descriptor > diff --git a/util.h b/util.h > index f811975..8a430ca 100644 > --- a/util.h > +++ b/util.h > @@ -156,6 +156,7 @@ char *line_read(char *buf, size_t len, int fd); > void ns_enter(const struct ctx *c); > bool ns_is_init(void); > int open_in_ns(const struct ctx *c, const char *path, int flags); > +int pidfile_open(const char *path); > void pidfile_write(int fd, pid_t pid); > int __daemon(int pidfile_fd, int devnull_fd); > int fls(unsigned long x); --=20 David Gibson | 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 --YRPGBtwSFAOhy69R Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAEBCAAdFiEEO+dNsU4E3yXUXRK2zQJF27ox2GcFAmZVgfoACgkQzQJF27ox 2GfUDA//XuVPMQvGBpg88x4eI3pykixOsK9P0okoFYpnofzkdSrFqh5SZa8QcXGX mdPqNrzWCRO903va0vR5tHNfL2qlV282nycWu7NqIV8JfKrB71v+3eh4KpSO1/jT M1NhZTalEQ88bAtOk9jVjXU9B7GHAUZMTAAYyjcCzfDRKURlvz1VhwjDKJTvEYfU 513bbgyGOH2R1/F1mtEOaqUdf75BPaEFeWkvpdCFfq8PTDHF4cpOYYEAEbcUFjfz Uzdn7YPsLgoIKOmyVM2YddMg4D05RM7EMcm23m1cs6z9k6oJC5pgte1FtZptRCFL zXxKHw2WXaS05FBvE84yD4Xk9IYkPGzgRsOiHPlM/+9jyB6C4a+hNA6ZX9IvdHv2 picY8bdLXt6kN1VEfLU9Rfby2cuqiMwtLrAd8neKi1OCgnOwd4hAHGeNxxCInom9 pgI4MtUQhxVqbk72e/zkrnybLccS5boXyV3GvRpRej4LJnYmPKDhlppwYy8MyEs5 dIx9XAaW/JF/y2jMFq+gos3nbfQHUPWlUYjZPFxbVDmkP/f3u2NHqgjODfP+2DhA f82r2gDnjdoKkfaFXPNy7elO3HeNkLa0iPX6xSVm7tG1VRoGRl3FpyyG5ul9UX7q wzwE97FUvuP2ZxRwobl6A50hu3aiOW9bP6fDV6r7fRvdcFMsqBU= =O3m0 -----END PGP SIGNATURE----- --YRPGBtwSFAOhy69R--