From nobody@FreeBSD.org  Thu Jun  2 20:24:08 2011
Return-Path: <nobody@FreeBSD.org>
Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34])
	by hub.freebsd.org (Postfix) with ESMTP id 3669F106566B
	for <freebsd-gnats-submit@FreeBSD.org>; Thu,  2 Jun 2011 20:24:08 +0000 (UTC)
	(envelope-from nobody@FreeBSD.org)
Received: from red.freebsd.org (red.freebsd.org [IPv6:2001:4f8:fff6::22])
	by mx1.freebsd.org (Postfix) with ESMTP id 25B2C8FC14
	for <freebsd-gnats-submit@FreeBSD.org>; Thu,  2 Jun 2011 20:24:08 +0000 (UTC)
Received: from red.freebsd.org (localhost [127.0.0.1])
	by red.freebsd.org (8.14.4/8.14.4) with ESMTP id p52KO7Zw021846
	for <freebsd-gnats-submit@FreeBSD.org>; Thu, 2 Jun 2011 20:24:07 GMT
	(envelope-from nobody@red.freebsd.org)
Received: (from nobody@localhost)
	by red.freebsd.org (8.14.4/8.14.4/Submit) id p52KO74p021845;
	Thu, 2 Jun 2011 20:24:07 GMT
	(envelope-from nobody)
Message-Id: <201106022024.p52KO74p021845@red.freebsd.org>
Date: Thu, 2 Jun 2011 20:24:07 GMT
From: David Naylor <naylor.b.david@gmail.com>
To: freebsd-gnats-submit@FreeBSD.org
Subject: pkg_add fails to install with -C from bad path
X-Send-Pr-Version: www-3.1
X-GNATS-Notify:

>Number:         157543
>Category:       bin
>Synopsis:       [patch] pkg_add(1) fails to install with -C from bad path
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    bapt
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Thu Jun 02 20:30:13 UTC 2011
>Closed-Date:    Tue Oct 02 17:08:10 UTC 2012
>Last-Modified:  Tue Oct 02 17:08:10 UTC 2012
>Originator:     David Naylor
>Release:        9.0-CURRENT
>Organization:
Private
>Environment:
FreeBSD dragon.dg 9.0-CURRENT FreeBSD 9.0-CURRENT #0: Sat May 21 22:12:29 SAST 2011
>Description:
pkg_add appears to chdir back to its calling directory after installed the package.  When installing into a chroot that directory may not exist in the chroot and pkg_add fails.
>How-To-Repeat:
# mkdir /does/not/exist/in/scratchpad
# cd /does/not/exist/in/scratchpad
# pkg_add -C /scratchpad/pkg_env /usr/ports/packages/All/perl-threaded-5.12.3.txz

Removing stale symlinks from /usr/bin...
    Removing /usr/bin/perl
    Removing /usr/bin/perl5
Done.
Creating various symlinks in /usr/bin...
    Symlinking /usr/local/bin/perl5.12.3 to /usr/bin/perl
    Symlinking /usr/local/bin/perl5.12.3 to /usr/bin/perl5
Done.
Cleaning up /etc/make.conf... Done.
Spamming /etc/make.conf... Done.
pkg_add: leave_playpen: can't chdir back to ''
# echo $?
2
>Fix:
Use `chroot /scratchpad/pkg_env pkg_add ...` or start pkg_add from a directory that exists in the chroot environment.

>Release-Note:
>Audit-Trail:
Responsible-Changed-From-To: freebsd-bugs->portmgr 
Responsible-Changed-By: linimon 
Responsible-Changed-When: Fri Jun 3 14:31:54 UTC 2011 
Responsible-Changed-Why:  
Over to maintainer(s). 

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

From: Dmitry Banschikov <d.banschikov@peterhost.ru>
To: bug-followup@FreeBSD.org, naylor.b.david@gmail.com
Cc:  
Subject: Re: bin/157543: pkg_add(1) fails to install with -C from bad path
Date: Fri, 08 Jul 2011 11:14:22 +0400

 This is a multi-part message in MIME format.
 --------------000300070803070900060101
 Content-Type: text/plain; charset=ISO-8859-1; format=flowed
 Content-Transfer-Encoding: 7bit
 
 Hi!
 
 The reason for this error is that pkg_add creates temporary directories 
 for each package while performing its task. After installation of each 
 package pkg_add tries to return back to the previous directory (previous 
 dirs are stored in the stack structure). If you run pkg_add with -C and 
 your CWD does not exists in the chroot dir, pkg_add
 tries to return back to non-existing path in the chroot (error takes 
 place on the 168 line of lib/pen.c while trying to chdir(PenLocation)).
 The solution is to force chdir() to the chroot path before really chroot().
 
 
 
 -- 
 
 Dmitry Banschikov
 
 --------------000300070803070900060101
 Content-Type: text/plain;
  name="add-main.c.patch.txt"
 Content-Transfer-Encoding: 7bit
 Content-Disposition: attachment;
  filename="add-main.c.patch.txt"
 
 --- main.c.orig	2011-07-07 18:26:32.000000000 +0000
 +++ main.c	2011-07-07 18:24:17.000000000 +0000
 @@ -278,7 +278,10 @@
      }
      /* Perform chroot if requested */
      if (Chroot != NULL) {
 -	if (chroot(Chroot))
 +	/* Force chdir to the chroot dir */
 +	if (chdir(Chroot))
 +	    errx(1, "chdir to %s failed", Chroot);
 +	if (chroot("."))
  	    errx(1, "chroot to %s failed", Chroot);
      }
      /* Make sure the sub-execs we invoke get found */
 
 --------------000300070803070900060101--

From: David Naylor <naylor.b.david@gmail.com>
To: Dmitry Banschikov <d.banschikov@peterhost.ru>
Cc: bug-followup@freebsd.org
Subject: Re: bin/157543: pkg_add(1) fails to install with -C from bad path
Date: Thu, 28 Jul 2011 23:12:30 +0200

 On Friday, 8 July 2011 09:14:22 Dmitry Banschikov wrote:
 > Hi!
 > 
 > The reason for this error is that pkg_add creates temporary directories
 > for each package while performing its task. After installation of each
 > package pkg_add tries to return back to the previous directory (previous
 > dirs are stored in the stack structure). If you run pkg_add with -C and
 > your CWD does not exists in the chroot dir, pkg_add
 > tries to return back to non-existing path in the chroot (error takes
 > place on the 168 line of lib/pen.c while trying to chdir(PenLocation)).
 > The solution is to force chdir() to the chroot path before really chroot().
 
 I tested the previously attached patch and it works.  I also found that 
 getting my program to chdir to '/' is a good enough workaround.  
State-Changed-From-To: open->patched 
State-Changed-By: bapt 
State-Changed-When: Tue Sep 18 22:13:08 UTC 2012 
State-Changed-Why:  
awaiting MFC 


Responsible-Changed-From-To: portmgr->bapt 
Responsible-Changed-By: bapt 
Responsible-Changed-When: Tue Sep 18 22:13:08 UTC 2012 
Responsible-Changed-Why:  
awaiting MFC 

http://www.freebsd.org/cgi/query-pr.cgi?pr=157543 
State-Changed-From-To: patched->closed 
State-Changed-By: bapt 
State-Changed-When: Tue Oct 2 17:08:10 UTC 2012 
State-Changed-Why:  
MFCed, thanks! 

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