From nobody Thu Nov 19 13:23:59 2026 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4jVfLl4BvXz7108X for ; Thu, 19 Nov 2026 13:23:59 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mx1.freebsd.org (Postfix) with ESMTPS id 4jVfLl3Lh7z4Rfx for ; Thu, 19 Nov 2026 13:23:59 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1795094639; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=GGrGE5KmLwtTjFxnp51Shy/TkWoNfyBGkQJ+/AQvLRc=; b=t1EkRkxQp0HmInn/rdek5B2EkJBjW8Mxo5CyrmgW0vMUF9FuHSRGf/W9GuyoTNwFuIEzgE 2NzpT9sCDtOEcPXxbnpnXChFPJ3KmS3MGGabspaZwJXppiMvBgi6sbGmTmPYo+Pc+lRVSG Stxj+y28QhbDibB+65s0OJY4kI95zu9MZG/L9q/VQoRSDQncBnl/Tux7sKCVYUfwy2nx65 oYhRkCGDhPRwnupoQG7dXhuYUTm/2ty5TTYMBjnFrMEKLZ/VNpQLpwARmJvvEoCBgN0zVU 7xUUrDFITrgBv4l4ZUlklhHwMqt3Sd5d6RSkMEPyd8uGkq1GGy1zXg35aH2XwA== ARC-Seal: i=1; a=rsa-sha256; d=freebsd.org; s=dkim; cv=none; t=1795094639; b=A6U9/wKIOG/J48tZfCTIdX7T6iIrF8ojyrVQYoQOQYHR53FPpvBdMlxY24uzJMLOaImDrT JabAWMlDhTGMimncrWLRahjXNOmBDuez2kEfmlcRltD10Ltni45zklRnklbwqADeE20m4s SR/cG35dxA1mZgpqEi1CdcuLT95lbZzte89NEgf/6WOcU72qKOk24Tl5dTXyFGuS8NRXMc QUBE27VgxrYUY1F1z7RIEqh/PCQnyf7mjJzOfrafU+SKjiLnQWclOfaA3wm7DqsESJLrTF +70/oOKEijB7+AC1hhS4NjUYbA1bXGrqPm9dOU2AmIBi6qHS3S9oCmskAjpHZg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1795094639; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=GGrGE5KmLwtTjFxnp51Shy/TkWoNfyBGkQJ+/AQvLRc=; b=dLTg2s0FpwpUnCz00hWUEuXkgBTyOJTFUnWlDNucILaOt4JLvNtDD8XggXT99F4M/WOMjN PH5yKlSC1Wl5uay959X33P7F6SH0kqGj6GTKhP7tYCkZEUxGCCfNCjBnnP/Ffxn5oU/C26 dBY6e1KQdj5kTaf9WZKTLylbhH4QUIxOToCqjEsjo64zdhvu33KN3L+5oncql5lIkYtxR0 iC0IqK3aA8ZCAUn7VwEs8Oc+OpDB33h7ycLh+S3gWEKToSiQrwc+794XBGXbhKHm2Bpedq meoUSaQuDF5v98YoNGBXAi6gVIyFFl9XdDdCmx3GxwlxR9D5Ye/GhOhkLmIb+A== ARC-Authentication-Results: i=1; mx1.freebsd.org; none Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) by mxrelay.nyi.freebsd.org (Postfix) with ESMTP id 4jVfLl21gbz12xQ for ; Thu, 19 Nov 2026 13:23:59 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from git (uid 1279) (envelope-from git@FreeBSD.org) id 1cff2 by gitrepo.freebsd.org (DragonFly Mail Agent v0.13+ on gitrepo.freebsd.org); Thu, 19 Nov 2026 13:23:59 +0000 To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Dag-Erling=?utf-8?Q? Sm=C3=B8rg?=rav Subject: git: cad99ff75342 - main - pkg: Add -j and -r options List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all X-BeenThere: dev-commits-src-all@freebsd.org List-Id: Commit messages for all branches of the src repository List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: des X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: cad99ff753429625788549fdda895c71a1414e11 Auto-Submitted: auto-generated Date: Thu, 19 Nov 2026 13:23:59 +0000 Message-Id: <6afef86f.1cff2.74607eb5@gitrepo.freebsd.org> The branch main has been updated by des: URL: https://cgit.FreeBSD.org/src/commit/?id=cad99ff753429625788549fdda895c71a1414e11 commit cad99ff753429625788549fdda895c71a1414e11 Author: Dag-Erling Smørgrav AuthorDate: 2026-11-19 13:22:25 +0000 Commit: Dag-Erling Smørgrav CommitDate: 2026-11-19 13:23:46 +0000 pkg: Add -j and -r options This allows pkg(7) to be used to bootstrap a jail or chroot, and to recognize the -j and -r options and pass them through to pkg(8) if already bootstrapped. The -j option greatly complicates the option-parsing loop because we can no longer pass our original argv unmodified to pkg(8); specifcially, we must strip -j and its argument, which in practice means constructing a whole new argv with the options we wish to pass on. MFC after: 1 week Reviewed by: imp, bapt, markj Differential Revision: https://reviews.freebsd.org/D58165 --- usr.sbin/pkg/Makefile | 2 +- usr.sbin/pkg/pkg.7 | 28 +++++-- usr.sbin/pkg/pkg.c | 213 ++++++++++++++++++++++++++++++++++++++------------ 3 files changed, 188 insertions(+), 55 deletions(-) diff --git a/usr.sbin/pkg/Makefile b/usr.sbin/pkg/Makefile index 68b862826992..3b3249c538dd 100644 --- a/usr.sbin/pkg/Makefile +++ b/usr.sbin/pkg/Makefile @@ -27,7 +27,7 @@ MAN= pkg.7 CFLAGS+=-I${SRCTOP}/contrib/libucl/include .PATH: ${SRCTOP}/contrib/libucl/include -LIBADD= archive der fetch pkgecc ucl crypto ssl util md +LIBADD= archive der fetch jail pkgecc ucl crypto ssl util md CFLAGS+=-I${SRCTOP}/contrib/libder/libder CFLAGS+=-I${SRCTOP}/crypto/libecc/include diff --git a/usr.sbin/pkg/pkg.7 b/usr.sbin/pkg/pkg.7 index d2246f74a3fc..8214eb2586f7 100644 --- a/usr.sbin/pkg/pkg.7 +++ b/usr.sbin/pkg/pkg.7 @@ -22,7 +22,7 @@ .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" -.Dd April 29, 2025 +.Dd July 20, 2026 .Dt PKG 7 .Os .Sh NAME @@ -31,17 +31,25 @@ .Sh SYNOPSIS .Nm .Op Fl d +.Op Fl j Ar jail +.Op Fl r Ar rootdir .Ar command ... .Nm .Op Fl d +.Op Fl j Ar jail +.Op Fl r Ar rootdir .Cm add .Op Fl fy .Op Fl r Ar reponame .Ar pkg.pkg .Nm +.Op Fl j Ar jail +.Op Fl r Ar rootdir .Fl N .Nm .Op Fl 46d +.Op Fl j Ar jail +.Op Fl r Ar rootdir .Cm bootstrap .Op Fl fy .Op Fl r Ar reponame @@ -61,14 +69,14 @@ will bootstrap the real .Xr pkg 8 from a remote repository. .Bl -tag -.It Nm Ar command ... +.It Nm Oo Fl j Ar jail Oc Oo Fl r Ar rootdir Oc Ar command ... If .Xr pkg 8 is not installed yet, it will be fetched, have its signature verified, installed, and then have the original command forwarded to it. If already installed, the command requested will be forwarded to the real .Xr pkg 8 . -.It Nm Cm add Oo Fl fy Oc Oo Fl r Ar reponame Oc Ar pkg.pkg +.It Nm Oo Fl j Ar jail Oc Oo Fl r Ar rootdir Oc Cm add Oo Fl fy Oc Oo Fl r Ar reponame Oc Ar pkg.pkg Install .Xr pkg 8 from a local package instead of fetching from remote. @@ -88,13 +96,13 @@ If a .Ar reponame has been specified, then the signature configuration for that repository will be used. -.It Nm Fl N +.It Nm Oo Fl j Ar jail Oc Oo Fl r Ar rootdir Oc Fl N Do not bootstrap, just determine if .Xr pkg 8 is actually installed or not. Returns 0 and the number of packages installed if it is, otherwise 1. -.It Nm Oo Fl 46 Oc Cm bootstrap Oo Fl fy Oc \ +.It Nm Oo Fl 46d Oc Oo Fl j Ar jail Oc Oo Fl r Ar rootdir Oc Cm bootstrap Oo Fl fy Oc \ Oo Fl r Ar reponame Oc Attempt to bootstrap and do not forward anything to .Xr pkg 8 @@ -131,6 +139,16 @@ May be specified more than once to increase the level of detail. When specified twice, .Xr fetch 3 debug output is enabled. +.It Fl j Ar jail , Fl -jail Ar jail +Attach to the jail designated by the name or numerical identifier +.Ar jail . +Note that +.Nm +will attach to the jail immediately upon encountering this option, so +any paths provided should be relative to the root of the jail. +.It Fl r Ar rootdir , Fl -rootdir Ar rootdir +Operate on the system rooted at +.Ar rootdir . .El .Sh CONFIGURATION Configuration varies in whether it is in a repository configuration file diff --git a/usr.sbin/pkg/pkg.c b/usr.sbin/pkg/pkg.c index 6f40ae6c0f16..d6cb10797fb4 100644 --- a/usr.sbin/pkg/pkg.c +++ b/usr.sbin/pkg/pkg.c @@ -28,8 +28,7 @@ */ #include -#include -#include +#include #include #include @@ -41,6 +40,7 @@ #include #include #include +#include #include #include #include @@ -92,6 +92,8 @@ struct fingerprint { }; static const char *bootstrap_name = "pkg.pkg"; +static const char *jailname = NULL; +static const char *rootdir = NULL; STAILQ_HEAD(fingerprint_list, fingerprint); @@ -168,7 +170,8 @@ extract_pkg_static(int fd, char *p, int sz) { struct archive *a; struct archive_entry *ae; - const char *end; + const char *name, *end; + char *rname; int ret, r; ret = -1; @@ -192,11 +195,22 @@ extract_pkg_static(int fd, char *p, int sz) ae = NULL; while ((r = archive_read_next_header(a, &ae)) == ARCHIVE_OK) { - end = strrchr(archive_entry_pathname(ae), '/'); + name = archive_entry_pathname(ae); + end = strrchr(name, '/'); if (end == NULL) continue; if (strcmp(end, "/pkg-static") == 0) { + if (rootdir) { + if (asprintf(&rname, "%s%s", rootdir, name) < 0) + err(1, NULL); + archive_entry_copy_pathname(ae, rname); + free(rname); + } + if (debug) { + fprintf(stderr, "extracting %s...\n", + archive_entry_pathname(ae)); + } r = archive_read_extract(a, ae, ARCHIVE_EXTRACT_OWNER | ARCHIVE_EXTRACT_PERM | ARCHIVE_EXTRACT_TIME | ARCHIVE_EXTRACT_ACL | @@ -221,32 +235,55 @@ cleanup: static int install_pkg_static(const char *path, const char *pkgpath, bool force) { + const char *argv[16]; + int argc = 0; int pstat; pid_t pid; - switch ((pid = fork())) { - case -1: + /* assemble the command line */ + argv[argc++] = "pkg-static"; + if (rootdir) { + argv[argc++] = "-r"; + argv[argc++] = rootdir; + } + argv[argc++] = "add"; + if (jailname || rootdir) { + /* + * Installing into an empty directory will normally fail + * because none of the shared libraries required by pkg + * are present, but pkg-static will still work and can be + * used to install a packaged base, so tell pkg-static to + * ignore missing dependencies. + */ + argv[argc++] = "-M"; + } + if (force) + argv[argc++] = "-f"; + argv[argc++] = pkgpath; + argv[argc] = NULL; + + if (debug) { + fprintf(stderr, "%s", path); + for (int i = 1; i < argc; i++) + fprintf(stderr, " %s", argv[i]); + fprintf(stderr, "\n"); + } + + /* fork and exec */ + if ((pid = fork()) < 0) return (-1); - case 0: - if (force) - execl(path, "pkg-static", "add", "-f", pkgpath, - (char *)NULL); - else - execl(path, "pkg-static", "add", pkgpath, - (char *)NULL); + if (pid == 0) { + /* child */ + execv(path, __DECONST(char **, argv)); _exit(1); - default: - break; } + /* wait for pkg-static to complete */ while (waitpid(pid, &pstat, 0) == -1) if (errno != EINTR) return (-1); - if (WEXITSTATUS(pstat)) - return (WEXITSTATUS(pstat)); - else if (WIFSIGNALED(pstat)) - return (128 & (WTERMSIG(pstat))); + /* this will be zero iff pkg-static exited zero */ return (pstat); } @@ -946,7 +983,7 @@ static const char non_interactive_message[] = static const char args_bootstrap_message[] = "Too many arguments\n" -"Usage: pkg [-4|-6] bootstrap [-f] [-y]\n"; +"Usage: pkg [-46d] [-j jail] [-r rootdir] bootstrap [-fy]\n"; static int pkg_query_yes_no(void) @@ -978,7 +1015,7 @@ bootstrap_pkg_local(const char *pkgpath, bool force) fd_sig = -1; ret = -1; - fd_pkg = open(pkgpath, O_RDONLY); + fd_pkg = open(pkgpath, O_RDONLY | O_CLOEXEC); if (fd_pkg == -1) err(EXIT_FAILURE, "Unable to open %s", pkgpath); @@ -992,7 +1029,8 @@ bootstrap_pkg_local(const char *pkgpath, bool force) snprintf(path, sizeof(path), "%s.sig", pkgpath); - if ((fd_sig = open(path, O_RDONLY)) == -1) { + fd_sig = open(path, O_RDONLY | O_CLOEXEC); + if (fd_sig == -1) { fprintf(stderr, "Signature for pkg not " "available.\n"); goto cleanup; @@ -1005,7 +1043,8 @@ bootstrap_pkg_local(const char *pkgpath, bool force) snprintf(path, sizeof(path), "%s.pubkeysig", pkgpath); - if ((fd_sig = open(path, O_RDONLY)) == -1) { + fd_sig = open(path, O_RDONLY | O_CLOEXEC); + if (fd_sig == -1) { fprintf(stderr, "Signature for pkg not " "available.\n"); goto cleanup; @@ -1073,57 +1112,120 @@ pkg_is_pkg_pkg(const char *pkg) int main(int argc, char *argv[]) { - char pkgpath[MAXPATHLEN]; - char **original_argv; - const char *pkgarg, *repo_name; - bool activation_test, add_pkg, bootstrap_only, force, yes; - signed char ch; - const char *fetchOpts; + char pathbuf[MAXPATHLEN], *pkgpath = pathbuf, *arg; struct repositories *repositories; + const char *fetchOpts, *localbase, **pkgargv, *posarg, *repo_name; + int ch, jid, pkgargc, pkgargn, rootd; + bool activation_test, add_pkg, bootstrap_only, force, yes; activation_test = false; add_pkg = false; bootstrap_only = false; fetchOpts = ""; force = false; - original_argv = argv; - pkgarg = NULL; + posarg = NULL; repo_name = NULL; yes = false; - struct option longopts[] = { + static const struct option longopts[] = { { "debug", no_argument, NULL, 'd' }, + { "jail", required_argument, NULL, 'j' }, { "only-ipv4", no_argument, NULL, '4' }, { "only-ipv6", no_argument, NULL, '6' }, + { "option", required_argument, NULL, 'o' }, + { "rootdir", required_argument, NULL, 'r' }, { NULL, 0, NULL, 0 }, }; - snprintf(pkgpath, MAXPATHLEN, "%s/sbin/pkg", getlocalbase()); - - while ((ch = getopt_long(argc, argv, "+:dN46", longopts, NULL)) != -1) { + pkgargn = 16; + if ((pkgargv = malloc(pkgargn * sizeof(*pkgargv))) == NULL) + err(1, NULL); + pkgargv[0] = pkgpath; + pkgargc = 1; + + while ((ch = getopt_long(argc, argv, "+:dj:No:r:46", longopts, NULL)) != -1) { + if (pkgargc + 2 >= pkgargn) { + pkgargn *= 2; + pkgargv = reallocarray(pkgargv, pkgargn, sizeof(*pkgargv)); + if (pkgargv == NULL) + err(1, NULL); + } switch (ch) { + case '4': + fetchOpts = "4"; + pkgargv[pkgargc++] = "-4"; + break; + case '6': + fetchOpts = "6"; + pkgargv[pkgargc++] = "-6"; + break; case 'd': debug++; + pkgargv[pkgargc++] = "-d"; + break; + case 'j': + jailname = optarg; + if ((jid = jail_getid(jailname)) == -1) + err(1, "%s", jail_errmsg); + if (jail_attach(jid) != 0) + err(1, "jail_attach(%s)", jailname); + /* do not pass on to pkg(8) */ + break; + case 'l': + pkgargv[pkgargc++] = "-l"; break; case 'N': activation_test = true; + pkgargv[pkgargc++] = "-N"; break; - case '4': - fetchOpts = "4"; + case 'o': + /* just pass it on */ + pkgargv[pkgargc++] = "-o"; + pkgargv[pkgargc++] = optarg; break; - case '6': - fetchOpts = "6"; + case 'r': + rootdir = optarg; + pkgargv[pkgargc++] = "-r"; + pkgargv[pkgargc++] = rootdir; break; default: + if (optopt == 0) { + /* unrecognized long option */ + pkgargv[pkgargc++] = argv[optind - 1]; + } else { + /* unrecognized short option */ + if (asprintf(&arg, "-%c", optopt) < 0) + err(1, NULL); + pkgargv[pkgargc++] = arg; + } + /* did it have an argument? */ + if (optarg != NULL) + pkgargv[pkgargc++] = optarg; break; } } + if (debug > 1) fetchDebug = 1; argc -= optind; argv += optind; + if (pkgargc + argc >= pkgargn) { + pkgargn = pkgargc + argc + 1; + pkgargv = reallocarray(pkgargv, pkgargn, sizeof(*pkgargv)); + if (pkgargv == NULL) + err(1, NULL); + } + for (int i = 0; i < argc; i++) + pkgargv[pkgargc++] = argv[i]; + pkgargv[pkgargc] = NULL; + + localbase = getlocalbase(); + if (localbase[0] == '/' && localbase[1] == '\0') + localbase++; + snprintf(pkgpath, MAXPATHLEN, "%s/sbin/pkg", localbase); + if (argc >= 1) { if (strcmp(argv[0], "bootstrap") == 0) { bootstrap_only = true; @@ -1134,7 +1236,7 @@ main(int argc, char *argv[]) optreset = 1; optind = 1; if (bootstrap_only || add_pkg) { - struct option sub_longopts[] = { + static const struct option sub_longopts[] = { { "force", no_argument, NULL, 'f' }, { "yes", no_argument, NULL, 'y' }, { NULL, 0, NULL, 0 }, @@ -1166,7 +1268,7 @@ main(int argc, char *argv[]) * `pkg install -y foobar` work as expected when pkg is * not yet bootstrapped. */ - struct option sub_longopts[] = { + static const struct option sub_longopts[] = { { "yes", no_argument, NULL, 'y' }, { NULL, 0, NULL, 0 }, }; @@ -1195,7 +1297,7 @@ main(int argc, char *argv[]) fprintf(stderr, "Path to pkg.pkg required\n"); exit(EXIT_FAILURE); } else if (argc == 1 && pkg_is_pkg_pkg(argv[0])) { - pkgarg = argv[0]; + posarg = argv[0]; } else { /* * If the target package is not pkg.pkg @@ -1207,7 +1309,13 @@ main(int argc, char *argv[]) } } - if ((bootstrap_only && force) || access(pkgpath, X_OK) == -1) { + rootd = open(rootdir ? rootdir : "/", + O_DIRECTORY | O_SEARCH | O_CLOEXEC); + if (rootd < 0) + err(1, "%s", rootdir ? rootdir : "/"); + + if ((bootstrap_only && force) || + faccessat(rootd, pkgpath + 1, X_OK, 0) == -1) { struct repository *repo; int ret = 0; /* @@ -1221,12 +1329,12 @@ main(int argc, char *argv[]) config_init(repo_name); if (add_pkg) { - assert(pkgarg != NULL); - if (access(pkgarg, R_OK) == -1) { - fprintf(stderr, "No such file: %s\n", pkgarg); + assert(posarg != NULL); + if (access(posarg, R_OK) == -1) { + fprintf(stderr, "No such file: %s\n", posarg); exit(EXIT_FAILURE); } - if (bootstrap_pkg_local(pkgarg, force) != 0) + if (bootstrap_pkg_local(posarg, force) != 0) exit(EXIT_FAILURE); exit(EXIT_SUCCESS); } @@ -1259,11 +1367,18 @@ main(int argc, char *argv[]) if (bootstrap_only) exit(EXIT_SUCCESS); } else if (bootstrap_only) { - printf("pkg already bootstrapped at %s\n", pkgpath); + printf("pkg already bootstrapped at %s%s\n", + rootdir ? rootdir : "", pkgpath); exit(EXIT_SUCCESS); } - execv(pkgpath, original_argv); + if (rootdir) { + if (fchdir(rootd) != 0) + err(1, "%s", rootdir); + while (*pkgpath == '/') + pkgpath++; + } + execv(pkgpath, __DECONST(char **, pkgargv)); /* NOT REACHED */ return (EXIT_FAILURE);