From nick@van-laarhoven.org  Mon Feb 19 20:29:31 2007
Return-Path: <nick@van-laarhoven.org>
Received: from mx1.freebsd.org (mx1.freebsd.org [69.147.83.52])
	by hub.freebsd.org (Postfix) with ESMTP id 4010D16D0D0
	for <FreeBSD-gnats-submit@freebsd.org>; Mon, 19 Feb 2007 20:29:31 +0000 (UTC)
	(envelope-from nick@van-laarhoven.org)
Received: from bali.sjc.webweaving.org (bali.sjc.webweaving.org [216.218.185.16])
	by mx1.freebsd.org (Postfix) with ESMTP id 25CD013C467
	for <FreeBSD-gnats-submit@freebsd.org>; Mon, 19 Feb 2007 20:29:29 +0000 (UTC)
	(envelope-from nick@van-laarhoven.org)
Received: from van-laarhoven.org (ap-zvhz-13f05.adsl.wanadoo.nl [81.69.93.5])
	(authenticated bits=0)
	by bali.sjc.webweaving.org (8.12.11/8.12.11) with ESMTP id l1JKTGJM098539
	for <FreeBSD-gnats-submit@freebsd.org>; Mon, 19 Feb 2007 12:29:25 -0800 (PST)
	(envelope-from nick@van-laarhoven.org)
Received: (nullmailer pid 23448 invoked by uid 1001);
	Mon, 19 Feb 2007 10:07:49 -0000
Message-Id: <1171879669.708965.23447.nullmailer@van-laarhoven.org>
Date: Mon, 19 Feb 2007 11:07:49 +0100
From: Nick Hibma <nick@anywi.com>
To: FreeBSD-gnats-submit@freebsd.org
Subject: pkg_add using chroot exits with error if wrong directory
X-Send-Pr-Version: 3.113
X-GNATS-Notify:

>Number:         109334
>Category:       bin
>Synopsis:       pkg_add(1) using chroot exits with error if wrong directory
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    portmgr
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Mon Feb 19 20:30:05 GMT 2007
>Closed-Date:    Wed Sep 18 10:38:33 UTC 2013
>Last-Modified:  Wed Sep 18 10:45:26 UTC 2013
>Originator:     Nick Hibma
>Release:        FreeBSD 6.2-PRERELEASE i386
>Organization:
AnyWi Technologies
>Environment:
System: FreeBSD hind.van-laarhoven.org 6.2-PRERELEASE FreeBSD 6.2-PRERELEASE #5: Wed Jan 17 18:49:39 CET 2007 root@:/usr/src/sys/i386/compile/HIND i386

>Description:

	PKG_DBDIR=/pkgdb
	PKG_PATH=/packages
	export PKG_DBDIR
	export PKG_PATH
	mkdir /tmp/somedir
	cd /tmp/somedir
	pkg_add -C /usr/obj/nanobsd/_.w /packages/perl.tbz

produces an error on exit as the directory /tmp/somedir does not exist in the
chroot-ed environment.

Fix is to add a 'cd /tmp' just before the package add, but this might not be an option in cases where the package is referenced relative.

>How-To-Repeat:

	See above.

>Fix:

Most probably the chdir should be moved to the part of the code that is outside the chroot.
>Release-Note:
>Audit-Trail:

From: Nick Hibma <nick@anywi.com>
To: Remko Lodder <remko@elvandar.org>
Cc: FreeBSD-gnats-submit@FreeBSD.org
Subject: Re: bin/109334: pkg_add using chroot exits with error if wrong    
  directory
Date: Tue, 20 Feb 2007 16:34:17 +0100 (CET)

 Remko,
 
 nick@hind:/usr/obj/nanobsd.koole.ship-2.4 % cd /tmp/
 nick@hind:/tmp % mkdir somedir
 nick@hind:/tmp % cd somedir/
 nick@hind:/tmp/somedir % export PKG_DBDIR=/pkgdb
 nick@hind:/tmp/somedir % export PKG_PATH=/packages
 nick@hind:/tmp/somedir % sudo pkg_add -C /usr/obj/nanobsd.koole.ship-2.4/_.w /packages/mpd-3.18_5.tbz
 pkg_add: leave_playpen: can't chdir back to '/tmp/somedir'
 nick@hind:/tmp/somedir % echo $?
 2
 nick@hind:/tmp/somedir % ls -l 
 /usr/obj/nanobsd.koole.ship-2.4/_.w/usr/local/sbin/mpd -r-xr-xr-x  1 root  wheel  220084 Jan 19 02:35 /usr/obj/nanobsd.koole.ship-2.4/_.w/usr/local/sbin/mpd
 
 The error happens at the very end. The package gets installed correctly 
 but the error code is still set.
 
 Nick
 

