]> xenbits.xensource.com Git - unikraft/unikraft.git/commitdiff
lib/posix-fd: Move open file headers into own lib
authorAndrei Tatar <andrei@unikraft.io>
Thu, 22 Feb 2024 20:28:22 +0000 (21:28 +0100)
committerUnikraft Bot <monkey@unikraft.io>
Thu, 6 Feb 2025 08:50:08 +0000 (08:50 +0000)
This changes merges the headers uk/ofile.h and uk/posix-fd.h and moves
them into their own library, providing shared definitions related to
open file descriptions for both posix-fdio and posix-fdtab, without
introducing an undue dedependency between the two.

Checkpatch-Ignore: REPEATED_WORD
Signed-off-by: Andrei Tatar <andrei@unikraft.io>
Approved-by: Sergiu Moga <sergiu@unikraft.io>
Reviewed-by: Sergiu Moga <sergiu@unikraft.io>
GitHub-Closes: #1574

12 files changed:
lib/Makefile.uk
lib/posix-fd/Config.uk [new file with mode: 0644]
lib/posix-fd/Makefile.uk [new file with mode: 0644]
lib/posix-fd/include/uk/posix-fd.h [new file with mode: 0644]
lib/posix-fdio/Config.uk
lib/posix-fdio/include/uk/posix-fd.h [deleted file]
lib/posix-fdtab/Config.uk
lib/posix-fdtab/fdtab.c
lib/posix-fdtab/include/uk/posix-fdtab.h
lib/posix-socket/Config.uk
lib/ukfile/Config.uk
lib/ukfile/include/uk/ofile.h [deleted file]

index e2a14e553f2152bb8a5d1562e05d617951589dbe..f26eeb7628182ae85ba82f29ed7a0d658d78fd0b 100644 (file)
@@ -11,6 +11,7 @@ $(eval $(call import_lib,$(CONFIG_UK_BASE)/lib/ukgcov))
 $(eval $(call import_lib,$(CONFIG_UK_BASE)/lib/isrlib))
 $(eval $(call import_lib,$(CONFIG_UK_BASE)/lib/nolibc))
 $(eval $(call import_lib,$(CONFIG_UK_BASE)/lib/posix-environ))
+$(eval $(call import_lib,$(CONFIG_UK_BASE)/lib/posix-fd))
 $(eval $(call import_lib,$(CONFIG_UK_BASE)/lib/posix-fdtab))
 $(eval $(call import_lib,$(CONFIG_UK_BASE)/lib/posix-fdio))
 $(eval $(call import_lib,$(CONFIG_UK_BASE)/lib/posix-eventfd))
diff --git a/lib/posix-fd/Config.uk b/lib/posix-fd/Config.uk
new file mode 100644 (file)
index 0000000..5f513f4
--- /dev/null
@@ -0,0 +1,5 @@
+config LIBPOSIX_FD
+       bool "posix-fd: Open file descriptions"
+       select LIBUKFILE
+       select LIBUKLOCK
+       select LIBUKLOCK_MUTEX
diff --git a/lib/posix-fd/Makefile.uk b/lib/posix-fd/Makefile.uk
new file mode 100644 (file)
index 0000000..9c22bde
--- /dev/null
@@ -0,0 +1,4 @@
+$(eval $(call addlib_s,libposix_fd,$(CONFIG_LIBPOSIX_FD)))
+
+CINCLUDES-$(CONFIG_LIBPOSIX_FD) += -I$(LIBPOSIX_FD_BASE)/include
+CXXINCLUDES-$(CONFIG_LIBPOSIX_FD) += -I$(LIBPOSIX_FD_BASE)/include
diff --git a/lib/posix-fd/include/uk/posix-fd.h b/lib/posix-fd/include/uk/posix-fd.h
new file mode 100644 (file)
index 0000000..6343651
--- /dev/null
@@ -0,0 +1,63 @@
+/* SPDX-License-Identifier: BSD-3-Clause */
+/* Copyright (c) 2023, Unikraft GmbH and The Unikraft Authors.
+ * Licensed under the BSD-3-Clause License (the "License").
+ * You may not use this file except in compliance with the License.
+ */
+
+/* Common definitions pertaining to POSIX open file descriptions */
+
+#ifndef __UK_POSIX_FD_H__
+#define __UK_POSIX_FD_H__
+
+#include <fcntl.h>
+#include <sys/epoll.h>
+
+#include <uk/essentials.h>
+#include <uk/file.h>
+#include <uk/mutex.h>
+#include <uk/refcount.h>
+
+/* Open file description */
+struct uk_ofile {
+       const struct uk_file *file;
+       unsigned int mode;
+       __atomic refcnt;
+       off_t pos; /* Current file read/write offset position */
+       struct uk_mutex lock; /* Lock for modifying open file state */
+};
+
+/**
+ * Initialize an open file description with a refcount of 1.
+ *
+ * @param of Open file description to be initialized
+ * @param f File to reference
+ * @param mode Mode bits of open file description
+ */
+static inline
+void uk_ofile_init(struct uk_ofile *of,
+                  const struct uk_file *f, unsigned int mode)
+{
+       uk_refcount_init(&of->refcnt, 1);
+       uk_mutex_init(&of->lock);
+       of->file = f;
+       of->mode = mode;
+       of->pos = 0;
+}
+
+
+/* Mode bits from fcntl.h that open files are interested in */
+#define UKFD_MODE_MASK \
+       (O_WRONLY|O_RDWR|O_NONBLOCK|O_APPEND|O_DIRECT|O_SYNC|O_DSYNC)
+
+/* Unikraft-specific mode bits, chosen to not overlap with any O_* flags */
+/* Open file is not seekable (e.g. for pipes, sockets & FIFOs) */
+#define UKFD_O_NOSEEK   010
+/* File I/O should not use the file locks (e.g. if driver handles them) */
+#define UKFD_O_NOIOLOCK 020
+
+/* Event sets */
+#define UKFD_POLL_ALWAYS (EPOLLERR|EPOLLHUP)
+#define UKFD_POLLIN (EPOLLIN|EPOLLRDNORM|EPOLLRDBAND)
+#define UKFD_POLLOUT (EPOLLOUT|EPOLLWRNORM|EPOLLWRBAND)
+
+#endif /* __UK_POSIX_FD_H__ */
index 47264a8a508365b6f8a2d3c05486feea9364a6ec..8ceec3006c29f3ccb7690617141a053b75bb754c 100644 (file)
@@ -2,6 +2,7 @@ config LIBPOSIX_FDIO
        bool "posix-fdio: File I/O and control"
        select LIBUKATOMIC
        select LIBUKFILE
