summaryrefslogtreecommitdiff
path: root/Ladybird/HelperProcess.cpp
blob: 96128f259a46e240e65c27708e379378c482ae40 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/*
 * Copyright (c) 2023, Andrew Kaster <akaster@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include "HelperProcess.h"
#include "Utilities.h"
#include <AK/String.h>
#include <QCoreApplication>

ErrorOr<void> spawn_helper_process(StringView process_name, ReadonlySpan<StringView> arguments, Core::System::SearchInPath search_in_path, Optional<ReadonlySpan<StringView>> environment)
{
    auto paths = TRY(get_paths_for_helper_process(process_name));
    VERIFY(!paths.is_empty());
    ErrorOr<void> result;
    for (auto const& path : paths) {
        result = Core::System::exec(path, arguments, search_in_path, environment);
        if (!result.is_error())
            break;
    }

    return result;
}

ErrorOr<Vector<String>> get_paths_for_helper_process(StringView process_name)
{
    Vector<String> paths;
    TRY(paths.try_append(TRY(String::formatted("./{}/{}", process_name, process_name))));
    TRY(paths.try_append(TRY(String::formatted("{}/{}", TRY(ak_string_from_qstring(QCoreApplication::applicationDirPath())), process_name))));
    TRY(paths.try_append(TRY(String::formatted("./{}", process_name))));
    // NOTE: Add platform-specific paths here
    return paths;
}