summaryrefslogtreecommitdiff
path: root/Ladybird/SQLServer/main.cpp
blob: 70789c2f848f0084df59e58d35c730146cf8f819 (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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*
 * Copyright (c) 2022, Tim Flynn <trflynn89@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#define AK_DONT_REPLACE_STD

#include <AK/DeprecatedString.h>
#include <LibCore/ArgsParser.h>
#include <LibCore/Directory.h>
#include <LibCore/EventLoop.h>
#include <LibCore/StandardPaths.h>
#include <LibIPC/MultiServer.h>
#include <LibMain/Main.h>
#include <SQLServer/ConnectionFromClient.h>

ErrorOr<int> serenity_main(Main::Arguments arguments)
{
    DeprecatedString pid_file;

    Core::ArgsParser args_parser;
    args_parser.add_option(pid_file, "Path to the PID file for the SQLServer singleton process", "pid-file", 'p', "pid_file");
    args_parser.parse(arguments);

    VERIFY(!pid_file.is_empty());

    auto database_path = DeprecatedString::formatted("{}/Ladybird", Core::StandardPaths::data_directory());
    TRY(Core::Directory::create(database_path, Core::Directory::CreateDirectories::Yes));

    Core::EventLoop loop;

    auto server = TRY(IPC::MultiServer<SQLServer::ConnectionFromClient>::try_create());
    u64 connection_count { 0 };

    server->on_new_client = [&](auto& client) {
        client.set_database_path(database_path);
        ++connection_count;

        client.on_disconnect = [&]() {
            if (--connection_count == 0) {
                MUST(Core::System::unlink(pid_file));
                loop.quit(0);
            }
        };
    };

    return loop.exec();
}