Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions file.c
Original file line number Diff line number Diff line change
Expand Up @@ -816,15 +816,15 @@ static void uci_file_commit(struct uci_context *ctx, struct uci_package **packag
done:
free(name);
free(path);
uci_close_stream(f1);
if (do_rename) {
path = realpath(p->path, NULL);
if (!path || stat(path, &statbuf) || chmod(filename, statbuf.st_mode) || rename(filename, path)) {
unlink(filename);
UCI_THROW(ctx, UCI_ERR_IO);
ctx->err = UCI_ERR_IO;
}
free(path);
}
uci_close_stream(f1);
if (ctx->err)
UCI_THROW(ctx, ctx->err);
}
Expand Down
29 changes: 26 additions & 3 deletions util.c
Original file line number Diff line number Diff line change
Expand Up @@ -218,9 +218,32 @@ __private FILE *uci_open_stream(struct uci_context *ctx, const char *filename, c
if (fd < 0)
goto error;

ret = flock(fd, (write ? LOCK_EX : LOCK_SH));
if ((ret < 0) && (errno != ENOSYS))
goto error_close;
if (write) {
ret = flock(fd, LOCK_EX);
if ((ret < 0) && (errno != ENOSYS))
goto error_close;
} else {
while (1) {
ret = flock(fd, LOCK_SH | LOCK_NB);
if (ret == 0 || errno == ENOSYS)
break;

if (errno == EINTR)
continue;

if (errno == EWOULDBLOCK || errno == EAGAIN) {
/* Reopen to follow a potentially replaced inode while writer holds lock */
close(fd);
usleep(1000);
fd = open(filename, flags, mode);
if (fd < 0)
goto error;
continue;
}

goto error_close;
}
}

ret = lseek(fd, 0, pos);

Expand Down
Loading