Skip to content

Commit

Permalink
Implement a file_info_type function.
Browse files Browse the repository at this point in the history
Create a series of defines for the file type

Inspect the file type for each file handler and perform the relevant
release code to free any memory.

If the type cannot be determined, print an error and return -EINVAL.

This should also be slightly faster than a strcmp() but its not likely
that measurable.

This code could be used elsewhere in the process to reduce the strcmp
requirements, but for now just handle the release/releasedir case.

Signed-off-by: Matthew Ife <matthewi@mustardsystems.com>
  • Loading branch information
Matthew Ife committed Jan 12, 2023
1 parent 63c9b1d commit 0b749c9
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 10 deletions.
7 changes: 7 additions & 0 deletions src/bindings.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#define LXCFS_NUMSTRLEN64 21

enum lxcfs_virt_t {
LXC_TYPE_START,
LXC_TYPE_CGDIR,
LXC_TYPE_CGFILE,

Expand Down Expand Up @@ -67,8 +68,14 @@ enum lxcfs_virt_t {

LXC_TYPE_SYS_DEVICES_SYSTEM_CPU_ONLINE,
#define LXC_TYPE_SYS_DEVICES_SYSTEM_CPU_ONLINE_PATH "/sys/devices/system/cpu/online"
LXC_TYPE_END,
};

#define LXCFS_TYPE_CGROUP(type) (type >= LXC_TYPE_CGDIR && type <= LXC_TYPE_CGFILE)
#define LXCFS_TYPE_PROC(type) (type >= LXC_TYPE_PROC_MEMINFO && type <= LXC_TYPE_PROC_SLABINFO)
#define LXCFS_TYPE_SYS(type) (type >= LXC_TYPE_SYS && type <= LXC_TYPE_SYS_DEVICES_SYSTEM_CPU_ONLINE)
#define LXCFS_TYPE_OK(type) (type > LXC_TYPE_START && type < LXC_TYPE_END)

struct file_info {
char *controller;
char *cgroup;
Expand Down
47 changes: 37 additions & 10 deletions src/lxcfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,22 @@ static inline void users_unlock(void)
unlock_mutex(&user_count_mutex);
}

/* Returns file info type of custom type declaration carried
* in fuse_file_info */
static inline enum lxcfs_virt_t file_info_type(struct fuse_file_info *fi)
{
struct file_info *f;

f = INTTYPE_TO_PTR(fi->fh);
if (!f)
return -1;

if (!LXCFS_TYPE_OK(f->type))
return -1;

return f->type;
}

static pthread_t loadavg_pid = 0;

/* Returns zero on success */
Expand Down Expand Up @@ -770,27 +786,33 @@ static int lxcfs_access(const char *path, int mode)
static int lxcfs_releasedir(const char *path, struct fuse_file_info *fi)
{
int ret;
enum lxcfs_virt_t type;

if (strcmp(path, "/") == 0)
return 0;
type = file_info_type(fi);

if (strncmp(path, "/cgroup", 7) == 0) {
if (LXCFS_TYPE_CGROUP(type)) {
up_users();
ret = do_cg_releasedir(path, fi);
down_users();
return ret;
}

if (strcmp(path, "/proc") == 0)
return 0;

if (strncmp(path, "/sys", 4) == 0) {
if (LXCFS_TYPE_SYS(type)) {
up_users();
ret = do_sys_releasedir(path, fi);
down_users();
return ret;
}

if (LXCFS_TYPE_PROC(type))
return 0;

if (path && strcmp(path, "/") == 0)
return 0;

lxcfs_error("lxcfs_releasedir() unknown file type: path=%s, type=%d\n",
path, type);

return -EINVAL;
}

Expand Down Expand Up @@ -895,28 +917,33 @@ static int lxcfs_flush(const char *path, struct fuse_file_info *fi)
static int lxcfs_release(const char *path, struct fuse_file_info *fi)
{
int ret;
enum lxcfs_virt_t type;

if (strncmp(path, "/cgroup", 7) == 0) {
type = file_info_type(fi);

if (LXCFS_TYPE_CGROUP(type)) {
up_users();
ret = do_cg_release(path, fi);
down_users();
return ret;
}

if (strncmp(path, "/proc", 5) == 0) {
if (LXCFS_TYPE_PROC(type)) {
up_users();
ret = do_proc_release(path, fi);
down_users();
return ret;
}

if (strncmp(path, "/sys", 4) == 0) {
if (LXCFS_TYPE_SYS(type)) {
up_users();
ret = do_sys_release(path, fi);
down_users();
return ret;
}

lxcfs_error("lxcfs_release() unknown file type: path=%s, type=%d\n",
path, type);
return -EINVAL;
}

Expand Down

0 comments on commit 0b749c9

Please sign in to comment.