From: Garrett Cooper <yanegomi@gmail.com>
To: bug-followup@FreeBSD.org, nick@anywi.com
Cc:  
Subject: Re: bin/109334: pkg_add(1) using chroot exits with error if wrong 
	directory
Date: Sat, 20 Mar 2010 18:36:20 -0700

 Hi Nick,
     I realize that this PR has been sitting around for a long time,
 but is it possible that you can dig up more details, like filesystem
 layout and/or environment variables used when trying to run pkg_add? I
 know this seems like a lot, and yes I might be able to reproduce the
 issue on my own, but the fact is that there are a large number of
 potential failures that could occur based on what you had set in your
 environment and what did not exist on your chrooted filesystem.
 Thanks,
 -Garrett

From: Garrett Cooper <yanegomi@gmail.com>
To: bug-followup@freebsd.org, nick@anywi.com
Cc:  
Subject: Re: bin/109334: pkg_add(1) using chroot exits with error if wrong 
	directory
Date: Sun, 21 Mar 2010 01:42:52 -0700

 On Sat, Mar 20, 2010 at 6:36 PM, Garrett Cooper <yanegomi@gmail.com> wrote:
 > Hi Nick,
 > =A0 =A0I realize that this PR has been sitting around for a long time,
 > but is it possible that you can dig up more details, like filesystem
 > layout and/or environment variables used when trying to run pkg_add? I
 > know this seems like a lot, and yes I might be able to reproduce the
 > issue on my own, but the fact is that there are a large number of
 > potential failures that could occur based on what you had set in your
 > environment and what did not exist on your chrooted filesystem.
 
 Nevermind. This is easy to produce and I know why it's happening. From
 chroot(2):
 
      It should be noted that chroot() has no effect on the process's curren=
 t
      directory.
 
 Let me think about the best way to resolve this issue and I'll get
 back to you by Monday or Tuesday.
 
 Thanks,
 -Garrett

From: Garrett Cooper <yanegomi@gmail.com>
To: bug-followup@FreeBSD.org, nick@anywi.com
Cc: randi@freebsd.org
Subject: Re: bin/109334: pkg_add(1) using chroot exits with error if wrong 
	directory
Date: Tue, 23 Mar 2010 23:52:42 -0700

     The original comment states that sysinstall was the primary
 consumer of the -C option, and while that may have been the case (once
 upon a time), it isn't today according to the code I see in
 usr.sbin/sysinstall/package.c .
     So what I'm going to propose is the following: change the chroot,
 s.t. it only affects the install, operation, which means that instead
 of chroot'ing immediately, it'll first try to open the packages, then
 prior to executing the install instructions will do the chroot and
 will `chdir' to the default prefix (user specified); the default value
 will be '/'. That way the chdir back to the starting point will be
 sane.
     Another less invasive solution would be to merely pop '/' on to
 the playpen `stack' for the master in make_playpen, i.e.:
 
     if (!getcwd(cwd, FILENAME_MAX)) {
         upchuck("getcwd");
         return NULL;
     }
 
     if (chdir(pen) == FAIL) { /* <-- 2. source of confusion */
         cleanup(0);
         errx(2, "%s: can't chdir to '%s'", __func__, pen);
     }
 
     strcpy(PenLocation, pen);
     return pushPen(cwd); /* <-- 2. source of confusion */
 
     The pseudo code being:
 
     if chroot'ed then push '/' on the stack
     else push the current working directory on the stack
 
     The only potential issue I could see is when installing multiple
 packages, but that use is tainted anyhow because we chrooted early on,
 so the assumption is that the packages have been properly mirrored in
 the target environment like they are in the host non-chrooted
 environment.
     It's up to you guys -- it's a toss-up either way as there will be
 some level of confusion, but given that pkg_install is a mess and I
 hope to refactor it over the course of the next couple of years, this
 will be a minor mess that will need to be cleaned up later.
 Thanks,
 -Garrett
