Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 16 Feb 2024 12:45:14 -0700
From:      Warner Losh <imp@bsdimp.com>
To:        Konstantin Belousov <kostikbel@gmail.com>
Cc:        Warner Losh <imp@freebsd.org>, src-committers@freebsd.org,  dev-commits-src-all@freebsd.org, dev-commits-src-main@freebsd.org
Subject:   Re: git: 33a2406eed00 - main - reboot: Use posix_spawn instead of system
Message-ID:  <CANCZdfp9HisN83yF2e07XifjbZfjprUTO%2BkOD=tQX%2BU4fFO5SQ@mail.gmail.com>
In-Reply-To: <Zc9kFvcR-h3G_95H@kib.kiev.ua>
References:  <202402160400.41G40Jgd018717@gitrepo.freebsd.org> <Zc9kFvcR-h3G_95H@kib.kiev.ua>

next in thread | previous in thread | raw e-mail | index | archive | help

[-- Attachment #1 --]
On Fri, Feb 16, 2024 at 6:34 AM Konstantin Belousov <kostikbel@gmail.com>
wrote:

> On Fri, Feb 16, 2024 at 04:00:19AM +0000, Warner Losh wrote:
> > The branch main has been updated by imp:
> >
> > URL:
> https://cgit.FreeBSD.org/src/commit/?id=33a2406eed009dbffd7dfa44285c23f0db5a3750
> >
> > commit 33a2406eed009dbffd7dfa44285c23f0db5a3750
> > Author:     Warner Losh <imp@FreeBSD.org>
> > AuthorDate: 2024-02-16 03:52:31 +0000
> > Commit:     Warner Losh <imp@FreeBSD.org>
> > CommitDate: 2024-02-16 03:59:22 +0000
> >
> >     reboot: Use posix_spawn instead of system
> >
> >     Use posix_spawn to avoid having to allocate memory needed for the
> system
> >     command line.
> >
> >     Sponsored by:           Netflix
> >     Reviewed by:            jrtc27
> >     Differential Revision:  https://reviews.freebsd.org/D43860
> > ---
> >  sbin/reboot/reboot.c | 54
> ++++++++++++++++++++++++++++++++++++----------------
> >  1 file changed, 38 insertions(+), 16 deletions(-)
> >
> > diff --git a/sbin/reboot/reboot.c b/sbin/reboot/reboot.c
> > index e9d6487da6a5..d3f1fc9bbb86 100644
> > --- a/sbin/reboot/reboot.c
> > +++ b/sbin/reboot/reboot.c
> > @@ -29,19 +29,21 @@
> >   * SUCH DAMAGE.
> >   */
> >
> > -#include <sys/types.h>
> >  #include <sys/boottrace.h>
> >  #include <sys/mount.h>
> >  #include <sys/reboot.h>
> >  #include <sys/stat.h>
> >  #include <sys/sysctl.h>
> >  #include <sys/time.h>
> > +#include <sys/types.h>
> > +#include <sys/wait.h>
> sys/types.h should go first, the previous location was right
>

Yea, it's not needed at all, so I'm removing it.


> >
> >  #include <err.h>
> >  #include <errno.h>
> >  #include <fcntl.h>
> >  #include <pwd.h>
> >  #include <signal.h>
> > +#include <spawn.h>
> >  #include <stdbool.h>
> >  #include <stdio.h>
> >  #include <stdlib.h>
> > @@ -69,23 +71,43 @@ static bool donextboot;
> >  static void
> >  zfsbootcfg(const char *pool, bool force)
> >  {
> > -     char *k;
> > -     int rv;
> > -
> > -     asprintf(&k,
> > -         "zfsbootcfg -z %s -n freebsd:nvstore -k nextboot_enable -v
> YES",
> > -         pool);
> > -     if (k == NULL)
> > -             E("No memory for zfsbootcfg");
> > -
> > -     rv = system(k);
> > -     if (rv == 0)
> > -             return;
> > +     const char * const av[] = {
> Why not 'static'?
>
> > +             "zfsbootcfg",
> > +             "-z",
> > +             pool,
> > +             "-n",
> > +             "freebsd:nvstore",
> > +             "-k",
> > +             "nextboot_enable"
> > +             "-v",
> > +             "YES",
> > +             NULL
> > +     };
>

Because 'pool' is not a compile time constant?

Warner


> > +     int rv, status;
> > +     pid_t p;
> > +     extern char **environ;
> > +
> > +     rv = posix_spawnp(&p, av[0], NULL, NULL, __DECONST(char **, av),
> > +         environ);
> >       if (rv == -1)
> >               E("system zfsbootcfg");
> > -     if (rv == 127)
> > -             E("zfsbootcfg not found in path");
> > -     E("zfsbootcfg returned %d", rv);
> > +     if (waitpid(p, &status, WEXITED) < 0) {
> > +             if (errno == EINTR)
> > +                     return;
> > +             E("waitpid zfsbootcfg");
> > +     }
> > +     if (WIFEXITED(status)) {
> > +             int e = WEXITSTATUS(status);
> > +
> > +             if (e == 0)
> > +                     return;
> > +             if (e == 127)
> > +                     E("zfsbootcfg not found in path");
> > +             E("zfsbootcfg returned %d", e);
> > +     }
> > +     if (WIFSIGNALED(status))
> > +             E("zfsbootcfg died with signal %d", WTERMSIG(status));
> > +     E("zfsbootcfg unexpected status %d", status);
> >  }
> >
> >  static void
>

[-- Attachment #2 --]
<div dir="ltr"><div dir="ltr"><br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Fri, Feb 16, 2024 at 6:34 AM Konstantin Belousov &lt;<a href="mailto:kostikbel@gmail.com">kostikbel@gmail.com</a>&gt; wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">On Fri, Feb 16, 2024 at 04:00:19AM +0000, Warner Losh wrote:<br>
&gt; The branch main has been updated by imp:<br>
&gt; <br>
&gt; URL: <a href="https://cgit.FreeBSD.org/src/commit/?id=33a2406eed009dbffd7dfa44285c23f0db5a3750" rel="noreferrer" target="_blank">https://cgit.FreeBSD.org/src/commit/?id=33a2406eed009dbffd7dfa44285c23f0db5a3750</a><br>;
&gt; <br>
&gt; commit 33a2406eed009dbffd7dfa44285c23f0db5a3750<br>
&gt; Author:     Warner Losh &lt;imp@FreeBSD.org&gt;<br>
&gt; AuthorDate: 2024-02-16 03:52:31 +0000<br>
&gt; Commit:     Warner Losh &lt;imp@FreeBSD.org&gt;<br>
&gt; CommitDate: 2024-02-16 03:59:22 +0000<br>
&gt; <br>
&gt;     reboot: Use posix_spawn instead of system<br>
&gt;     <br>
&gt;     Use posix_spawn to avoid having to allocate memory needed for the system<br>
&gt;     command line.<br>
&gt;     <br>
&gt;     Sponsored by:           Netflix<br>
&gt;     Reviewed by:            jrtc27<br>
&gt;     Differential Revision:  <a href="https://reviews.freebsd.org/D43860" rel="noreferrer" target="_blank">https://reviews.freebsd.org/D43860</a><br>;
&gt; ---<br>
&gt;  sbin/reboot/reboot.c | 54 ++++++++++++++++++++++++++++++++++++----------------<br>
&gt;  1 file changed, 38 insertions(+), 16 deletions(-)<br>
&gt; <br>
&gt; diff --git a/sbin/reboot/reboot.c b/sbin/reboot/reboot.c<br>
&gt; index e9d6487da6a5..d3f1fc9bbb86 100644<br>
&gt; --- a/sbin/reboot/reboot.c<br>
&gt; +++ b/sbin/reboot/reboot.c<br>
&gt; @@ -29,19 +29,21 @@<br>
&gt;   * SUCH DAMAGE.<br>
&gt;   */<br>
&gt;  <br>
&gt; -#include &lt;sys/types.h&gt;<br>
&gt;  #include &lt;sys/boottrace.h&gt;<br>
&gt;  #include &lt;sys/mount.h&gt;<br>
&gt;  #include &lt;sys/reboot.h&gt;<br>
&gt;  #include &lt;sys/stat.h&gt;<br>
&gt;  #include &lt;sys/sysctl.h&gt;<br>
&gt;  #include &lt;sys/time.h&gt;<br>
&gt; +#include &lt;sys/types.h&gt;<br>
&gt; +#include &lt;sys/wait.h&gt;<br>
sys/types.h should go first, the previous location was right<br></blockquote><div><br></div><div>Yea, it&#39;s not needed at all, so I&#39;m removing it.<br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
&gt;  <br>
&gt;  #include &lt;err.h&gt;<br>
&gt;  #include &lt;errno.h&gt;<br>
&gt;  #include &lt;fcntl.h&gt;<br>
&gt;  #include &lt;pwd.h&gt;<br>
&gt;  #include &lt;signal.h&gt;<br>
&gt; +#include &lt;spawn.h&gt;<br>
&gt;  #include &lt;stdbool.h&gt;<br>
&gt;  #include &lt;stdio.h&gt;<br>
&gt;  #include &lt;stdlib.h&gt;<br>
&gt; @@ -69,23 +71,43 @@ static bool donextboot;<br>
&gt;  static void<br>
&gt;  zfsbootcfg(const char *pool, bool force)<br>
&gt;  {<br>
&gt; -     char *k;<br>
&gt; -     int rv;<br>
&gt; -<br>
&gt; -     asprintf(&amp;k,<br>
&gt; -         &quot;zfsbootcfg -z %s -n freebsd:nvstore -k nextboot_enable -v YES&quot;,<br>
&gt; -         pool);<br>
&gt; -     if (k == NULL)<br>
&gt; -             E(&quot;No memory for zfsbootcfg&quot;);<br>
&gt; -<br>
&gt; -     rv = system(k);<br>
&gt; -     if (rv == 0)<br>
&gt; -             return;<br>
&gt; +     const char * const av[] = {<br>
Why not &#39;static&#39;?<br>
<br>
&gt; +             &quot;zfsbootcfg&quot;,<br>
&gt; +             &quot;-z&quot;,<br>
&gt; +             pool,<br>
&gt; +             &quot;-n&quot;,<br>
&gt; +             &quot;freebsd:nvstore&quot;,<br>
&gt; +             &quot;-k&quot;,<br>
&gt; +             &quot;nextboot_enable&quot;<br>
&gt; +             &quot;-v&quot;,<br>
&gt; +             &quot;YES&quot;,<br>
&gt; +             NULL<br>
&gt; +     };<br></blockquote><div><br></div><div>Because &#39;pool&#39; is not a compile time constant?</div><div><br></div><div>Warner<br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
&gt; +     int rv, status;<br>
&gt; +     pid_t p;<br>
&gt; +     extern char **environ;<br>
&gt; +<br>
&gt; +     rv = posix_spawnp(&amp;p, av[0], NULL, NULL, __DECONST(char **, av),<br>
&gt; +         environ);<br>
&gt;       if (rv == -1)<br>
&gt;               E(&quot;system zfsbootcfg&quot;);<br>
&gt; -     if (rv == 127)<br>
&gt; -             E(&quot;zfsbootcfg not found in path&quot;);<br>
&gt; -     E(&quot;zfsbootcfg returned %d&quot;, rv);<br>
&gt; +     if (waitpid(p, &amp;status, WEXITED) &lt; 0) {<br>
&gt; +             if (errno == EINTR)<br>
&gt; +                     return;<br>
&gt; +             E(&quot;waitpid zfsbootcfg&quot;);<br>
&gt; +     }<br>
&gt; +     if (WIFEXITED(status)) {<br>
&gt; +             int e = WEXITSTATUS(status);<br>
&gt; +<br>
&gt; +             if (e == 0)<br>
&gt; +                     return;<br>
&gt; +             if (e == 127)<br>
&gt; +                     E(&quot;zfsbootcfg not found in path&quot;);<br>
&gt; +             E(&quot;zfsbootcfg returned %d&quot;, e);<br>
&gt; +     }<br>
&gt; +     if (WIFSIGNALED(status))<br>
&gt; +             E(&quot;zfsbootcfg died with signal %d&quot;, WTERMSIG(status));<br>
&gt; +     E(&quot;zfsbootcfg unexpected status %d&quot;, status);<br>
&gt;  }<br>
&gt;  <br>
&gt;  static void<br>
</blockquote></div></div>

Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?CANCZdfp9HisN83yF2e07XifjbZfjprUTO%2BkOD=tQX%2BU4fFO5SQ>