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 May 22,  2:57pm, manu%netbsd.org@localhost (Emmanuel Dreyfus) wrote:
-- Subject: Re: importing basename_r and dirname_r from FreeBSD?

| 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. 

The portable fix in this case which should be accepted upstream is to:

        char *apath;

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

dirname() is explicitly not thread safe and cannot be used in threaded
programs without locks.

christos


Home | Main Index | Thread Index | Old Index