[PATCH 06/10] ss: replaced old output mechanisms with fmt handlers interfaces
Now, since the fmt (json, hr) handlers are in place, all can be output via these newly deviced code parts. Signed-off-by: Matthias Tafelmeier matthias.tafelme...@gmx.net Suggested-by: Hagen Paul Pfeifer ha...@jauu.net --- misc/ss.c | 330 +- 1 file changed, 152 insertions(+), 178 deletions(-) diff --git a/misc/ss.c b/misc/ss.c index 8fb6e7d..993a87b 100644 --- a/misc/ss.c +++ b/misc/ss.c @@ -105,6 +105,7 @@ int show_sock_ctx = 0; int user_ent_hash_build_init = 0; int follow_events = 0; int json_output = 0; +int json_first_elem = 1; int netid_width; int state_width; @@ -113,6 +114,8 @@ int addr_width; int serv_width; int screen_width; +enum out_fmt_type fmt_type = FMT_HR; + static const char *TCP_PROTO = tcp; static const char *UDP_PROTO = udp; static const char *RAW_PROTO = raw; @@ -346,6 +349,16 @@ static FILE *ephemeral_ports_open(void) #define USER_ENT_HASH_SIZE 256 struct user_ent *user_ent_hash[USER_ENT_HASH_SIZE]; +static void json_print_opening(void) +{ + if (json_output json_first_elem) { + json_first_elem = 0; + printf({\n); + } else if (json_output) { + printf(,\n{\n); + } +} + static int user_ent_hashfn(unsigned int ino) { int val = (ino 24) ^ (ino 16) ^ (ino 8) ^ ino; @@ -791,7 +804,8 @@ do_numeric: return buf; } -static void inet_addr_print(const inet_prefix *a, int port, unsigned int ifindex) +static void inet_addr_print(const inet_prefix *a, int port, + unsigned int ifindex, char *peer_kind) { char buf[1024]; const char *ap = buf; @@ -819,8 +833,8 @@ static void inet_addr_print(const inet_prefix *a, int port, unsigned int ifindex est_len -= strlen(ifname) + 1; /* +1 for percent char */ } - sock_addr_print_width(est_len, ap, :, serv_width, resolve_service(port), - ifname); + sock_addr_fmt(ap, est_len, :, serv_width, resolve_service(port), + ifname, peer_kind); } static int inet2_addr_match(const inet_prefix *a, const inet_prefix *p, @@ -1352,21 +1366,27 @@ static void inet_stats_print(struct sockstat *s, int protocol) { char *buf = NULL; - sock_state_print(s, proto_name(protocol)); + sock_state_fmt(s, sstate_name, proto_name(protocol), + netid_width, state_width); - inet_addr_print(s-local, s-lport, s-iface); - inet_addr_print(s-remote, s-rport, 0); + if (json_output) + printf(\t,\peers\: {\n); + + inet_addr_print(s-local, s-lport, s-iface, local); + inet_addr_print(s-remote, s-rport, 0, remote); + if (json_output) + printf(}); if (show_proc_ctx || show_sock_ctx) { if (find_entry(s-ino, buf, - (show_proc_ctx show_sock_ctx) ? - PROC_SOCK_CTX : PROC_CTX) 0) { - printf( users:(%s), buf); + (show_proc_ctx show_sock_ctx) ? + PROC_SOCK_CTX : PROC_CTX) 0) { + sock_users_fmt(buf); free(buf); } } else if (show_users) { if (find_entry(s-ino, buf, USERS) 0) { - printf( users:(%s), buf); + sock_users_fmt(buf); free(buf); } } @@ -1470,16 +1490,16 @@ static int tcp_show_line(char *line, const struct filter *f, int family) inet_stats_print(s.ss, IPPROTO_TCP); if (show_options) - tcp_timer_print(s); + tcp_timer_fmt(s); if (show_details) { - sock_details_print(s.ss); + sock_details_fmt(s.ss, GENERIC_DETAIL, 0, 0); if (opt[0]) - printf( opt:\%s\, opt); + opt_fmt(opt); } if (show_tcpinfo) - tcp_stats_print(s); + tcp_stats_fmt(s); printf(\n); return 0; @@ -1523,31 +1543,14 @@ static void print_skmeminfo(struct rtattr *tb[], int attrtype) const struct inet_diag_meminfo *minfo = RTA_DATA(tb[INET_DIAG_MEMINFO]); - printf( mem:(r%u,w%u,f%u,t%u), - minfo-idiag_rmem, - minfo-idiag_wmem, - minfo-idiag_fmem, - minfo-idiag_tmem); + mem_fmt(minfo); } return; } skmeminfo = RTA_DATA(tb[attrtype]); - printf( skmem:(r%u,rb%u,t%u,tb%u,f%u,w%u,o%u, - skmeminfo[SK_MEMINFO_RMEM_ALLOC], - skmeminfo[SK_MEMINFO_RCVBUF], -
[PATCH 06/10] ss: replaced old output mechanisms with fmt handlers interfaces
Now, since the fmt (json, hr) handlers are in place, all can be output via these newly deviced code parts. Signed-off-by: Matthias Tafelmeier matthias.tafelme...@gmx.net Suggested-by: Hagen Paul Pfeifer ha...@jauu.net --- misc/ss.c | 332 +- 1 file changed, 153 insertions(+), 179 deletions(-) diff --git a/misc/ss.c b/misc/ss.c index f563710..a760cf9 100644 --- a/misc/ss.c +++ b/misc/ss.c @@ -105,6 +105,7 @@ int show_sock_ctx = 0; int user_ent_hash_build_init = 0; int follow_events = 0; int json_output = 0; +int json_first_elem = 1; int netid_width; int state_width; @@ -113,6 +114,8 @@ int addr_width; int serv_width; int screen_width; +enum out_fmt_type fmt_type = FMT_HR; + static const char *TCP_PROTO = tcp; static const char *UDP_PROTO = udp; static const char *RAW_PROTO = raw; @@ -346,6 +349,16 @@ static FILE *ephemeral_ports_open(void) #define USER_ENT_HASH_SIZE 256 struct user_ent *user_ent_hash[USER_ENT_HASH_SIZE]; +static void json_print_opening(void) +{ + if (json_output json_first_elem) { + json_first_elem = 0; + printf({\n); + } else if (json_output) { + printf(,\n{\n); + } +} + static int user_ent_hashfn(unsigned int ino) { int val = (ino 24) ^ (ino 16) ^ (ino 8) ^ ino; @@ -791,7 +804,8 @@ do_numeric: return buf; } -static void inet_addr_print(const inet_prefix *a, int port, unsigned int ifindex) +static void inet_addr_print(const inet_prefix * a, int port, + unsigned int ifindex, char *peer_kind) { char buf[1024]; const char *ap = buf; @@ -819,8 +833,8 @@ static void inet_addr_print(const inet_prefix *a, int port, unsigned int ifindex est_len -= strlen(ifname) + 1; /* +1 for percent char */ } - sock_addr_print_width(est_len, ap, :, serv_width, resolve_service(port), - ifname); + sock_addr_fmt(ap, est_len, :, serv_width, resolve_service(port), + ifname, peer_kind); } static int inet2_addr_match(const inet_prefix *a, const inet_prefix *p, @@ -1352,21 +1366,28 @@ static void inet_stats_print(struct sockstat *s, int protocol) { char *buf = NULL; - sock_state_print(s, proto_name(protocol)); + sock_state_fmt(s, sstate_name, proto_name(protocol), + netid_width, state_width); - inet_addr_print(s-local, s-lport, s-iface); - inet_addr_print(s-remote, s-rport, 0); + if (json_output) { + printf(\t,\peers\: {\n); + } + inet_addr_print(s-local, s-lport, s-iface, local); + inet_addr_print(s-remote, s-rport, 0, remote); + if (json_output) { + printf(}); + } if (show_proc_ctx || show_sock_ctx) { if (find_entry(s-ino, buf, - (show_proc_ctx show_sock_ctx) ? - PROC_SOCK_CTX : PROC_CTX) 0) { - printf( users:(%s), buf); + (show_proc_ctx show_sock_ctx) ? + PROC_SOCK_CTX : PROC_CTX) 0) { + sock_users_fmt(buf); free(buf); } } else if (show_users) { if (find_entry(s-ino, buf, USERS) 0) { - printf( users:(%s), buf); + sock_users_fmt(buf); free(buf); } } @@ -1470,16 +1491,16 @@ static int tcp_show_line(char *line, const struct filter *f, int family) inet_stats_print(s.ss, IPPROTO_TCP); if (show_options) - tcp_timer_print(s); + tcp_timer_fmt(s); if (show_details) { - sock_details_print(s.ss); + sock_details_fmt(s.ss, GENERIC_DETAIL, 0, 0); if (opt[0]) - printf( opt:\%s\, opt); + opt_fmt(opt); } if (show_tcpinfo) - tcp_stats_print(s); + tcp_stats_fmt(s); printf(\n); return 0; @@ -1523,31 +1544,14 @@ static void print_skmeminfo(struct rtattr *tb[], int attrtype) const struct inet_diag_meminfo *minfo = RTA_DATA(tb[INET_DIAG_MEMINFO]); - printf( mem:(r%u,w%u,f%u,t%u), - minfo-idiag_rmem, - minfo-idiag_wmem, - minfo-idiag_fmem, - minfo-idiag_tmem); + mem_fmt(minfo); } return; } skmeminfo = RTA_DATA(tb[attrtype]); - printf( skmem:(r%u,rb%u,t%u,tb%u,f%u,w%u,o%u, - skmeminfo[SK_MEMINFO_RMEM_ALLOC], - skmeminfo[SK_MEMINFO_RCVBUF], -