diff --git a/sbin/reboot/reboot.c b/sbin/reboot/reboot.c --- a/sbin/reboot/reboot.c +++ b/sbin/reboot/reboot.c @@ -29,19 +29,21 @@ * SUCH DAMAGE. */ -#include #include #include #include #include #include #include +#include +#include #include #include #include #include #include +#include #include #include #include @@ -69,23 +71,43 @@ 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[] = { + "zfsbootcfg", + "-z", + pool, + "-n", + "freebsd:nvstore", + "-k", + "nextboot_enable" + "-v", + "YES", + NULL + }; + 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