|
| 1 | +/* |
| 2 | + * Copyright (C) 2025 Apple Inc. All rights reserved. |
| 3 | + * |
| 4 | + * Redistribution and use in source and binary forms, with or without |
| 5 | + * modification, are permitted provided that the following conditions |
| 6 | + * are met: |
| 7 | + * 1. Redistributions of source code must retain the above copyright |
| 8 | + * notice, this list of conditions and the following disclaimer. |
| 9 | + * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | + * notice, this list of conditions and the following disclaimer in the |
| 11 | + * documentation and/or other materials provided with the distribution. |
| 12 | + * |
| 13 | + * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' |
| 14 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 15 | + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 | + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
| 17 | + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 18 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 19 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 20 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 21 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 22 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 23 | + * THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | + */ |
| 25 | + |
| 26 | +#import "config.h" |
| 27 | +#import "LaunchLogHook.h" |
| 28 | + |
| 29 | +#import "LaunchLogMessages.h" |
| 30 | +#import "Logging.h" |
| 31 | +#import "XPCEndpoint.h" |
| 32 | +#import <wtf/BlockPtr.h> |
| 33 | +#import <wtf/SystemFree.h> |
| 34 | +#import <wtf/spi/cocoa/OSLogSPI.h> |
| 35 | + |
| 36 | +#if ENABLE(LOGD_BLOCKING_IN_WEBCONTENT) |
| 37 | + |
| 38 | +namespace WebKit { |
| 39 | + |
| 40 | +LaunchLogHook& LaunchLogHook::singleton() |
| 41 | +{ |
| 42 | + static std::once_flag onceKey; |
| 43 | + static LazyNeverDestroyed<LaunchLogHook> logHook; |
| 44 | + std::call_once(onceKey, [] { |
| 45 | + logHook.construct(); |
| 46 | + }); |
| 47 | + return logHook.get(); |
| 48 | +} |
| 49 | + |
| 50 | +void LaunchLogHook::initialize(xpc_connection_t connection) |
| 51 | +{ |
| 52 | + m_connection = connection; |
| 53 | + |
| 54 | + static os_log_hook_t prevHook = nullptr; |
| 55 | + |
| 56 | + auto blockPtr = makeBlockPtr([](os_log_type_t type, os_log_message_t msg) { |
| 57 | + if (prevHook) |
| 58 | + prevHook(type, msg); |
| 59 | + |
| 60 | + if (!LaunchLogHook::singleton().m_connection) |
| 61 | + return; |
| 62 | + |
| 63 | + if (type & OS_LOG_TYPE_DEBUG) |
| 64 | + return; |
| 65 | + |
| 66 | + if (type == OS_LOG_TYPE_FAULT) |
| 67 | + type = OS_LOG_TYPE_ERROR; |
| 68 | + |
| 69 | + if (auto messageString = WTF::adoptSystem<char[]>(os_log_copy_message_string(msg))) { |
| 70 | + auto message = adoptOSObject(xpc_dictionary_create(nullptr, nullptr, 0)); |
| 71 | + xpc_dictionary_set_string(message.get(), XPCEndpoint::xpcMessageNameKey, logMessageName); |
| 72 | + if (auto* subsystem = msg->subsystem) |
| 73 | + xpc_dictionary_set_string(message.get(), subsystemKey, subsystem); |
| 74 | + if (auto* category = msg->category) |
| 75 | + xpc_dictionary_set_string(message.get(), categoryKey, category); |
| 76 | + xpc_dictionary_set_string(message.get(), messageStringKey, messageString.get()); |
| 77 | + xpc_dictionary_set_uint64(message.get(), logTypeKey, type); |
| 78 | + xpc_connection_send_message(LaunchLogHook::singleton().m_connection.get(), message.get()); |
| 79 | + } |
| 80 | + }); |
| 81 | + |
| 82 | + prevHook = os_log_set_hook(OS_LOG_TYPE_DEFAULT, blockPtr.get()); |
| 83 | + RELEASE_LOG(Process, "Installed launch log hook"); |
| 84 | +} |
| 85 | + |
| 86 | +void LaunchLogHook::disable() |
| 87 | +{ |
| 88 | + RELEASE_LOG(Process, "Disabling launch log hook"); |
| 89 | + auto message = adoptOSObject(xpc_dictionary_create(nullptr, nullptr, 0)); |
| 90 | + xpc_dictionary_set_string(message.get(), XPCEndpoint::xpcMessageNameKey, disableLogMessageName); |
| 91 | + xpc_connection_send_message(m_connection.get(), message.get()); |
| 92 | + m_connection = nullptr; |
| 93 | +} |
| 94 | + |
| 95 | +} // namespace WebKit |
| 96 | + |
| 97 | +#endif |
0 commit comments