pespin has submitted this change. ( https://gerrit.osmocom.org/c/libosmocore/+/39276?usp=email )
Change subject: osmo_iofd_register: fix the case of changing fd ......................................................................
osmo_iofd_register: fix the case of changing fd
Doxygen description for this function states:
\param[in] fd the system fd number that will be registered. If you did not yet specify the file descriptor number during osmo_fd_setup(), or if it has changed since then, you can state the [new] file descriptor number as argument. If you wish to proceed with the previously specified file descriptor number, pass -1.
However, the case where a new fd is passed to osmo_iofd_register() while the structure contains an old (but unregistered) fd was not handled correctly: the code would proceed with re-registering the old fd, ignoring the newly passed one.
Fixes: df1ee8568b97dbf6d5268a83d1715a1c1fffb2de Change-Id: If8b8486ad7934afa203dfe1e996c9217373a017b --- M src/core/osmo_io.c 1 file changed, 13 insertions(+), 11 deletions(-)
Approvals: Jenkins Builder: Verified laforge: Looks good to me, but someone else must approve pespin: Looks good to me, approved
diff --git a/src/core/osmo_io.c b/src/core/osmo_io.c index e109cdf..286ae24 100644 --- a/src/core/osmo_io.c +++ b/src/core/osmo_io.c @@ -746,21 +746,23 @@ { int rc = 0;
- if (fd < 0 && iofd->fd < 0) { + if (IOFD_FLAG_ISSET(iofd, IOFD_FLAG_FD_REGISTERED)) { + /* If re-registering same fd, handle as NO-OP. + * And it is an even more explicit NO-OP + * if the caller passed in -1. */ + if (fd < 0 || fd == iofd->fd) + return 0; + /* New FD should go through unregister() first. */ + return -ENOTSUP; + } + + if (fd >= 0) + iofd->fd = fd; + if (iofd->fd < 0) { /* this might happen if both osmo_iofd_setup() and osmo_iofd_register() are called with -1 */ LOGPIO(iofd, LOGL_ERROR, "Cannot register io_fd using invalid fd == %d\n", iofd->fd); return -EBADF; } - if (fd < 0) - fd = iofd->fd; - else if (iofd->fd < 0) - iofd->fd = fd; - - if (IOFD_FLAG_ISSET(iofd, IOFD_FLAG_FD_REGISTERED)) { - /* If re-registering same fd, handle as NO-OP. - * New FD should go through unregister() first. */ - return iofd->fd == fd ? 0 : -ENOTSUP; - }
rc = osmo_iofd_ops.register_fd(iofd); if (rc)