Skip to content

Commit

Permalink
UBUNTU: Add ubuntu-host module
Browse files Browse the repository at this point in the history
ubuntu-host is a module for providing data to containers via proc.
Initially it is populated with a single file, esm-token, for
supplying ESM access tokens.

Signed-off-by: Seth Forshee <seth.forshee@canonical.com>
  • Loading branch information
Seth Forshee authored and Andrea Righi committed Dec 15, 2020
1 parent 1887e71 commit cab29ff
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 0 deletions.
4 changes: 4 additions & 0 deletions ubuntu/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ source "ubuntu/hio/Kconfig"
##
##
##
source "ubuntu/ubuntu-host/Kconfig"
##
##
##
##
##
##
Expand Down
4 changes: 4 additions & 0 deletions ubuntu/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ obj-$(CONFIG_HIO) += hio/
##
##
##
obj-$(CONFIG_UBUNTU_HOST) += ubuntu-host/
##
##
##
ifeq ($(ARCH),x86)
obj-y += xr-usb-serial/
endif
Expand Down
5 changes: 5 additions & 0 deletions ubuntu/ubuntu-host/Kconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
config UBUNTU_HOST
tristate "proc dir for exporting host data to containers"
help
Creates an ubuntu-host directory in proc for providing data from
Ubuntu hosts to containers.
1 change: 1 addition & 0 deletions ubuntu/ubuntu-host/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
obj-$(CONFIG_UBUNTU_HOST) += ubuntu-host.o
68 changes: 68 additions & 0 deletions ubuntu/ubuntu-host/ubuntu-host.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#include <linux/uaccess.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/proc_fs.h>
#include <asm/uaccess.h>

#define PROC_DIR "ubuntu-host"

#define ESM_TOKEN_FILE "esm-token"
#define ESM_TOKEN_MAX_SIZE 64

static struct proc_dir_entry *proc_dir;
static char esm_token_buffer[ESM_TOKEN_MAX_SIZE];

static ssize_t esm_token_read(struct file *f, char __user *buf, size_t len,
loff_t *off)
{
return simple_read_from_buffer(buf, len, off, esm_token_buffer,
strlen(esm_token_buffer));
}

static ssize_t esm_token_write(struct file *f, const char __user *buf,
size_t len, loff_t *off)
{
ssize_t ret;

if (len >= ESM_TOKEN_MAX_SIZE - 1)
return -EINVAL;

ret = simple_write_to_buffer(esm_token_buffer, ESM_TOKEN_MAX_SIZE - 1,
off, buf, len);
if (ret >= 0)
esm_token_buffer[ret] = '\0';

return ret;
}

static const struct proc_ops esm_token_fops = {
.proc_read = esm_token_read,
.proc_write = esm_token_write,
};

static void ubuntu_host_cleanup(void)
{
remove_proc_entry(ESM_TOKEN_FILE, proc_dir);
proc_remove(proc_dir);
}

static int __init ubuntu_host_init(void)
{
proc_dir = proc_mkdir(PROC_DIR, NULL);
if (!proc_dir) {
pr_err("Failed to create ubuntu-host dir\n");
return -ENOMEM;
}

if (!proc_create_data(ESM_TOKEN_FILE, 0644, proc_dir, &esm_token_fops, NULL)) {
pr_err("Failed to create esm-tokan file\n");
ubuntu_host_cleanup();
return -ENOMEM;
}

return 0;
}

module_init(ubuntu_host_init);
module_exit(ubuntu_host_cleanup);
MODULE_LICENSE("GPL");

0 comments on commit cab29ff

Please sign in to comment.