]> git.proxmox.com Git - mirror_ovs.git/blame - ovsdb/ovsdb-tool.c
Fix two memory leaks.
[mirror_ovs.git] / ovsdb / ovsdb-tool.c
CommitLineData
f85f8ebb 1/*
a15fce8e 2 * Copyright (c) 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
f85f8ebb
BP
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#include <errno.h>
19#include <fcntl.h>
20#include <getopt.h>
21#include <signal.h>
22#include <stdlib.h>
23#include <string.h>
24
4e92542c 25#include "column.h"
f85f8ebb
BP
26#include "command-line.h"
27#include "compiler.h"
e4476f74 28#include "dirs.h"
4e92542c 29#include "dynamic-string.h"
8a777cf6 30#include "fatal-signal.h"
bd06962a 31#include "file.h"
1e19e50e 32#include "lockfile.h"
41709ccc 33#include "log.h"
f85f8ebb
BP
34#include "json.h"
35#include "ovsdb.h"
4e92542c 36#include "ovsdb-data.h"
f85f8ebb 37#include "ovsdb-error.h"
1e19e50e 38#include "socket-util.h"
f85f8ebb
BP
39#include "table.h"
40#include "timeval.h"
41#include "util.h"
f85f8ebb 42#include "vlog.h"
5136ce49 43
c6782bb0
BP
44/* -m, --more: Verbosity level for "show-log" command output. */
45static int show_log_verbosity;
46
3815d6c2 47static const struct command *get_all_commands(void);
f85f8ebb
BP
48
49static void usage(void) NO_RETURN;
50static void parse_options(int argc, char *argv[]);
51
e4476f74
BP
52static const char *default_db(void);
53static const char *default_schema(void);
54
f85f8ebb
BP
55int
56main(int argc, char *argv[])
57{
58 set_program_name(argv[0]);
f85f8ebb 59 parse_options(argc, argv);
8a777cf6 60 fatal_ignore_sigpipe();
3815d6c2 61 run_command(argc - optind, argv + optind, get_all_commands());
f85f8ebb
BP
62 return 0;
63}
64
65static void
66parse_options(int argc, char *argv[])
67{
07fc4ed3 68 static const struct option long_options[] = {
e3c17733
BP
69 {"more", no_argument, NULL, 'm'},
70 {"verbose", optional_argument, NULL, 'v'},
71 {"help", no_argument, NULL, 'h'},
72 {"version", no_argument, NULL, 'V'},
73 {NULL, 0, NULL, 0},
f85f8ebb
BP
74 };
75 char *short_options = long_options_to_short_options(long_options);
76
77 for (;;) {
78 int c;
79
80 c = getopt_long(argc, argv, short_options, long_options, NULL);
81 if (c == -1) {
82 break;
83 }
84
85 switch (c) {
c6782bb0
BP
86 case 'm':
87 show_log_verbosity++;
88 break;
89
f85f8ebb
BP
90 case 'h':
91 usage();
92
93 case 'V':
55d5bb44 94 ovs_print_version(0, 0);
f85f8ebb
BP
95 exit(EXIT_SUCCESS);
96
97 case 'v':
98 vlog_set_verbosity(optarg);
99 break;
100
101 case '?':
102 exit(EXIT_FAILURE);
103
104 default:
105 abort();
106 }
107 }
108 free(short_options);
109}
110
111static void
112usage(void)
113{
114 printf("%s: Open vSwitch database management utility\n"
115 "usage: %s [OPTIONS] COMMAND [ARG...]\n"
e4476f74
BP
116 " create [DB [SCHEMA]] create DB with the given SCHEMA\n"
117 " compact [DB [DST]] compact DB in-place (or to DST)\n"
118 " convert [DB [SCHEMA [DST]]] convert DB to SCHEMA (to DST)\n"
119 " db-version [DB] report version of schema used by DB\n"
120 " db-cksum [DB] report checksum of schema used by DB\n"
121 " schema-version [SCHEMA] report SCHEMA's schema version\n"
122 " schema-cksum [SCHEMA] report SCHEMA's checksum\n"
123 " query [DB] TRNS execute read-only transaction on DB\n"
124 " transact [DB] TRNS execute read/write transaction on DB\n"
125 " [-m]... show-log [DB] print DB's log entries\n"
126 "The default DB is %s.\n"
127 "The default SCHEMA is %s.\n",
128 program_name, program_name, default_db(), default_schema());
f85f8ebb
BP
129 vlog_usage();
130 printf("\nOther options:\n"
c6782bb0 131 " -m, --more increase show-log verbosity\n"
f85f8ebb
BP
132 " -h, --help display this help message\n"
133 " -V, --version display version information\n");
134 exit(EXIT_SUCCESS);
135}
e4476f74
BP
136
137static const char *
138default_db(void)
139{
140 static char *db;
141 if (!db) {
f973f2af 142 db = xasprintf("%s/conf.db", ovs_dbdir());
e4476f74
BP
143 }
144 return db;
145}
146
147static const char *
148default_schema(void)
149{
150 static char *schema;
151 if (!schema) {
152 schema = xasprintf("%s/vswitch.ovsschema", ovs_pkgdatadir());
153 }
154 return schema;
155}
f85f8ebb
BP
156\f
157static struct json *
158parse_json(const char *s)
159{
160 struct json *json = json_from_string(s);
161 if (json->type == JSON_STRING) {
162 ovs_fatal(0, "\"%s\": %s", s, json->u.string);
163 }
164 return json;
165}
166
167static void
168print_and_free_json(struct json *json)
169{
170 char *string = json_to_string(json, JSSF_SORT);
171 json_destroy(json);
172 puts(string);
173 free(string);
174}
175
176static void
177check_ovsdb_error(struct ovsdb_error *error)
178{
179 if (error) {
180 ovs_fatal(0, "%s", ovsdb_error_to_string(error));
181 }
182}
183\f
184static void
e4476f74 185do_create(int argc, char *argv[])
f85f8ebb 186{
e4476f74
BP
187 const char *db_file_name = argc >= 2 ? argv[1] : default_db();
188 const char *schema_file_name = argc >= 3 ? argv[2] : default_schema();
f85f8ebb 189 struct ovsdb_schema *schema;
41709ccc 190 struct ovsdb_log *log;
f85f8ebb
BP
191 struct json *json;
192
193 /* Read schema from file and convert to JSON. */
194 check_ovsdb_error(ovsdb_schema_from_file(schema_file_name, &schema));
195 json = ovsdb_schema_to_json(schema);
90cc4071 196 ovsdb_schema_destroy(schema);
f85f8ebb
BP
197
198 /* Create database file. */
7446f148
BP
199 check_ovsdb_error(ovsdb_log_open(db_file_name, OVSDB_LOG_CREATE,
200 -1, &log));
41709ccc
BP
201 check_ovsdb_error(ovsdb_log_write(log, json));
202 check_ovsdb_error(ovsdb_log_commit(log));
203 ovsdb_log_close(log);
f85f8ebb
BP
204
205 json_destroy(json);
206}
207
1e19e50e 208static void
a35ae81c 209compact_or_convert(const char *src_name_, const char *dst_name_,
1e19e50e
BP
210 const struct ovsdb_schema *new_schema,
211 const char *comment)
212{
a35ae81c 213 char *src_name, *dst_name;
1e19e50e
BP
214 struct lockfile *src_lock;
215 struct lockfile *dst_lock;
a35ae81c 216 bool in_place = dst_name_ == NULL;
1e19e50e
BP
217 struct ovsdb *db;
218 int retval;
219
a35ae81c
BP
220 /* Dereference symlinks for source and destination names. In the in-place
221 * case this ensures that, if the source name is a symlink, we replace its
222 * target instead of replacing the symlink by a regular file. In the
223 * non-in-place, this has the same effect for the destination name. */
224 src_name = follow_symlinks(src_name_);
225 dst_name = (in_place
226 ? xasprintf("%s.tmp", src_name)
227 : follow_symlinks(dst_name_));
228
aebfc869 229 /* Lock the source, if we will be replacing it. */
1e19e50e 230 if (in_place) {
4770e795 231 retval = lockfile_lock(src_name, &src_lock);
aebfc869
BP
232 if (retval) {
233 ovs_fatal(retval, "%s: failed to lock lockfile", src_name);
234 }
1e19e50e
BP
235 }
236
aebfc869 237 /* Get (temporary) destination and lock it. */
4770e795 238 retval = lockfile_lock(dst_name, &dst_lock);
1e19e50e
BP
239 if (retval) {
240 ovs_fatal(retval, "%s: failed to lock lockfile", dst_name);
241 }
242
243 /* Save a copy. */
244 check_ovsdb_error(new_schema
245 ? ovsdb_file_open_as_schema(src_name, new_schema, &db)
ada496b5 246 : ovsdb_file_open(src_name, true, &db, NULL));
1e19e50e
BP
247 check_ovsdb_error(ovsdb_file_save_copy(dst_name, false, comment, db));
248 ovsdb_destroy(db);
249
250 /* Replace source. */
251 if (in_place) {
10c119c3
GS
252#ifdef _WIN32
253 unlink(src_name);
254#endif
1e19e50e
BP
255 if (rename(dst_name, src_name)) {
256 ovs_fatal(errno, "failed to rename \"%s\" to \"%s\"",
257 dst_name, src_name);
258 }
259 fsync_parent_dir(dst_name);
1e19e50e
BP
260 lockfile_unlock(src_lock);
261 }
262
263 lockfile_unlock(dst_lock);
e49190c4 264
a35ae81c
BP
265 free(src_name);
266 free(dst_name);
1e19e50e
BP
267}
268
269static void
e4476f74 270do_compact(int argc, char *argv[])
1e19e50e 271{
e4476f74
BP
272 const char *db = argc >= 2 ? argv[1] : default_db();
273 const char *target = argc >= 3 ? argv[2] : NULL;
274
8a07709c 275 compact_or_convert(db, target, NULL, "compacted by ovsdb-tool "VERSION);
1e19e50e
BP
276}
277
278static void
e4476f74 279do_convert(int argc, char *argv[])
1e19e50e 280{
e4476f74
BP
281 const char *db = argc >= 2 ? argv[1] : default_db();
282 const char *schema = argc >= 3 ? argv[2] : default_schema();
283 const char *target = argc >= 4 ? argv[3] : NULL;
1e19e50e
BP
284 struct ovsdb_schema *new_schema;
285
e4476f74
BP
286 check_ovsdb_error(ovsdb_schema_from_file(schema, &new_schema));
287 compact_or_convert(db, target, new_schema,
8a07709c 288 "converted by ovsdb-tool "VERSION);
1e19e50e
BP
289 ovsdb_schema_destroy(new_schema);
290}
291
403e3a25 292static void
e4476f74 293do_needs_conversion(int argc, char *argv[])
403e3a25 294{
e4476f74
BP
295 const char *db_file_name = argc >= 2 ? argv[1] : default_db();
296 const char *schema_file_name = argc >= 3 ? argv[2] : default_schema();
403e3a25
BP
297 struct ovsdb_schema *schema1, *schema2;
298
299 check_ovsdb_error(ovsdb_file_read_schema(db_file_name, &schema1));
300 check_ovsdb_error(ovsdb_schema_from_file(schema_file_name, &schema2));
301 puts(ovsdb_schema_equal(schema1, schema2) ? "no" : "yes");
302 ovsdb_schema_destroy(schema1);
303 ovsdb_schema_destroy(schema2);
304}
305
8159b984 306static void
e4476f74 307do_db_version(int argc, char *argv[])
8159b984 308{
e4476f74 309 const char *db_file_name = argc >= 2 ? argv[1] : default_db();
e1ebc8ce 310 struct ovsdb_schema *schema;
8159b984 311
e1ebc8ce
BP
312 check_ovsdb_error(ovsdb_file_read_schema(db_file_name, &schema));
313 puts(schema->version);
314 ovsdb_schema_destroy(schema);
8159b984
BP
315}
316
6aa09313
BP
317static void
318do_db_cksum(int argc OVS_UNUSED, char *argv[])
319{
e4476f74 320 const char *db_file_name = argc >= 2 ? argv[1] : default_db();
6aa09313
BP
321 struct ovsdb_schema *schema;
322
323 check_ovsdb_error(ovsdb_file_read_schema(db_file_name, &schema));
324 puts(schema->cksum);
325 ovsdb_schema_destroy(schema);
326}
327
8159b984 328static void
e4476f74 329do_schema_version(int argc, char *argv[])
8159b984 330{
e4476f74 331 const char *schema_file_name = argc >= 2 ? argv[1] : default_schema();
8159b984
BP
332 struct ovsdb_schema *schema;
333
334 check_ovsdb_error(ovsdb_schema_from_file(schema_file_name, &schema));
335 puts(schema->version);
336 ovsdb_schema_destroy(schema);
337}
338
6aa09313 339static void
e4476f74 340do_schema_cksum(int argc, char *argv[])
6aa09313 341{
e4476f74 342 const char *schema_file_name = argc >= 2 ? argv[1] : default_schema();
6aa09313
BP
343 struct ovsdb_schema *schema;
344
345 check_ovsdb_error(ovsdb_schema_from_file(schema_file_name, &schema));
346 puts(schema->cksum);
347 ovsdb_schema_destroy(schema);
348}
349
f85f8ebb 350static void
e4476f74 351transact(bool read_only, int argc, char *argv[])
f85f8ebb 352{
e4476f74
BP
353 const char *db_file_name = argc >= 3 ? argv[1] : default_db();
354 const char *transaction = argv[argc - 1];
f85f8ebb
BP
355 struct json *request, *result;
356 struct ovsdb *db;
357
ada496b5 358 check_ovsdb_error(ovsdb_file_open(db_file_name, read_only, &db, NULL));
f85f8ebb
BP
359
360 request = parse_json(transaction);
da897f41 361 result = ovsdb_execute(db, NULL, request, 0, NULL);
f85f8ebb
BP
362 json_destroy(request);
363
364 print_and_free_json(result);
365 ovsdb_destroy(db);
366}
367
368static void
e4476f74 369do_query(int argc, char *argv[])
f85f8ebb 370{
e4476f74 371 transact(true, argc, argv);
f85f8ebb
BP
372}
373
374static void
e4476f74 375do_transact(int argc, char *argv[])
f85f8ebb 376{
e4476f74 377 transact(false, argc, argv);
f85f8ebb
BP
378}
379
c6782bb0 380static void
4e92542c
BP
381print_db_changes(struct shash *tables, struct shash *names,
382 const struct ovsdb_schema *schema)
c6782bb0
BP
383{
384 struct shash_node *n1;
385
386 SHASH_FOR_EACH (n1, tables) {
387 const char *table = n1->name;
4e92542c 388 struct ovsdb_table_schema *table_schema;
c6782bb0
BP
389 struct json *rows = n1->data;
390 struct shash_node *n2;
391
392 if (n1->name[0] == '_' || rows->type != JSON_OBJECT) {
393 continue;
394 }
395
4e92542c 396 table_schema = shash_find_data(&schema->tables, table);
c6782bb0
BP
397 SHASH_FOR_EACH (n2, json_object(rows)) {
398 const char *row_uuid = n2->name;
399 struct json *columns = n2->data;
400 struct shash_node *n3;
401 char *old_name, *new_name;
402 bool free_new_name = false;
403
404 old_name = new_name = shash_find_data(names, row_uuid);
405 if (columns->type == JSON_OBJECT) {
406 struct json *new_name_json;
407
408 new_name_json = shash_find_data(json_object(columns), "name");
409 if (new_name_json) {
410 new_name = json_to_string(new_name_json, JSSF_SORT);
411 free_new_name = true;
412 }
413 }
414
415 printf("\ttable %s", table);
416
417 if (!old_name) {
418 if (new_name) {
a0a15a00 419 printf(" insert row %s (%.8s):\n", new_name, row_uuid);
c6782bb0
BP
420 } else {
421 printf(" insert row %.8s:\n", row_uuid);
422 }
423 } else {
a0a15a00 424 printf(" row %s (%.8s):\n", old_name, row_uuid);
c6782bb0
BP
425 }
426
427 if (columns->type == JSON_OBJECT) {
428 if (show_log_verbosity > 1) {
429 SHASH_FOR_EACH (n3, json_object(columns)) {
430 const char *column = n3->name;
4e92542c 431 const struct ovsdb_column *column_schema;
c6782bb0 432 struct json *value = n3->data;
4e92542c
BP
433 char *value_string = NULL;
434
435 column_schema =
436 (table_schema
437 ? shash_find_data(&table_schema->columns, column)
438 : NULL);
439 if (column_schema) {
4e92542c 440 const struct ovsdb_type *type;
a15fce8e 441 struct ovsdb_error *error;
4e92542c
BP
442 struct ovsdb_datum datum;
443
444 type = &column_schema->type;
445 error = ovsdb_datum_from_json(&datum, type,
446 value, NULL);
447 if (!error) {
448 struct ds s;
449
450 ds_init(&s);
451 ovsdb_datum_to_string(&datum, type, &s);
452 value_string = ds_steal_cstr(&s);
a15fce8e
BP
453 } else {
454 ovsdb_error_destroy(error);
4e92542c
BP
455 }
456 }
457 if (!value_string) {
458 value_string = json_to_string(value, JSSF_SORT);
459 }
c6782bb0
BP
460 printf("\t\t%s=%s\n", column, value_string);
461 free(value_string);
462 }
463 }
464 if (!old_name
465 || (new_name != old_name && strcmp(old_name, new_name))) {
466 if (old_name) {
467 shash_delete(names, shash_find(names, row_uuid));
468 free(old_name);
469 }
470 shash_add(names, row_uuid, (new_name
471 ? xstrdup(new_name)
472 : xmemdup0(row_uuid, 8)));
473 }
474 } else if (columns->type == JSON_NULL) {
b932d88b
BP
475 struct shash_node *node;
476
c6782bb0 477 printf("\t\tdelete row\n");
b932d88b
BP
478 node = shash_find(names, row_uuid);
479 if (node) {
480 shash_delete(names, node);
481 }
c6782bb0
BP
482 free(old_name);
483 }
484
485 if (free_new_name) {
486 free(new_name);
487 }
488 }
489 }
490}
491
722f6301 492static void
e4476f74 493do_show_log(int argc, char *argv[])
722f6301 494{
e4476f74 495 const char *db_file_name = argc >= 2 ? argv[1] : default_db();
c6782bb0 496 struct shash names;
722f6301 497 struct ovsdb_log *log;
4e92542c 498 struct ovsdb_schema *schema;
722f6301
BP
499 unsigned int i;
500
7446f148
BP
501 check_ovsdb_error(ovsdb_log_open(db_file_name, OVSDB_LOG_READ_ONLY,
502 -1, &log));
c6782bb0 503 shash_init(&names);
4e92542c 504 schema = NULL;
722f6301
BP
505 for (i = 0; ; i++) {
506 struct json *json;
507
508 check_ovsdb_error(ovsdb_log_read(log, &json));
509 if (!json) {
510 break;
511 }
512
513 printf("record %u:", i);
4e92542c
BP
514 if (i == 0) {
515 check_ovsdb_error(ovsdb_schema_from_json(json, &schema));
516 printf(" \"%s\" schema, version=\"%s\", cksum=\"%s\"\n",
517 schema->name, schema->version, schema->cksum);
518 } else if (json->type == JSON_OBJECT) {
722f6301
BP
519 struct json *date, *comment;
520
521 date = shash_find_data(json_object(json), "_date");
522 if (date && date->type == JSON_INTEGER) {
1ff1065c
PI
523 long long int t = json_integer(date);
524 char *s;
525
526 if (t < INT32_MAX) {
527 /* Older versions of ovsdb wrote timestamps in seconds. */
528 t *= 1000;
529 }
530
57093462 531 s = xastrftime_msec(" %Y-%m-%d %H:%M:%S.###", t, true);
3e78870d
BP
532 fputs(s, stdout);
533 free(s);
722f6301
BP
534 }
535
536 comment = shash_find_data(json_object(json), "_comment");
537 if (comment && comment->type == JSON_STRING) {
538 printf(" \"%s\"", json_string(comment));
539 }
c6782bb0
BP
540
541 if (i > 0 && show_log_verbosity > 0) {
542 putchar('\n');
4e92542c 543 print_db_changes(json_object(json), &names, schema);
c6782bb0 544 }
722f6301
BP
545 }
546 json_destroy(json);
547 putchar('\n');
548 }
c6782bb0 549
400eb935 550 ovsdb_log_close(log);
4e92542c 551 ovsdb_schema_destroy(schema);
c6782bb0 552 /* XXX free 'names'. */
722f6301
BP
553}
554
f85f8ebb 555static void
c69ee87c 556do_help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
f85f8ebb
BP
557{
558 usage();
559}
560
561static const struct command all_commands[] = {
e4476f74
BP
562 { "create", 0, 2, do_create },
563 { "compact", 0, 2, do_compact },
564 { "convert", 0, 3, do_convert },
565 { "needs-conversion", 0, 2, do_needs_conversion },
566 { "db-version", 0, 1, do_db_version },
567 { "db-cksum", 0, 1, do_db_cksum },
568 { "schema-version", 0, 1, do_schema_version },
569 { "schema-cksum", 0, 1, do_schema_cksum },
570 { "query", 1, 2, do_query },
571 { "transact", 1, 2, do_transact },
572 { "show-log", 0, 1, do_show_log },
f85f8ebb
BP
573 { "help", 0, INT_MAX, do_help },
574 { NULL, 0, 0, NULL },
575};
3815d6c2
LS
576
577static const struct command *get_all_commands(void)
578{
579 return all_commands;
580}