Bug 166091

Summary: [libc][patch] fts(3) should document cases where FTS_NOCHDIR option is set as a side-effect
Product: Documentation Reporter: Matthew Story <matthewstory>
Component: Books & ArticlesAssignee: Jilles Tjoelker <jilles>
Status: Closed FIXED    
Severity: Affects Only Me    
Priority: Normal    
Version: Latest   
Hardware: Any   
OS: Any   
Attachments:
Description Flags
file.diff
none
fts.3.add_FTS_NOCHDIR_side_effect_cases.patch.txt none

Description Matthew Story 2012-03-14 15:20:01 UTC
fts(3) sets the FTS_NOCHDIR flag in 2 cases aside from when it is explicitly provided to fts_open(3)

1. FTS_LOGICAL flag is provided to fts_open(3)

 145     /* Logical walks turn on NOCHDIR; symbolic links are too hard. */
 146     if (ISSET(FTS_LOGICAL))
 147         SET(FTS_NOCHDIR);

2. fts_open(3) cannot open(2) `.'

 209     /*
 210      * If using chdir(2), grab a file descriptor pointing to dot to ensure
 211      * that we can get back here; this could be avoided for some paths,
 212      * but almost certainly not worth the effort.  Slashes, symbolic links,
 213      * and ".." are all fairly nasty problems.  Note, if we can't get the
 214      * descriptor we run anyway, just more slowly.
 215      */
 216     if (!ISSET(FTS_NOCHDIR) && (sp->fts_rfd = _open(".", O_RDONLY, 0)) < 0)
 217         SET(FTS_NOCHDIR);

A misunderstanding of the former case caused find(1) to not traverse directories properly for -execdir when the -L option was specified (see bin/166056)

Fix: apply patch (patch is against head), patch is available via http here:

http://axe0.blackskyresearch.net/patches/matt/fts.3.add_FTS_NOCHDIR_side_effect_cases.patch.txt

Patch attached with submission follows:
How-To-Repeat: man 3 fts
Comment 1 Jilles Tjoelker freebsd_committer freebsd_triage 2012-03-16 22:38:53 UTC
> [fts(3) automatically sets FTS_NOCHDIR option in some cases]

I consider the automatic FTS_NOCHDIR a semi-bug that should not be
relied on. If FTS_NOCHDIR is set, fts(3) runs slower and is subject to
{PATH_MAX}. The latter would violate POSIX in various utilities.

I tried to allow FTS_LOGICAL without FTS_NOCHDIR a while ago, but while
it is conceptually possible, actually making it work is hard.

The open(".", O_RDONLY) can use O_SEARCH when it is added (for now,
O_EXEC works) so it only needs 'x' right not also 'r'.

-- 
Jilles Tjoelker
Comment 2 Matthew Story 2012-03-16 23:02:47 UTC
On Fri, Mar 16, 2012 at 6:38 PM, Jilles Tjoelker <jilles@stack.nl> wrote:

> > [fts(3) automatically sets FTS_NOCHDIR option in some cases]
>
> I consider the automatic FTS_NOCHDIR a semi-bug that should not be
> relied on.


I agree with this, but as the behavior is non-obvious I think it should be
noted.  Perhaps this is more appropriate for the BUGS section than the
fts_open section?


> If FTS_NOCHDIR is set, fts(3) runs slower and is subject to
> {PATH_MAX}. The latter would violate POSIX in various utilities.
>

this would mean that find -L is currently in violation of POSIX?

I tried to allow FTS_LOGICAL without FTS_NOCHDIR a while ago, but while
> it is conceptually possible, actually making it work is hard.
>

Is anyone currently looking into this?


>
> The open(".", O_RDONLY) can use O_SEARCH when it is added (for now,
> O_EXEC works) so it only needs 'x' right not also 'r'.
>

So this would then fall back to FTS_NOCHDIR if `.' is not searchable?


>
> --
> Jilles Tjoelker
>



-- 
regards,
matt
Comment 3 Jilles Tjoelker freebsd_committer freebsd_triage 2012-03-17 15:11:03 UTC
On Fri, Mar 16, 2012 at 07:02:47PM -0400, Matthew Story wrote:
> On Fri, Mar 16, 2012 at 6:38 PM, Jilles Tjoelker <jilles@stack.nl> wrote:

> > > [fts(3) automatically sets FTS_NOCHDIR option in some cases]

> > I consider the automatic FTS_NOCHDIR a semi-bug that should not be
> > relied on.

> I agree with this, but as the behavior is non-obvious I think it should be
> noted.  Perhaps this is more appropriate for the BUGS section than the
> fts_open section?

Yes.

> > If FTS_NOCHDIR is set, fts(3) runs slower and is subject to
> > {PATH_MAX}. The latter would violate POSIX in various utilities.

> this would mean that find -L is currently in violation of POSIX?

Yes. The POSIX page about find is pretty clear on this issue:

% The find utility shall be able to descend to arbitrary depths in a
% file hierarchy and shall not fail due to path length limitations
% (unless a path operand specified by the application exceeds {PATH_MAX}
% requirements)

> I tried to allow FTS_LOGICAL without FTS_NOCHDIR a while ago, but while
> > it is conceptually possible, actually making it work is hard.

> Is anyone currently looking into this?

No. My patch (from over a year ago) is at
http://www.stack.nl/~jilles/unix/fts-logical-chdir-2.patch but I forgot
what severe breakage it causes.

> > The open(".", O_RDONLY) can use O_SEARCH when it is added (for now,
> > O_EXEC works) so it only needs 'x' right not also 'r'.

