Fix: liblttng-ctl comm: lttng_event is not packed
[lttng-tools.git] / src / common / userspace-probe.c
index 6331ff13574e9f03e14a4dc426284fe97390e526..e85c3c56fc67eea6163b5442694a1855332892ad 100644 (file)
@@ -1,18 +1,8 @@
 /*
- * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
+ * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
  *
- * This library is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License, version 2.1 only,
- * as published by the Free Software Foundation.
+ * SPDX-License-Identifier: LGPL-2.1-only
  *
- * This library 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 Lesser General Public License
- * for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this library; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
 #include <assert.h>
@@ -276,6 +266,7 @@ lttng_userspace_probe_location_tracepoint_create_no_check(const char *binary_pat
 error:
        free(probe_name_copy);
        free(provider_name_copy);
+       free(binary_path_copy);
        if (binary_fd >= 0) {
                if (close(binary_fd)) {
                        PERROR("Error closing binary fd in error path");
@@ -404,7 +395,7 @@ lttng_userspace_probe_location_function_copy(
        struct lttng_userspace_probe_location_lookup_method *lookup_method = NULL;
        const char *binary_path = NULL;
        const char *function_name = NULL;
-       int fd;
+       int fd, new_fd;
 
        assert(location);
        assert(location->type == LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION);
@@ -422,11 +413,20 @@ lttng_userspace_probe_location_function_copy(
                goto error;
        }
 
-       /* Duplicate the binary fd */
-       fd = dup(lttng_userspace_probe_location_function_get_binary_fd(location));
-       if (fd == -1) {
-               PERROR("Error duplicating file descriptor to binary");
-               goto error;
+       /*
+        * Duplicate the binary fd if possible. The binary fd can be -1 on
+        * listing
+        */
+       fd = lttng_userspace_probe_location_function_get_binary_fd(location);
+       if (fd > -1) {
+               new_fd = dup(fd);
+               if (new_fd == -1) {
+                       PERROR("Error duplicating file descriptor to binary");
+                       goto error;
+               }
+       } else {
+               /* The original fd is not set. */
+               new_fd = -1;
        }
 
        /*
@@ -456,7 +456,7 @@ lttng_userspace_probe_location_function_copy(
        }
 
        /* Set the duplicated fd to the new probe_location */
-       if (lttng_userspace_probe_location_function_set_binary_fd(new_location, fd) < 0) {
+       if (lttng_userspace_probe_location_function_set_binary_fd(new_location, new_fd) < 0) {
                goto destroy_probe_location;
        }
 
@@ -467,7 +467,7 @@ destroy_probe_location:
 destroy_lookup_method:
        lttng_userspace_probe_location_lookup_method_destroy(lookup_method);
 close_fd:
-       if (close(fd) < 0) {
+       if (close(new_fd) < 0) {
                PERROR("Error closing duplicated file descriptor in error path");
        }
 error:
@@ -486,7 +486,7 @@ lttng_userspace_probe_location_tracepoint_copy(
        const char *binary_path = NULL;
        const char *probe_name = NULL;
        const char *provider_name = NULL;
-       int fd;
+       int fd, new_fd;
 
        assert(location);
        assert(location->type == LTTNG_USERSPACE_PROBE_LOCATION_TYPE_TRACEPOINT);
@@ -511,10 +511,16 @@ lttng_userspace_probe_location_tracepoint_copy(
        }
 
        /* Duplicate the binary fd */
-       fd = dup(lttng_userspace_probe_location_tracepoint_get_binary_fd(location));
-       if (fd == -1) {
-               PERROR("Error duplicating file descriptor to binary");
-               goto error;
+       fd = lttng_userspace_probe_location_tracepoint_get_binary_fd(location);
+       if (fd > -1) {
+               new_fd = dup(fd);
+               if (new_fd == -1) {
+                       PERROR("Error duplicating file descriptor to binary");
+                       goto error;
+               }
+       } else {
+               /* The original fd is not set. */
+               new_fd = -1;
        }
 
        /*
@@ -544,7 +550,7 @@ lttng_userspace_probe_location_tracepoint_copy(
        }
 
        /* Set the duplicated fd to the new probe_location */
-       if (lttng_userspace_probe_location_tracepoint_set_binary_fd(new_location, fd) < 0) {
+       if (lttng_userspace_probe_location_tracepoint_set_binary_fd(new_location, new_fd) < 0) {
                goto destroy_probe_location;
        }
 
@@ -555,7 +561,7 @@ destroy_probe_location:
 destroy_lookup_method:
        lttng_userspace_probe_location_lookup_method_destroy(lookup_method);
 close_fd:
-       if (close(fd) < 0) {
+       if (close(new_fd) < 0) {
                PERROR("Error closing duplicated file descriptor in error path");
        }
 error:
@@ -1093,12 +1099,14 @@ int lttng_userspace_probe_location_function_create_from_buffer(
        function_name = lttng_strndup(function_name_src, LTTNG_SYMBOL_NAME_LEN);
        if (!function_name) {
                PERROR("lttng_strndup");
+               ret = -LTTNG_ERR_NOMEM;
                goto end;
        }
 
        binary_path = lttng_strndup(binary_path_src, LTTNG_PATH_MAX);
        if (!binary_path) {
                PERROR("lttng_strndup");
+               ret = -LTTNG_ERR_NOMEM;
                goto end;
        }
 
@@ -1166,18 +1174,21 @@ int lttng_userspace_probe_location_tracepoint_create_from_buffer(
 
        probe_name = lttng_strndup(probe_name_src, LTTNG_SYMBOL_NAME_LEN);
        if (!probe_name) {
-               PERROR("lttng_strndup");
+               PERROR("Failed to allocate probe name");
+               ret = -LTTNG_ERR_INVALID;
                goto end;
        }
        provider_name = lttng_strndup(provider_name_src, LTTNG_SYMBOL_NAME_LEN);
        if (!provider_name) {
-               PERROR("lttng_strndup");
+               PERROR("Failed to allocate provider name");
+               ret = -LTTNG_ERR_INVALID;
                goto end;
        }
 
-       binary_path = lttng_strndup(binary_path_src, LTTNG_SYMBOL_NAME_LEN);
+       binary_path = lttng_strndup(binary_path_src, LTTNG_PATH_MAX);
        if (!binary_path) {
-               PERROR("lttng_strndup");
+               PERROR("Failed to allocate binary path");
+               ret = -LTTNG_ERR_INVALID;
                goto end;
        }
 
This page took 0.025531 seconds and 4 git commands to generate.