Skip to content

Commit

Permalink
TOOLS/VFS: convert stack allocation at vfs_mount() to heap
Browse files Browse the repository at this point in the history
  • Loading branch information
michal-shalev committed Sep 16, 2024
1 parent a1489b4 commit 1bc0050
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions src/tools/vfs/vfs_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@

#include "vfs_daemon.h"

#include <ucs/sys/string.h>
#include <ucs/debug/log_def.h>
#include <ucs/debug/memtrack_int.h>
#include <sys/wait.h>
#include <limits.h>
#include <stdlib.h>
Expand Down Expand Up @@ -151,10 +153,17 @@ static const char *vfs_get_process_name(int pid, char *buf, size_t max_length)

int vfs_mount(int pid)
{
char mountpoint[PATH_MAX];
char mountopts[1024];
char name[NAME_MAX];
char *mountpoint;
int fuse_fd, ret;
ucs_status_t status;

status = ucs_string_alloc_path_buffer(&mountpoint, "mountpoint");
if (status != UCS_OK) {
ret = -ENOMEM;
goto out;
}

/* Add common mount options:
* - File system name (source) : process name and pid
Expand All @@ -168,29 +177,36 @@ int vfs_mount(int pid)
vfs_get_process_name(pid, name, sizeof(name)),
(strlen(g_opts.mount_opts) > 0) ? "," : "", g_opts.mount_opts);
if (ret >= sizeof(mountopts)) {
return -ENOMEM;
ret = -ENOMEM;
goto out_free_mountpoint;
}

/* Create the mount point directory, and ignore "already exists" error */
vfs_get_mountpoint(pid, mountpoint, sizeof(mountpoint));
vfs_get_mountpoint(pid, mountpoint, PATH_MAX);
ret = mkdir(mountpoint, S_IRWXU);
if ((ret < 0) && (errno != EEXIST)) {
ret = -errno;
vfs_error("failed to create directory '%s': %m", mountpoint);
return ret;
goto out_free_mountpoint;
}

/* Mount a new FUSE filesystem in the mount point directory */
vfs_log("mounting directory '%s' with options '%s'", mountpoint, mountopts);
fuse_fd = fuse_open_channel(mountpoint, mountopts);
if (fuse_fd < 0) {
ret = fuse_fd;
vfs_error("fuse_open_channel(%s,opts=%s) failed: %m", mountpoint,
mountopts);
return fuse_fd;
goto out_free_mountpoint;
}

vfs_log("mounted directory '%s' with fd %d", mountpoint, fuse_fd);
return fuse_fd;
ret = fuse_fd;

out_free_mountpoint:
ucs_free(mountpoint);
out:
return ret;
}

int vfs_unmount(int pid)
Expand Down

0 comments on commit 1bc0050

Please sign in to comment.