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