All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Brian J. Watson" <Brian.J.Watson@compaq.com>
To: mike@bangstate.com
Cc: Linux Kernel <linux-kernel@vger.kernel.org>
Subject: Re: kernel space getcwd()? (using current() to find out cwd)
Date: Tue, 17 Apr 2001 15:43:19 -0700	[thread overview]
Message-ID: <3ADCC707.449F7B05@compaq.com> (raw)

> This is probably a stupid question, and probably directed to the wrong
> list. Apologies in advance, but I'm stumped
> 
> I've been working on a kernel module to report on "changed files". It
> works just fine -- I wrap the orignal system calls with my
> [...]


At least in the 2.4 kernels, there's already a __d_path() routine (fs/dcache.c)
that builds the pathname using the mechanism you discussed.

Here's one way you could use it:

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

        if (!path)
                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, path, PAGE_SIZE);
        spin_unlock(&dcache_lock);

        mntput(pwdmnt);
        dput(pwd);

        return path;
}


If you only want the pathname back to the process root, use d_path() instead
(and don't grab the dcache_lock).

When you're done with path, free it with free_page() and not kfree().

BTW, I'm not subscribed to the kernel mailing list (I just read it on the web),
so please copy me on any response.


--
Brian Watson
Compaq Computer

             reply	other threads:[~2001-04-17 22:44 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-04-17 22:43 Brian J. Watson [this message]
2001-04-17 23:28 ` kernel space getcwd()? (using current() to find out cwd) Brian J. Watson
  -- strict thread matches above, loose matches on Subject: below --
2001-04-16 22:42 Michael L. Welles
2001-04-17 12:30 ` Christoph Hellwig

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=3ADCC707.449F7B05@compaq.com \
    --to=brian.j.watson@compaq.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mike@bangstate.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.