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
diff --git a/src/core/libosmocore.map b/src/core/libosmocore.map
index bce3399..68f7270 100644
--- a/src/core/libosmocore.map
+++ b/src/core/libosmocore.map
@@ -258,6 +258,7 @@
 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 b0287ec..c9b0b29 100644
--- a/src/core/osmo_io.c
+++ b/src/core/osmo_io.c
@@ -128,7 +128,8 @@
 
 	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 @@
 	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 @@
 	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 bdd2ac5..cd620e7 100644
--- a/src/core/osmo_io_internal.h
+++ b/src/core/osmo_io_internal.h
@@ -44,7 +44,7 @@
 	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;