Fix: unchecked return value of cds_lfht_destroy()
[lttng-tools.git] / src / bin / lttng-sessiond / tracker.c
index 909a04845c1770bef7d6d1be62cba1594d75f4da..302971767742a41f792f907484e2e91bc10e9c7f 100644 (file)
@@ -1,18 +1,8 @@
 /*
- * Copyright (C) 2018 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
+ * Copyright (C) 2018 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License, version 2 only,
- * as published by the Free Software Foundation.
+ * SPDX-License-Identifier: GPL-2.0-only
  *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
 #define _LGPL_SOURCE
@@ -178,7 +168,7 @@ int lttng_tracker_list_add(struct lttng_tracker_list *tracker_list,
                goto error;
        }
 
-       n->id = lttng_tracker_id_copy(_id);
+       n->id = lttng_tracker_id_duplicate(_id);
        if (!n->id) {
                ret = LTTNG_ERR_NOMEM;
                goto error;
@@ -235,11 +225,14 @@ end:
 
 void lttng_tracker_list_destroy(struct lttng_tracker_list *tracker_list)
 {
+       int ret;
+
        if (!tracker_list) {
                return;
        }
        lttng_tracker_list_reset(tracker_list);
-       cds_lfht_destroy(tracker_list->ht, NULL);
+       ret = cds_lfht_destroy(tracker_list->ht, NULL);
+       assert(!ret);
        free(tracker_list);
 }
 
@@ -415,12 +408,14 @@ int lttng_tracker_id_lookup_string(enum lttng_tracker_type tracker_type,
  * Protected by session mutex held by caller.
  * On success, _ids and the ids it contains must be freed by the caller.
  */
-ssize_t lttng_tracker_id_get_list(const struct lttng_tracker_list *tracker_list,
-               struct lttng_tracker_id ***_ids)
+int lttng_tracker_id_get_list(const struct lttng_tracker_list *tracker_list,
+               struct lttng_tracker_id**_ids)
 {
+       int retval = LTTNG_OK, ret;
        struct lttng_tracker_list_node *n;
-       ssize_t count = 0, i = 0, retval = 0;
-       struct lttng_tracker_id **ids;
+       ssize_t count = 0, i = 0;
+       struct lttng_tracker_ids *ids = NULL;
+       struct lttng_tracker_id *id;
        enum lttng_tracker_id_status status;
 
        switch (tracker_list->state) {
@@ -429,7 +424,7 @@ ssize_t lttng_tracker_id_get_list(const struct lttng_tracker_list *tracker_list,
                                n, &tracker_list->list_head, list_node) {
                        count++;
                }
-               ids = zmalloc(sizeof(*ids) * count);
+               ids = lttng_tracker_ids_create(count);
                if (ids == NULL) {
                        PERROR("Failed to allocate tracked ID list");
                        retval = -LTTNG_ERR_NOMEM;
@@ -437,67 +432,91 @@ ssize_t lttng_tracker_id_get_list(const struct lttng_tracker_list *tracker_list,
                }
                cds_list_for_each_entry (
                                n, &tracker_list->list_head, list_node) {
-                       ids[i] = lttng_tracker_id_copy(n->id);
-                       if (!ids[i]) {
+                       id = lttng_tracker_ids_get_pointer_of_index(ids, i);
+                       if (!id) {
+                               retval = -LTTNG_ERR_INVALID;
+                               goto error;
+                       }
+
+                       ret = lttng_tracker_id_copy(id, n->id);
+                       if (ret) {
                                retval = -LTTNG_ERR_NOMEM;
                                goto error;
                        }
                        i++;
                }
-               *_ids = ids;
-               retval = count;
                break;
        case LTTNG_TRACK_ALL:
-               ids = zmalloc(sizeof(*ids));
+
+               ids = lttng_tracker_ids_create(1);
                if (ids == NULL) {
                        PERROR("Failed to allocate tracked ID list");
                        retval = -LTTNG_ERR_NOMEM;
                        goto end;
                }
-               ids[0] = lttng_tracker_id_create();
-               status = lttng_tracker_id_set_all(ids[0]);
+
+               id = lttng_tracker_ids_get_pointer_of_index(ids, 0);
+               status = lttng_tracker_id_set_all(id);
                if (status != LTTNG_TRACKER_ID_STATUS_OK) {
                        ERR("Invalid tracker id for track all");
                        retval = -LTTNG_ERR_INVALID;
                        goto error;
                }
-               *_ids = ids;
-               retval = 1;
                break;
        case LTTNG_TRACK_NONE:
-               /* No ids track, so we return 0 element. */
-               *_ids = NULL;
+               /* No ids track, so we return 0 element collection. */
+               ids = lttng_tracker_ids_create(0);
+               if (ids == NULL) {
+                       PERROR("alloc list ids");
+                       retval = -LTTNG_ERR_NOMEM;
+                       goto end;
+               }
                break;
        }
+       *_ids = ids;
+
 end:
        return retval;
 
 error:
-       lttng_tracker_ids_destroy(ids, count);
-       free(ids);
+       lttng_tracker_ids_destroy(ids);
        return retval;
 }
 
 int lttng_tracker_id_set_list(struct lttng_tracker_list *tracker_list,
-               struct lttng_tracker_id **_ids,
-               size_t count)
+               const struct lttng_tracker_ids *ids)
 {
-       size_t i;
+       unsigned int i, count;
+       const struct lttng_tracker_id *id;
+       enum lttng_tracker_id_status status;
+
+       assert(tracker_list);
+       assert(ids);
 
        lttng_tracker_list_reset(tracker_list);
-       if (count == 1 && lttng_tracker_id_get_type(_ids[0])) {
-               /* Track all. */
-               return LTTNG_OK;
+
+       status = lttng_tracker_ids_get_count(ids, &count);
+       if (status != LTTNG_TRACKER_ID_STATUS_OK) {
+               return LTTNG_ERR_INVALID;
        }
+
        if (count == 0) {
                /* Set state to "track none". */
                tracker_list->state = LTTNG_TRACK_NONE;
                return LTTNG_OK;
        }
+
+       if (count == 1) {
+               id = lttng_tracker_ids_get_at_index(ids, 0);
+               if (lttng_tracker_id_get_type(id) == LTTNG_ID_ALL) {
+                       /* Track all. */
+                       return LTTNG_OK;
+               }
+       }
+
        for (i = 0; i < count; i++) {
-               struct lttng_tracker_id *id = _ids[i];
                int ret;
-
+               id = lttng_tracker_ids_get_at_index(ids, i);
                ret = lttng_tracker_list_add(tracker_list, id);
                if (ret != LTTNG_OK) {
                        return ret;
This page took 0.025614 seconds and 4 git commands to generate.