From 63e45e6c6ae8b223994e0fa7b080d08e33d0effa Mon Sep 17 00:00:00 2001 From: Pau Espin Pedrol Date: Fri, 16 Jun 2023 16:19:45 +0200 Subject: [PATCH] osmo_io: Make name optional, add _set_name() API This allows renaming the iofd at any later point in time. This is useful for instance if the parent object holding the iofd changes its name. Change-Id: If2772a3ccaa98616e0189862a49ab0243435e343 --- include/osmocom/core/osmo_io.h | 1 + src/core/libosmocore.map | 1 + src/core/osmo_io.c | 14 ++++++++++++-- src/core/osmo_io_internal.h | 2 +- 4 files changed, 15 insertions(+), 3 deletions(-) diff --git a/include/osmocom/core/osmo_io.h b/include/osmocom/core/osmo_io.h index d278ce037..aac25a348 100644 --- a/include/osmocom/core/osmo_io.h +++ b/include/osmocom/core/osmo_io.h @@ -88,5 +88,6 @@ void osmo_iofd_set_priv_nr(struct osmo_io_fd *iofd, unsigned int priv_nr); int osmo_iofd_get_fd(const struct osmo_io_fd *iofd); const char *osmo_iofd_get_name(const struct osmo_io_fd *iofd); +void osmo_iofd_set_name(struct osmo_io_fd *iofd, const char *name); void osmo_iofd_set_ioops(struct osmo_io_fd *iofd, const struct osmo_io_ops *ioops); diff --git a/src/core/libosmocore.map b/src/core/libosmocore.map index bce339934..68f72709c 100644 --- a/src/core/libosmocore.map +++ b/src/core/libosmocore.map @@ -258,6 +258,7 @@ osmo_iofd_free; osmo_iofd_get_data; osmo_iofd_get_fd; osmo_iofd_get_name; +osmo_iofd_set_name; osmo_iofd_get_priv_nr; osmo_iofd_init; osmo_iofd_ops; diff --git a/src/core/osmo_io.c b/src/core/osmo_io.c index b0287ec30..c9b0b29c8 100644 --- a/src/core/osmo_io.c +++ b/src/core/osmo_io.c @@ -128,7 +128,8 @@ struct msgb *iofd_msgb_alloc(struct osmo_io_fd *iofd) OSMO_ASSERT(iofd->msgb_alloc.size < 0xffff - headroom); return msgb_alloc_headroom_c(iofd->msgb_alloc.ctx, - iofd->msgb_alloc.size + headroom, headroom, iofd->name); + iofd->msgb_alloc.size + headroom, headroom, + iofd->name ? : "iofd_msgb"); } /*! return the pending msgb in iofd or NULL if there is none*/ @@ -388,7 +389,8 @@ struct osmo_io_fd *osmo_iofd_setup(const void *ctx, int fd, const char *name, en iofd->fd = fd; iofd->mode = mode; - iofd->name = talloc_strdup(iofd, name); + if (name) + iofd->name = talloc_strdup(iofd, name); if (ioops) iofd->io_ops = *ioops; @@ -578,6 +580,14 @@ const char *osmo_iofd_get_name(const struct osmo_io_fd *iofd) return iofd->name; } +/*! Set the name of the file descriptor + * \param[in] iofd the file descriptor + * \param[in] name the name to set on the file descriptor */ +void osmo_iofd_set_name(struct osmo_io_fd *iofd, const char *name) +{ + osmo_talloc_replace_string(iofd, &iofd->name, name); +} + /*! Set the osmo_io_ops for an iofd * \param[in] iofd Target iofd file descriptor * \param[in] ioops osmo_io_ops structure to be set */ diff --git a/src/core/osmo_io_internal.h b/src/core/osmo_io_internal.h index bdd2ac580..cd620e79b 100644 --- a/src/core/osmo_io_internal.h +++ b/src/core/osmo_io_internal.h @@ -44,7 +44,7 @@ struct osmo_io_fd { bool to_free; /*! human-readable name to associte with fd */ - const char *name; + char *name; /*! send/recv (msg) callback functions */ struct osmo_io_ops io_ops;