ovsdb-client: Remove --wide option.
[cascardo/ovs.git] / ovsdb / ovsdb-client.c
1 /*
2  * Copyright (c) 2009, 2010 Nicira Networks.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <config.h>
18
19 #include <assert.h>
20 #include <errno.h>
21 #include <getopt.h>
22 #include <limits.h>
23 #include <signal.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27
28 #include "command-line.h"
29 #include "column.h"
30 #include "compiler.h"
31 #include "daemon.h"
32 #include "dynamic-string.h"
33 #include "json.h"
34 #include "jsonrpc.h"
35 #include "ovsdb.h"
36 #include "ovsdb-error.h"
37 #include "stream.h"
38 #include "stream-ssl.h"
39 #include "table.h"
40 #include "timeval.h"
41 #include "util.h"
42
43 #include "vlog.h"
44 #define THIS_MODULE VLM_ovsdb_client
45
46 /* --format: Output formatting. */
47 static enum {
48     FMT_TABLE,                  /* Textual table. */
49     FMT_HTML,                   /* HTML table. */
50     FMT_CSV                     /* Comma-separated lines. */
51 } output_format;
52
53 /* --no-headings: Whether table output should include headings. */
54 static int output_headings = true;
55
56 /* --pretty: Flags to pass to json_to_string(). */
57 static int json_flags = JSSF_SORT;
58
59 static const struct command all_commands[];
60
61 static void usage(void) NO_RETURN;
62 static void parse_options(int argc, char *argv[]);
63
64 int
65 main(int argc, char *argv[])
66 {
67     proctitle_init(argc, argv);
68     set_program_name(argv[0]);
69     time_init();
70     vlog_init();
71     parse_options(argc, argv);
72     signal(SIGPIPE, SIG_IGN);
73     run_command(argc - optind, argv + optind, all_commands);
74     return 0;
75 }
76
77 static void
78 parse_options(int argc, char *argv[])
79 {
80     enum {
81         OPT_BOOTSTRAP_CA_CERT = UCHAR_MAX + 1
82     };
83     static struct option long_options[] = {
84         {"format", required_argument, 0, 'f'},
85             {"no-headings", no_argument, &output_headings, 0},
86         {"pretty", no_argument, &json_flags, JSSF_PRETTY | JSSF_SORT},
87         {"verbose", optional_argument, 0, 'v'},
88         {"help", no_argument, 0, 'h'},
89         {"version", no_argument, 0, 'V'},
90         DAEMON_LONG_OPTIONS,
91 #ifdef HAVE_OPENSSL
92         {"bootstrap-ca-cert", required_argument, 0, OPT_BOOTSTRAP_CA_CERT},
93         STREAM_SSL_LONG_OPTIONS
94 #endif
95         {0, 0, 0, 0},
96     };
97     char *short_options = long_options_to_short_options(long_options);
98
99     for (;;) {
100         int c;
101
102         c = getopt_long(argc, argv, short_options, long_options, NULL);
103         if (c == -1) {
104             break;
105         }
106
107         switch (c) {
108         case 'f':
109             if (!strcmp(optarg, "table")) {
110                 output_format = FMT_TABLE;
111             } else if (!strcmp(optarg, "html")) {
112                 output_format = FMT_HTML;
113             } else if (!strcmp(optarg, "csv")) {
114                 output_format = FMT_CSV;
115             } else {
116                 ovs_fatal(0, "unknown output format \"%s\"", optarg);
117             }
118             break;
119
120         case 'h':
121             usage();
122
123         case 'V':
124             OVS_PRINT_VERSION(0, 0);
125             exit(EXIT_SUCCESS);
126
127         case 'v':
128             vlog_set_verbosity(optarg);
129             break;
130
131         DAEMON_OPTION_HANDLERS
132
133 #ifdef HAVE_OPENSSL
134         STREAM_SSL_OPTION_HANDLERS
135
136         case OPT_BOOTSTRAP_CA_CERT:
137             stream_ssl_set_ca_cert_file(optarg, true);
138             break;
139 #endif
140
141         case '?':
142             exit(EXIT_FAILURE);
143
144         case 0:
145             /* getopt_long() already set the value for us. */
146             break;
147
148         default:
149             abort();
150         }
151     }
152     free(short_options);
153 }
154
155 static void
156 usage(void)
157 {
158     printf("%s: Open vSwitch database JSON-RPC client\n"
159            "usage: %s [OPTIONS] COMMAND [ARG...]\n"
160            "\nValid commands are:\n"
161            "\n  list-dbs SERVER\n"
162            "    list databases available on SERVER\n"
163            "\n  get-schema SERVER DATABASE\n"
164            "    retrieve schema for DATABASE from SERVER\n"
165            "\n  list-tables SERVER DATABSE\n"
166            "    list tables for DATABSAE on SERVER\n"
167            "\n  list-columns SERVER DATABASE [TABLE]\n"
168            "    list columns in TABLE (or all tables) in DATABASE on SERVER\n"
169            "\n  transact SERVER TRANSACTION\n"
170            "    run TRANSACTION (a JSON array of operations) on SERVER\n"
171            "    and print the results as JSON on stdout\n"
172            "\n  monitor SERVER DATABASE TABLE [COLUMN,...] [SELECT,...]\n"
173            "    monitor contents of (COLUMNs in) TABLE in DATABASE on SERVER\n"
174            "    Valid SELECTs are: initial, insert, delete, modify\n",
175            program_name, program_name);
176     stream_usage("SERVER", true, true, true);
177     printf("\nOutput formatting options:\n"
178            "  -f, --format=FORMAT         set output formatting to FORMAT\n"
179            "                              (\"table\", \"html\", or \"csv\"\n"
180            "  --no-headings               omit table heading row\n"
181            "  --pretty                    pretty-print JSON in output");
182     daemon_usage();
183     vlog_usage();
184     printf("\nOther options:\n"
185            "  -h, --help                  display this help message\n"
186            "  -V, --version               display version information\n");
187     exit(EXIT_SUCCESS);
188 }
189 \f
190 static struct json *
191 parse_json(const char *s)
192 {
193     struct json *json = json_from_string(s);
194     if (json->type == JSON_STRING) {
195         ovs_fatal(0, "\"%s\": %s", s, json->u.string);
196     }
197     return json;
198 }
199
200 static struct jsonrpc *
201 open_jsonrpc(const char *server)
202 {
203     struct stream *stream;
204     int error;
205
206     error = stream_open_block(server, &stream);
207     if (error == EAFNOSUPPORT) {
208         struct pstream *pstream;
209
210         error = pstream_open(server, &pstream);
211         if (error) {
212             ovs_fatal(error, "failed to connect or listen to \"%s\"", server);
213         }
214
215         VLOG_INFO("%s: waiting for connection...", server);
216         error = pstream_accept_block(pstream, &stream);
217         if (error) {
218             ovs_fatal(error, "failed to accept connection on \"%s\"", server);
219         }
220
221         pstream_close(pstream);
222     } else if (error) {
223         ovs_fatal(error, "failed to connect to \"%s\"", server);
224     }
225
226     return jsonrpc_open(stream);
227 }
228
229 static void
230 print_json(struct json *json)
231 {
232     char *string = json_to_string(json, json_flags);
233     fputs(string, stdout);
234     free(string);
235 }
236
237 static void
238 print_and_free_json(struct json *json)
239 {
240     print_json(json);
241     json_destroy(json);
242 }
243
244 static void
245 check_ovsdb_error(struct ovsdb_error *error)
246 {
247     if (error) {
248         ovs_fatal(0, "%s", ovsdb_error_to_string(error));
249     }
250 }
251
252 static struct ovsdb_schema *
253 fetch_schema_from_rpc(struct jsonrpc *rpc, const char *database)
254 {
255     struct jsonrpc_msg *request, *reply;
256     struct ovsdb_schema *schema;
257     int error;
258
259     request = jsonrpc_create_request("get_schema",
260                                      json_array_create_1(
261                                          json_string_create(database)),
262                                      NULL);
263     error = jsonrpc_transact_block(rpc, request, &reply);
264     if (error) {
265         ovs_fatal(error, "transaction failed");
266     }
267     check_ovsdb_error(ovsdb_schema_from_json(reply->result, &schema));
268     jsonrpc_msg_destroy(reply);
269
270     return schema;
271 }
272
273 static struct ovsdb_schema *
274 fetch_schema(const char *server, const char *database)
275 {
276     struct ovsdb_schema *schema;
277     struct jsonrpc *rpc;
278
279     rpc = open_jsonrpc(server);
280     schema = fetch_schema_from_rpc(rpc, database);
281     jsonrpc_close(rpc);
282
283     return schema;
284 }
285 \f
286 struct column {
287     char *heading;
288     int width;
289 };
290
291 struct table {
292     char **cells;
293     struct column *columns;
294     size_t n_columns, allocated_columns;
295     size_t n_rows, allocated_rows;
296     size_t current_column;
297 };
298
299 static void
300 table_init(struct table *table)
301 {
302     memset(table, 0, sizeof *table);
303 }
304
305 static void
306 table_destroy(struct table *table)
307 {
308     size_t i;
309
310     for (i = 0; i < table->n_columns; i++) {
311         free(table->columns[i].heading);
312     }
313     free(table->columns);
314
315     for (i = 0; i < table->n_columns * table->n_rows; i++) {
316         free(table->cells[i]);
317     }
318     free(table->cells);
319 }
320
321 static void
322 table_add_column(struct table *table, const char *heading, ...)
323     PRINTF_FORMAT(2, 3);
324
325 static void
326 table_add_column(struct table *table, const char *heading, ...)
327 {
328     struct column *column;
329     va_list args;
330
331     assert(!table->n_rows);
332     if (table->n_columns >= table->allocated_columns) {
333         table->columns = x2nrealloc(table->columns, &table->allocated_columns,
334                                     sizeof *table->columns);
335     }
336     column = &table->columns[table->n_columns++];
337
338     va_start(args, heading);
339     column->heading = xvasprintf(heading, args);
340     column->width = strlen(column->heading);
341     va_end(args);
342 }
343
344 static char **
345 table_cell__(const struct table *table, size_t row, size_t column)
346 {
347     return &table->cells[column + row * table->n_columns];
348 }
349
350 static void
351 table_add_row(struct table *table)
352 {
353     size_t x, y;
354
355     if (table->n_rows >= table->allocated_rows) {
356         table->cells = x2nrealloc(table->cells, &table->allocated_rows,
357                                   table->n_columns * sizeof *table->cells);
358     }
359
360     y = table->n_rows++;
361     table->current_column = 0;
362     for (x = 0; x < table->n_columns; x++) {
363         *table_cell__(table, y, x) = NULL;
364     }
365 }
366
367 static void
368 table_add_cell_nocopy(struct table *table, char *s)
369 {
370     size_t x, y;
371     int length;
372
373     assert(table->n_rows > 0);
374     assert(table->current_column < table->n_columns);
375
376     x = table->current_column++;
377     y = table->n_rows - 1;
378     *table_cell__(table, y, x) = s;
379
380     length = strlen(s);
381     if (length > table->columns[x].width) {
382         table->columns[x].width = length;
383     }
384 }
385
386 static void
387 table_add_cell(struct table *table, const char *format, ...)
388 {
389     va_list args;
390
391     va_start(args, format);
392     table_add_cell_nocopy(table, xvasprintf(format, args));
393     va_end(args);
394 }
395
396 static void
397 table_print_table_line__(struct ds *line)
398 {
399     puts(ds_cstr(line));
400     ds_clear(line);
401 }
402
403 static void
404 table_print_table__(const struct table *table)
405 {
406     struct ds line = DS_EMPTY_INITIALIZER;
407     size_t x, y;
408
409     if (output_headings) {
410         for (x = 0; x < table->n_columns; x++) {
411             const struct column *column = &table->columns[x];
412             if (x) {
413                 ds_put_char(&line, ' ');
414             }
415             ds_put_format(&line, "%-*s", column->width, column->heading);
416         }
417         table_print_table_line__(&line);
418
419         for (x = 0; x < table->n_columns; x++) {
420             const struct column *column = &table->columns[x];
421             int i;
422
423             if (x) {
424                 ds_put_char(&line, ' ');
425             }
426             for (i = 0; i < column->width; i++) {
427                 ds_put_char(&line, '-');
428             }
429         }
430         table_print_table_line__(&line);
431     }
432
433     for (y = 0; y < table->n_rows; y++) {
434         for (x = 0; x < table->n_columns; x++) {
435             const char *cell = *table_cell__(table, y, x);
436             if (x) {
437                 ds_put_char(&line, ' ');
438             }
439             ds_put_format(&line, "%-*s", table->columns[x].width, cell);
440         }
441         table_print_table_line__(&line);
442     }
443
444     ds_destroy(&line);
445 }
446
447 static void
448 table_print_html_cell__(const char *element, const char *content)
449 {
450     const char *p;
451
452     printf("    <%s>", element);
453     for (p = content; *p != '\0'; p++) {
454         switch (*p) {
455         case '&':
456             fputs("&amp;", stdout);
457             break;
458         case '<':
459             fputs("&lt;", stdout);
460             break;
461         case '>':
462             fputs("&gt;", stdout);
463             break;
464         default:
465             putchar(*p);
466             break;
467         }
468     }
469     printf("</%s>\n", element);
470 }
471
472 static void
473 table_print_html__(const struct table *table)
474 {
475     size_t x, y;
476
477     fputs("<table>\n", stdout);
478
479     if (output_headings) {
480         fputs("  <tr>\n", stdout);
481         for (x = 0; x < table->n_columns; x++) {
482             const struct column *column = &table->columns[x];
483             table_print_html_cell__("th", column->heading);
484         }
485         fputs("  </tr>\n", stdout);
486     }
487
488     for (y = 0; y < table->n_rows; y++) {
489         fputs("  <tr>\n", stdout);
490         for (x = 0; x < table->n_columns; x++) {
491             table_print_html_cell__("td", *table_cell__(table, y, x));
492         }
493         fputs("  </tr>\n", stdout);
494     }
495
496     fputs("</table>\n", stdout);
497 }
498
499 static void
500 table_print_csv_cell__(const char *content)
501 {
502     const char *p;
503
504     if (!strpbrk(content, "\n\",")) {
505         fputs(content, stdout);
506     } else {
507         putchar('"');
508         for (p = content; *p != '\0'; p++) {
509             switch (*p) {
510             case '"':
511                 fputs("\"\"", stdout);
512                 break;
513             default:
514                 putchar(*p);
515                 break;
516             }
517         }
518         putchar('"');
519     }
520 }
521
522 static void
523 table_print_csv__(const struct table *table)
524 {
525     size_t x, y;
526
527     if (output_headings) {
528         for (x = 0; x < table->n_columns; x++) {
529             const struct column *column = &table->columns[x];
530             if (x) {
531                 putchar(',');
532             }
533             table_print_csv_cell__(column->heading);
534         }
535         putchar('\n');
536     }
537
538     for (y = 0; y < table->n_rows; y++) {
539         for (x = 0; x < table->n_columns; x++) {
540             if (x) {
541                 putchar(',');
542             }
543             table_print_csv_cell__(*table_cell__(table, y, x));
544         }
545         putchar('\n');
546     }
547 }
548
549 static void
550 table_print(const struct table *table)
551 {
552     switch (output_format) {
553     case FMT_TABLE:
554         table_print_table__(table);
555         break;
556
557     case FMT_HTML:
558         table_print_html__(table);
559         break;
560
561     case FMT_CSV:
562         table_print_csv__(table);
563         break;
564     }
565 }
566 \f
567 static void
568 do_list_dbs(int argc OVS_UNUSED, char *argv[])
569 {
570     struct jsonrpc_msg *request, *reply;
571     struct jsonrpc *rpc;
572     int error;
573     size_t i;
574
575     rpc = open_jsonrpc(argv[1]);
576     request = jsonrpc_create_request("list_dbs", json_array_create_empty(),
577                                      NULL);
578     error = jsonrpc_transact_block(rpc, request, &reply);
579     if (error) {
580         ovs_fatal(error, "transaction failed");
581     }
582
583     if (reply->result->type != JSON_ARRAY) {
584         ovs_fatal(0, "list_dbs response is not array");
585     }
586
587     for (i = 0; i < reply->result->u.array.n; i++) {
588         const struct json *name = reply->result->u.array.elems[i];
589
590         if (name->type != JSON_STRING) {
591             ovs_fatal(0, "list_dbs response %zu is not string", i);
592         }
593         puts(name->u.string);
594     }
595     jsonrpc_msg_destroy(reply);
596 }
597
598 static void
599 do_get_schema(int argc OVS_UNUSED, char *argv[])
600 {
601     struct ovsdb_schema *schema = fetch_schema(argv[1], argv[2]);
602     print_and_free_json(ovsdb_schema_to_json(schema));
603     ovsdb_schema_destroy(schema);
604 }
605
606 static void
607 do_list_tables(int argc OVS_UNUSED, char *argv[])
608 {
609     struct ovsdb_schema *schema;
610     struct shash_node *node;
611     struct table t;
612
613     schema = fetch_schema(argv[1], argv[2]);
614     table_init(&t);
615     table_add_column(&t, "Table");
616     table_add_column(&t, "Comment");
617     SHASH_FOR_EACH (node, &schema->tables) {
618         struct ovsdb_table_schema *ts = node->data;
619
620         table_add_row(&t);
621         table_add_cell(&t, ts->name);
622         if (ts->comment) {
623             table_add_cell(&t, ts->comment);
624         }
625     }
626     ovsdb_schema_destroy(schema);
627     table_print(&t);
628 }
629
630 static void
631 do_list_columns(int argc OVS_UNUSED, char *argv[])
632 {
633     const char *table_name = argv[3];
634     struct ovsdb_schema *schema;
635     struct shash_node *table_node;
636     struct table t;
637
638     schema = fetch_schema(argv[1], argv[2]);
639     table_init(&t);
640     if (!table_name) {
641         table_add_column(&t, "Table");
642     }
643     table_add_column(&t, "Column");
644     table_add_column(&t, "Type");
645     table_add_column(&t, "Comment");
646     SHASH_FOR_EACH (table_node, &schema->tables) {
647         struct ovsdb_table_schema *ts = table_node->data;
648
649         if (!table_name || !strcmp(table_name, ts->name)) {
650             struct shash_node *column_node;
651
652             SHASH_FOR_EACH (column_node, &ts->columns) {
653                 const struct ovsdb_column *column = column_node->data;
654                 struct json *type = ovsdb_type_to_json(&column->type);
655
656                 table_add_row(&t);
657                 if (!table_name) {
658                     table_add_cell(&t, ts->name);
659                 }
660                 table_add_cell(&t, column->name);
661                 table_add_cell_nocopy(&t, json_to_string(type, JSSF_SORT));
662                 if (column->comment) {
663                     table_add_cell(&t, column->comment);
664                 }
665
666                 json_destroy(type);
667             }
668         }
669     }
670     ovsdb_schema_destroy(schema);
671     table_print(&t);
672 }
673
674 static void
675 do_transact(int argc OVS_UNUSED, char *argv[])
676 {
677     struct jsonrpc_msg *request, *reply;
678     struct json *transaction;
679     struct jsonrpc *rpc;
680     int error;
681
682     transaction = parse_json(argv[2]);
683
684     rpc = open_jsonrpc(argv[1]);
685     request = jsonrpc_create_request("transact", transaction, NULL);
686     error = jsonrpc_transact_block(rpc, request, &reply);
687     if (error) {
688         ovs_fatal(error, "transaction failed");
689     }
690     if (reply->error) {
691         ovs_fatal(error, "transaction returned error: %s",
692                   json_to_string(reply->error, json_flags));
693     }
694     print_json(reply->result);
695     putchar('\n');
696     jsonrpc_msg_destroy(reply);
697     jsonrpc_close(rpc);
698 }
699
700 static void
701 monitor_print_row(struct json *row, const char *type, const char *uuid,
702                   const struct ovsdb_column_set *columns, struct table *t)
703 {
704     size_t i;
705
706     if (!row) {
707         ovs_error(0, "missing %s row", type);
708         return;
709     } else if (row->type != JSON_OBJECT) {
710         ovs_error(0, "<row> is not object");
711         return;
712     }
713
714     table_add_row(t);
715     table_add_cell(t, uuid);
716     table_add_cell(t, type);
717     for (i = 0; i < columns->n_columns; i++) {
718         const struct ovsdb_column *column = columns->columns[i];
719         struct json *value = shash_find_data(json_object(row), column->name);
720         if (value) {
721             table_add_cell_nocopy(t, json_to_string(value, JSSF_SORT));
722         } else {
723             table_add_cell(t, "");
724         }
725     }
726 }
727
728 static void
729 monitor_print(struct json *table_updates,
730               const struct ovsdb_table_schema *table,
731               const struct ovsdb_column_set *columns, bool initial)
732 {
733     struct json *table_update;
734     struct shash_node *node;
735     struct table t;
736     size_t i;
737
738     table_init(&t);
739
740     if (table_updates->type != JSON_OBJECT) {
741         ovs_error(0, "<table-updates> is not object");
742         return;
743     }
744     table_update = shash_find_data(json_object(table_updates), table->name);
745     if (!table_update) {
746         return;
747     }
748     if (table_update->type != JSON_OBJECT) {
749         ovs_error(0, "<table-update> is not object");
750         return;
751     }
752
753     table_add_column(&t, "row");
754     table_add_column(&t, "action");
755     for (i = 0; i < columns->n_columns; i++) {
756         table_add_column(&t, "%s", columns->columns[i]->name);
757     }
758     SHASH_FOR_EACH (node, json_object(table_update)) {
759         struct json *row_update = node->data;
760         struct json *old, *new;
761
762         if (row_update->type != JSON_OBJECT) {
763             ovs_error(0, "<row-update> is not object");
764             continue;
765         }
766         old = shash_find_data(json_object(row_update), "old");
767         new = shash_find_data(json_object(row_update), "new");
768         if (initial) {
769             monitor_print_row(new, "initial", node->name, columns, &t);
770         } else if (!old) {
771             monitor_print_row(new, "insert", node->name, columns, &t);
772         } else if (!new) {
773             monitor_print_row(old, "delete", node->name, columns, &t);
774         } else {
775             monitor_print_row(old, "old", node->name, columns, &t);
776             monitor_print_row(new, "new", "", columns, &t);
777         }
778     }
779     table_print(&t);
780     table_destroy(&t);
781 }
782
783 static void
784 do_monitor(int argc, char *argv[])
785 {
786     const char *server = argv[1];
787     const char *database = argv[2];
788     const char *table_name = argv[3];
789     struct ovsdb_column_set columns = OVSDB_COLUMN_SET_INITIALIZER;
790     struct ovsdb_table_schema *table;
791     struct ovsdb_schema *schema;
792     struct jsonrpc_msg *request;
793     struct jsonrpc *rpc;
794     struct json *select, *monitor, *monitor_request, *monitor_requests,
795         *request_id;
796
797     rpc = open_jsonrpc(server);
798
799     schema = fetch_schema_from_rpc(rpc, database);
800     table = shash_find_data(&schema->tables, table_name);
801     if (!table) {
802         ovs_fatal(0, "%s: %s does not have a table named \"%s\"",
803                   server, database, table_name);
804     }
805
806     if (argc >= 5 && *argv[4] != '\0') {
807         char *save_ptr = NULL;
808         char *token;
809
810         for (token = strtok_r(argv[4], ",", &save_ptr); token != NULL;
811              token = strtok_r(NULL, ",", &save_ptr)) {
812             const struct ovsdb_column *column;
813             column = ovsdb_table_schema_get_column(table, token);
814             if (!column) {
815                 ovs_fatal(0, "%s: table \"%s\" in %s does not have a "
816                           "column named \"%s\"",
817                           server, table_name, database, token);
818             }
819             ovsdb_column_set_add(&columns, column);
820         }
821     } else {
822         struct shash_node *node;
823
824         SHASH_FOR_EACH (node, &table->columns) {
825             const struct ovsdb_column *column = node->data;
826             if (column->index != OVSDB_COL_UUID) {
827                 ovsdb_column_set_add(&columns, column);
828             }
829         }
830     }
831
832     if (argc >= 6 && *argv[5] != '\0') {
833         char *save_ptr = NULL;
834         char *token;
835
836         select = json_object_create();
837         for (token = strtok_r(argv[5], ",", &save_ptr); token != NULL;
838              token = strtok_r(NULL, ",", &save_ptr)) {
839             json_object_put(select, token, json_boolean_create(true));
840         }
841     } else {
842         select = NULL;
843     }
844
845     monitor_request = json_object_create();
846     json_object_put(monitor_request,
847                     "columns", ovsdb_column_set_to_json(&columns));
848     if (select) {
849         json_object_put(monitor_request, "select", select);
850     }
851
852     monitor_requests = json_object_create();
853     json_object_put(monitor_requests, table_name, monitor_request);
854
855     monitor = json_array_create_3(json_string_create(database),
856                                   json_null_create(), monitor_requests);
857     request = jsonrpc_create_request("monitor", monitor, NULL);
858     request_id = json_clone(request->id);
859     jsonrpc_send(rpc, request);
860     for (;;) {
861         struct jsonrpc_msg *msg;
862         int error;
863
864         error = jsonrpc_recv_block(rpc, &msg);
865         if (error) {
866             ovsdb_schema_destroy(schema);
867             ovs_fatal(error, "%s: receive failed", server);
868         }
869
870         if (msg->type == JSONRPC_REQUEST && !strcmp(msg->method, "echo")) {
871             jsonrpc_send(rpc, jsonrpc_create_reply(json_clone(msg->params),
872                                                    msg->id));
873         } else if (msg->type == JSONRPC_REPLY
874                    && json_equal(msg->id, request_id)) {
875             monitor_print(msg->result, table, &columns, true);
876             fflush(stdout);
877             if (get_detach()) {
878                 /* daemonize() closes the standard file descriptors.  We output
879                  * to stdout, so we need to save and restore STDOUT_FILENO. */
880                 int fd = dup(STDOUT_FILENO);
881                 daemonize();
882                 dup2(fd, STDOUT_FILENO);
883                 close(fd);
884             }
885         } else if (msg->type == JSONRPC_NOTIFY
886                    && !strcmp(msg->method, "update")) {
887             struct json *params = msg->params;
888             if (params->type == JSON_ARRAY
889                 && params->u.array.n == 2
890                 && params->u.array.elems[0]->type == JSON_NULL) {
891                 monitor_print(params->u.array.elems[1],
892                               table, &columns, false);
893                 fflush(stdout);
894             }
895         }
896         jsonrpc_msg_destroy(msg);
897     }
898 }
899
900 static void
901 do_help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
902 {
903     usage();
904 }
905
906 static const struct command all_commands[] = {
907     { "list-dbs", 1, 1, do_list_dbs },
908     { "get-schema", 2, 2, do_get_schema },
909     { "list-tables", 2, 2, do_list_tables },
910     { "list-columns", 2, 3, do_list_columns },
911     { "transact", 2, 2, do_transact },
912     { "monitor", 3, 5, do_monitor },
913     { "help", 0, INT_MAX, do_help },
914     { NULL, 0, 0, NULL },
915 };