From 6634edbee85ed0b28c483d9fc6284f3ba6761f0f Mon Sep 17 00:00:00 2001 From: Christian Mauderer Date: Tue, 16 Aug 2016 08:52:06 +0200 Subject: devfs: Create path. --- rtemsbsd/sys/fs/devfs/devfs_devs.c | 35 +++++++++++++++++++++++++++++++++-- testsuite/cdev01/test_main.c | 15 +++++++++++---- 2 files changed, 44 insertions(+), 6 deletions(-) diff --git a/rtemsbsd/sys/fs/devfs/devfs_devs.c b/rtemsbsd/sys/fs/devfs/devfs_devs.c index 027013b4..10735f9b 100644 --- a/rtemsbsd/sys/fs/devfs/devfs_devs.c +++ b/rtemsbsd/sys/fs/devfs/devfs_devs.c @@ -36,6 +36,7 @@ #include #include #include +#include #include #include @@ -228,7 +229,7 @@ devfs_alloc(int flags) { struct cdev *cdev; - cdev = malloc(sizeof *cdev); + cdev = malloc(sizeof *cdev, M_TEMP, 0); if (cdev == NULL) return (NULL); @@ -243,7 +244,33 @@ devfs_alloc(int flags) void devfs_free(struct cdev *cdev) { - free(cdev); + free(cdev, M_TEMP); +} + +/* + * Create the directory for a device. + * Note: This don't uses dirname() because this function is not defined thread + * save in POSIX. + */ +static void +devfs_create_directory(const char *devname) +{ + char *dir = NULL; + char *lastsep = NULL; + int rv; + mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO; + + dir = strdup(devname, M_TEMP); + BSD_ASSERT(dir != NULL); + + lastsep = strrchr(dir, '/'); + if(lastsep != NULL) { + *lastsep = 0; + rv = rtems_mkdir(dir, mode); + BSD_ASSERT(rv == 0); + } + + free(dir, M_TEMP); } /* @@ -258,6 +285,8 @@ devfs_create(struct cdev *dev) int rv; mode_t mode = S_IFCHR | S_IRWXU | S_IRWXG | S_IRWXO; + devfs_create_directory(dev->si_path); + rv = IMFS_make_generic_node(dev->si_path, mode, &devfs_imfs_control, dev); BSD_ASSERT(rv == 0); @@ -270,6 +299,8 @@ devfs_destroy(struct cdev *dev) rv = unlink(dev->si_path); BSD_ASSERT(rv == 0); + + /* FIXME: Check if directory is empty and remove it. */ } int diff --git a/testsuite/cdev01/test_main.c b/testsuite/cdev01/test_main.c index 898eb174..cbff9133 100644 --- a/testsuite/cdev01/test_main.c +++ b/testsuite/cdev01/test_main.c @@ -40,18 +40,24 @@ #include #include #include +#include #include +#include #include #include "test_cdev01.h" #define TEST_NAME "LIBBSD CDEV 1" -static void test_cdev(void) +static void test_cdev(const char *path) { - const char *name = "test"; - const char *path = "/dev/test"; const struct timespec *timeout = NULL; + const char *name; + + /* Remove leading "/dev/" and use the rest as a name. */ + name = path + sizeof("/dev/") - 1; + + printf("Test creating a cdev named \"%s\" at \"%s\".\n", name, path); test_state state = TEST_NEW; int rv = 0; @@ -131,7 +137,8 @@ static void test_cdev(void) static void test_main(void) { - test_cdev(); + test_cdev("/dev/test"); + test_cdev("/dev/some/sub/dir/somedev"); exit(0); } -- cgit v1.2.3