lkml.org 
[lkml]   [2001]   [Apr]   [17]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
Date
From
SubjectRe: kernel space getcwd()? (using current() to find out cwd)
"Brian J. Watson" wrote:
> path = __d_path(pwd, pwdmnt, NULL, NULL, path, PAGE_SIZE);


Oops! That's no good. Here's the new and improved version:

char *
kgetcwd(char **bufp)
{
char *path, *buf = (char *) __get_free_page(GFP_USER);
struct vfsmnt *pwdmnt;
struct dentry *pwd;

*bufp = NULL;
if (!buf)
return ERR_PTR(-ENOMEM);

read_lock(&current->fs->lock);
pwdmnt = mntget(current->fs->pwdmnt);
pwd = dget(current->fs->pwd);
read_unlock(&current->fs->lock);

spin_lock(&dcache_lock);
path = __d_path(pwd, pwdmnt, NULL, NULL, buf, PAGE_SIZE);
spin_unlock(&dcache_lock);

mntput(pwdmnt);
dput(pwd);

*bufp = buf;
return path;
}


The returned pointer is for the beginning of the path name. The pointer filled
into bufp is for the beginning of the allocated space. To deallocate, call
free_page() on the value in bufp.

The reason for the distinction is that __d_path builds the pathname from the end
of the buffer, working its way back toward the beginning. Rarely will the string
begin at the same address as the allocated buffer.


--
Brian Watson
Compaq Computer
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

\
 
 \ /
  Last update: 2005-03-22 13:26    [W:0.032 / U:0.436 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site