From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from imap.gmail.com [173.194.76.109] by localhost with POP3 (fetchmail-6.3.26) for (single-drop); Thu, 23 May 2024 12:07:09 +0200 (CEST) Received: by 2002:a05:6a11:2489:b0:55f:c3c0:ed08 with SMTP id sg9csp1203686pxb; Thu, 23 May 2024 03:06:52 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCVhXoHMauxQHUfis1G/A4kQgS/X1/tAVldezzq4lQsvAaeRsTwBn5a+pWpMlT05oW+s7H+llji+J+8noIwkuaAfn/O1sajPWiQ= X-Google-Smtp-Source: AGHT+IHUXVWperrKubosqbNMOewtTkPnWSircIE+eW97/iBaZWKX7v7OD9aVFPPWNDkrzpIakiFy X-Received: by 2002:a05:6102:c86:b0:47b:ca6a:c5d9 with SMTP id ada2fe7eead31-48909fefccamr4466797137.28.1716458812054; Thu, 23 May 2024 03:06:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1716458812; cv=none; d=google.com; s=arc-20160816; b=wkDbJcU5FOKZpzzW4fM3s1YMqKdRTqfi0ZHE7Oau2PpvSz+rRDKYM0vPr/nbJKOxld GkLXGsl3TTtJmEgYG2LauVNm+traugP9R15OJk4+LPy2OXOqYZLnBemHCQjTlCACjiLN QJBdv4uCBCbDDf5jk02xMKce0iEqB/3q2tu8sjo5IbqaVcfs2mUKb23HS/bjPmwvLUVp f7zY01EhsnizhfBC7QjisXxr5FZUZwnv+6K1lMtyZaYaR6WfzyQVdarfQ06BfT69Frp7 8kHL5tFXoJgp/d62nkkzGkhG1jIDH7ND/kwGqmOSAI62eNUddOL+OCke7dF9+lBB6bTM hTrQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-disposition:user-agent:in-reply-to:mime-version:references :message-id:subject:cc:to:from:date:delivered-to; bh=QtO83KLHKsRhL3pICP5ckNoJ5Cl2IYs70vAhIPuCHOM=; fh=sa22/YzBg7gBlP1vWMAMO0ID50cEEzmrnuJNDeV9zT4=; b=SCM4T5RvGFodQ1sbXOGKYQK1Zy0FL0zr3/AQaxZNAR62DcQXamlwgiv2sEW+mn9gbf eB3ApAn7YP31buO4JqsYXQCWdyaP+U1EIt5Za5MdKbvcjdJG/GYbCpBx8e8QT4EKf57x hTKDEuzBf2GzpuCNux+xvPcpMhbt6FwTC+RC2KMmJeOVtusOugQDorxdPYK10D17EQFR xz/KOjWbL7Sw0V55Z4KQas6LKjRgjSvMROvpVWDIcFbmSSYxAShXmuzfsRN745rIqvC4 3vBh9p/o1NptREdgG0EICtRm3uhsy5MnCT+XKnpH1IJZsxKuIK4BiZO7Vp5k5//lPlt6 A4jA==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; gateway.spf=pass (google.com: domain gapps.redhat.com configured 170.10.128.131 as internal address) smtp.mailfrom=rjones@redhat.com smtp.remote-ip=170.10.128.131 policy.d=gapps.redhat.com Return-Path: Received: from us-smtp-inbound-delivery-1.mimecast.com (us-smtp-delivery-1.mimecast.com. [170.10.128.131]) by mx.google.com with ESMTPS id d75a77b69052e-43df54a1f14si10507101cf.48.2024.05.23.03.06.51 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 May 2024 03:06:52 -0700 (PDT) Received-SPF: pass (google.com: domain gapps.redhat.com configured 170.10.128.131 as internal address) Authentication-Results: mx.google.com; gateway.spf=pass (google.com: domain gapps.redhat.com configured 170.10.128.131 as internal address) smtp.mailfrom=rjones@redhat.com smtp.remote-ip=170.10.128.131 policy.d=gapps.redhat.com Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-629-BzuzLiiTP2O6wZpqxMsa8g-1; Thu, 23 May 2024 06:06:50 -0400 X-MC-Unique: BzuzLiiTP2O6wZpqxMsa8g-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 3ACE4800169 for ; Thu, 23 May 2024 10:06:50 +0000 (UTC) Received: by smtp.corp.redhat.com (Postfix) id 376F0C159FA; Thu, 23 May 2024 10:06:50 +0000 (UTC) Received: from localhost (unknown [10.42.28.23]) by smtp.corp.redhat.com (Postfix) with ESMTP id 55B7CC15BF4; Thu, 23 May 2024 10:06:48 +0000 (UTC) Date: Thu, 23 May 2024 11:06:45 +0100 From: "Richard W.M. Jones" To: Stefano Brivio Cc: passt-dev@passt.top, David Gibson , Minxi Hou Subject: Re: [PATCH 6/8] passt, util: Move opening of PID file to its own function Message-ID: <20240523100645.GV4345@redhat.com> References: <20240522205911.261325-1-sbrivio@redhat.com> <20240522205911.261325-7-sbrivio@redhat.com> MIME-Version: 1.0 In-Reply-To: <20240522205911.261325-7-sbrivio@redhat.com> User-Agent: Mutt/1.5.21 (2010-09-15) X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.8 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=us-ascii Content-Disposition: inline List-Id: On Wed, May 22, 2024 at 10:59:09PM +0200, Stefano Brivio wrote: > We won't call it from main() any longer: move it. > > Signed-off-by: Stefano Brivio > --- > passt.c | 11 ++--------- > util.c | 22 ++++++++++++++++++++++ > util.h | 1 + > 3 files changed, 25 insertions(+), 9 deletions(-) > > 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 = -1, pidfile_fd = -1; > + int nfds, i, devnull_fd = -1, pidfile_fd; > struct epoll_event events[EPOLL_EVENTS]; > char *log_name, argv0[PATH_MAX], *name; > struct ctx c = { 0 }; > @@ -299,14 +299,7 @@ int main(int argc, char **argv) > } > } > > - if (*c.pid_file) { > - if ((pidfile_fd = 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 = pidfile_open(c.pid_file); > > 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); > } > > +/** > + * 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 = 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 exits > * @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); Trivial refactoring: Reviewed-by: Richard W.M. Jones Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com virt-p2v converts physical machines to virtual machines. Boot with a live CD or over the network (PXE) and turn machines into KVM guests. http://libguestfs.org/virt-v2v