| 
									
										
										
										
											2020-01-18 09:38:21 +01:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2021-04-22 01:24:48 -07:00
										 |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							| 
									
										
										
										
											2020-01-18 09:38:21 +01:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-06 15:04:03 +01:00
										 |  |  | #include <LibCore/LocalServer.h>
 | 
					
						
							|  |  |  | #include <LibCore/LocalSocket.h>
 | 
					
						
							|  |  |  | #include <LibCore/Notifier.h>
 | 
					
						
							| 
									
										
										
										
											2021-01-12 19:21:59 +01:00
										 |  |  | #include <fcntl.h>
 | 
					
						
							| 
									
										
										
										
											2019-07-27 10:21:25 +02:00
										 |  |  | #include <stdio.h>
 | 
					
						
							|  |  |  | #include <sys/socket.h>
 | 
					
						
							| 
									
										
										
										
											2019-11-26 19:27:21 +03:00
										 |  |  | #include <sys/stat.h>
 | 
					
						
							|  |  |  | #include <unistd.h>
 | 
					
						
							| 
									
										
										
										
											2020-05-29 22:22:01 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-25 17:09:52 +01:00
										 |  |  | #ifndef SOCK_NONBLOCK
 | 
					
						
							| 
									
										
										
										
											2020-05-29 22:22:01 +02:00
										 |  |  | #    include <sys/ioctl.h>
 | 
					
						
							| 
									
										
										
										
											2019-12-25 17:09:52 +01:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2019-07-27 10:21:25 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 12:34:39 +01:00
										 |  |  | namespace Core { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | LocalServer::LocalServer(Object* parent) | 
					
						
							|  |  |  |     : Object(parent) | 
					
						
							| 
									
										
										
										
											2019-07-27 10:21:25 +02:00
										 |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 12:34:39 +01:00
										 |  |  | LocalServer::~LocalServer() | 
					
						
							| 
									
										
										
										
											2019-07-27 10:21:25 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-08-08 15:54:22 +03:00
										 |  |  |     if (m_fd >= 0) | 
					
						
							|  |  |  |         ::close(m_fd); | 
					
						
							| 
									
										
										
										
											2019-07-27 10:21:25 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-14 21:35:49 +00:00
										 |  |  | bool LocalServer::take_over_from_system_server(String const& socket_path) | 
					
						
							| 
									
										
										
										
											2019-11-26 19:27:21 +03:00
										 |  |  | { | 
					
						
							|  |  |  |     if (m_listening) | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-14 21:35:49 +00:00
										 |  |  |     if (!LocalSocket::s_overtaken_sockets_parsed) | 
					
						
							|  |  |  |         LocalSocket::parse_sockets_from_system_server(); | 
					
						
							| 
									
										
										
										
											2019-11-26 19:27:21 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-14 21:35:49 +00:00
										 |  |  |     int fd = -1; | 
					
						
							|  |  |  |     if (socket_path.is_null()) { | 
					
						
							|  |  |  |         // We want the first (and only) socket.
 | 
					
						
							|  |  |  |         if (LocalSocket::s_overtaken_sockets.size() == 1) { | 
					
						
							|  |  |  |             fd = LocalSocket::s_overtaken_sockets.begin()->value; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         auto it = LocalSocket::s_overtaken_sockets.find(socket_path); | 
					
						
							|  |  |  |         if (it != LocalSocket::s_overtaken_sockets.end()) { | 
					
						
							|  |  |  |             fd = it->value; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (fd >= 0) { | 
					
						
							| 
									
										
										
										
											2019-11-26 19:27:21 +03:00
										 |  |  |         // Sanity check: it has to be a socket.
 | 
					
						
							|  |  |  |         struct stat stat; | 
					
						
							| 
									
										
										
										
											2021-04-14 21:35:49 +00:00
										 |  |  |         int rc = fstat(fd, &stat); | 
					
						
							| 
									
										
										
										
											2019-11-26 19:27:21 +03:00
										 |  |  |         if (rc == 0 && S_ISSOCK(stat.st_mode)) { | 
					
						
							| 
									
										
										
										
											2021-04-14 21:35:49 +00:00
										 |  |  |             // The SystemServer has passed us the socket, so use that instead of
 | 
					
						
							|  |  |  |             // creating our own.
 | 
					
						
							|  |  |  |             m_fd = fd; | 
					
						
							| 
									
										
										
										
											2019-11-26 19:27:21 +03:00
										 |  |  |             // It had to be !CLOEXEC for obvious reasons, but we
 | 
					
						
							|  |  |  |             // don't need it to be !CLOEXEC anymore, so set the
 | 
					
						
							|  |  |  |             // CLOEXEC flag now.
 | 
					
						
							|  |  |  |             fcntl(m_fd, F_SETFD, FD_CLOEXEC); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             m_listening = true; | 
					
						
							|  |  |  |             setup_notifier(); | 
					
						
							|  |  |  |             return true; | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             if (rc != 0) | 
					
						
							|  |  |  |                 perror("fstat"); | 
					
						
							| 
									
										
										
										
											2021-01-09 18:51:44 +01:00
										 |  |  |             dbgln("It's not a socket, what the heck??"); | 
					
						
							| 
									
										
										
										
											2019-11-26 19:27:21 +03:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-09 18:51:44 +01:00
										 |  |  |     dbgln("Failed to take the socket over from SystemServer"); | 
					
						
							| 
									
										
										
										
											2019-11-26 19:27:21 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return false; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 12:34:39 +01:00
										 |  |  | void LocalServer::setup_notifier() | 
					
						
							| 
									
										
										
										
											2019-11-26 19:27:21 +03:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-02-02 12:34:39 +01:00
										 |  |  |     m_notifier = Notifier::construct(m_fd, Notifier::Event::Read, this); | 
					
						
							| 
									
										
										
										
											2019-11-26 19:27:21 +03:00
										 |  |  |     m_notifier->on_ready_to_read = [this] { | 
					
						
							|  |  |  |         if (on_ready_to_accept) | 
					
						
							|  |  |  |             on_ready_to_accept(); | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 12:34:39 +01:00
										 |  |  | bool LocalServer::listen(const String& address) | 
					
						
							| 
									
										
										
										
											2019-07-27 10:21:25 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     if (m_listening) | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     int rc; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-25 17:09:52 +01:00
										 |  |  | #ifdef SOCK_NONBLOCK
 | 
					
						
							| 
									
										
										
										
											2019-11-26 19:27:21 +03:00
										 |  |  |     m_fd = socket(AF_LOCAL, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0); | 
					
						
							| 
									
										
										
										
											2019-12-25 17:09:52 +01:00
										 |  |  | #else
 | 
					
						
							|  |  |  |     m_fd = socket(AF_LOCAL, SOCK_STREAM, 0); | 
					
						
							|  |  |  |     int option = 1; | 
					
						
							|  |  |  |     ioctl(m_fd, FIONBIO, &option); | 
					
						
							|  |  |  |     fcntl(m_fd, F_SETFD, FD_CLOEXEC); | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2021-02-23 20:42:32 +01:00
										 |  |  |     VERIFY(m_fd >= 0); | 
					
						
							| 
									
										
										
										
											2021-09-12 09:06:33 -07:00
										 |  |  | #ifndef AK_OS_MACOS
 | 
					
						
							| 
									
										
										
										
											2020-01-03 20:21:39 +01:00
										 |  |  |     rc = fchmod(m_fd, 0600); | 
					
						
							|  |  |  |     if (rc < 0) { | 
					
						
							|  |  |  |         perror("fchmod"); | 
					
						
							| 
									
										
										
										
											2021-02-23 20:42:32 +01:00
										 |  |  |         VERIFY_NOT_REACHED(); | 
					
						
							| 
									
										
										
										
											2020-01-03 20:21:39 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-05-29 22:22:01 +02:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2020-01-03 20:21:39 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 12:34:39 +01:00
										 |  |  |     auto socket_address = SocketAddress::local(address); | 
					
						
							| 
									
										
										
										
											2020-08-23 13:47:52 +02:00
										 |  |  |     auto un_optional = socket_address.to_sockaddr_un(); | 
					
						
							|  |  |  |     if (!un_optional.has_value()) { | 
					
						
							|  |  |  |         perror("bind"); | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     auto un = un_optional.value(); | 
					
						
							| 
									
										
										
										
											2019-07-27 10:21:25 +02:00
										 |  |  |     rc = ::bind(m_fd, (const sockaddr*)&un, sizeof(un)); | 
					
						
							| 
									
										
										
										
											2019-11-26 19:27:21 +03:00
										 |  |  |     if (rc < 0) { | 
					
						
							|  |  |  |         perror("bind"); | 
					
						
							| 
									
										
										
										
											2020-05-28 18:19:49 +03:00
										 |  |  |         return false; | 
					
						
							| 
									
										
										
										
											2019-11-26 19:27:21 +03:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-07-27 10:21:25 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     rc = ::listen(m_fd, 5); | 
					
						
							| 
									
										
										
										
											2019-11-26 19:27:21 +03:00
										 |  |  |     if (rc < 0) { | 
					
						
							|  |  |  |         perror("listen"); | 
					
						
							| 
									
										
										
										
											2020-05-28 18:19:49 +03:00
										 |  |  |         return false; | 
					
						
							| 
									
										
										
										
											2019-11-26 19:27:21 +03:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-07-27 10:21:25 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-26 19:27:21 +03:00
										 |  |  |     m_listening = true; | 
					
						
							|  |  |  |     setup_notifier(); | 
					
						
							| 
									
										
										
										
											2019-07-27 10:21:25 +02:00
										 |  |  |     return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 12:34:39 +01:00
										 |  |  | RefPtr<LocalSocket> LocalServer::accept() | 
					
						
							| 
									
										
										
										
											2019-07-27 10:21:25 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-02-23 20:42:32 +01:00
										 |  |  |     VERIFY(m_listening); | 
					
						
							| 
									
										
										
										
											2019-07-27 10:21:25 +02:00
										 |  |  |     sockaddr_un un; | 
					
						
							|  |  |  |     socklen_t un_size = sizeof(un); | 
					
						
							| 
									
										
										
										
											2021-05-16 12:13:19 +02:00
										 |  |  | #ifndef AK_OS_MACOS
 | 
					
						
							|  |  |  |     int accepted_fd = ::accept4(m_fd, (sockaddr*)&un, &un_size, SOCK_NONBLOCK | SOCK_CLOEXEC); | 
					
						
							|  |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2019-07-27 10:21:25 +02:00
										 |  |  |     int accepted_fd = ::accept(m_fd, (sockaddr*)&un, &un_size); | 
					
						
							| 
									
										
										
										
											2021-05-16 12:13:19 +02:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2019-07-27 10:21:25 +02:00
										 |  |  |     if (accepted_fd < 0) { | 
					
						
							|  |  |  |         perror("accept"); | 
					
						
							|  |  |  |         return nullptr; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-16 12:13:19 +02:00
										 |  |  | #ifdef AK_OS_MACOS
 | 
					
						
							|  |  |  |     int option = 1; | 
					
						
							|  |  |  |     ioctl(m_fd, FIONBIO, &option); | 
					
						
							|  |  |  |     (void)fcntl(accepted_fd, F_SETFD, FD_CLOEXEC); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 12:34:39 +01:00
										 |  |  |     return LocalSocket::construct(accepted_fd); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-27 10:21:25 +02:00
										 |  |  | } |