diff options
author | Dmitry V. Levin <ldv@strace.io> | 2022-03-12 08:00:00 +0000 |
---|---|---|
committer | Dmitry V. Levin <ldv@strace.io> | 2022-03-12 08:00:00 +0000 |
commit | 0cefd3aa4aff52965b074d7808ee6220b7c8d02a (patch) | |
tree | 02395b6a6007f5fdf004b920d92cf178f2ac6bff | |
parent | 95fbe15f9089959be732bc5e60809dad03785cee (diff) | |
download | strace-0cefd3aa4aff52965b074d7808ee6220b7c8d02a.tar.gz |
util: introduce get_inode_of_socket_path
* src/util.c (get_inode_of_socket_path): New function.
(getfdinode, printsocket): Use it.
-rw-r--r-- | src/util.c | 43 |
1 files changed, 24 insertions, 19 deletions
diff --git a/src/util.c b/src/util.c index 527d67dda..e46681e54 100644 --- a/src/util.c +++ b/src/util.c @@ -589,20 +589,31 @@ getfdproto(struct tcb *tcp, int fd) #endif } +static unsigned long +get_inode_of_socket_path(const char *path) +{ + const char *str = STR_STRIP_PREFIX(path, "socket:["); + char *end; + size_t len; + unsigned long r; + + if ((str != path) + && (len = strlen(str)) + && (str[len - 1] == ']') + && (r = strtoul(str, &end, 10)) + && (end == &str[len - 1])) + return r; + + return 0; +} + unsigned long getfdinode(struct tcb *tcp, int fd) { char path[PATH_MAX + 1]; - if (getfdpath(tcp, fd, path, sizeof(path)) >= 0) { - const char *str = STR_STRIP_PREFIX(path, "socket:["); - - if (str != path) { - const size_t str_len = strlen(str); - if (str_len && str[str_len - 1] == ']') - return strtoul(str, NULL, 10); - } - } + if (getfdpath(tcp, fd, path, sizeof(path)) >= 0) + return get_inode_of_socket_path(path); return 0; } @@ -618,17 +629,11 @@ print_string_in_angle_brackets(const char *str) static bool printsocket(struct tcb *tcp, int fd, const char *path) { - const char *str = STR_STRIP_PREFIX(path, "socket:["); - size_t len; - unsigned long inode; - const char *details = NULL; - - if ((str != path) - && (len = strlen(str)) - && (str[len - 1] == ']') - && (inode = strtoul(str, NULL, 10))) - details = get_sockaddr_by_inode(tcp, fd, inode); + unsigned long inode = get_inode_of_socket_path(path); + if (!inode) + return false; + const char *details = get_sockaddr_by_inode(tcp, fd, inode); if (details) { print_string_in_angle_brackets(details); return true; |