+       select LIBPOSIX_FD
        select LIBPOSIX_TIME
        select LIBUKTIMECONV
        select LIBPOSIX_FDTAB
diff --git a/lib/posix-fdio/include/uk/posix-fd.h b/lib/posix-fdio/include/uk/posix-fd.h
deleted file mode 100644 (file)
index eda2fc0..0000000
+++ /dev/null
@@ -1,32 +0,0 @@
-/* SPDX-License-Identifier: BSD-3-Clause */
-/* Copyright (c) 2023, Unikraft GmbH and The Unikraft Authors.
- * Licensed under the BSD-3-Clause License (the "License").
- * You may not use this file except in compliance with the License.
- */
-
-/* Common definitions pertaining to POSIX open file descriptions */
-
-#ifndef __UK_POSIX_FD_H__
-#define __UK_POSIX_FD_H__
-
-#include <fcntl.h>
-#include <sys/epoll.h>
-
-#include <uk/ofile.h>
-
-/* Mode bits from fcntl.h that open files are interested in */
-#define UKFD_MODE_MASK \
-       (O_WRONLY|O_RDWR|O_NONBLOCK|O_APPEND|O_DIRECT|O_SYNC|O_DSYNC)
-
-/* Unikraft-specific mode bits, chosen to not overlap with any O_* flags */
-/* Open file is not seekable (e.g. for pipes, sockets & FIFOs) */
-#define UKFD_O_NOSEEK   010
-/* File I/O should not use the file locks (e.g. if driver handles them) */
-#define UKFD_O_NOIOLOCK 020
-
-/* Event sets */
-#define UKFD_POLL_ALWAYS (EPOLLERR|EPOLLHUP)
-#define UKFD_POLLIN (EPOLLIN|EPOLLRDNORM|EPOLLRDBAND)
-#define UKFD_POLLOUT (EPOLLOUT|EPOLLWRNORM|EPOLLWRBAND)
-
-#endif /* __UK_POSIX_FD_H__ */
index 538a9fd4c76b9643029403d10a647aaeefe864d8..eaba238b524b5fc269be9b83e801d474bfb8c2a9 100644 (file)
@@ -2,6 +2,7 @@ menuconfig LIBPOSIX_FDTAB
        bool "posix-fdtab: File descriptor table"
        select LIBUKATOMIC
        select LIBUKFILE
+       select LIBPOSIX_FD
 
 if LIBPOSIX_FDTAB
        config LIBPOSIX_FDTAB_MAXFDS
@@ -11,6 +12,5 @@ if LIBPOSIX_FDTAB
        # Hidden, selected by core components when needed
        config LIBPOSIX_FDTAB_LEGACY_SHIM
        bool
