From: Karel Zak Date: Fri, 5 Jun 2015 08:45:15 +0000 (+0200) Subject: lslocks: add --json X-Git-Tag: v2.27-rc1~170 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b3a2e889dff3f7c1503aa164a1471fc8145cb558;p=thirdparty%2Futil-linux.git lslocks: add --json Signed-off-by: Karel Zak --- diff --git a/misc-utils/lslocks.8 b/misc-utils/lslocks.8 index 9dfc0244ab..7d983c839d 100644 --- a/misc-utils/lslocks.8 +++ b/misc-utils/lslocks.8 @@ -15,6 +15,9 @@ lists information about all the currently held file locks in a Linux system. .SH OPTIONS .TP +.BR \-J , " \-\-json" +Use JSON output format. +.TP .BR \-n , " \-\-noheadings" Do not print a header line. .TP diff --git a/misc-utils/lslocks.c b/misc-utils/lslocks.c index e2229adc3b..01469ce5ff 100644 --- a/misc-utils/lslocks.c +++ b/misc-utils/lslocks.c @@ -44,6 +44,7 @@ #include "c.h" #include "list.h" #include "closestream.h" +#include "optutils.h" /* column IDs */ enum { @@ -91,6 +92,7 @@ static struct libmnt_table *tab; /* /proc/self/mountinfo */ /* basic output flags */ static int no_headings; static int raw; +static int json; struct lock { struct list_head locks; @@ -464,8 +466,12 @@ static int show_locks(struct list_head *locks) return -1; } scols_table_enable_raw(table, raw); + scols_table_enable_json(table, json); scols_table_enable_noheadings(table, no_headings); + if (json) + scols_table_set_name(table, "locks"); + for (i = 0; i < ncolumns; i++) { struct colinfo *col = get_column_info(i); @@ -512,7 +518,8 @@ static void __attribute__ ((__noreturn__)) usage(FILE * out) fputs(_("List local system locks.\n"), out); fputs(USAGE_OPTIONS, out); - fputs(_(" -p, --pid process id\n" + fputs(_(" -J, --json use JSON output format\n" + " -p, --pid process id\n" " -o, --output define which output columns to use\n" " -n, --noheadings don't print headings\n" " -r, --raw use the raw output format\n" @@ -536,6 +543,7 @@ int main(int argc, char *argv[]) struct list_head locks; char *outarg = NULL; static const struct option long_opts[] = { + { "json", no_argument, NULL, 'J' }, { "pid", required_argument, NULL, 'p' }, { "help", no_argument, NULL, 'h' }, { "output", required_argument, NULL, 'o' }, @@ -546,15 +554,25 @@ int main(int argc, char *argv[]) { NULL, 0, NULL, 0 } }; + static const ul_excl_t excl[] = { /* rows and cols in ASCII order */ + { 'J','r' }, + { 0 } + }; + int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT; setlocale(LC_ALL, ""); bindtextdomain(PACKAGE, LOCALEDIR); textdomain(PACKAGE); atexit(close_stdout); while ((c = getopt_long(argc, argv, - "p:o:nruhV", long_opts, NULL)) != -1) { + "Jp:o:nruhV", long_opts, NULL)) != -1) { + + err_exclusive_options(c, long_opts, excl, excl_st); switch(c) { + case 'J': + json = 1; + break; case 'p': pid = strtos32_or_err(optarg, _("invalid PID argument")); break;