From: Andrei Tatar Date: Wed, 25 Sep 2024 14:53:47 +0000 (+0200) Subject: lib/posix-fd: Add optional name field to ofiles X-Git-Url: http://xenbits.xensource.com/gitweb?a=commitdiff_plain;h=4f02caea94fe2f77a0db2e6dc60fad0053b6e69d;p=unikraft%2Funikraft.git lib/posix-fd: Add optional name field to ofiles This change adds an optional, binary-compatible, name field to open file descriptions, allowing files to be assigned a meaningful name on open. Signed-off-by: Andrei Tatar Approved-by: Sergiu Moga Reviewed-by: Sergiu Moga GitHub-Closes: #1592 --- diff --git a/lib/posix-fd/include/uk/posix-fd.h b/lib/posix-fd/include/uk/posix-fd.h index 6532f7852..a2ead2305 100644 --- a/lib/posix-fd/include/uk/posix-fd.h +++ b/lib/posix-fd/include/uk/posix-fd.h @@ -24,8 +24,15 @@ struct uk_ofile { __atomic refcnt; size_t pos; /* Current file read/write offset position */ struct uk_mutex lock; /* Lock for modifying open file state */ + char name[]; /* Name of open file description; driver dependent */ + /* Filesystem-backed files should be named after the path they were + * opened with. Pseudo-files should be named something descriptive. + */ }; +/* Allocation size of a named uk_ofile with a name of length `namelen` */ +#define UKFD_OFILE_SIZE(namelen) (sizeof(struct uk_ofile) + (namelen) + 1) + /** * Initialize an open file description with a refcount of 1. * @@ -85,6 +92,24 @@ int uk_ofile_release(struct uk_ofile *of) /* File I/O should not use the file locks (e.g. if driver handles them) */ #define UKFD_O_NOIOLOCK 020 +/* INTERNAL. Open file description has .name field allocated */ +#define UKFD_O_NAMED 040 + +/** + * Retrieve the name of open file description `of`, or `fallback` if anonymous. + * + * @param of Open file description + * @param fallback Name to return if `of` is anonymous + * + * @return + * Name of `of` (NUL-terminated string) or `fallback` if `of` is anonymous + */ +static inline +const char *uk_ofile_name(const struct uk_ofile *of, const char *fallback) +{ + return (of->mode & UKFD_O_NAMED) ? of->name : fallback; +} + /* Event sets */ #define UKFD_POLL_ALWAYS (EPOLLERR|EPOLLHUP) #define UKFD_POLLIN (EPOLLIN|EPOLLRDNORM|EPOLLRDBAND)