Skip to content

Commit

Permalink
fuse_session_custom_io: Add a struct size field
Browse files Browse the repository at this point in the history
As in commit 73cd124 'struct fuse_custom_io' might
get extended and in order to stay ABI compatible libfuse
has to zero all extended fields. In order to do so it has
to know the size of the struct the application knows about.

Signed-off-by: Bernd Schubert <[email protected]>
  • Loading branch information
bsbernd committed Apr 18, 2024
1 parent 73cd124 commit 9f93ee0
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
2 changes: 1 addition & 1 deletion example/hello_ll_uds.c
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ int main(int argc, char *argv[])
if (cfd == -1)
goto err_out3;

if (fuse_session_custom_io(se, &io, cfd) != 0)
if (fuse_session_custom_io(se, &io, sizeof(io), cfd) != 0)
goto err_out3;

/* Block until ctrl+c */
Expand Down
9 changes: 7 additions & 2 deletions include/fuse_lowlevel.h
Original file line number Diff line number Diff line change
Expand Up @@ -2043,15 +2043,20 @@ struct fuse_session *fuse_session_new(struct fuse_args *args,
* @param se session object
* @param io Custom io to use when retrieving/sending requests/responses
* @param fd file descriptor for the session
*
* @param custom_io_size the size of the struct the callers knows about.
* The struct might be extended in higher libfuse versions and in order
* to stay ABI compatible extended fields are zeroed - libuse needs to know
* the size the caller knows about.
* @return 0 on success
* @return -EINVAL if `io`, `io->read` or `ìo->writev` are NULL
* @return -EBADF if `fd` was smaller than 0
* @return -errno if failed to allocate memory to store `io`
*
**/
int fuse_session_custom_io(struct fuse_session *se,
const struct fuse_custom_io *io, int fd);
const struct fuse_custom_io *io,
size_t custom_io_size,
int fd);

/**
* Mount a FUSE file system.
Expand Down
20 changes: 17 additions & 3 deletions lib/fuse_lowlevel.c
Original file line number Diff line number Diff line change
Expand Up @@ -3117,8 +3117,17 @@ struct fuse_session *fuse_session_new(struct fuse_args *args,
}

int fuse_session_custom_io(struct fuse_session *se, const struct fuse_custom_io *io,
int fd)
size_t prog_custom_io_sz, int fd)
{
size_t lib_custom_io_sz = sizeof(*se->io);

if (prog_custom_io_sz > lib_custom_io_sz) {
fuse_log(FUSE_LOG_ERR,
"Provided 'fuse_custom_io_sz' (%zu) is larger than struct (%zu)\n",
prog_custom_io_sz, lib_custom_io_sz);
return -EINVAL;
}

if (fd < 0) {
fuse_log(FUSE_LOG_ERR, "Invalid file descriptor value %d passed to "
"fuse_session_custom_io()\n", fd);
Expand All @@ -3138,15 +3147,20 @@ int fuse_session_custom_io(struct fuse_session *se, const struct fuse_custom_io
return -EINVAL;
}

se->io = malloc(sizeof(struct fuse_custom_io));
se->io = calloc(1, lib_custom_io_sz);
if (se->io == NULL) {
fuse_log(FUSE_LOG_ERR, "Failed to allocate memory for custom io. "
"Error: %s\n", strerror(errno));
return -errno;
}

se->fd = fd;
*se->io = *io;

/* Above the struct was zeroed. For ABI compatibility we memcopy in
* what the caller knows about.
*/
memcpy(se->io, io, prog_custom_io_sz);

return 0;
}

Expand Down

0 comments on commit 9f93ee0

Please sign in to comment.