From 8c0723960b3a27fa245bd96fb69c630f4dcf5083 Mon Sep 17 00:00:00 2001 From: Brendan Coles Date: Fri, 12 Mar 2021 12:25:38 +0000 Subject: [PATCH] QuickShow: Replace posix_spawn with LibDesktop::Launcher When multiple images are dragged and dropped onto the image widget, QuickShow will use LibDesktop::Launcher to launch a new instance of QuickShow for each item, rather than spawn a child QuickShow process for each item with posix_spawn. This allows `proc` and `exec` pledges to be removed :^) --- .../Applications/QuickShow/CMakeLists.txt | 2 +- Userland/Applications/QuickShow/main.cpp | 42 ++++++++++--------- 2 files changed, 24 insertions(+), 20 deletions(-) diff --git a/Userland/Applications/QuickShow/CMakeLists.txt b/Userland/Applications/QuickShow/CMakeLists.txt index 8d2b46e217b..7fa51f67d8d 100644 --- a/Userland/Applications/QuickShow/CMakeLists.txt +++ b/Userland/Applications/QuickShow/CMakeLists.txt @@ -4,4 +4,4 @@ set(SOURCES ) serenity_app(QuickShow ICON filetype-image) -target_link_libraries(QuickShow LibGUI LibGfx) +target_link_libraries(QuickShow LibDesktop LibGUI LibGfx) diff --git a/Userland/Applications/QuickShow/main.cpp b/Userland/Applications/QuickShow/main.cpp index 6ed4df5585b..1130729c418 100644 --- a/Userland/Applications/QuickShow/main.cpp +++ b/Userland/Applications/QuickShow/main.cpp @@ -28,6 +28,7 @@ #include #include #include +#include #include #include #include @@ -45,24 +46,33 @@ #include #include #include -#include #include #include int main(int argc, char** argv) { - if (pledge("stdio recvfd sendfd accept cpath rpath wpath unix cpath fattr proc exec thread", nullptr) < 0) { + if (pledge("stdio recvfd sendfd accept rpath wpath cpath unix fattr thread", nullptr) < 0) { perror("pledge"); return 1; } auto app = GUI::Application::construct(argc, argv); - if (pledge("stdio recvfd sendfd accept cpath rpath wpath proc exec thread", nullptr) < 0) { + if (pledge("stdio recvfd sendfd accept cpath rpath wpath unix thread", nullptr) < 0) { perror("pledge"); return 1; } + if (!Desktop::Launcher::add_allowed_handler_with_any_url("/bin/QuickShow")) { + warnln("Failed to set up allowed launch URLs"); + return 1; + } + + if (!Desktop::Launcher::seal_allowlist()) { + warnln("Failed to seal allowed launch URLs"); + return 1; + } + auto app_icon = GUI::Icon::default_icon("filetype-image"); const char* path = nullptr; @@ -106,24 +116,18 @@ int main(int argc, char** argv) widget.on_drop = [&](auto& event) { window->move_to_front(); - if (event.mime_data().has_urls()) { - auto urls = event.mime_data().urls(); + if (!event.mime_data().has_urls()) + return; - if (!urls.is_empty()) { - auto url = urls.first(); - widget.load_from_file(url.path()); - } + auto urls = event.mime_data().urls(); - pid_t child; - for (size_t i = 1; i < urls.size(); ++i) { - const char* argv[] = { "/bin/QuickShow", urls[i].path().characters(), nullptr }; - if ((errno = posix_spawn(&child, "/bin/QuickShow", nullptr, nullptr, const_cast(argv), environ))) { - perror("posix_spawn"); - } else { - if (disown(child) < 0) - perror("disown"); - } - } + if (urls.is_empty()) + return; + + widget.load_from_file(urls.first().path()); + + for (size_t i = 1; i < urls.size(); ++i) { + Desktop::Launcher::open(URL::create_with_file_protocol(urls[i].path().characters()), "/bin/QuickShow"); } }; widget.on_doubleclick = [&] {