Re: Equivalent of openat() on osx?
site_archiver@lists.apple.com Delivered-To: darwin-dev@lists.apple.com Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:in-reply-to:references:date :message-id:subject:from:to:content-type:content-transfer-encoding; bh=FAQZbkvLaGmVSoXsVM2VAX0N4p8Ax5D/40sjQcXgtlY=; b=VcHgwuwfC0589ySybx5C6RME8zt5v6X5witMsUfqXjEPhwWhoQkx2CVFyQ5Bll1ku9 U6CUPwCtXW+1L7AU2xutbMPh+BJo3GX0sNtwTJQWG7UD3JwUWU5C2pzaniibrr4gsWH+ wAmzsfD1zyPPl8dl1lwnsIQuCISLYwkpowwRM= Domainkey-signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type:content-transfer-encoding; b=XkNEcCKCRXIjA0Tppw7AlS7VGiOtjC3QpSoUZxzR28qEiieaAePdnmJdajI7bd5srX kafQfnEU9x5bT+hpVsjYdsxuMYmFW4YbPj3t3rHRE87lIibLC0Zs92VV+rnn9WZCk8XV aCqH46XDbx2PfHP9ZY5yBNxtVlbqXVMSC2TFo= On Tue, May 3, 2011 at 9:13 AM, Anatol Pomozov <anatol.pomozov@gmail.com> wrote:
Hi,
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?
After using openat() function implementation based on chdir I found that this is not the best solution. chdir sets process-wide working directory and this means that you need to protect all open/stat/lstat and other functions that might use relative paths. Otherwise you'll have race condition. This is just too much hassle. I decided to go with the following function implementation that IMHO better: int openat(int dirfd, const char *pathname, int flags, ...) { char fullpath[MAXPATHLEN]; fcntl(dirfd, F_GETPATH, fullpath); strlcat(fullpath, "/", MAXPATHLEN); strlcat(fullpath, pathname, MAXPATHLEN); // Use fullpath here } The idea is to get the path to the file descriptor that is used as a base. _______________________________________________ Do not post admin requests to the list. They will be ignored. Darwin-dev mailing list (Darwin-dev@lists.apple.com) Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/darwin-dev/site_archiver%40lists.appl... This email sent to site_archiver@lists.apple.com
participants (1)
-
Anatol Pomozov