Subject: Re: mkdir with trailing / (patch proposed)
To: None <rmk@rmkhome.com>
From: Greg A. Woods <woods@weird.com>
List: tech-userlevel
Date: 04/28/2002 21:09:35
[ On Sunday, April 28, 2002 at 17:59:02 (-0600), Rick Kelly wrote: ]
> Subject: Re: mkdir with trailing / (patch proposed)
>
> On the other hand, at least up as far as System 3 UNIX, there was no
> mkdir(2). That function was performed by mknod(2), which seems to just
> gamely try to create whatever is passed to it or return an error.

That's because the race condition in the set-ID-root user-level
implementation of 'mkdir' in older unix systems was considered to be
next to impossible to exploit.  In modern unix and unix-like systems the
access() and mknod() calls are wrapped into one atomic mkdir(2) system
call to avoid this race condition (and to avoid having to make 'mkdir' a
set-ID-root program too of course).

> Since
> System 3 didn't support "mkdir -p", mkdir(1) just has a simple loop that
> avoids extra "/" characters.

No, that's actually not exactly what the code in System III's mkdir(1) does.

The loop in mkdir.c:mkdir() simply finds the index of the last slash, if
there is one.  I.e. it's logically equivalent to (strrchr(d)-d+1).  It
does this only so that it can copy the dirname (if there is one), and
then append a '.' after that last slash in the copy, and then pass the
new, explicit, directory name to access(2).  If the access() test passes
then mknod() is passed the original pathname, as it was typed on the
command line.  At no time are any slashes "avoided".

-- 
								Greg A. Woods

+1 416 218-0098;  <gwoods@acm.org>;  <g.a.woods@ieee.org>;  <woods@robohack.ca>
Planix, Inc. <woods@planix.com>; VE3TCP; Secrets of the Weird <woods@weird.com>