]> git.lttng.org Git - lttng-ust.git/commitdiff
Fix: stream fd leaks on error
authorMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Tue, 15 Dec 2020 14:01:38 +0000 (09:01 -0500)
committerMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Wed, 24 Feb 2021 15:17:05 +0000 (10:17 -0500)
Use a regular pattern for all commands:

If the command callback takes ownership of a pointer or file descriptor,
it sets them to NULL or -1. Therefore, the caller can always try to free
the pointer, or close it if it is greater or equal to 0.

This eliminates memory and fd leaks on error.

Change-Id: I447129ab1672ce4fc6cf3c0baf18dbf27bfcfaf8
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
liblttng-ust/lttng-ust-abi.c
liblttng-ust/lttng-ust-comm.c

index daf0d5e4caa0185652605e9a0496c2ad0fb2f0b3..1bf7a5aeab03a84b09c05dda0fdfe9ab8b7ead97 100644 (file)
@@ -772,6 +772,9 @@ int lttng_abi_map_stream(int channel_objd, struct lttng_ust_stream *info,
                info->stream_nr, info->len);
        if (ret)
                goto error_add_stream;
+       /* Take ownership of shm_fd and wakeup_fd. */
+       uargs->stream.shm_fd = -1;
+       uargs->stream.wakeup_fd = -1;
 
        return 0;
 
index 18063ffed722523c212d0db796831396a3a36aab..d3ef8a26d7b6a6d6757f1e3ce74720ae605e5b6e 100644 (file)
@@ -938,6 +938,8 @@ int handle_message(struct sock_info *sock_info,
        }
        case LTTNG_UST_STREAM:
        {
+               int close_ret;
+
                /* Receive shm_fd, wakeup_fd */
                ret = ustcomm_recv_stream_from_sessiond(sock,
                        NULL,
@@ -953,6 +955,22 @@ int handle_message(struct sock_info *sock_info,
                                        &args, sock_info);
                else
                        ret = -ENOSYS;
+               if (args.stream.shm_fd >= 0) {
+                       lttng_ust_lock_fd_tracker();
+                       close_ret = close(args.stream.shm_fd);
+                       lttng_ust_unlock_fd_tracker();
+                       args.stream.shm_fd = -1;
+                       if (close_ret)
+                               PERROR("close");
+               }
+               if (args.stream.wakeup_fd >= 0) {
+                       lttng_ust_lock_fd_tracker();
+                       close_ret = close(args.stream.wakeup_fd);
+                       lttng_ust_unlock_fd_tracker();
+                       args.stream.wakeup_fd = -1;
+                       if (close_ret)
+                               PERROR("close");
+               }
                break;
        }
        case LTTNG_UST_CONTEXT:
This page took 0.029795 seconds and 4 git commands to generate.