Skip to content

Commit

Permalink
tools: bpftool: use "/proc/self/" i.o. crafting links with getpid()
Browse files Browse the repository at this point in the history
The getpid() function is called in a couple of places in bpftool to
craft links of the shape "/proc/<pid>/...". Instead, it is possible to
use the "/proc/self/" shortcut, which makes things a bit easier, in
particular in jit_disasm.c.

Do the replacement, and remove the includes of <sys/types.h> from the
relevant files, now we do not use getpid() anymore.

Signed-off-by: Quentin Monnet <quentin.monnet@netronome.com>
Reviewed-by: Jakub Kicinski <jakub.kicinski@netronome.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
  • Loading branch information
Quentin Monnet authored and Alexei Starovoitov committed Nov 30, 2018
1 parent cc2b8ed commit 327e5da
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 13 deletions.
5 changes: 2 additions & 3 deletions tools/bpf/bpftool/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
#include <sys/mount.h>
#include <sys/resource.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/vfs.h>

#include <bpf.h>
Expand Down Expand Up @@ -276,7 +275,7 @@ int get_fd_type(int fd)
char buf[512];
ssize_t n;

snprintf(path, sizeof(path), "/proc/%d/fd/%d", getpid(), fd);
snprintf(path, sizeof(path), "/proc/self/fd/%d", fd);

n = readlink(path, buf, sizeof(buf));
if (n < 0) {
Expand Down Expand Up @@ -304,7 +303,7 @@ char *get_fdinfo(int fd, const char *key)
ssize_t n;
FILE *fdi;

snprintf(path, sizeof(path), "/proc/%d/fdinfo/%d", getpid(), fd);
snprintf(path, sizeof(path), "/proc/self/fdinfo/%d", fd);

fdi = fopen(path, "r");
if (!fdi) {
Expand Down
11 changes: 1 addition & 10 deletions tools/bpf/bpftool/jit_disasm.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#include <string.h>
#include <bfd.h>
#include <dis-asm.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <limits.h>

Expand All @@ -28,20 +27,12 @@

static void get_exec_path(char *tpath, size_t size)
{
const char *path = "/proc/self/exe";
ssize_t len;
char *path;

snprintf(tpath, size, "/proc/%d/exe", (int) getpid());
tpath[size - 1] = 0;

path = strdup(tpath);
assert(path);

len = readlink(path, tpath, size - 1);
assert(len > 0);
tpath[len] = 0;

free(path);
}

static int oper_count;
Expand Down

0 comments on commit 327e5da

Please sign in to comment.