[rtems-libbsd commit] devfs: Create path.

Sebastian Huber sebh at rtems.org
Tue Aug 16 07:18:39 UTC 2016


Module:    rtems-libbsd
Branch:    master
Commit:    6634edbee85ed0b28c483d9fc6284f3ba6761f0f
Changeset: http://git.rtems.org/rtems-libbsd/commit/?id=6634edbee85ed0b28c483d9fc6284f3ba6761f0f

Author:    Christian Mauderer <Christian.Mauderer at embedded-brains.de>
Date:      Tue Aug 16 08:52:06 2016 +0200

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 027013b..10735f9 100644
--- a/rtemsbsd/sys/fs/devfs/devfs_devs.c
+++ b/rtemsbsd/sys/fs/devfs/devfs_devs.c
@@ -36,6 +36,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <sys/file.h>
+#include <sys/malloc.h>
 #include <unistd.h>
 
 #include <sys/conf.h>
@@ -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 898eb17..cbff913 100644
--- a/testsuite/cdev01/test_main.c
+++ b/testsuite/cdev01/test_main.c
@@ -40,18 +40,24 @@
 #include <errno.h>
 #include <fcntl.h>
 #include <poll.h>
+#include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 #include <unistd.h>
 
 #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);
 }




More information about the vc mailing list