[PATCH v3 4/7] Introduce get_sockaddr_by_inode_cached and get_sockaddr_by_inode

Dmitry V. Levin ldv at altlinux.org
Fri Jun 17 00:15:49 UTC 2016


On Wed, Jun 15, 2016 at 12:43:02PM +0000, Fabien Siron wrote:
> Because netlink parsing needs a way to get the netlink protocol and the
> socketutils.c file can only print the protocol, this commit adds an interface
> to get some socket information without printing it.
> 
> * defs.h
> (get_sockaddr_by_inode_cached, get_sockaddr_by_inode): Add.
> * socketutils.c (cache_print_inode_details): Remove function.
> (cache_inode_details, get_sockaddr_by_inode_cached, get_sockaddr_by_inode,
> netlink_get): New functions.
> (inet_print): Call print_sockaddr_by_inode_details.
> (unix_print): Likewise.
> (netlink_print): Likewise.
> (unix_parse_response): Call cache_inode_details.
> (netlink_parse_resonse): Likewise.
> (inet_parse_response): Likewise.

Please split this into 2 commits:
+ the first that changes cache_and_print_inode_details to
cache_inode_details and adds print_sockaddr_by_inode_cached calls,
+ the second that introduces get_sockaddr_by_inode_cached,
get_sockaddr_by_inode, and netlink_get.

> ---
>  defs.h        |  3 +++
>  socketutils.c | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++--------
>  2 files changed, 59 insertions(+), 8 deletions(-)
> 
> diff --git a/defs.h b/defs.h
> index ee35dab..e9a9392 100644
> --- a/defs.h
> +++ b/defs.h
> @@ -523,6 +523,9 @@ extern int umoven_or_printaddr(struct tcb *, long, unsigned int, void *);
k>  	umoven_or_printaddr((pid), (addr), sizeof(*(objp)), (void *) (objp))
>  extern int umovestr(struct tcb *, long, unsigned int, char *);
>  extern int upeek(int pid, long, long *);
> +extern char *get_sockaddr_by_inode_cached(const unsigned long);
> +extern char *get_sockaddr_by_inode(const unsigned long inode,
> +				   const enum sock_proto);

Let's move them closer to print_sockaddr_by_inode
and print_sockaddr_by_inode_cached.

> +static char *
> +netlink_get(const int fd, const unsigned long inode)
> +{
> +	if (netlink_send_query(fd, inode)
> +	    && receive_responses(fd, inode, "NETLINK",
> +				 netlink_parse_response)) {
> +		cache_entry *e = &cache[inode & CACHE_MASK];
> +		if (e && inode == e->inode)
> +			return e->details;

This duplicates the code from get_sockaddr_by_inode_cached.

> +char *
> +get_sockaddr_by_inode(const unsigned long inode, const enum sock_proto proto)
> +{
> +	if ((unsigned int) proto >= ARRAY_SIZE(protocols) ||
> +	    (proto != SOCK_PROTO_UNKNOWN && !protocols[proto].print))
> +		return false;
> +
> +	const int fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_SOCK_DIAG);
> +	if (fd < 0)
> +		return NULL;
> +	char *details = NULL;
> +
> +	if (proto != SOCK_PROTO_UNKNOWN) {
> +		if (proto == SOCK_PROTO_NETLINK)
> +			details = netlink_get(fd, inode);
> +	}
> +
> +	close(fd);
> +
> +	return details;

This is too complicated for the simple task it solves.


-- 
ldv
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 819 bytes
Desc: not available
URL: <http://lists.strace.io/pipermail/strace-devel/attachments/20160617/c6a739e8/attachment.bin>


More information about the Strace-devel mailing list