]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lslocks: add --json
authorKarel Zak <kzak@redhat.com>
Fri, 5 Jun 2015 08:45:15 +0000 (10:45 +0200)
committerKarel Zak <kzak@redhat.com>
Fri, 5 Jun 2015 08:45:15 +0000 (10:45 +0200)
Signed-off-by: Karel Zak <kzak@redhat.com>
misc-utils/lslocks.8
misc-utils/lslocks.c

index 9dfc0244ab60651d2eaf829ab51d469003ef1fbf..7d983c839d7ccddb5b9707d32b2cd983f1099e32 100644 (file)
@@ -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
index e2229adc3bbedc7d5cd21b6cae67ed28aa776492..01469ce5ff5951cba1e19178fc626e611ab6c241 100644 (file)
@@ -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 <pid>        process id\n"
+       fputs(_(" -J, --json             use JSON output format\n"
+               " -p, --pid <pid>        process id\n"
                " -o, --output <list>    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;