[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Minios-devel] [UNIKRAFT PATCH v2 2/3] lib/devfs: Add /dev/null and /dev/zero support



On 17.12.19 08:43, Costin Lupu wrote:
This is shamelessly copied and adapted from our implementation
for /dev/random device support.

Signed-off-by: Costin Lupu <costin.lupu@xxxxxxxxx>
---
  lib/devfs/Config.uk               |  17 ++++
  lib/devfs/Makefile.uk             |   2 +
  lib/devfs/null.c                  | 145 ++++++++++++++++++++++++++++++
  lib/vfscore/include/vfscore/uio.h |   1 +
  4 files changed, 165 insertions(+)
  create mode 100644 lib/devfs/null.c

diff --git a/lib/devfs/Config.uk b/lib/devfs/Config.uk
index 6f21c01c..0f459c4e 100644
--- a/lib/devfs/Config.uk
+++ b/lib/devfs/Config.uk
@@ -8,4 +8,21 @@ if LIBDEVFS
          bool "Mount /dev during boot"
        depends on LIBVFSCORE_AUTOMOUNT_ROOTFS
          default n
+
+       # hidden
+       config LIBDEVFS_DEV_NULL_ZERO
+               bool
+               default n
+
+       config LIBDEVFS_DEV_NULL
+               bool "/dev/null"

Calling it "/dev/null" may be misleading when the automount option is not enabled. I would call it "Register null device" and the other "Register zero device" - this would be inline with ukswrand.

+               default y if LIBDEVFS_AUTOMOUNT
+               select LIBDEVFS_DEV_NULL_ZERO
+               default n
+
+       config LIBDEVFS_DEV_ZERO
+               bool "/dev/zero"
+               default y if LIBDEVFS_AUTOMOUNT
+               select LIBDEVFS_DEV_NULL_ZERO
+               default n
  endif
diff --git a/lib/devfs/Makefile.uk b/lib/devfs/Makefile.uk
index c496fd56..2cffefab 100644
--- a/lib/devfs/Makefile.uk
+++ b/lib/devfs/Makefile.uk
@@ -3,6 +3,8 @@ $(eval $(call addlib_s,libdevfs,$(CONFIG_LIBDEVFS)))
  CINCLUDES-y += -I$(LIBDEVFS_BASE)/include
LIBDEVFS_CFLAGS-$(call gcc_version_ge,8,0) += -Wno-cast-function-type
+LIBDEVFS_CFLAGS-y += -Wno-unused-parameter

Can you use the __unused macro in your code instead? In the base repo, we shouldn't suppress warnings if not necessary (the previous `-Wno-cast-function-type` is required because of the VFS operations).


Other from these two comments, the patch seems to be fine.

LIBDEVFS_SRCS-y += $(LIBDEVFS_BASE)/device.c
  LIBDEVFS_SRCS-y += $(LIBDEVFS_BASE)/devfs_vnops.c
+LIBDEVFS_SRCS-$(CONFIG_LIBDEVFS_DEV_NULL_ZERO) += $(LIBDEVFS_BASE)/null.c
diff --git a/lib/devfs/null.c b/lib/devfs/null.c
new file mode 100644
index 00000000..f08c27d2
--- /dev/null
+++ b/lib/devfs/null.c
@@ -0,0 +1,145 @@
+/* SPDX-License-Identifier: BSD-3-Clause */
+/*
+ * Authors: Costin Lupu <costin.lupu@xxxxxxxxx>
+ *
+ * Copyright (c) 2019, University Politehnica of Bucharest. All rights 
reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the copyright holder nor the names of its
+ *    contributors may be used to endorse or promote products derived from
+ *    this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ * THIS HEADER MAY NOT BE EXTRACTED OR MODIFIED IN ANY WAY.
+ */
+
+#include <uk/config.h>
+#include <stdlib.h>
+#include <string.h>
+#include <uk/ctors.h>
+#include <uk/print.h>
+#include <vfscore/uio.h>
+#include <devfs/device.h>
+
+
+int dev_null_write(struct device *dev, struct uio *uio, int flags)
+{
+       uio->uio_resid = 0;
+       return 0;
+}
+
+int dev_null_open(struct device *device, int mode)
+{
+       return 0;
+}
+
+
+int dev_null_close(struct device *device)
+{
+       return 0;
+}
+
+#ifdef CONFIG_LIBDEVFS_DEV_NULL
+
+#define DEV_NULL_NAME "null"
+
+int dev_null_read(struct device *dev, struct uio *uio, int flags)
+{
+       uio->uio_resid = uio->uio_iov->iov_len;
+       return 0;
+}
+
+static struct devops null_devops = {
+       .read = dev_null_read,
+       .write = dev_null_write,
+       .open = dev_null_open,
+       .close = dev_null_close,
+};
+
+static struct driver drv_null = {
+       .devops = &null_devops,
+       .devsz = 0,
+       .name = DEV_NULL_NAME
+};
+#endif
+
+#ifdef CONFIG_LIBDEVFS_DEV_ZERO
+
+#define DEV_ZERO_NAME "zero"
+
+int dev_zero_read(struct device *dev, struct uio *uio, int flags)
+{
+       size_t count;
+       char *buf;
+
+       buf = uio->uio_iov->iov_base;
+       count = uio->uio_iov->iov_len;
+
+       memset(buf, 0, count);
+       uio->uio_resid = 0;
+       return 0;
+}
+
+static struct devops zero_devops = {
+       .read = dev_zero_read,
+       .write = dev_null_write,
+       .open = dev_null_open,
+       .close = dev_null_close,
+};
+
+static struct driver drv_zero = {
+       .devops = &zero_devops,
+       .devsz = 0,
+       .name = DEV_ZERO_NAME
+};
+#endif
+
+static int devfs_register_null(void)
+{
+       struct device *dev;
+
+#ifdef CONFIG_LIBDEVFS_DEV_NULL
+       uk_pr_debug("Register '%s' to devfs\n", DEV_NULL_NAME);
+
+       /* register /dev/null */
+       dev = device_create(&drv_null, DEV_NULL_NAME, D_CHR);
+       if (dev == NULL) {
+               uk_pr_err("Failed to register '%s' to devfs\n", DEV_NULL_NAME);
+               return -1;
+       }
+#endif
+
+#ifdef CONFIG_LIBDEVFS_DEV_ZERO
+       uk_pr_debug("Register '%s' to devfs\n", DEV_ZERO_NAME);
+
+       /* register /dev/zero */
+       dev = device_create(&drv_zero, DEV_ZERO_NAME, D_CHR);
+       if (dev == NULL) {
+               uk_pr_err("Failed to register '%s' to devfs\n", DEV_ZERO_NAME);
+               return -1;
+       }
+#endif
+
+       return 0;
+}
+
+devfs_initcall(devfs_register_null);
diff --git a/lib/vfscore/include/vfscore/uio.h 
b/lib/vfscore/include/vfscore/uio.h
index 83dd5b9a..b14fdb82 100644
--- a/lib/vfscore/include/vfscore/uio.h
+++ b/lib/vfscore/include/vfscore/uio.h
@@ -36,6 +36,7 @@
  #include <sys/types.h>
  #include <sys/uio.h>
  #include <limits.h>
+#include <uk/assert.h>
enum uio_rw { UIO_READ, UIO_WRITE };

_______________________________________________
Minios-devel mailing list
Minios-devel@xxxxxxxxxxxxxxxxxxxx
https://lists.xenproject.org/mailman/listinfo/minios-devel

 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.