-       default n
 
 endif
index e942926f4ec54d4f53adef880ebe9733fc30f5cb..5aae05c1cb50fefb55af50ad87023d2887f1ca25 100644 (file)
@@ -103,12 +103,14 @@ static inline struct fdval fdtab_decode(void *p)
 }
 
 /* struct uk_ofile allocation & refcounting */
-static inline struct uk_ofile *ofile_new(struct uk_fdtab *tab)
+static inline struct uk_ofile *ofile_new(struct uk_fdtab *tab,
+                                        const struct uk_file *f,
+                                        unsigned int mode)
 {
        struct uk_ofile *of = uk_malloc(tab->alloc, sizeof(*of));
 
        if (of)
-               uk_ofile_init(of);
+               uk_ofile_init(of, f, mode);
        return of;
 }
 static inline void ofile_del(struct uk_fdtab *tab, struct uk_ofile *of)
@@ -171,16 +173,10 @@ int uk_fdtab_open(const struct uk_file *f, unsigned int mode)
        UK_ASSERT(f);
 
        tab = _active_tab();
-       of = ofile_new(tab);
+       of = ofile_new(tab, f, mode & ~O_CLOEXEC);
        if (!of)
                return -ENOMEM;
-       /* Take refs on file & ofile */
        uk_file_acquire(f);
-       ofile_acq(of);
-       /* Prepare open file */
-       of->file = f;
-       of->pos = 0;
-       of->mode = mode & ~O_CLOEXEC;
        /* Place the file in fdtab */
        flags = (mode & O_CLOEXEC) ? UK_FDTAB_CLOEXEC : 0;
        entry = fdtab_encode(of, flags);
index 078c22c092660a309558188822a6c3710bc793f8..d7a74d551175835eea6ca5ae21db36021c57f433 100644 (file)
@@ -10,7 +10,7 @@
 #define __UK_POSIX_FDTAB_H__
 
 #include <uk/config.h>
-#include <uk/ofile.h>
+#include <uk/posix-fd.h>
 
 #define UK_LIBPOSIX_FDTAB_INIT_PRIO    UK_PRIO_EARLIEST
 #define UK_FD_MAX INT_MAX
index 38fcf1cc0173fdf087530b0f999c12f483fd6a1e..2f7274133cc595f321e57ef091299a5454304c3b 100644 (file)
@@ -3,6 +3,7 @@ menuconfig LIBPOSIX_SOCKET
        select LIBUKDEBUG
        select LIBUKALLOC
        select LIBUKFILE
+       select LIBPOSIX_FD
        select LIBPOSIX_FDIO
        help
                This microlibrary allows other microlibraries to register a
index 8d47c45293e15a3ee73e4209c9b718e1224b4c89..6efaf876eb8f0481b016668233b5ddd71b23e352 100644 (file)
@@ -2,7 +2,6 @@ config LIBUKFILE
        bool "ukfile: Common support for files"
        select LIBUKATOMIC
        select LIBUKLOCK
-       select LIBUKLOCK_MUTEX
        select LIBUKLOCK_RWLOCK
        select LIBUKSCHED
        select LIBNOLIBC if !HAVE_LIBC
@@ -13,3 +12,4 @@ config LIBUKFILE_CHAINUPDATE
 
 config LIBUKFILE_FINALIZERS
        bool
+       select LIBUKLOCK_MUTEX
diff --git a/lib/ukfile/include/uk/ofile.h b/lib/ukfile/include/uk/ofile.h
deleted file mode 100644 (file)
index d552ef6..0000000
+++ /dev/null
@@ -1,31 +0,0 @@
-/* SPDX-License-Identifier: BSD-3-Clause */
-/* Copyright (c) 2023, Unikraft GmbH and The Unikraft Authors.
- * Licensed under the BSD-3-Clause License (the "License").
- * You may not use this file except in compliance with the License.
- */
-
-/* Open file description */
-
-#ifndef __UKFILE_OFILE_H__
-#define __UKFILE_OFILE_H__
-
-#include <uk/essentials.h>
-#include <uk/file.h>
-#include <uk/mutex.h>
-
-struct uk_ofile {
-       const struct uk_file *file;
-       unsigned int mode;
-       __atomic refcnt;
-       off_t pos;
-       struct uk_mutex lock; /* Lock for modifying open file state */
-};
-
-static inline
-void uk_ofile_init(struct uk_ofile *of)
-{
-       uk_refcount_init(&of->refcnt, 0);
-       uk_mutex_init(&of->lock);
-}
-
-#endif /* __UKFILE_OFILE_H__ */