From mboxrd@z Thu Jan 1 00:00:00 1970 From: Matthias Tafelmeier Subject: [PATCH 05/10] ss: framed skeleton for json output in ss Date: Mon, 10 Aug 2015 01:13:21 +0200 Message-ID: <1439162006-11421-6-git-send-email-matthias.tafelmeier@gmx.net> References: <1439162006-11421-1-git-send-email-matthias.tafelmeier@gmx.net> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: QUOTED-PRINTABLE Cc: hagen@jauu.net, shemminger@osdl.org, fw@strlen.de, edumazet@google.com, daniel@iogearbox.net To: netdev@vger.kernel.org Return-path: Received: from mout.gmx.net ([212.227.15.15]:56008 "EHLO mout.gmx.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751226AbbHIXNg (ORCPT ); Sun, 9 Aug 2015 19:13:36 -0400 In-Reply-To: <1439162006-11421-1-git-send-email-matthias.tafelmeier@gmx.net> Sender: netdev-owner@vger.kernel.org List-ID: This patch just adds the --json flag to ss. Also it ensures proper stats components bracketization =E2=80=93 that goes for ex. TCP, UDP, N= ETLINK etc. Moreover, this patch prevents human readable headers to be printed. The first element flag ensures, that every first output json container element is treated specially, while all the others are treated equally. That is, only the first one does not print a coma ahead of itself. The rest does. This mechanism ensures the correct coma setting as demaned b= y the spec. Illustration in the following: PSEUDOCODE: { >>>> no comma {first } >>>> , {sec} >>>> , {third} . . . } Signed-off-by: Matthias Tafelmeier Suggested-by: Hagen Paul Pfeifer --- misc/ss.c | 200 ++++++++++++++++++++++++++++++++++++++++++++++++------= -------- 1 file changed, 157 insertions(+), 43 deletions(-) diff --git a/misc/ss.c b/misc/ss.c index 1b3ef90..f563710 100644 --- a/misc/ss.c +++ b/misc/ss.c @@ -34,6 +34,9 @@ #include "libnetlink.h" #include "namespace.h" #include "SNAPSHOT.h" +#include "ss_out_fmt.h" +#include "ss_json_fmt.h" +#include "ss_types.h" =20 #include #include @@ -101,6 +104,7 @@ int show_sock_ctx =3D 0; /* If show_users & show_proc_ctx only do user_ent_hash_build() once */ int user_ent_hash_build_init =3D 0; int follow_events =3D 0; +int json_output =3D 0; =20 int netid_width; int state_width; @@ -714,7 +718,6 @@ static int is_ephemeral(int port) return (port >=3D ip_local_port_min && port<=3D ip_local_port_max); } =20 - static const char *__resolve_service(int port) { struct scache *c; @@ -3064,6 +3067,9 @@ static int print_summary(void) =20 printf("\n"); =20 + if (json_output && has_successor) + printf(",\n"); + return 0; } =20 @@ -3090,6 +3096,7 @@ static void _usage(FILE *dest) " -z, --contexts display process and socket SELinux security co= ntexts\n" " -N, --net switch to the specified network namespace name= \n" "\n" +" -j, --json format output in JSON\n" " -4, --ipv4 display only IP version 4 sockets\n" " -6, --ipv6 display only IP version 6 sockets\n" " -0, --packet display PACKET sockets\n" @@ -3189,6 +3196,7 @@ static const struct option long_opts[] =3D { { "help", 0, 0, 'h' }, { "context", 0, 0, 'Z' }, { "contexts", 0, 0, 'z' }, + { "json", 0, 0, 'j' }, { "net", 1, 0, 'N' }, { 0 } =20 @@ -3204,7 +3212,7 @@ int main(int argc, char *argv[]) int ch; int state_filter =3D 0; =20 - while ((ch =3D getopt_long(argc, argv, "dhaletuwxnro460spbEf:miA:D:F:= vVzZN:", + while ((ch =3D getopt_long(argc, argv, "dhaletuwxnro460spbEf:miA:D:F:= vVzZN:j", long_opts, NULL)) !=3D EOF) { switch(ch) { case 'n': @@ -3383,6 +3391,10 @@ int main(int argc, char *argv[]) if (netns_switch(optarg)) exit(1); break; + case 'j': + fmt_type =3D FMT_JSON; + json_output =3D 1; + break; case 'h': case '?': help(); @@ -3464,11 +3476,34 @@ int main(int argc, char *argv[]) exit(-1); } } + printf("\"TCP\": [\n"); inet_show_netlink(¤t_filter, dump_fp, IPPROTO_TCP); + res_json_fmt_branch(current_filter.dbs & (1<