Skip to content

Commit

Permalink
procfs: implement /proc/self/exe
Browse files Browse the repository at this point in the history
This patch adds /proc/self/exe which symlinks
to the application being executed. It also adds symlink
support to procfs.

Signed-off-by: Waldemar Kozaczuk <[email protected]>
  • Loading branch information
wkozaczuk committed Nov 7, 2019
1 parent 2d5f2d5 commit 3357ae0
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 1 deletion.
17 changes: 16 additions & 1 deletion fs/procfs/procfs_vnops.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include <unistd.h>

#include <osv/app.hh>
#include <osv/mount.h>
#include <osv/prex.h>
#include <osv/sched.hh>
Expand Down Expand Up @@ -144,6 +145,17 @@ static std::string procfs_hostname()
return std::string(hostname);
}

static std::string procfs_exe()
{
auto app = sched::thread::current_app();

if (app && app->lib()) {
return app->lib()->pathname();
} else {
return "";
}
}

static int
procfs_mount(mount* mp, const char *dev, int flags, const void* data)
{
Expand All @@ -154,6 +166,9 @@ procfs_mount(mount* mp, const char *dev, int flags, const void* data)
self->add("stat", inode_count++, procfs_stats);
self->add("status", inode_count++, procfs_status);

auto exe = make_shared<pseudo_symlink_node>(inode_count++, procfs_exe);
self->add("exe", exe);

auto kernel = make_shared<pseudo_dir_node>(inode_count++);
kernel->add("hostname", inode_count++, procfs_hostname);

Expand Down Expand Up @@ -216,7 +231,7 @@ vnops procfs_vnops = {
(vnop_link_t) vop_eperm, // vop_link
(vnop_cache_t) nullptr, // vop_arc
(vnop_fallocate_t) vop_nullop, // vop_fallocate
(vnop_readlink_t) vop_nullop, // vop_readlink
pseudofs::readlink, // vop_readlink
(vnop_symlink_t) vop_nullop, // vop_symlink
};

Expand Down
20 changes: 20 additions & 0 deletions fs/pseudofs/pseudofs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ static pseudo_file_node *to_file_node(vnode *vp) {
return dynamic_cast<pseudo_file_node *>(np);
}

static pseudo_symlink_node *to_symlink_node(vnode *vp) {
auto *np = to_node(vp);

return dynamic_cast<pseudo_symlink_node *>(np);
}

int open(file *fp) {
auto *np = to_file_node(fp->f_dentry->d_vnode);
if (np) {
Expand Down Expand Up @@ -65,6 +71,18 @@ int read(vnode *vp, file *fp, uio *uio, int ioflags) {
return uiomove(const_cast<char *>(data->data()) + uio->uio_offset, len, uio);
}

int readlink(vnode *vp, uio *uio) {
if (vp->v_type != VLNK)
return EINVAL;

auto *np = to_symlink_node(vp);
auto *target_path = np->target_path();
if (uio->uio_offset >= (off_t) target_path->size())
return 0;

return uiomove(const_cast<char *>(target_path->data()) + uio->uio_offset, target_path->size(), uio);
}

int write(vnode *vp, uio *uio, int ioflags) {
return EINVAL;
}
Expand Down Expand Up @@ -135,6 +153,8 @@ int readdir(vnode *vp, file *fp, dirent *dir) {
auto np = dir_entry->second;
if (np->type() == VDIR) {
dir->d_type = DT_DIR;
} else if (np->type() == VLNK) {
dir->d_type = DT_LNK;
} else {
dir->d_type = DT_REG;
}
Expand Down
23 changes: 23 additions & 0 deletions fs/pseudofs/pseudofs.hh
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,27 @@ private:
function<string()> _gen;
};

class pseudo_symlink_node : public pseudo_node {
public:
pseudo_symlink_node(uint64_t ino, function<string()> gen)
: pseudo_node(ino, VLNK), _target_path_gen(gen) {}

virtual off_t size() const override {
return 0;
}

virtual mode_t mode() const override {
return S_IRUSR | S_IRGRP | S_IROTH;
}

string *target_path() const {
return new string(_target_path_gen());
}

private:
function<string()> _target_path_gen;
};

class pseudo_dir_node : public pseudo_node {
public:
pseudo_dir_node(uint64_t ino) : pseudo_node(ino, VDIR) {}
Expand Down Expand Up @@ -112,6 +133,8 @@ int close(vnode *vp, file *fp);

int read(vnode *vp, file *fp, uio *uio, int ioflags);

int readlink(vnode *vp, uio *uio);

int write(vnode *vp, uio *uio, int ioflags);

int ioctl(vnode *vp, file *fp, u_long cmd, void *arg);
Expand Down

0 comments on commit 3357ae0

Please sign in to comment.