Re: Equivalent of openat() on osx?
Re: Equivalent of openat() on osx?
- Subject: Re: Equivalent of openat() on osx?
- From: Anatol Pomozov <email@hidden>
- Date: Tue, 03 May 2011 11:17:53 -0700
Hi
On Tue, May 3, 2011 at 10:45 AM, Tilghman Lesher <email@hidden> wrote:
> On Tuesday 03 May 2011 11:13:09 Anatol Pomozov wrote:
>> I am porting a Linux program to macosx (target is 10.6 version) and
>> the program uses openat/unlinkat/... functions. Although these
>> functions are not in POSIX, they are widely used on *nixes and the
>> part proposal the next POSIX.
>>
>> My question what is the best equivalent for the opentat() functions
>> that can be used in multithreaded application. Currently I use
>> something like this:
>>
>>
>> int openat(int dirfd, const char *pathname, int flags, ...)
>> {
>> int fd;
>>
>> if(fchdir(dirfd) < 0) {
>> perror("fchdir");
>> return -1;
>> }
>> fd = open(pathname, flags, mode);
>> // chdir to the original CWD
>> return fd;
>> }
>>
>> But this is not a perfect solution - current working directory is
>> shared among all threads, it means that openat() may affect other
>> threads. Is there an equivalent of this implementation without
>> changing the current directory?
>
> If you aren't using chroot(2) (or moving directories) in your application,
> you can do this. Wrap calls to opendir(2) and closedir(2), mapping
> descriptors to directory paths (and disposing of them), then in your *at()
> functions, remap the relative pathname back to an absolute pathname.
> You're right that this isn't a perfect compromise, but I think it should
> work, with the caveats above.
>
> Alternatively, you can use a global mutex in your *at functions, ensuring
> that only one thread is accessing the environment path, but that
> potentially creates a major bottleneck.
Yeah, currently I use a global mutex in my *at() functions. It works
fine for me but theoretically we need to guard with the mutex all
chdir() calls and file operations that use relative path.
Here is the code that I mentioned above
https://github.com/anatol/tup/blob/fix_openat_macosx/src/compat/openat.c
BTW openat() mentioned in the Open Group specifications
http://pubs.opengroup.org/onlinepubs/9699919799/ is there any chance
that darwin team implement openat() functions family in the next
macosx version?
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Darwin-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden