Stop using a large static buffer in getfdpath
text data bss dec hex filename 245075 680 9836 255591 3e667 strace_old 245143 680 5708 251531 3d68b strace Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
parent
7672946e74
commit
61ad0a401c
2
defs.h
2
defs.h
@ -605,7 +605,7 @@ extern const char *signame(int);
|
||||
extern int is_restart_error(struct tcb *);
|
||||
extern void pathtrace_select(const char *);
|
||||
extern int pathtrace_match(struct tcb *);
|
||||
extern const char *getfdpath(struct tcb *, int);
|
||||
extern int getfdpath(struct tcb *, int, char *, unsigned);
|
||||
|
||||
extern const char *xlookup(const struct xlat *, int);
|
||||
|
||||
|
25
pathtrace.c
25
pathtrace.c
@ -73,9 +73,10 @@ upathmatch(struct tcb *tcp, unsigned long upath)
|
||||
static int
|
||||
fdmatch(struct tcb *tcp, int fd)
|
||||
{
|
||||
const char *path = getfdpath(tcp, fd);
|
||||
char path[PATH_MAX + 1];
|
||||
int n = getfdpath(tcp, fd, path, sizeof(path));
|
||||
|
||||
return path && pathmatch(path);
|
||||
return n >= 0 && pathmatch(path);
|
||||
}
|
||||
|
||||
/*
|
||||
@ -100,22 +101,24 @@ storepath(const char *path)
|
||||
/*
|
||||
* Get path associated with fd.
|
||||
*/
|
||||
const char *
|
||||
getfdpath(struct tcb *tcp, int fd)
|
||||
int
|
||||
getfdpath(struct tcb *tcp, int fd, char *buf, unsigned bufsize)
|
||||
{
|
||||
static char path[PATH_MAX+1];
|
||||
char linkpath[sizeof("/proc/%u/fd/%u") + 2 * sizeof(int)*3];
|
||||
ssize_t n;
|
||||
|
||||
if (fd < 0)
|
||||
return NULL;
|
||||
return -1;
|
||||
|
||||
sprintf(linkpath, "/proc/%u/fd/%u", tcp->pid, fd);
|
||||
n = readlink(linkpath, path, (sizeof path) - 1);
|
||||
if (n <= 0)
|
||||
return NULL;
|
||||
path[n] = '\0';
|
||||
return path;
|
||||
n = readlink(linkpath, buf, bufsize - 1);
|
||||
/*
|
||||
* NB: if buf is too small, readlink doesn't fail,
|
||||
* it returns truncated result (IOW: n == bufsize - 1).
|
||||
*/
|
||||
if (n >= 0)
|
||||
buf[n] = '\0';
|
||||
return n;
|
||||
}
|
||||
|
||||
/*
|
||||
|
6
util.c
6
util.c
@ -353,10 +353,10 @@ printnum_int(struct tcb *tcp, long addr, const char *fmt)
|
||||
void
|
||||
printfd(struct tcb *tcp, int fd)
|
||||
{
|
||||
const char *p;
|
||||
char path[PATH_MAX + 1];
|
||||
|
||||
if (show_fd_path && (p = getfdpath(tcp, fd)))
|
||||
tprintf("%d<%s>", fd, p);
|
||||
if (show_fd_path && getfdpath(tcp, fd, path, sizeof(path)) >= 0)
|
||||
tprintf("%d<%s>", fd, path);
|
||||
else
|
||||
tprintf("%d", fd);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user