Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unix: Don't fail on FreeBSD running ZFS #461

Merged
merged 2 commits into from
Mar 17, 2022
Merged
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
71 changes: 49 additions & 22 deletions lib/unix.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,44 @@ open_mmap_file(char *path, uint32_t file_flags)
return open(path, file_flags, 0600);
}

#if defined(QB_BSD) || !defined(HAVE_POSIX_FALLOCATE)
static int local_fallocate(int fd, size_t bytes)
{
long page_size = sysconf(_SC_PAGESIZE);
long write_size = QB_MIN(page_size, bytes);
char *buffer = NULL;
int i;
size_t written;

if (page_size < 0) {
goto error_exit;
}
buffer = calloc(1, write_size);
if (buffer == NULL) {
goto error_exit;
}

for (i = 0; i < (bytes / write_size); i++) {
retry_write:
written = write(fd, buffer, write_size);
if (written == -1 && errno == EINTR) {
goto retry_write;
}
if (written != write_size) {
free(buffer);
errno = ENOSPC;
goto error_exit;
}
}
free(buffer);

return 0;

error_exit:
return -1;
}
#endif

int32_t
qb_sys_mmap_file_open(char *path, const char *file, size_t bytes,
uint32_t file_flags)
Expand Down Expand Up @@ -132,6 +170,16 @@ qb_sys_mmap_file_open(char *path, const char *file, size_t bytes,
if (res == EINTR) {
qb_util_log(LOG_DEBUG, "got EINTR trying to allocate file %s, retrying...", path);
continue;
#ifdef QB_BSD
} else if (res == EINVAL) { /* posix_fallocate() fails on ZFS
https://lists.freebsd.org/pipermail/freebsd-current/2018-February/068448.html */
qb_util_log(LOG_DEBUG, "posix_fallocate returned EINVAL - running on ZFS?");
if (file_flags & O_CREAT) {
if (local_fallocate(fd, bytes)) {
goto unlink_exit;
}
}
#endif
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another possibility would be to do what's currently in the #else block if posix_fallocate() fails, as a fallback (regardless of platform). Not sure if that's better though ...

} else if (res != 0) {
errno = res;
res = -1 * res;
Expand All @@ -142,30 +190,9 @@ qb_sys_mmap_file_open(char *path, const char *file, size_t bytes,
} while (fallocate_retry > 0);
#else
if (file_flags & O_CREAT) {
long page_size = sysconf(_SC_PAGESIZE);
long write_size = QB_MIN(page_size, bytes);
if (page_size < 0) {
res = -errno;
if (local_fallocate(fd, bytes)) {
goto unlink_exit;
}
buffer = calloc(1, write_size);
if (buffer == NULL) {
res = -ENOMEM;
goto unlink_exit;
}
for (i = 0; i < (bytes / write_size); i++) {
retry_write:
written = write(fd, buffer, write_size);
if (written == -1 && errno == EINTR) {
goto retry_write;
}
if (written != write_size) {
res = -ENOSPC;
free(buffer);
goto unlink_exit;
}
}
free(buffer);
}
#endif /* HAVE_POSIX_FALLOCATE */

Expand Down
2 changes: 1 addition & 1 deletion tests/check_ipc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1959,8 +1959,8 @@ test_ipc_disconnect_after_created(void)
ck_assert_int_eq(res, -ENOTCONN);
}
ck_assert_int_eq(QB_FALSE, qb_ipcc_is_connected(conn));

qb_ipcc_disconnect(conn);
sleep(1); /* Give it time to stop */
kill_server(pid);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want to make sure this doesn't get overlooked, you could make the sleep time an argument to kill_server()

}

Expand Down