Responsible-Changed-From-To: freebsd-bugs->portmgr 
Responsible-Changed-By: flz 
Responsible-Changed-When: Thu Apr 1 17:21:00 UTC 2010 
Responsible-Changed-Why:  
pkg_install is maintained by portmgr. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=109334 

From: Garrett Cooper <yanegomi@gmail.com>
To: nick@anywi.com
Cc: portmgr@freebsd.org, bug-followup <bug-followup@freebsd.org>
Subject: Re: bin/109334: pkg_add(1) using chroot exits with error if wrong 
	directory
Date: Sat, 10 Apr 2010 22:58:38 -0700

 --0016e68e8b703c01c00483efb8bb
 Content-Type: text/plain; charset=ISO-8859-1
 Content-Transfer-Encoding: quoted-printable
 
 Sorry -- forgot to CC bug-followup@
 Thanks,
 -Garrett
 
 On Wed, Apr 7, 2010 at 1:09 AM, Garrett Cooper <yanegomi@gmail.com> wrote:
 > (Moving randi@ to BCC)
 >
 > On Tue, Mar 23, 2010 at 11:52 PM, Garrett Cooper <yanegomi@gmail.com> wro=
 te:
 >> =A0 =A0The original comment states that sysinstall was the primary
 >> consumer of the -C option, and while that may have been the case (once
 >> upon a time), it isn't today according to the code I see in
 >> usr.sbin/sysinstall/package.c .
 >> =A0 =A0So what I'm going to propose is the following: change the chroot,
 >> s.t. it only affects the install, operation, which means that instead
 >> of chroot'ing immediately, it'll first try to open the packages, then
 >> prior to executing the install instructions will do the chroot and
 >> will `chdir' to the default prefix (user specified); the default value
 >> will be '/'. That way the chdir back to the starting point will be
 >> sane.
 >> =A0 =A0Another less invasive solution would be to merely pop '/' on to
 >> the playpen `stack' for the master in make_playpen, i.e.:
 >>
 >> =A0 =A0if (!getcwd(cwd, FILENAME_MAX)) {
 >> =A0 =A0 =A0 =A0upchuck("getcwd");
 >> =A0 =A0 =A0 =A0return NULL;
 >> =A0 =A0}
 >>
 >> =A0 =A0if (chdir(pen) =3D=3D FAIL) { /* <-- 2. source of confusion */
 >> =A0 =A0 =A0 =A0cleanup(0);
 >> =A0 =A0 =A0 =A0errx(2, "%s: can't chdir to '%s'", __func__, pen);
 >> =A0 =A0}
 >>
 >> =A0 =A0strcpy(PenLocation, pen);
 >> =A0 =A0return pushPen(cwd); /* <-- 2. source of confusion */
 >>
 >> =A0 =A0The pseudo code being:
 >>
 >> =A0 =A0if chroot'ed then push '/' on the stack
 >> =A0 =A0else push the current working directory on the stack
 >>
 >> =A0 =A0The only potential issue I could see is when installing multiple
 >> packages, but that use is tainted anyhow because we chrooted early on,
 >> so the assumption is that the packages have been properly mirrored in
 >> the target environment like they are in the host non-chrooted
 >> environment.
 >> =A0 =A0It's up to you guys -- it's a toss-up either way as there will be
 >> some level of confusion, but given that pkg_install is a mess and I
 >> hope to refactor it over the course of the next couple of years, this
 >> will be a minor mess that will need to be cleaned up later.
 >
 > =A0 =A0Personally, I like the idea of dealing with a chroot within
 > pkg_install, because it makes the code required to do installations
 > and tests potentially simpler, but I honestly don't see the added
 > advantage vs the gain as there's a lot of setup required in many cases
 > to get a working jail up and going (/etc/rc.d/ldconfig start, specify
 > UNAME_m maybe, specify OSRELVERSION, etc). I hacked up a workaround to
 > this issue in 10 minutes via bourne shells and Makefiles, and it's
 > considerably easier to understand than the needed complexity would
 > provide.
 > =A0 =A0Does portmgr think this functionality should still exist as it's
 > not used in any of the mainline tools or methods (portmaster, ports,
 > sysinstall)?
 
 =A0 =A0Given that no one has replied with a nay on either arch or ports
 (kientzle gave a yay, as well as flz), here's the patch that would
 remove -C that I'm currently testing.
 Thanks,
 -Garrett
 
 --0016e68e8b703c01c00483efb8bb
 Content-Type: application/octet-stream; name="bin.109334.diff"
 Content-Disposition: attachment; filename="bin.109334.diff"
 Content-Transfer-Encoding: base64
 X-Attachment-Id: f_g7vfzxrs1
 
 SW5kZXg6IGFkZC9tYWluLmMKPT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09
 PT09PT09PT09PT09PT09PT09PT09PT09PT09PQotLS0gYWRkL21haW4uYwkocmV2aXNpb24gMjA2
 MTczKQorKysgYWRkL21haW4uYwkod29ya2luZyBjb3B5KQpAQCAtMzEsNyArMzEsNiBAQAogCiBj
 aGFyCSpQcmVmaXgJCT0gTlVMTDsKIEJvb2xlYW4JUHJlZml4UmVjdXJzaXZlCT0gRkFMU0U7Ci1j
 aGFyCSpDaHJvb3QJCT0gTlVMTDsKIEJvb2xlYW4JTm9JbnN0YWxsCT0gRkFMU0U7CiBCb29sZWFu
 CU5vUmVjb3JkCT0gRkFMU0U7CiBCb29sZWFuIFJlbW90ZQkJPSBGQUxTRTsKQEAgLTEwMiw3ICsx
 MDEsNiBAQAogCiBzdGF0aWMgY2hhciBvcHRzW10gPSAiaHZpSVJmRm5ycDpQOlNNdDpDOksiOwog
 c3RhdGljIHN0cnVjdCBvcHRpb24gbG9uZ29wdHNbXSA9IHsKLQl7ICJjaHJvb3QiLAlyZXF1aXJl
 ZF9hcmd1bWVudCwJTlVMTCwJCSdDJyB9LAogCXsgImRyeS1ydW4iLAlub19hcmd1bWVudCwJCU5V
 TEwsCQknbicgfSwKIAl7ICJmb3JjZSIsCW5vX2FyZ3VtZW50LAkJTlVMTCwJCSd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EgZmFpcmx5IGNvbXBsZXRl
 IGZpbGUgc3lzdGVtLCBpbmNsdWRpbmcgZXZlcnl0aGluZyBub3JtYWxseQotbmVlZGVkIGJ5Ci0u
 Tm0KLXRvIHJ1bi4KLVRoaXMgZmxhZyB3YXMgYWRkZWQgdG8gaGVscCBzdXBwb3J0IG9wZXJhdGlv
 bnMgZG9uZSBieQotLlhyIHN5c2luc3RhbGwgOAotYW5kIGlzIG5vdCBleHBlY3RlZCB0byBiZSB1
 c2VmdWwgZm9yIG11Y2ggZWxzZS4KLUJlIGNhcmVmdWwgdGhhdAotLkFyIGNocm9vdGRpcgotaXMg
 cHJvcGVybHkgY29uZmlndXJlZCBhbmQgY2Fubm90IGJlIG1vZGlmaWVkIGJ5IG5vcm1hbCB1c2Vy
 cywKLXZlcnNpb25zIG9mIGNvbW1hbmRzIGxpa2UKLS5YciBmZXRjaCAxCi1tYXkgYmUgcnVuIGlu
 c2lkZQotLkFyIGNocm9vdGRpcgotYXMgYSBzaWRlIGVmZmVjdC4KLS5FbAogLlBwCiBPbmUgb3Ig
 bW9yZQogLkFyIHBrZy1uYW1lCg==
 --0016e68e8b703c01c00483efb8bb--

From: Nick Hibma <nick@anywi.com>
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: bin/109334: pkg_add(1) using chroot exits with error if wrong directory
Date: Thu, 8 Nov 2012 21:40:16 +0100

 I don't have a problem with removing the -C switch. I would suggest to =
 add an example that explains how to use a chroot when needed.
 
 A work-around is to change to /tmp before doing the chroot:
 
 cd /tmp
 pkg_add -C $NANO_WORLDDIR ...
 
 and that should be replaceable by 
 
 cd /tmp
 chroot $NANO_WORLDDIR pkg_add 
 
 Please note that pkg_add is being phased out and therefore I close this PR.
 
 Nick Hibma
 AnyWi Technologies
 
State-Changed-From-To: open->closed 
State-Changed-By: n_hibma 
State-Changed-When: Wed Sep 18 10:37:00 UTC 2013 
State-Changed-Why:  
Closed as pkg_add is being phased out in CURRENT. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=109334 
>Unformatted:
