[PATCH v2 1/4] fs: devfs: implement cdev_fdopen

Ahmad Fatoum ahmad at a3f.at
Sun Jan 29 23:27:04 PST 2023


As an alternative to cdev_open and using cdev_read/write, we define a
new cdev_fdopen function that returns a file descriptor. The benefit of
this is that code using it may use all the more high level helpers we
have for reading/writing file descriptors.

Signed-off-by: Ahmad Fatoum <ahmad at a3f.at>
---
 fs/devfs-core.c  | 20 ++++++++++++++++++++
 include/driver.h |  1 +
 2 files changed, 21 insertions(+)

diff --git a/fs/devfs-core.c b/fs/devfs-core.c
index 2a259c2fe0ef..fbcf68e81597 100644
--- a/fs/devfs-core.c
+++ b/fs/devfs-core.c
@@ -177,6 +177,26 @@ int cdev_open(struct cdev *cdev, unsigned long flags)
 	return 0;
 }
 
+int cdev_fdopen(struct cdev *cdev, unsigned long flags)
+{
+	char *path;
+	int fd;
+
+	if (!cdev)
+		return -ENODEV;
+	if (IS_ERR(cdev))
+		return PTR_ERR(cdev);
+
+	path = basprintf("/dev/%s", cdev->name);
+	if (!path)
+		return -ENOMEM;
+
+	fd = open(path, flags);
+
+	free(path);
+	return fd;
+}
+
 struct cdev *cdev_open_by_name(const char *name, unsigned long flags)
 {
 	struct cdev *cdev;
diff --git a/include/driver.h b/include/driver.h
index f0a0b9d6ae8f..f53668711b81 100644
--- a/include/driver.h
+++ b/include/driver.h
@@ -535,6 +535,7 @@ struct cdev *cdev_open_by_name(const char *name, unsigned long flags);
 struct cdev *cdev_create_loop(const char *path, ulong flags, loff_t offset);
 void cdev_remove_loop(struct cdev *cdev);
 int cdev_open(struct cdev *, unsigned long flags);
+int cdev_fdopen(struct cdev *cdev, unsigned long flags);
 void cdev_close(struct cdev *cdev);
 int cdev_flush(struct cdev *cdev);
 ssize_t cdev_read(struct cdev *cdev, void *buf, size_t count, loff_t offset, ulong flags);
-- 
2.38.1




More information about the barebox mailing list