From: Christian Brauner Date: Wed, 25 Aug 2021 12:38:00 +0000 (+0200) Subject: conf: port state_clients to new list type X-Git-Tag: lxc-5.0.0~103^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d2e75eba7e021ca5b7422a76af5762d51673b931;p=thirdparty%2Flxc.git conf: port state_clients to new list type Signed-off-by: Christian Brauner --- diff --git a/src/lxc/commands.c b/src/lxc/commands.c index 124b25cde..535242f10 100644 --- a/src/lxc/commands.c +++ b/src/lxc/commands.c @@ -1905,22 +1905,14 @@ static void lxc_cmd_fd_cleanup(int fd, struct lxc_handler *handler, const lxc_cmd_t cmd) { if (cmd == LXC_CMD_ADD_STATE_CLIENT) { - struct lxc_list *cur, *next; - - lxc_list_for_each_safe(cur, &handler->conf->state_clients, next) { - struct lxc_state_client *client = cur->elem; + struct lxc_state_client *client, *nclient; + list_for_each_entry_safe(client, nclient, &handler->conf->state_clients, head) { if (client->clientfd != fd) continue; - /* - * Only kick client from list so it can't be found - * anymore. The actual close happens, as for all other - * file descriptors, below. - */ - lxc_list_del(cur); - free(cur->elem); - free(cur); + list_del(&client->head); + free(client); /* * No need to walk the whole list. If we found the state diff --git a/src/lxc/commands_utils.c b/src/lxc/commands_utils.c index 4f9ee4a11..b2957b007 100644 --- a/src/lxc/commands_utils.c +++ b/src/lxc/commands_utils.c @@ -168,7 +168,6 @@ int lxc_add_state_client(int state_client_fd, struct lxc_handler *handler, lxc_state_t states[MAX_STATE]) { __do_free struct lxc_state_client *newclient = NULL; - __do_free struct lxc_list *tmplist = NULL; int state; newclient = zalloc(sizeof(*newclient)); @@ -179,14 +178,10 @@ int lxc_add_state_client(int state_client_fd, struct lxc_handler *handler, memcpy(newclient->states, states, sizeof(newclient->states)); newclient->clientfd = state_client_fd; - tmplist = zalloc(sizeof(*tmplist)); - if (!tmplist) - return -ENOMEM; - state = handler->state; if (states[state] != 1) { - lxc_list_add_elem(tmplist, move_ptr(newclient)); - lxc_list_add_tail(&handler->conf->state_clients, move_ptr(tmplist)); + list_add_tail(&newclient->head, &handler->conf->state_clients); + move_ptr(newclient); } else { TRACE("Container already in requested state"); return state; diff --git a/src/lxc/conf.c b/src/lxc/conf.c index dd75ccacf..34cb24689 100644 --- a/src/lxc/conf.c +++ b/src/lxc/conf.c @@ -3395,7 +3395,7 @@ struct lxc_conf *lxc_conf_init(void) for (i = 0; i < NUM_LXC_HOOKS; i++) lxc_list_init(&new->hooks[i]); lxc_list_init(&new->groups); - lxc_list_init(&new->state_clients); + INIT_LIST_HEAD(&new->state_clients); new->lsm_aa_profile = NULL; lxc_list_init(&new->lsm_aa_raw); new->lsm_se_context = NULL; diff --git a/src/lxc/conf.h b/src/lxc/conf.h index 786631731..e5b57737c 100644 --- a/src/lxc/conf.h +++ b/src/lxc/conf.h @@ -299,6 +299,7 @@ __hidden extern char *lxchook_names[NUM_LXC_HOOKS]; struct lxc_state_client { int clientfd; lxc_state_t states[MAX_STATE]; + struct list_head head; }; typedef enum lxc_bpf_devices_rule_t { @@ -486,7 +487,7 @@ struct lxc_conf { char *init_cwd; /* A list of clients registered to be informed about a container state. */ - struct lxc_list state_clients; + struct list_head state_clients; /* sysctls */ struct lxc_list sysctls; diff --git a/src/lxc/start.c b/src/lxc/start.c index 247dd1a18..41f820279 100644 --- a/src/lxc/start.c +++ b/src/lxc/start.c @@ -245,6 +245,7 @@ int lxc_check_inherited(struct lxc_conf *conf, bool closeall, DIR *dir; struct dirent *direntp; unsigned int listen_fds_max; + struct lxc_state_client *client, *nclient; if (conf && conf->close_all_fds) closeall = true; @@ -267,7 +268,6 @@ restart: while ((direntp = readdir(dir))) { int ret; - struct lxc_list *cur; bool matched = false; if (strequal(direntp->d_name, ".")) @@ -292,9 +292,7 @@ restart: /* Keep state clients that wait on reboots. */ if (conf) { - lxc_list_for_each(cur, &conf->state_clients) { - struct lxc_state_client *client = cur->elem; - + list_for_each_entry_safe(client, nclient, &conf->state_clients, head) { if (client->clientfd != fd) continue; @@ -472,10 +470,13 @@ static int signal_handler(int fd, uint32_t events, void *data, int lxc_serve_state_clients(const char *name, struct lxc_handler *handler, lxc_state_t state) { + struct lxc_msg msg = { + .type = lxc_msg_state, + .value = state, + }; size_t retlen; ssize_t ret; - struct lxc_list *cur, *next; - struct lxc_msg msg = {.type = lxc_msg_state, .value = state}; + struct lxc_state_client *client, *nclient; if (state == THAWED) handler->state = RUNNING; @@ -484,16 +485,14 @@ int lxc_serve_state_clients(const char *name, struct lxc_handler *handler, TRACE("Set container state to %s", lxc_state2str(state)); - if (lxc_list_empty(&handler->conf->state_clients)) + if (list_empty(&handler->conf->state_clients)) return log_trace(0, "No state clients registered"); retlen = strlcpy(msg.name, name, sizeof(msg.name)); if (retlen >= sizeof(msg.name)) return -E2BIG; - lxc_list_for_each_safe(cur, &handler->conf->state_clients, next) { - struct lxc_state_client *client = cur->elem; - + list_for_each_entry_safe(client, nclient, &handler->conf->state_clients, head) { if (client->states[state] == 0) { TRACE("State %s not registered for state client %d", lxc_state2str(state), client->clientfd); @@ -508,10 +507,9 @@ int lxc_serve_state_clients(const char *name, struct lxc_handler *handler, SYSERROR("Failed to send message to client"); /* kick client from list */ - lxc_list_del(cur); + list_del(&client->head); close(client->clientfd); - free(cur->elem); - free(cur); + free(client); } return 0; @@ -706,7 +704,7 @@ struct lxc_handler *lxc_init_handler(struct lxc_handler *old, handler->state_socket_pair[0] = -EBADF; handler->state_socket_pair[1] = -EBADF; if (handler->conf->reboot == REBOOT_NONE) - lxc_list_init(&handler->conf->state_clients); + INIT_LIST_HEAD(&handler->conf->state_clients); for (lxc_namespace_t idx = 0; idx < LXC_NS_MAX; idx++) { handler->nsfd[idx] = -EBADF; @@ -915,9 +913,9 @@ void lxc_expose_namespace_environment(const struct lxc_handler *handler) void lxc_end(struct lxc_handler *handler) { int ret; - struct lxc_list *cur, *next; const char *name = handler->name; struct cgroup_ops *cgroup_ops = handler->cgroup_ops; + struct lxc_state_client *client, *nclient; /* The STOPPING state is there for future cleanup code which can take * awhile. @@ -1009,19 +1007,16 @@ void lxc_end(struct lxc_handler *handler) /* The command socket is now closed, no more state clients can register * themselves from now on. So free the list of state clients. */ - lxc_list_for_each_safe(cur, &handler->conf->state_clients, next) { - struct lxc_state_client *client = cur->elem; - + list_for_each_entry_safe(client, nclient, &handler->conf->state_clients, head) { /* Keep state clients that want to be notified about reboots. */ if ((handler->conf->reboot > REBOOT_NONE) && (client->states[RUNNING] == 2)) continue; /* close state client socket */ - lxc_list_del(cur); + list_del(&client->head); close(client->clientfd); - free(cur->elem); - free(cur); + free(client); } if (handler->conf->ephemeral == 1 && handler->conf->reboot != REBOOT_REQ)