tech-userlevel archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: importing basename_r and dirname_r from FreeBSD?



On Sun, May 22, 2011 at 02:43:24PM +0000, Christos Zoulas wrote:
> >Unfortunately they do not have a dirname_r to steal :-)
> These are not portable or sanctioned by TOG.

You mean the FreeBSD implmentation, or dirname_r() prototype itself?

The problem I am trying to solve is that basename/dirname output is 
*not* mandated by SUSv2 to come from the input buffer or static memory.
Both implementation are standard-compliant, this is explicitely told
in the standard.

Implmentation using memory from the input buffer are thread-safe,
but the modify the input path. Linux does that. Implementation 
using static memory do not modify the input path but are not
re-entrant. NetBSD does it that later way.

Some programs assume the Linux behavior, and use dirname in 
threaded progrms. This makes them difficult to port to NetBSD. If you 
manage to add locks whenever needed, you still have trouble with
code like this, which works fine on Linux, but get a well-deserved
SIGSEGV on NetBSD:

        path = malloc(len);
        (...)
        path = dirname(path);
        (...)
        free(path);

The solution is to have a dirname_r() as a drop-in replacement, 
using a macro. For porting glusterfs to NetBSD, I had to add
GNU libc's implementation on dirname() through a patch. Of course
that change will not be accepted upstream, therefore the search
for an alternative. 

-- 
Emmanuel Dreyfus
manu%netbsd.org@localhost


Home | Main Index | Thread Index | Old Index