1 /* 2 * Copyright (C) 2019 The Android Open Source Project 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * * Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * * Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in 12 * the documentation and/or other materials provided with the 13 * distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #pragma once 30 31 #include <sys/cdefs.h> 32 #include <stdatomic.h> 33 34 #include "platform/bionic/fdtrack.h" 35 36 #include "bionic/pthread_internal.h" 37 #include "private/bionic_tls.h" 38 #include "private/ErrnoRestorer.h" 39 40 extern "C" _Atomic(android_fdtrack_hook_t) __android_fdtrack_hook; 41 42 // Macro to record file descriptor creation. 43 // e.g.: 44 // int socket(int domain, int type, int protocol) { 45 // return FDTRACK_CREATE_NAME("socket", __socket(domain, type, protocol)); 46 // } 47 #define FDTRACK_CREATE_NAME(name, fd_value) \ 48 ({ \ 49 int __fd = (fd_value); \ 50 if (__fd != -1 && __predict_false(__android_fdtrack_hook) && \ 51 !__predict_false(__get_thread()->is_vforked())) { \ 52 bionic_tls& tls = __get_bionic_tls(); \ 53 /* fdtrack_disabled is only true during reentrant calls. */ \ 54 if (!__predict_false(tls.fdtrack_disabled)) { \ 55 ErrnoRestorer r; \ 56 tls.fdtrack_disabled = true; \ 57 android_fdtrack_event event; \ 58 event.fd = __fd; \ 59 event.type = ANDROID_FDTRACK_EVENT_TYPE_CREATE; \ 60 event.data.create.function_name = name; \ 61 __android_fdtrack_hook(&event); \ 62 tls.fdtrack_disabled = false; \ 63 } \ 64 } \ 65 __fd; \ 66 }) 67 68 // Macro to record file descriptor creation, with the current function's name. 69 // e.g.: 70 // int socket(int domain, int type, int protocol) { 71 // return FDTRACK_CREATE_NAME(__socket(domain, type, protocol)); 72 // } 73 #define FDTRACK_CREATE(fd_value) FDTRACK_CREATE_NAME(__func__, (fd_value)) 74 75 // Macro to record file descriptor closure. 76 // Note that this does not actually close the file descriptor. 77 #define FDTRACK_CLOSE(fd_value) \ 78 ({ \ 79 int __fd = (fd_value); \ 80 if (__fd != -1 && __predict_false(__android_fdtrack_hook) && \ 81 !__predict_false(__get_thread()->is_vforked())) { \ 82 bionic_tls& tls = __get_bionic_tls(); \ 83 if (!__predict_false(tls.fdtrack_disabled)) { \ 84 int saved_errno = errno; \ 85 tls.fdtrack_disabled = true; \ 86 android_fdtrack_event event; \ 87 event.fd = __fd; \ 88 event.type = ANDROID_FDTRACK_EVENT_TYPE_CLOSE; \ 89 __android_fdtrack_hook(&event); \ 90 tls.fdtrack_disabled = false; \ 91 errno = saved_errno; \ 92 } \ 93 } \ 94 __fd; \ 95 }) 96