> So this would then fall back to FTS_NOCHDIR if `.' is not searchable?

Yes. This could be avoided by using functions like openat(2) and
fstatat(2) instead of changing the current directory, but only by
changing the API (because the current API requires fts(3) to furnish a
pathname that will access the object, not an fd and a pathname relative
to it).

-- 
Jilles Tjoelker
Comment 4 dfilter service freebsd_committer freebsd_triage 2012-03-18 20:34:11 UTC
Author: jilles
Date: Sun Mar 18 20:34:01 2012
New Revision: 233130
URL: http://svn.freebsd.org/changeset/base/233130

Log:
  fts(3): Document cases where FTS_NOCHDIR is set implicitly.
  
  PR:		docs/166091
  Submitted by:	Matthew Story
  MFC after:	1 week

Modified:
  head/lib/libc/gen/fts.3

Modified: head/lib/libc/gen/fts.3
==============================================================================
--- head/lib/libc/gen/fts.3	Sun Mar 18 19:35:30 2012	(r233129)
+++ head/lib/libc/gen/fts.3	Sun Mar 18 20:34:01 2012	(r233130)
@@ -28,7 +28,7 @@
 .\"     @(#)fts.3	8.5 (Berkeley) 4/16/94
 .\" $FreeBSD$
 .\"
-.Dd November 25, 2009
+.Dd March 18, 2012
 .Dt FTS 3
 .Os
 .Sh NAME
@@ -798,3 +798,13 @@ functions were introduced in
 principally to provide for alternative interfaces to the
 .Nm
 functionality using different data structures.
+.Sh BUGS
+The 
+.Fn fts_open
+function will automatically set the
+.Dv FTS_NOCHDIR
+option if the 
+.Dv FTS_LOGICAL
+option is provided, or if it cannot 
+.Xr open 2
+the current directory.
_______________________________________________
svn-src-all@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/svn-src-all
To unsubscribe, send any mail to "svn-src-all-unsubscribe@freebsd.org"
Comment 5 Jilles Tjoelker freebsd_committer freebsd_triage 2012-03-18 20:46:25 UTC
State Changed
From-To: open->patched

Fixed in head. 


Comment 6 Jilles Tjoelker freebsd_committer freebsd_triage 2012-03-18 20:46:25 UTC
Responsible Changed
From-To: freebsd-doc->jilles

Take.
Comment 7 dfilter service freebsd_committer freebsd_triage 2012-03-25 21:01:12 UTC
Author: jilles
Date: Sun Mar 25 20:01:03 2012
New Revision: 233474
URL: http://svn.freebsd.org/changeset/base/233474

Log:
  MFC r233130: fts(3): Document cases where FTS_NOCHDIR is set implicitly.
  
  PR:		docs/166091
  Submitted by:	Matthew Story

Modified:
  stable/9/lib/libc/gen/fts.3
Directory Properties:
  stable/9/lib/libc/   (props changed)

Modified: stable/9/lib/libc/gen/fts.3
==============================================================================
--- stable/9/lib/libc/gen/fts.3	Sun Mar 25 19:34:05 2012	(r233473)
+++ stable/9/lib/libc/gen/fts.3	Sun Mar 25 20:01:03 2012	(r233474)
@@ -28,7 +28,7 @@
 .\"     @(#)fts.3	8.5 (Berkeley) 4/16/94
 .\" $FreeBSD$
 .\"
-.Dd November 25, 2009
+.Dd March 18, 2012
 .Dt FTS 3
 .Os
 .Sh NAME
@@ -798,3 +798,13 @@ functions were introduced in
 principally to provide for alternative interfaces to the
 .Nm
 functionality using different data structures.
+.Sh BUGS
+The 
+.Fn fts_open
+function will automatically set the
+.Dv FTS_NOCHDIR
+option if the 
+.Dv FTS_LOGICAL
+option is provided, or if it cannot 
+.Xr open 2
+the current directory.
_______________________________________________
svn-src-all@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/svn-src-all
To unsubscribe, send any mail to "svn-src-all-unsubscribe@freebsd.org"
Comment 8 dfilter service freebsd_committer freebsd_triage 2012-03-25 21:08:03 UTC
Author: jilles
Date: Sun Mar 25 20:07:50 2012
New Revision: 233476
URL: http://svn.freebsd.org/changeset/base/233476

Log:
  MFC r233130: fts(3): Document cases where FTS_NOCHDIR is set implicitly.
  
  PR:		docs/166091
  Submitted by:	Matthew Story

Modified:
  stable/8/lib/libc/gen/fts.3
Directory Properties:
  stable/8/lib/libc/   (props changed)

Modified: stable/8/lib/libc/gen/fts.3
==============================================================================
--- stable/8/lib/libc/gen/fts.3	Sun Mar 25 20:03:13 2012	(r233475)
+++ stable/8/lib/libc/gen/fts.3	Sun Mar 25 20:07:50 2012	(r233476)
@@ -28,7 +28,7 @@
 .\"     @(#)fts.3	8.5 (Berkeley) 4/16/94
 .\" $FreeBSD$
 .\"
-.Dd November 25, 2009
+.Dd March 18, 2012
 .Dt FTS 3
 .Os
 .Sh NAME
@@ -798,3 +798,13 @@ functions were introduced in
 principally to provide for alternative interfaces to the
 .Nm
 functionality using different data structures.
+.Sh BUGS
+The 
+.Fn fts_open
+function will automatically set the
+.Dv FTS_NOCHDIR
+option if the 
+.Dv FTS_LOGICAL
+option is provided, or if it cannot 
+.Xr open 2
+the current directory.
_______________________________________________
svn-src-all@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/svn-src-all
To unsubscribe, send any mail to "svn-src-all-unsubscribe@freebsd.org"
Comment 9 Jilles Tjoelker freebsd_committer freebsd_triage 2012-03-25 21:27:15 UTC
State Changed
From-To: patched->closed

Fixed in head, stable/9 and stable/8.