Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
Add support for jail and chroot, document it in the man page
Baptiste Daroussin committed 14 years ago
commit fedd1d7b9b70d616b1048d62ec60cb597f42d999
parent 7acb4f9
3 files changed +62 -9
modified pkg/Makefile
@@ -22,7 +22,8 @@ DEBUG_FLAGS+= -g -O0
CFLAGS+=	-I${.CURDIR}/../libpkg
LDADD+=		-L../libpkg \
		-lpkgng \
-
		-lutil
+
		-lutil \
+
		-ljail
WARNS?=		6
MAN=	pkg.1 \
	pkg-add.1 \
modified pkg/main.c
@@ -5,6 +5,9 @@
#include <string.h>
#include <sysexits.h>
#include <unistd.h>
+
#include <sys/param.h>
+
#include <sys/jail.h>
+
#include <jail.h>

#include <pkg.h>

@@ -55,7 +58,7 @@ const unsigned int cmd_len = (sizeof(cmd)/sizeof(cmd[0]));
static void
usage(void)
{
-
	fprintf(stderr, "usage: pkg [-d] <command> [<args>]\n\n");
+
	fprintf(stderr, "usage: pkg [-d] [-j <jail name or id>|-c <chroot path>] <command> [<args>]\n\n");
	fprintf(stderr, "Global options supported:\n");
	fprintf(stderr, "  -d: Increment debug level\n\n");
	fprintf(stderr, "Commands supported:\n");
@@ -115,6 +118,9 @@ main(int argc, char **argv)
	unsigned int i;
	struct commands *command = NULL;
	unsigned int ambiguous = 0;
+
	const char *chroot_path = NULL;
+
	int jid;
+
	const char *jail_str = NULL;
	size_t len;
	char ch;
	int debug = 0;
@@ -124,11 +130,17 @@ main(int argc, char **argv)

	pkg_event_register(&event_callback, NULL);

-
	while ((ch = getopt(argc, argv, "d")) != -1) {
+
	while ((ch = getopt(argc, argv, "dj:c:")) != -1) {
		switch(ch) {
			case 'd':
				debug++;
				break;
+
			case 'c':
+
				chroot_path = optarg;
+
				break;
+
			case 'j':
+
				jail_str = optarg;
+
				break;
			default:
				break;
		}
@@ -139,6 +151,30 @@ main(int argc, char **argv)
	optreset = 1;
	optind = 1;

+
	if (jail_str != NULL && chroot_path != NULL) {
+
		fprintf(stderr, "-j and -c can be used at the same time\n");
+
		usage();
+
	}
+

+
	if (chroot_path != NULL)
+
		if (chroot(chroot_path) == -1)
+
			errx(EX_SOFTWARE, "chroot failed");
+

+
	if (jail_str != NULL) {
+
		jid = jail_getid(jail_str);
+
		if (jid < 0)
+
			errx(1, "%s", jail_errmsg);
+

+
		if (jail_attach(jid) == -1)
+
			err(1, "jail_attach(%s)", jail_str);
+
	}
+

+
	if (jail_str != NULL || chroot_path != NULL)
+
		if (chdir("/") == -1)
+
			errx(EX_SOFTWARE, "chdir() failed");
+

+

+

	len = strlen(argv[0]);
	for (i = 0; i < cmd_len; i++) {
		if (strncmp(argv[0], cmd[i].name, len) == 0) {
modified pkg/pkg.1
@@ -23,15 +23,34 @@
.Nd a utility for manipulating packages 
.Sh SYNOPSIS
.Nm
-
.Ar <command> Ar <flags>
+
.Op Fl d
+
.Op Fl j Ao jail name or id Ac | Fl c Ao chroot path Ac
+
.Ao command Ac Ao Ar flags Ac
.Sh DESCRIPTION
-
.Nm
provides an interface for manupulating packages - registering,
adding, removing and upgrading of packages.
+
.Sh OPTIONS
+
.Pp
+
The following options are supported by
+
.Nm:
+
.Bl -tag -width indent
+
.It Fl d
+
Show debug informations
+
.It Fl j Ao jail name or id Ac
+
.Nm
+
will execute in the given
+
.Ao jail name or id Ac
+
.Xr jail 8
+
.It Fl c Ao chroot path Ac
+
.Nm
+
will chroot in the
+
.Ao chroot path Ac
+
Environment
+
.El
.Sh COMMANDS
The following commands are supported by
.Nm :
-
.Bl -tag -width F1
+
.Bl -tag -width indent
.It \fBhelp\fP Ar <command>
Displays usage information of the specified command.
.It \fBadd\fP
@@ -84,7 +103,6 @@ System-wide configuration file. This file format and available configuration
options are described in
.Xr pkg.conf 5 .
.El
-

.Bl -tag -width ".Pa /etc/pkg/repositories"
.It Pa /etc/pkg/repositories
Global repositories file. This file contains a pair of
@@ -113,5 +131,3 @@ definitions of remote repositories.
.Xr pkg.conf 5
.Sh AUTHORS AND CONTRIBUTORS
.Sh BUGS
-
.Sh AUTHORS AND CONTRIBUTORS
-
.Sh BUGS