1 /* Copyright (c) 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
30 #include "ovsdb-error.h"
32 #include "socket-util.h"
33 #include "transaction.h"
45 struct lockfile *lockfile;
47 struct ovsdb_error *read_error;
49 enum ovsdb_log_mode mode;
52 /* Attempts to open 'name' with the specified 'open_mode'. On success, stores
53 * the new log into '*filep' and returns NULL; otherwise returns NULL and
54 * stores NULL into '*filep'.
56 * Whether the file will be locked using lockfile_lock() depends on 'locking':
57 * use true to lock it, false not to lock it, or -1 to lock it only if
58 * 'open_mode' is a mode that allows writing.
61 ovsdb_log_open(const char *name, enum ovsdb_log_open_mode open_mode,
62 int locking, struct ovsdb_log **filep)
64 struct lockfile *lockfile;
65 struct ovsdb_error *error;
66 struct ovsdb_log *file;
74 ovs_assert(locking == -1 || locking == false || locking == true);
76 locking = open_mode != OVSDB_LOG_READ_ONLY;
79 int retval = lockfile_lock(name, &lockfile);
81 error = ovsdb_io_error(retval, "%s: failed to lock lockfile",
89 if (open_mode == OVSDB_LOG_READ_ONLY) {
91 } else if (open_mode == OVSDB_LOG_READ_WRITE) {
93 } else if (open_mode == OVSDB_LOG_CREATE) {
95 if (stat(name, &s) == -1 && errno == ENOENT
96 && lstat(name, &s) == 0 && S_ISLNK(s.st_mode)) {
97 /* 'name' is a dangling symlink. We want to create the file that
98 * the symlink points to, but POSIX says that open() with O_EXCL
99 * must fail with EEXIST if the named file is a symlink. So, we
100 * have to leave off O_EXCL and accept the race. */
101 flags = O_RDWR | O_CREAT;
103 flags = O_RDWR | O_CREAT | O_EXCL;
106 flags = O_RDWR | O_CREAT | O_EXCL;
112 flags = flags | O_BINARY;
114 fd = open(name, flags, 0666);
116 const char *op = open_mode == OVSDB_LOG_CREATE ? "create" : "open";
117 error = ovsdb_io_error(errno, "%s: %s failed", op, name);
121 if (!fstat(fd, &s) && s.st_size == 0) {
122 /* It's (probably) a new file so fsync() its parent directory to ensure
123 * that its directory entry is committed to disk. */
124 fsync_parent_dir(name);
127 stream = fdopen(fd, open_mode == OVSDB_LOG_READ_ONLY ? "rb" : "w+b");
129 error = ovsdb_io_error(errno, "%s: fdopen failed", name);
133 file = xmalloc(sizeof *file);
134 file->name = xstrdup(name);
135 file->lockfile = lockfile;
136 file->stream = stream;
137 file->prev_offset = 0;
139 file->read_error = NULL;
140 file->write_error = false;
141 file->mode = OVSDB_LOG_READ;
148 lockfile_unlock(lockfile);
154 ovsdb_log_close(struct ovsdb_log *file)
158 fclose(file->stream);
159 lockfile_unlock(file->lockfile);
160 ovsdb_error_destroy(file->read_error);
165 static const char magic[] = "OVSDB JSON ";
168 parse_header(char *header, unsigned long int *length,
169 uint8_t sha1[SHA1_DIGEST_SIZE])
173 /* 'header' must consist of a magic string... */
174 if (strncmp(header, magic, strlen(magic))) {
178 /* ...followed by a length in bytes... */
179 *length = strtoul(header + strlen(magic), &p, 10);
180 if (!*length || *length == ULONG_MAX || *p != ' ') {
185 /* ...followed by a SHA-1 hash... */
186 if (!sha1_from_hex(sha1, p)) {
189 p += SHA1_HEX_DIGEST_LEN;
191 /* ...and ended by a new-line. */
199 struct ovsdb_log_read_cbdata {
201 struct ovsdb_log *file;
203 unsigned long length;
206 static struct ovsdb_error *
207 parse_body(struct ovsdb_log *file, off_t offset, unsigned long int length,
208 uint8_t sha1[SHA1_DIGEST_SIZE], struct json **jsonp)
210 struct json_parser *parser;
214 parser = json_parser_create(JSPF_TRAILER);
220 chunk = MIN(length, sizeof input);
221 if (fread(input, 1, chunk, file->stream) != chunk) {
222 json_parser_abort(parser);
223 return ovsdb_io_error(ferror(file->stream) ? errno : EOF,
224 "%s: error reading %lu bytes "
225 "starting at offset %lld", file->name,
226 length, (long long int) offset);
228 sha1_update(&ctx, input, chunk);
229 json_parser_feed(parser, input, chunk);
233 sha1_final(&ctx, sha1);
234 *jsonp = json_parser_finish(parser);
239 ovsdb_log_read(struct ovsdb_log *file, struct json **jsonp)
241 uint8_t expected_sha1[SHA1_DIGEST_SIZE];
242 uint8_t actual_sha1[SHA1_DIGEST_SIZE];
243 struct ovsdb_error *error;
245 unsigned long data_length;
249 *jsonp = json = NULL;
251 if (file->read_error) {
252 return ovsdb_error_clone(file->read_error);
253 } else if (file->mode == OVSDB_LOG_WRITE) {
254 return OVSDB_BUG("reading file in write mode");
257 if (!fgets(header, sizeof header, file->stream)) {
258 if (feof(file->stream)) {
261 error = ovsdb_io_error(errno, "%s: read failed", file->name);
266 if (!parse_header(header, &data_length, expected_sha1)) {
267 error = ovsdb_syntax_error(NULL, NULL, "%s: parse error at offset "
268 "%lld in header line \"%.*s\"",
269 file->name, (long long int) file->offset,
270 (int) strcspn(header, "\n"), header);
274 data_offset = file->offset + strlen(header);
275 error = parse_body(file, data_offset, data_length, actual_sha1, &json);
280 if (memcmp(expected_sha1, actual_sha1, SHA1_DIGEST_SIZE)) {
281 error = ovsdb_syntax_error(NULL, NULL, "%s: %lu bytes starting at "
282 "offset %lld have SHA-1 hash "SHA1_FMT" "
283 "but should have hash "SHA1_FMT,
284 file->name, data_length,
285 (long long int) data_offset,
286 SHA1_ARGS(actual_sha1),
287 SHA1_ARGS(expected_sha1));
291 if (json->type == JSON_STRING) {
292 error = ovsdb_syntax_error(NULL, NULL, "%s: %lu bytes starting at "
293 "offset %lld are not valid JSON (%s)",
294 file->name, data_length,
295 (long long int) data_offset,
300 file->prev_offset = file->offset;
301 file->offset = data_offset + data_length;
306 file->read_error = ovsdb_error_clone(error);
311 /* Causes the log record read by the previous call to ovsdb_log_read() to be
312 * effectively discarded. The next call to ovsdb_log_write() will overwrite
313 * that previously read record.
315 * Calling this function more than once has no additional effect.
317 * This function is useful when ovsdb_log_read() successfully reads a record
318 * but that record does not make sense at a higher level (e.g. it specifies an
319 * invalid transaction). */
321 ovsdb_log_unread(struct ovsdb_log *file)
323 ovs_assert(file->mode == OVSDB_LOG_READ);
324 file->offset = file->prev_offset;
328 ovsdb_log_write(struct ovsdb_log *file, struct json *json)
330 uint8_t sha1[SHA1_DIGEST_SIZE];
331 struct ovsdb_error *error;
338 if (file->mode == OVSDB_LOG_READ || file->write_error) {
339 file->mode = OVSDB_LOG_WRITE;
340 file->write_error = false;
341 if (fseeko(file->stream, file->offset, SEEK_SET)) {
342 error = ovsdb_io_error(errno, "%s: cannot seek to offset %lld",
343 file->name, (long long int) file->offset);
346 if (ftruncate(fileno(file->stream), file->offset)) {
347 error = ovsdb_io_error(errno, "%s: cannot truncate to length %lld",
348 file->name, (long long int) file->offset);
353 if (json->type != JSON_OBJECT && json->type != JSON_ARRAY) {
354 error = OVSDB_BUG("bad JSON type");
358 /* Compose content. Add a new-line (replacing the null terminator) to make
359 * the file easier to read, even though it has no semantic value. */
360 json_string = json_to_string(json, 0);
361 length = strlen(json_string) + 1;
362 json_string[length - 1] = '\n';
364 /* Compose header. */
365 sha1_bytes(json_string, length, sha1);
366 snprintf(header, sizeof header, "%s%"PRIuSIZE" "SHA1_FMT"\n",
367 magic, length, SHA1_ARGS(sha1));
370 if (fwrite(header, strlen(header), 1, file->stream) != 1
371 || fwrite(json_string, length, 1, file->stream) != 1
372 || fflush(file->stream))
374 error = ovsdb_io_error(errno, "%s: write failed", file->name);
376 /* Remove any partially written data, ignoring errors since there is
377 * nothing further we can do. */
378 ignore(ftruncate(fileno(file->stream), file->offset));
383 file->offset += strlen(header) + length;
388 file->write_error = true;
394 ovsdb_log_commit(struct ovsdb_log *file)
396 if (fsync(fileno(file->stream))) {
397 return ovsdb_io_error(errno, "%s: fsync failed", file->name);
402 /* Returns the current offset into the file backing 'log', in bytes. This
403 * reflects the number of bytes that have been read or written in the file. If
404 * the whole file has been read, this is the file size. */
406 ovsdb_log_get_offset(const struct ovsdb_log *log)