summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibSQL/SQLClient.cpp
blob: dd6695c1564afd45ce6022b97ba172bfa22e9b7c (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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*
 * Copyright (c) 2021, Jan de Visser <jan@de-visser.net>
 * Copyright (c) 2022, the SerenityOS developers.
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <AK/DeprecatedString.h>
#include <AK/String.h>
#include <LibSQL/SQLClient.h>

#if !defined(AK_OS_SERENITY)
#    include <LibCore/DeprecatedFile.h>
#    include <LibCore/Directory.h>
#    include <LibCore/SocketAddress.h>
#    include <LibCore/StandardPaths.h>
#    include <LibCore/System.h>
#endif

namespace SQL {

#if !defined(AK_OS_SERENITY)

// This is heavily based on how SystemServer's Service creates its socket.
static ErrorOr<int> create_database_socket(DeprecatedString const& socket_path)
{
    if (Core::DeprecatedFile::exists(socket_path))
        TRY(Core::System::unlink(socket_path));

#    ifdef SOCK_NONBLOCK
    auto socket_fd = TRY(Core::System::socket(AF_LOCAL, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0));
#    else
    auto socket_fd = TRY(Core::System::socket(AF_LOCAL, SOCK_STREAM, 0));

    int option = 1;
    TRY(Core::System::ioctl(socket_fd, FIONBIO, &option));
    TRY(Core::System::fcntl(socket_fd, F_SETFD, FD_CLOEXEC));
#    endif

#    if !defined(AK_OS_MACOS) && !defined(AK_OS_FREEBSD) && !defined(AK_OS_OPENBSD)
    TRY(Core::System::fchmod(socket_fd, 0600));
#    endif

    auto socket_address = Core::SocketAddress::local(socket_path);
    auto socket_address_un = socket_address.to_sockaddr_un().release_value();

    TRY(Core::System::bind(socket_fd, reinterpret_cast<sockaddr*>(&socket_address_un), sizeof(socket_address_un)));
    TRY(Core::System::listen(socket_fd, 16));

    return socket_fd;
}

static ErrorOr<void> launch_server(DeprecatedString const& socket_path, DeprecatedString const& pid_path, Vector<String> candidate_server_paths)
{
    auto server_fd_or_error = create_database_socket(socket_path);
    if (server_fd_or_error.is_error()) {
        warnln("Failed to create a database socket at {}: {}", socket_path, server_fd_or_error.error());
        return server_fd_or_error.release_error();
    }
    auto server_fd = server_fd_or_error.value();
    auto server_pid = TRY(Core::System::fork());

    if (server_pid == 0) {
        TRY(Core::System::setsid());
        TRY(Core::System::signal(SIGCHLD, SIG_IGN));
        server_pid = TRY(Core::System::fork());

        if (server_pid != 0) {
            auto server_pid_file = TRY(Core::File::open(pid_path, Core::File::OpenMode::Write));
            TRY(server_pid_file->write(DeprecatedString::number(server_pid).bytes()));

            TRY(Core::System::kill(getpid(), SIGTERM));
        }

        server_fd = TRY(Core::System::dup(server_fd));

        auto takeover_string = DeprecatedString::formatted("SQLServer:{}", server_fd);
        TRY(Core::System::setenv("SOCKET_TAKEOVER"sv, takeover_string, true));

        ErrorOr<void> result;
        for (auto const& server_path : candidate_server_paths) {
            auto arguments = Array {
                server_path.bytes_as_string_view(),
                "--pid-file"sv,
                pid_path,
            };
            result = Core::System::exec(arguments[0], arguments, Core::System::SearchInPath::Yes);
            if (!result.is_error())
                break;
        }
        if (result.is_error()) {
            warnln("Could not launch any of {}: {}", candidate_server_paths, result.error());
            TRY(Core::System::unlink(pid_path));
        }

        VERIFY_NOT_REACHED();
    }

    TRY(Core::System::waitpid(server_pid));
    return {};
}

static ErrorOr<bool> should_launch_server(DeprecatedString const& pid_path)
{
    if (!Core::DeprecatedFile::exists(pid_path))
        return true;

    Optional<pid_t> pid;
    {
        auto server_pid_file = Core::File::open(pid_path, Core::File::OpenMode::Read);
        if (server_pid_file.is_error()) {
            warnln("Could not open SQLServer PID file '{}': {}", pid_path, server_pid_file.error());
            return server_pid_file.release_error();
        }

        auto contents = server_pid_file.value()->read_until_eof();
        if (contents.is_error()) {
            warnln("Could not read SQLServer PID file '{}': {}", pid_path, contents.error());
            return contents.release_error();
        }

        pid = StringView { contents.value() }.to_int<pid_t>();
    }

    if (!pid.has_value()) {
        warnln("SQLServer PID file '{}' exists, but with an invalid PID", pid_path);
        TRY(Core::System::unlink(pid_path));
        return true;
    }
    if (kill(*pid, 0) < 0) {
        warnln("SQLServer PID file '{}' exists with PID {}, but process cannot be found", pid_path, *pid);
        TRY(Core::System::unlink(pid_path));
        return true;
    }

    return false;
}

ErrorOr<NonnullRefPtr<SQLClient>> SQLClient::launch_server_and_create_client(Vector<String> candidate_server_paths)
{
    auto runtime_directory = TRY(Core::StandardPaths::runtime_directory());
    auto socket_path = DeprecatedString::formatted("{}/SQLServer.socket", runtime_directory);
    auto pid_path = DeprecatedString::formatted("{}/SQLServer.pid", runtime_directory);

    if (TRY(should_launch_server(pid_path)))
        TRY(launch_server(socket_path, pid_path, move(candidate_server_paths)));

    auto socket = TRY(Core::LocalSocket::connect(move(socket_path)));
    TRY(socket->set_blocking(true));

    return adopt_nonnull_ref_or_enomem(new (nothrow) SQLClient(move(socket)));
}

#endif

void SQLClient::execution_success(u64 statement_id, u64 execution_id, Vector<DeprecatedString> const& column_names, bool has_results, size_t created, size_t updated, size_t deleted)
{
    if (!on_execution_success) {
        outln("{} row(s) created, {} updated, {} deleted", created, updated, deleted);
        return;
    }

    ExecutionSuccess success {
        .statement_id = statement_id,
        .execution_id = execution_id,
        .column_names = move(const_cast<Vector<DeprecatedString>&>(column_names)),
        .has_results = has_results,
        .rows_created = created,
        .rows_updated = updated,
        .rows_deleted = deleted,
    };

    on_execution_success(move(success));
}

void SQLClient::execution_error(u64 statement_id, u64 execution_id, SQLErrorCode const& code, DeprecatedString const& message)
{
    if (!on_execution_error) {
        warnln("Execution error for statement_id {}: {} ({})", statement_id, message, to_underlying(code));
        return;
    }

    ExecutionError error {
        .statement_id = statement_id,
        .execution_id = execution_id,
        .error_code = code,
        .error_message = move(const_cast<DeprecatedString&>(message)),
    };

    on_execution_error(move(error));
}

void SQLClient::next_result(u64 statement_id, u64 execution_id, Vector<Value> const& row)
{
    if (!on_next_result) {
        StringBuilder builder;
        builder.join(", "sv, row, "\"{}\""sv);
        outln("{}", builder.string_view());
        return;
    }

    ExecutionResult result {
        .statement_id = statement_id,
        .execution_id = execution_id,
        .values = move(const_cast<Vector<Value>&>(row)),
    };

    on_next_result(move(result));
}

void SQLClient::results_exhausted(u64 statement_id, u64 execution_id, size_t total_rows)
{
    if (!on_results_exhausted) {
        outln("{} total row(s)", total_rows);
        return;
    }

    ExecutionComplete success {
        .statement_id = statement_id,
        .execution_id = execution_id,
        .total_rows = total_rows,
    };

    on_results_exhausted(move(success));
}

}