Serenity Operating System
1/*
2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
7#include <AK/HashMap.h>
8#include <RequestServer/Protocol.h>
9#include <errno.h>
10#include <fcntl.h>
11#include <string.h>
12#include <unistd.h>
13
14namespace RequestServer {
15
16static HashMap<DeprecatedString, Protocol*>& all_protocols()
17{
18 static HashMap<DeprecatedString, Protocol*> map;
19 return map;
20}
21
22Protocol* Protocol::find_by_name(DeprecatedString const& name)
23{
24 return all_protocols().get(name).value_or(nullptr);
25}
26
27Protocol::Protocol(DeprecatedString const& name)
28{
29 all_protocols().set(name, this);
30}
31
32Protocol::~Protocol()
33{
34 // FIXME: Do proper de-registration.
35 VERIFY_NOT_REACHED();
36}
37
38ErrorOr<Protocol::Pipe> Protocol::get_pipe_for_request()
39{
40 int fd_pair[2] { 0 };
41 if (pipe(fd_pair) != 0) {
42 auto saved_errno = errno;
43 dbgln("Protocol: pipe() failed: {}", strerror(saved_errno));
44 return Error::from_errno(saved_errno);
45 }
46 fcntl(fd_pair[1], F_SETFL, fcntl(fd_pair[1], F_GETFL) | O_NONBLOCK);
47 return Pipe { fd_pair[0], fd_pair[1] };
48}
49
50}