Serenity Operating System
1/*
2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include <LibCore/LocalServer.h>
28#include <LibCore/LocalSocket.h>
29#include <LibCore/Notifier.h>
30#include <stdio.h>
31#include <sys/socket.h>
32#include <sys/stat.h>
33#include <unistd.h>
34#ifndef SOCK_NONBLOCK
35#include <sys/ioctl.h>
36#endif
37
38namespace Core {
39
40LocalServer::LocalServer(Object* parent)
41 : Object(parent)
42{
43}
44
45LocalServer::~LocalServer()
46{
47}
48
49bool LocalServer::take_over_from_system_server()
50{
51 if (m_listening)
52 return false;
53
54 constexpr auto socket_takeover = "SOCKET_TAKEOVER";
55
56 if (getenv(socket_takeover)) {
57 dbg() << "Taking the socket over from SystemServer";
58
59 // Sanity check: it has to be a socket.
60 struct stat stat;
61 int rc = fstat(3, &stat);
62 if (rc == 0 && S_ISSOCK(stat.st_mode)) {
63 // The SystemServer has passed us the socket as fd 3,
64 // so use that instead of creating our own.
65 m_fd = 3;
66 // It had to be !CLOEXEC for obvious reasons, but we
67 // don't need it to be !CLOEXEC anymore, so set the
68 // CLOEXEC flag now.
69 fcntl(m_fd, F_SETFD, FD_CLOEXEC);
70 // We wouldn't want our children to think we're passing
71 // them a socket either, so unset the env variable.
72 unsetenv(socket_takeover);
73
74 m_listening = true;
75 setup_notifier();
76 return true;
77 } else {
78 if (rc != 0)
79 perror("fstat");
80 dbg() << "It's not a socket, what the heck??";
81 }
82 }
83
84 dbg() << "Failed to take the socket over from SystemServer";
85
86 return false;
87}
88
89void LocalServer::setup_notifier()
90{
91 m_notifier = Notifier::construct(m_fd, Notifier::Event::Read, this);
92 m_notifier->on_ready_to_read = [this] {
93 if (on_ready_to_accept)
94 on_ready_to_accept();
95 };
96}
97
98bool LocalServer::listen(const String& address)
99{
100 if (m_listening)
101 return false;
102
103 int rc;
104
105#ifdef SOCK_NONBLOCK
106 m_fd = socket(AF_LOCAL, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0);
107#else
108 m_fd = socket(AF_LOCAL, SOCK_STREAM, 0);
109 int option = 1;
110 ioctl(m_fd, FIONBIO, &option);
111 fcntl(m_fd, F_SETFD, FD_CLOEXEC);
112#endif
113 ASSERT(m_fd >= 0);
114
115 rc = fchmod(m_fd, 0600);
116 if (rc < 0) {
117 perror("fchmod");
118 ASSERT_NOT_REACHED();
119 }
120
121 auto socket_address = SocketAddress::local(address);
122 auto un = socket_address.to_sockaddr_un();
123 rc = ::bind(m_fd, (const sockaddr*)&un, sizeof(un));
124 if (rc < 0) {
125 perror("bind");
126 ASSERT_NOT_REACHED();
127 }
128
129 rc = ::listen(m_fd, 5);
130 if (rc < 0) {
131 perror("listen");
132 ASSERT_NOT_REACHED();
133 }
134
135 m_listening = true;
136 setup_notifier();
137 return true;
138}
139
140RefPtr<LocalSocket> LocalServer::accept()
141{
142 ASSERT(m_listening);
143 sockaddr_un un;
144 socklen_t un_size = sizeof(un);
145 int accepted_fd = ::accept(m_fd, (sockaddr*)&un, &un_size);
146 if (accepted_fd < 0) {
147 perror("accept");
148 return nullptr;
149 }
150
151 return LocalSocket::construct(accepted_fd);
152}
153
154}