[PATCH v3 1/7] Make getfdproto returns integer instead of string
Dmitry V. Levin
ldv at altlinux.org
Wed Jun 15 17:36:35 UTC 2016
On Wed, Jun 15, 2016 at 12:42:59PM +0000, Fabien Siron wrote:
> This commit aims to avoid future string comparisons with getfdproto function
> in changing its return type. It also adapts the functions that use it.
I'm not quite sure what does the first sentence mean.
> * defs.h (print_sockaddr_by_inode, get_proto_by_name): Add.
Only get_proto_by_name is a new prototype, print_sockaddr_by_inode was
there before the change.
> (sock_proto): New enum.
> * socketutils.c (print_sockaddr_by_inode): Move proto to int.
I'm not quite sure what does "Move proto to int" mean.
> (get_proto_by_name): New function.
> (protocols): New static table.
> * util.c (getfdproto): Return sock_proto instead of string.
> (printfd): Change type of proto.
> ---
> defs.h | 13 +++++++++++-
> socketutils.c | 63 ++++++++++++++++++++++++++++++++++++-----------------------
> util.c | 20 ++++++++++---------
> 3 files changed, 62 insertions(+), 34 deletions(-)
>
> diff --git a/defs.h b/defs.h
> index 724d4f3..ab06921 100644
> --- a/defs.h
> +++ b/defs.h
> @@ -431,6 +431,17 @@ extern const struct xlat whence_codes[];
> # define NEED_UID16_PARSERS 0
> #endif
>
> +enum sock_proto {
> + SOCK_PROTO_UNKNOWN,
> + SOCK_PROTO_UNIX,
> + SOCK_PROTO_TCP,
> + SOCK_PROTO_UDP,
> + SOCK_PROTO_TCPv6,
> + SOCK_PROTO_UDPv6,
> + SOCK_PROTO_NETLINK
> +};
> +extern enum sock_proto get_proto_by_name(const char *const name);
(const char *) is enough
> typedef enum {
> CFLAG_NONE = 0,
> CFLAG_ONLY_STATS,
> @@ -630,7 +641,7 @@ extern void printpathn(struct tcb *, long, unsigned int);
> #define TIMESPEC_TEXT_BUFSIZE \
> (sizeof(intmax_t)*3 * 2 + sizeof("{tv_sec=%jd, tv_nsec=%jd}"))
> extern void printfd(struct tcb *, int);
> -extern bool print_sockaddr_by_inode(const unsigned long, const char *);
> +extern bool print_sockaddr_by_inode(const unsigned long, const enum sock_proto);
> extern bool print_sockaddr_by_inode_cached(const unsigned long);
> extern void print_dirfd(struct tcb *, int);
> extern void printsock(struct tcb *, long, int);
> diff --git a/socketutils.c b/socketutils.c
> index 5d8d3ed..4a39522 100644
> --- a/socketutils.c
> +++ b/socketutils.c
> @@ -438,44 +438,59 @@ netlink_print(const int fd, const unsigned long inode)
> netlink_parse_response);
> }
>
> +static const struct {
> + const char *const name;
> + bool (*const print)(int, unsigned long);
> +} protocols[] = {
> + [SOCK_PROTO_UNIX] = { "UNIX", unix_print },
> + [SOCK_PROTO_TCP] = { "TCP", tcp_v4_print },
> + [SOCK_PROTO_UDP] = { "UDP", udp_v4_print },
> + [SOCK_PROTO_TCPv6] = { "TCPv6", tcp_v6_print },
> + [SOCK_PROTO_UDPv6] = { "UDPv6", udp_v6_print },
> + [SOCK_PROTO_NETLINK] = { "NETLINK", netlink_print }
> +};
> +
> /* Given an inode number of a socket, print out the details
> * of the ip address and port. */
> +
> +enum sock_proto
> +get_proto_by_name(const char *const name)
> +{
> + unsigned int i;
> + for (i = (unsigned int) SOCK_PROTO_UNKNOWN + 1;
> + i < ARRAY_SIZE(protocols); ++i) {
> + if (protocols[i].name && !strcmp(name, protocols[i].name))
> + return (enum sock_proto) i;
> + }
> + return SOCK_PROTO_UNKNOWN;
> +}
Please don't insert a new function between print_sockaddr_by_inode and its
description.
--
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/20160615/b80aa552/attachment.bin>
More information about the Strace-devel
mailing list