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