mirror of
https://git.proxmox.com/git/mirror_iproute2
synced 2025-10-05 03:59:55 +00:00

As Stephen Hemminger mentioned on the last submission the new_json_obj
function is always called with fp == stdout, so right now, there's no
need of this extra argument.
The background for the rework is the following:
The ip monitor didn't call `new_json_obj` (even for in non json context),
so the static FILE* _fp variable wasn't initialized, thus raising a
SIGSEGV in ipaddress.c. This patch should fix this issue for good, new
paths won't have to call `new_json_obj`.
How to reproduce:
$ ip -t mon label link
(gdb) bt
.#0 _IO_vfprintf_internal (s=s@entry=0x0, format=format@entry=0x45460d “%d: “, ap=ap@entry=0x7fffffff7f18) at vfprintf.c:1278
.#1 0x0000000000451310 in color_fprintf (fp=0x0, attr=<optimized out>, fmt=0x45460d “%d: “) at color.c:108
.#2 0x000000000044a856 in print_color_int (t=t@entry=PRINT_ANY, color=color@entry=4294967295, key=key@entry=0x4545fc “ifindex”,
fmt=fmt@entry=0x45460d “%d: “, value=<optimized out>) at ip_print.c:132
.#3 0x000000000040ccd2 in print_int (value=<optimized out>, fmt=0x45460d “%d: “, key=0x4545fc “ifindex”, t=PRINT_ANY) at ip_common.h:189
.#4 print_linkinfo (who=<optimized out>, n=0x7fffffffa380, arg=0x7ffff77a82a0 <_IO_2_1_stdout_>) at ipaddress.c:1107
.#5 0x0000000000422e13 in accept_msg (who=0x7fffffff8320, ctrl=0x7fffffff8310, n=0x7fffffffa380, arg=0x7ffff77a82a0 <_IO_2_1_stdout_>) at ipmonitor.c:89
.#6 0x000000000044c58f in rtnl_listen (rtnl=0x672160 <rth>, handler=handler@entry=0x422c70 <accept_msg>, jarg=0x7ffff77a82a0 <_IO_2_1_stdout_>)
at libnetlink.c:761
.#7 0x00000000004233db in do_ipmonitor (argc=<optimized out>, argv=0x7fffffffe5a0) at ipmonitor.c:310
.#8 0x0000000000408f74 in do_cmd (argv0=0x7fffffffe7f5 “mon”, argc=3, argv=0x7fffffffe588) at ip.c:116
.#9 0x0000000000408a94 in main (argc=4, argv=0x7fffffffe580) at ip.c:311
Fixes: 6377572f
("ip: ip_print: add new API to print JSON or regular format output")
Reported-by: David Ahern <dsa@cumulusnetworks.com>
Signed-off-by: Julien Fortin <julien@cumulusnetworks.com>
221 lines
4.9 KiB
C
221 lines
4.9 KiB
C
/*
|
|
* json_print.c "print regular or json output, based on json_writer".
|
|
*
|
|
* This program is free software; you can redistribute it and/or
|
|
* modify it under the terms of the GNU General Public License
|
|
* as published by the Free Software Foundation; either version
|
|
* 2 of the License, or (at your option) any later version.
|
|
*
|
|
* Authors: Julien Fortin, <julien@cumulusnetworks.com>
|
|
*/
|
|
|
|
#include <stdarg.h>
|
|
#include <stdio.h>
|
|
|
|
#include "utils.h"
|
|
#include "json_print.h"
|
|
|
|
static json_writer_t *_jw;
|
|
|
|
#define _IS_JSON_CONTEXT(type) ((type & PRINT_JSON || type & PRINT_ANY) && _jw)
|
|
#define _IS_FP_CONTEXT(type) (!_jw && (type & PRINT_FP || type & PRINT_ANY))
|
|
|
|
void new_json_obj(int json)
|
|
{
|
|
if (json) {
|
|
_jw = jsonw_new(stdout);
|
|
if (!_jw) {
|
|
perror("json object");
|
|
exit(1);
|
|
}
|
|
jsonw_pretty(_jw, true);
|
|
jsonw_start_array(_jw);
|
|
}
|
|
}
|
|
|
|
void delete_json_obj(void)
|
|
{
|
|
if (_jw) {
|
|
jsonw_end_array(_jw);
|
|
jsonw_destroy(&_jw);
|
|
}
|
|
}
|
|
|
|
bool is_json_context(void)
|
|
{
|
|
return _jw != NULL;
|
|
}
|
|
|
|
json_writer_t *get_json_writer(void)
|
|
{
|
|
return _jw;
|
|
}
|
|
|
|
void open_json_object(const char *str)
|
|
{
|
|
if (_IS_JSON_CONTEXT(PRINT_JSON)) {
|
|
if (str)
|
|
jsonw_name(_jw, str);
|
|
jsonw_start_object(_jw);
|
|
}
|
|
}
|
|
|
|
void close_json_object(void)
|
|
{
|
|
if (_IS_JSON_CONTEXT(PRINT_JSON))
|
|
jsonw_end_object(_jw);
|
|
}
|
|
|
|
/*
|
|
* Start json array or string array using
|
|
* the provided string as json key (if not null)
|
|
* or as array delimiter in non-json context.
|
|
*/
|
|
void open_json_array(enum output_type type, const char *str)
|
|
{
|
|
if (_IS_JSON_CONTEXT(type)) {
|
|
if (str)
|
|
jsonw_name(_jw, str);
|
|
jsonw_start_array(_jw);
|
|
} else if (_IS_FP_CONTEXT(type)) {
|
|
printf("%s", str);
|
|
}
|
|
}
|
|
|
|
/*
|
|
* End json array or string array
|
|
*/
|
|
void close_json_array(enum output_type type, const char *str)
|
|
{
|
|
if (_IS_JSON_CONTEXT(type)) {
|
|
jsonw_pretty(_jw, false);
|
|
jsonw_end_array(_jw);
|
|
jsonw_pretty(_jw, true);
|
|
} else if (_IS_FP_CONTEXT(type)) {
|
|
printf("%s", str);
|
|
}
|
|
}
|
|
|
|
/*
|
|
* pre-processor directive to generate similar
|
|
* functions handling different types
|
|
*/
|
|
#define _PRINT_FUNC(type_name, type) \
|
|
void print_color_##type_name(enum output_type t, \
|
|
enum color_attr color, \
|
|
const char *key, \
|
|
const char *fmt, \
|
|
type value) \
|
|
{ \
|
|
if (_IS_JSON_CONTEXT(t)) { \
|
|
if (!key) \
|
|
jsonw_##type_name(_jw, value); \
|
|
else \
|
|
jsonw_##type_name##_field(_jw, key, value); \
|
|
} else if (_IS_FP_CONTEXT(t)) { \
|
|
color_fprintf(stdout, color, fmt, value); \
|
|
} \
|
|
}
|
|
_PRINT_FUNC(int, int);
|
|
_PRINT_FUNC(hu, unsigned short);
|
|
_PRINT_FUNC(uint, uint64_t);
|
|
_PRINT_FUNC(lluint, unsigned long long int);
|
|
#undef _PRINT_FUNC
|
|
|
|
void print_color_string(enum output_type type,
|
|
enum color_attr color,
|
|
const char *key,
|
|
const char *fmt,
|
|
const char *value)
|
|
{
|
|
if (_IS_JSON_CONTEXT(type)) {
|
|
if (key && !value)
|
|
jsonw_name(_jw, key);
|
|
else if (!key && value)
|
|
jsonw_string(_jw, value);
|
|
else
|
|
jsonw_string_field(_jw, key, value);
|
|
} else if (_IS_FP_CONTEXT(type)) {
|
|
color_fprintf(stdout, color, fmt, value);
|
|
}
|
|
}
|
|
|
|
/*
|
|
* value's type is bool. When using this function in FP context you can't pass
|
|
* a value to it, you will need to use "is_json_context()" to have different
|
|
* branch for json and regular output. grep -r "print_bool" for example
|
|
*/
|
|
void print_color_bool(enum output_type type,
|
|
enum color_attr color,
|
|
const char *key,
|
|
const char *fmt,
|
|
bool value)
|
|
{
|
|
if (_IS_JSON_CONTEXT(type)) {
|
|
if (key)
|
|
jsonw_bool_field(_jw, key, value);
|
|
else
|
|
jsonw_bool(_jw, value);
|
|
} else if (_IS_FP_CONTEXT(type)) {
|
|
color_fprintf(stdout, color, fmt, value ? "true" : "false");
|
|
}
|
|
}
|
|
|
|
/*
|
|
* In JSON context uses hardcode %#x format: 42 -> 0x2a
|
|
*/
|
|
void print_color_0xhex(enum output_type type,
|
|
enum color_attr color,
|
|
const char *key,
|
|
const char *fmt,
|
|
unsigned int hex)
|
|
{
|
|
if (_IS_JSON_CONTEXT(type)) {
|
|
SPRINT_BUF(b1);
|
|
|
|
snprintf(b1, sizeof(b1), "%#x", hex);
|
|
print_string(PRINT_JSON, key, NULL, b1);
|
|
} else if (_IS_FP_CONTEXT(type)) {
|
|
color_fprintf(stdout, color, fmt, hex);
|
|
}
|
|
}
|
|
|
|
void print_color_hex(enum output_type type,
|
|
enum color_attr color,
|
|
const char *key,
|
|
const char *fmt,
|
|
unsigned int hex)
|
|
{
|
|
if (_IS_JSON_CONTEXT(type)) {
|
|
SPRINT_BUF(b1);
|
|
|
|
snprintf(b1, sizeof(b1), "%x", hex);
|
|
if (key)
|
|
jsonw_string_field(_jw, key, b1);
|
|
else
|
|
jsonw_string(_jw, b1);
|
|
} else if (_IS_FP_CONTEXT(type)) {
|
|
color_fprintf(stdout, color, fmt, hex);
|
|
}
|
|
}
|
|
|
|
/*
|
|
* In JSON context we don't use the argument "value" we simply call jsonw_null
|
|
* whereas FP context can use "value" to output anything
|
|
*/
|
|
void print_color_null(enum output_type type,
|
|
enum color_attr color,
|
|
const char *key,
|
|
const char *fmt,
|
|
const char *value)
|
|
{
|
|
if (_IS_JSON_CONTEXT(type)) {
|
|
if (key)
|
|
jsonw_null_field(_jw, key);
|
|
else
|
|
jsonw_null(_jw);
|
|
} else if (_IS_FP_CONTEXT(type)) {
|
|
color_fprintf(stdout, color, fmt, value);
|
|
}
|
|
}
|