]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
conf: port state_clients to new list type 3950/head
authorChristian Brauner <christian.brauner@ubuntu.com>
Wed, 25 Aug 2021 12:38:00 +0000 (14:38 +0200)
committerChristian Brauner <christian.brauner@ubuntu.com>
Wed, 25 Aug 2021 13:30:01 +0000 (15:30 +0200)
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
src/lxc/commands.c
src/lxc/commands_utils.c
src/lxc/conf.c
src/lxc/conf.h
src/lxc/start.c

index 124b25cdee4e60cc94c8b24e21103dd0c0d0c9c1..535242f10ca97a959a30796980b6f98613cd1f0a 100644 (file)
@@ -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
index 4f9ee4a11b27938dbc5dd7d70569ba5f8527e5b6..b2957b00756edc9dc1775c30318c85662207a583 100644 (file)
@@ -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;
index dd75ccacf4a8392670f281066e62621a2bacb262..34cb246896940bcccbff2008114952b7f8cd48c6 100644 (file)
@@ -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;
index 786631731e8332924bf34186a9db06eb42cb6fd0..e5b57737c990be99cfa0e17b0e85bf20495c82cc 100644 (file)
@@ -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;
index 247dd1a18c2ae129c04de5afedaee639ae9e61a6..41f820279d9912a776f672adb01cc5c7bc3b2d15 100644 (file)
@@ -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)