ovsdb: Fix use-after-free error in ovsdb_destroy().
[cascardo/ovs.git] / ovsdb / ovsdb.c
1 /* Copyright (c) 2009 Nicira Networks
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #include <config.h>
17
18 #include "ovsdb.h"
19
20 #include <fcntl.h>
21
22 #include "file.h"
23 #include "json.h"
24 #include "ovsdb-error.h"
25 #include "ovsdb-parser.h"
26 #include "table.h"
27 #include "transaction.h"
28
29 #define THIS_MODULE VLM_ovsdb
30 #include "vlog.h"
31
32 struct ovsdb_schema *
33 ovsdb_schema_create(const char *name, const char *comment)
34 {
35     struct ovsdb_schema *schema;
36
37     schema = xzalloc(sizeof *schema);
38     schema->name = xstrdup(name);
39     schema->comment = comment ? xstrdup(comment) : NULL;
40     shash_init(&schema->tables);
41
42     return schema;
43 }
44
45 void
46 ovsdb_schema_destroy(struct ovsdb_schema *schema)
47 {
48     struct shash_node *node;
49
50     SHASH_FOR_EACH (node, &schema->tables) {
51         ovsdb_table_schema_destroy(node->data);
52     }
53     shash_destroy(&schema->tables);
54     free(schema->comment);
55     free(schema->name);
56     free(schema);
57 }
58
59 struct ovsdb_error *
60 ovsdb_schema_from_file(const char *file_name, struct ovsdb_schema **schemap)
61 {
62     struct ovsdb_schema *schema;
63     struct ovsdb_error *error;
64     struct json *json;
65
66     *schemap = NULL;
67     json = json_from_file(file_name);
68     if (json->type == JSON_STRING) {
69         error = ovsdb_error("failed to read schema",
70                            "\"%s\" could not be read as JSON (%s)",
71                            file_name, json_string(json));
72         json_destroy(json);
73         return error;
74     }
75
76     error = ovsdb_schema_from_json(json, &schema);
77     if (error) {
78         json_destroy(json);
79         return ovsdb_wrap_error(error,
80                                 "failed to parse \"%s\" as ovsdb schema",
81                                 file_name);
82     }
83
84     *schemap = schema;
85     return NULL;
86 }
87
88 struct ovsdb_error *
89 ovsdb_schema_from_json(struct json *json, struct ovsdb_schema **schemap)
90 {
91     struct ovsdb_schema *schema;
92     const struct json *name, *comment, *tables;
93     struct ovsdb_error *error;
94     struct shash_node *node;
95     struct ovsdb_parser parser;
96
97     *schemap = NULL;
98
99     ovsdb_parser_init(&parser, json, "database schema");
100     name = ovsdb_parser_member(&parser, "name", OP_ID);
101     comment = ovsdb_parser_member(&parser, "comment", OP_STRING | OP_OPTIONAL);
102     tables = ovsdb_parser_member(&parser, "tables", OP_OBJECT);
103     error = ovsdb_parser_finish(&parser);
104     if (error) {
105         return error;
106     }
107
108     schema = ovsdb_schema_create(json_string(name),
109                                  comment ? json_string(comment) : NULL);
110     SHASH_FOR_EACH (node, json_object(tables)) {
111         struct ovsdb_table_schema *table;
112
113         if (node->name[0] == '_') {
114             error = ovsdb_syntax_error(json, NULL, "names beginning with "
115                                        "\"_\" are reserved");
116         } else {
117             error = ovsdb_table_schema_from_json(node->data, node->name,
118                                                  &table);
119         }
120         if (error) {
121             ovsdb_schema_destroy(schema);
122             return error;
123         }
124
125         shash_add(&schema->tables, table->name, table);
126     }
127     *schemap = schema;
128     return 0;
129 }
130
131 struct json *
132 ovsdb_schema_to_json(const struct ovsdb_schema *schema)
133 {
134     struct json *json, *tables;
135     struct shash_node *node;
136
137     json = json_object_create();
138     json_object_put_string(json, "name", schema->name);
139     if (schema->comment) {
140         json_object_put_string(json, "comment", schema->comment);
141     }
142
143     tables = json_object_create();
144
145     SHASH_FOR_EACH (node, &schema->tables) {
146         struct ovsdb_table_schema *table = node->data;
147         json_object_put(tables, table->name,
148                         ovsdb_table_schema_to_json(table));
149     }
150     json_object_put(json, "tables", tables);
151
152     return json;
153 }
154 \f
155 struct ovsdb *
156 ovsdb_create(struct ovsdb_file *file, struct ovsdb_schema *schema)
157 {
158     struct shash_node *node;
159     struct ovsdb *db;
160
161     db = xmalloc(sizeof *db);
162     db->schema = schema;
163     db->file = file;
164     list_init(&db->triggers);
165     db->run_triggers = false;
166
167     shash_init(&db->tables);
168     SHASH_FOR_EACH (node, &schema->tables) {
169         struct ovsdb_table_schema *ts = node->data;
170         shash_add(&db->tables, node->name, ovsdb_table_create(ts));
171     }
172
173     return db;
174 }
175
176 struct ovsdb_error *
177 ovsdb_open(const char *file_name, bool read_only, struct ovsdb **dbp)
178 {
179     struct ovsdb_schema *schema;
180     struct ovsdb_error *error;
181     struct ovsdb_file *file;
182     struct json *json;
183     struct ovsdb *db;
184
185     error = ovsdb_file_open(file_name, read_only ? O_RDONLY : O_RDWR, &file);
186     if (error) {
187         return error;
188     }
189
190     error = ovsdb_file_read(file, &json);
191     if (error) {
192         return error;
193     } else if (!json) {
194         return ovsdb_io_error(EOF, "%s: database file contains no schema",
195                               file_name);
196     }
197
198     error = ovsdb_schema_from_json(json, &schema);
199     if (error) {
200         json_destroy(json);
201         return ovsdb_wrap_error(error,
202                                 "failed to parse \"%s\" as ovsdb schema",
203                                 file_name);
204     }
205     json_destroy(json);
206
207     db = ovsdb_create(read_only ? file : NULL, schema);
208     while ((error = ovsdb_file_read(file, &json)) == NULL && json) {
209         struct ovsdb_txn *txn;
210
211         error = ovsdb_txn_from_json(db, json, &txn);
212         json_destroy(json);
213         if (error) {
214             break;
215         }
216
217         ovsdb_txn_commit(txn);
218     }
219     if (error) {
220         char *msg = ovsdb_error_to_string(error);
221         VLOG_WARN("%s", msg);
222         free(msg);
223
224         ovsdb_error_destroy(error);
225     }
226
227     if (read_only) {
228         ovsdb_file_close(file);
229     }
230
231     *dbp = db;
232     return NULL;
233 }
234
235 void
236 ovsdb_destroy(struct ovsdb *db)
237 {
238     if (db) {
239         struct shash_node *node;
240
241         /* Delete all the tables.  This also deletes their schemas. */
242         SHASH_FOR_EACH (node, &db->tables) {
243             struct ovsdb_table *table = node->data;
244             ovsdb_table_destroy(table);
245         }
246         shash_destroy(&db->tables);
247
248         /* The schemas, but not the table that points to them, were deleted in
249          * the previous step, so we need to clear out the table.  We can't
250          * destroy the table, because ovsdb_schema_destroy() will do that. */
251         shash_clear(&db->schema->tables);
252
253         ovsdb_schema_destroy(db->schema);
254         ovsdb_file_close(db->file);
255         free(db);
256     }
257 }
258
259 struct ovsdb_table *
260 ovsdb_get_table(const struct ovsdb *db, const char *name)
261 {
262     return shash_find_data(&db->tables, name);
263 }