Skip to content

Commit

Permalink
monitor: Introduce monitor_fdset_*free
Browse files Browse the repository at this point in the history
Introduce new functions to remove and free no longer used fds and
fdsets.

We need those to decouple the remove/free routines from
monitor_fdset_cleanup() which will go away in the next patches.

The new functions:

- monitor_fdset_free/_if_empty() will be used when a monitor
  connection closes and when an fd is removed to cleanup any fdset
  that is now empty.

- monitor_fdset_fd_free() will be used to remove one or more fds that
  have been explicitly targeted by qmp_remove_fd().

Reviewed-by: Peter Xu <peterx@redhat.com>
Signed-off-by: Fabiano Rosas <farosas@suse.de>
  • Loading branch information
Fabiano Rosas committed Jun 20, 2024
1 parent 1cd93fb commit a93ad56
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions monitor/fds.c
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,27 @@ int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp)
return -1;
}

static void monitor_fdset_free(MonFdset *mon_fdset)
{
QLIST_REMOVE(mon_fdset, next);
g_free(mon_fdset);
}

static void monitor_fdset_free_if_empty(MonFdset *mon_fdset)
{
if (QLIST_EMPTY(&mon_fdset->fds) && QLIST_EMPTY(&mon_fdset->dup_fds)) {
monitor_fdset_free(mon_fdset);
}
}

static void monitor_fdset_fd_free(MonFdsetFd *mon_fdset_fd)
{
close(mon_fdset_fd->fd);
g_free(mon_fdset_fd->opaque);
QLIST_REMOVE(mon_fdset_fd, next);
g_free(mon_fdset_fd);
}

static void monitor_fdset_cleanup(MonFdset *mon_fdset)
{
MonFdsetFd *mon_fdset_fd;
Expand All @@ -176,17 +197,11 @@ static void monitor_fdset_cleanup(MonFdset *mon_fdset)
if ((mon_fdset_fd->removed ||
(QLIST_EMPTY(&mon_fdset->dup_fds) && mon_refcount == 0)) &&
runstate_is_running()) {
close(mon_fdset_fd->fd);
g_free(mon_fdset_fd->opaque);
QLIST_REMOVE(mon_fdset_fd, next);
g_free(mon_fdset_fd);
monitor_fdset_fd_free(mon_fdset_fd);
}
}

if (QLIST_EMPTY(&mon_fdset->fds) && QLIST_EMPTY(&mon_fdset->dup_fds)) {
QLIST_REMOVE(mon_fdset, next);
g_free(mon_fdset);
}
monitor_fdset_free_if_empty(mon_fdset);
}

void monitor_fdsets_cleanup(void)
Expand Down

0 comments on commit a93ad56

Please sign in to comment.