From 97d09008353b0fcab7a107dee2341bf390f4e088 Mon Sep 17 00:00:00 2001 From: ReenigneArcher <42013603+ReenigneArcher@users.noreply.github.com> Date: Mon, 12 Oct 2026 01:16:10 -0400 Subject: [PATCH] refactor: replace deprecated native API calls --- src/platform/linux/kmsgrab.cpp | 71 ++++++++++++++------------------- src/platform/linux/misc.cpp | 21 +++++----- src/platform/linux/pipewire.cpp | 12 +++--- src/platform/windows/misc.cpp | 5 ++- 4 files changed, 53 insertions(+), 56 deletions(-) diff --git a/src/platform/linux/kmsgrab.cpp b/src/platform/linux/kmsgrab.cpp index 6c8d9c8ce..7671dbc4a 100644 --- a/src/platform/linux/kmsgrab.cpp +++ b/src/platform/linux/kmsgrab.cpp @@ -1,6 +1,7 @@ /** * @file src/platform/linux/kmsgrab.cpp * @brief Definitions for KMS screen capture. + * @note POSIX diagnostics preserve the native errno before logging and format owned error messages. */ // standard includes #include @@ -11,6 +12,7 @@ #include #include #include +#include #include #include @@ -525,9 +527,8 @@ namespace platf { for (; plane_p != end; ++plane_p) { plane_t plane = drmModeGetPlane(fd, *plane_p); if (!plane) { - logging::error([this](std::ostream &out) { - out << "Couldn't get drm plane ["sv << (end - plane_p) << "]: "sv << strerror(errno); - }); + const auto error = std::error_code(errno, std::generic_category()); + logging::error("Couldn't get drm plane [{}]: {}", (end - plane_p), error.message()); continue; } @@ -709,6 +710,8 @@ namespace platf { /** * @brief Open a DRM connector and cache its monitor metadata. * + * Render nodes retain read/write access; a duplicate of the primary node is used if the render node cannot be opened. + * * @param settings Immutable startup settings for this host. * @param path Filesystem path for the DRM device or resource. * @param window_system Desktop protocol used to select DRM access behavior. @@ -728,11 +731,10 @@ namespace platf { char *rendernode_path = drmGetRenderDeviceNameFromFd(fd.el); if (rendernode_path) { logging::debug("Opening render node: {}", rendernode_path); - render_fd.el = open(rendernode_path, O_RDWR); + render_fd.el = openat(AT_FDCWD, rendernode_path, O_RDWR); if (render_fd.el < 0) { - logging::warning([&rendernode_path](std::ostream &out) { - out << "Couldn't open render node: "sv << rendernode_path << ": "sv << strerror(errno); - }); + const auto error = std::error_code(errno, std::generic_category()); + logging::warning("Couldn't open render node: {}: {}", rendernode_path, error.message()); render_fd.el = dup(fd.el); } free(rendernode_path); @@ -1238,9 +1240,8 @@ namespace platf { auto fb = card.fb(plane.get()); if (!fb) { - logging::error([&plane](std::ostream &out) { - out << "Couldn't get drm fb for plane ["sv << plane->fb_id << "]: "sv << strerror(errno); - }); + const auto error = std::error_code(errno, std::generic_category()); + logging::error("Couldn't get drm fb for plane [{}]: {}", plane->fb_id, error.message()); return -1; } @@ -1256,18 +1257,16 @@ namespace platf { auto fb_fd = card.handleFD(fb->handles[i]); if (fb_fd.el < 0) { - logging::error([&fb](std::ostream &out) { - out << "Couldn't get primary file descriptor for Framebuffer ["sv << fb->fb_id << "]: "sv << strerror(errno); - }); + const auto error = std::error_code(errno, std::generic_category()); + logging::error("Couldn't get primary file descriptor for Framebuffer [{}]: {}", fb->fb_id, error.message()); continue; } } auto crtc = card.crtc(plane->crtc_id); if (!crtc) { - logging::error([](std::ostream &out) { - out << "Couldn't get CRTC info: "sv << strerror(errno); - }); + const auto error = std::error_code(errno, std::generic_category()); + logging::error("Couldn't get CRTC info: {}", error.message()); continue; } @@ -1411,9 +1410,8 @@ namespace platf { prop_blob_t hdr_metadata_blob = drmModeGetPropertyBlob(card.fd.el, *hdr_metadata_blob_id); if (hdr_metadata_blob == nullptr) { - logging::error([](std::ostream &out) { - out << "Unable to get HDR metadata blob: "sv << strerror(errno); - }); + const auto error = std::error_code(errno, std::generic_category()); + logging::error("Unable to get HDR metadata blob: {}", error.message()); return false; } @@ -1472,9 +1470,8 @@ namespace platf { prop_blob_t hdr_metadata_blob = drmModeGetPropertyBlob(card.fd.el, *hdr_metadata_blob_id); if (hdr_metadata_blob == nullptr) { - logging::error([](std::ostream &out) { - out << "Unable to get HDR metadata blob: "sv << strerror(errno); - }); + const auto error = std::error_code(errno, std::generic_category()); + logging::error("Unable to get HDR metadata blob: {}", error.message()); return false; } @@ -1626,9 +1623,8 @@ namespace platf { drm_mode_map_dumb map = {}; map.handle = fb->handles[0]; if (drmIoctl(card.fd.el, DRM_IOCTL_MODE_MAP_DUMB, &map) < 0) { - logging::error([](std::ostream &out) { - out << "Failed to map cursor FB as dumb buffer: "sv << strerror(errno); - }); + const auto error = std::error_code(errno, std::generic_category()); + logging::error("Failed to map cursor FB as dumb buffer: {}", error.message()); captured_cursor.visible = false; return; } @@ -1637,9 +1633,8 @@ namespace platf { } if (mapped_data == MAP_FAILED) { - logging::error([](std::ostream &out) { - out << "Failed to mmap cursor FB: "sv << strerror(errno); - }); + const auto error = std::error_code(errno, std::generic_category()); + logging::error("Failed to mmap cursor FB: {}", error.message()); captured_cursor.visible = false; return; } @@ -1722,9 +1717,8 @@ namespace platf { auto fb = card.fb(plane.get()); if (!fb) { // This can happen if the display is being reconfigured while streaming - logging::warning([&plane](std::ostream &out) { - out << "Couldn't get drm fb for plane ["sv << plane->fb_id << "]: "sv << strerror(errno); - }); + const auto error = std::error_code(errno, std::generic_category()); + logging::warning("Couldn't get drm fb for plane [{}]: {}", plane->fb_id, error.message()); return capture_e::timeout; } @@ -1746,9 +1740,8 @@ namespace platf { file[y] = card.handleFD(fb->handles[y]); if (file[y].el < 0) { - logging::error([&](std::ostream &out) { - out << "Couldn't get primary file descriptor for Framebuffer ["sv << fb->fb_id << "]: "sv << strerror(errno); - }); + const auto error = std::error_code(errno, std::generic_category()); + logging::error("Couldn't get primary file descriptor for Framebuffer [{}]: {}", fb->fb_id, error.message()); return capture_e::error; } @@ -2421,9 +2414,8 @@ namespace platf { auto fb = card.fb(plane.get()); if (!fb) { - logging::error([&plane](std::ostream &out) { - out << "Couldn't get drm fb for plane ["sv << plane->fb_id << "]: "sv << strerror(errno); - }); + const auto error = std::error_code(errno, std::generic_category()); + logging::error("Couldn't get drm fb for plane [{}]: {}", plane->fb_id, error.message()); continue; } @@ -2440,9 +2432,8 @@ namespace platf { // This appears to return the offset of the monitor auto crtc = card.crtc(plane->crtc_id); if (!crtc) { - logging::error([](std::ostream &out) { - out << "Couldn't get CRTC info: "sv << strerror(errno); - }); + const auto error = std::error_code(errno, std::generic_category()); + logging::error("Couldn't get CRTC info: {}", error.message()); continue; } diff --git a/src/platform/linux/misc.cpp b/src/platform/linux/misc.cpp index 596400af3..aad1c2015 100644 --- a/src/platform/linux/misc.cpp +++ b/src/platform/linux/misc.cpp @@ -20,6 +20,7 @@ #include #include #include +#include // platform includes #include @@ -538,6 +539,7 @@ namespace platf { /** * @brief Apply the requested scheduling priority to the current thread. + * @details Preserve failed system calls' errno values before logging diagnostics. */ void adjust_thread_priority(thread_priority_e priority) { #if defined(__FreeBSD__) @@ -597,9 +599,8 @@ namespace platf { if (!success) { // This will run on FreeBSD OR Linux if RTKit failed/was missing if (setpriority(PRIO_PROCESS, 0, linux_nice) == -1) { - logging::warning([&linux_nice](std::ostream &out) { - out << "setpriority failed for nice "sv << linux_nice << ": "sv << strerror(errno); - }); + const std::error_code error {errno, std::generic_category()}; + logging::warning("setpriority failed for nice {}: {}", linux_nice, error.message()); } else { logging::debug("setpriority success for nice {}", linux_nice); } @@ -1767,6 +1768,10 @@ namespace platf { return false; } + /** + * @brief Drop selected effective and permitted capabilities and restore dumpability. + * @details Capture each failed system call's errno before formatting its diagnostic. + */ void drop_elevated_privileges(bool all_caps) { #if !defined(__FreeBSD__) bool failed = false; @@ -1781,18 +1786,16 @@ namespace platf { cap_set_flag(caps, CAP_PERMITTED, caps_to_drop.size(), caps_to_drop.data(), CAP_CLEAR); if (cap_set_proc(caps) != 0) { - logging::error([](std::ostream &out) { - out << "[misc] drop_elevated_privileges failed to prune capabilities: "sv << std::strerror(errno); - }); + const std::error_code error {errno, std::generic_category()}; + logging::error("[misc] drop_elevated_privileges failed to prune capabilities: {}", error.message()); failed = true; } cap_free(caps); // Reset dumpable AFTER the caps have been pruned to ensure /proc/pid/root is accessible. if (prctl(PR_SET_DUMPABLE, 1) != 0) { - logging::error([](std::ostream &out) { - out << "[misc] drop_elevated_privileges failed to set PR_SET_DUMPABLE: "sv << std::strerror(errno); - }); + const std::error_code error {errno, std::generic_category()}; + logging::error("[misc] drop_elevated_privileges failed to set PR_SET_DUMPABLE: {}", error.message()); failed = true; } if (!failed) { diff --git a/src/platform/linux/pipewire.cpp b/src/platform/linux/pipewire.cpp index 26c2eede5..4d8785d2e 100644 --- a/src/platform/linux/pipewire.cpp +++ b/src/platform/linux/pipewire.cpp @@ -4,6 +4,7 @@ */ // standard includes #include +#include // lib includes #include @@ -264,6 +265,7 @@ namespace pipewire { /** * @brief Initialize PipeWire core objects and optional stream negotiation. + * @details Capture PipeWire failures' errno values before formatting diagnostics. * * @param stream_fd Stream fd. * @param stream_node Stream node. @@ -290,15 +292,13 @@ namespace pipewire { if (core) { pw_core_add_listener(core, &core_listener, &core_events, nullptr); } else { - logging::debug([](std::ostream &out) { - out << "[pipewire] Failed to connect to PW core. Error: "sv << errno << "(" << strerror(errno) << ")"sv; - }); + const std::error_code error {errno, std::generic_category()}; + logging::debug("[pipewire] Failed to connect to PW core. Error: {}({})", error.value(), error.message()); return -1; } } else { - logging::debug([](std::ostream &out) { - out << "[pipewire] Failed to setup PW context. Error: "sv << errno << "(" << strerror(errno) << ")"sv; - }); + const std::error_code error {errno, std::generic_category()}; + logging::debug("[pipewire] Failed to setup PW context. Error: {}({})", error.value(), error.message()); return -1; } diff --git a/src/platform/windows/misc.cpp b/src/platform/windows/misc.cpp index 1326f1760..d729f0244 100644 --- a/src/platform/windows/misc.cpp +++ b/src/platform/windows/misc.cpp @@ -3,6 +3,7 @@ * @brief Miscellaneous definitions for Windows. */ // standard includes +#include #include #include #include @@ -891,7 +892,9 @@ namespace platf { // FIXME: Maybe we can improve this in the future. if (res == HRESULT_FROM_WIN32(ERROR_NO_ASSOCIATION)) { logging::warning("Using trampoline to handle target: {}", raw_cmd); - std::wcscpy(shell_command_string.data(), L"cmd.exe /c start \"\" /wait \"%1\" %*"); + constexpr WCHAR trampoline[] = L"cmd.exe /c start \"\" /wait \"%1\" %*"; ///< Shell trampoline including its terminating wide NUL. + static_assert(std::size(trampoline) <= MAX_PATH); + std::copy(std::begin(trampoline), std::end(trampoline), shell_command_string.begin()); needs_cmd_escaping = true; // We must suppress the console window that would otherwise appear when starting cmd.exe.