| 
									
										
										
										
											2020-01-18 09:38:21 +01:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2021-12-06 16:27:57 +01:00
										 |  |  |  * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org> | 
					
						
							| 
									
										
										
										
											2020-01-18 09:38:21 +01:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											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/Notifier.h>
 | 
					
						
							| 
									
										
										
										
											2022-09-06 00:04:06 -06:00
										 |  |  | #include <LibCore/SessionManagement.h>
 | 
					
						
							| 
									
										
										
										
											2022-01-14 13:12:49 +00:00
										 |  |  | #include <LibCore/Stream.h>
 | 
					
						
							| 
									
										
										
										
											2021-12-06 16:27:57 +01:00
										 |  |  | #include <LibCore/System.h>
 | 
					
						
							| 
									
										
										
										
											2022-01-15 12:01:56 +00:00
										 |  |  | #include <LibCore/SystemServerTakeover.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-12-06 16:27:57 +01:00
										 |  |  | ErrorOr<void> LocalServer::take_over_from_system_server(String const& socket_path) | 
					
						
							| 
									
										
										
										
											2019-11-26 19:27:21 +03:00
										 |  |  | { | 
					
						
							|  |  |  |     if (m_listening) | 
					
						
							| 
									
										
										
										
											2022-07-11 17:57:32 +00:00
										 |  |  |         return Error::from_string_literal("Core::LocalServer: Can't perform socket takeover when already listening"); | 
					
						
							| 
									
										
										
										
											2019-11-26 19:27:21 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-06 00:04:06 -06:00
										 |  |  |     auto const parsed_path = TRY(Core::SessionManagement::parse_path_with_sid(socket_path)); | 
					
						
							| 
									
										
										
										
											2022-07-24 17:44:15 +02:00
										 |  |  |     auto socket = TRY(take_over_socket_from_system_server(parsed_path)); | 
					
						
							| 
									
										
										
										
											2022-01-15 12:01:56 +00:00
										 |  |  |     m_fd = TRY(socket->release_fd()); | 
					
						
							| 
									
										
										
										
											2019-11-26 19:27:21 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-06 16:27:57 +01:00
										 |  |  |     m_listening = true; | 
					
						
							|  |  |  |     setup_notifier(); | 
					
						
							|  |  |  |     return {}; | 
					
						
							| 
									
										
										
										
											2019-11-26 19:27:21 +03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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] { | 
					
						
							| 
									
										
										
										
											2021-11-29 18:42:01 +01:00
										 |  |  |         if (on_accept) { | 
					
						
							| 
									
										
										
										
											2022-01-14 13:12:49 +00:00
										 |  |  |             auto maybe_client_socket = accept(); | 
					
						
							|  |  |  |             if (maybe_client_socket.is_error()) { | 
					
						
							| 
									
										
										
										
											2022-11-08 09:30:25 -05:00
										 |  |  |                 dbgln("LocalServer::on_ready_to_read: Error accepting a connection: {}", maybe_client_socket.error()); | 
					
						
							|  |  |  |                 if (on_accept_error) | 
					
						
							|  |  |  |                     on_accept_error(maybe_client_socket.release_error()); | 
					
						
							| 
									
										
										
										
											2022-01-14 13:12:49 +00:00
										 |  |  |                 return; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             on_accept(maybe_client_socket.release_value()); | 
					
						
							| 
									
										
										
										
											2021-11-29 18:42:01 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-11-26 19:27:21 +03:00
										 |  |  |     }; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-01 20:58:27 +03:00
										 |  |  | bool LocalServer::listen(String const& 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(); | 
					
						
							| 
									
										
										
										
											2022-04-01 20:58:27 +03:00
										 |  |  |     rc = ::bind(m_fd, (sockaddr const*)&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; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-14 13:12:49 +00:00
										 |  |  | ErrorOr<NonnullOwnPtr<Stream::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) { | 
					
						
							| 
									
										
										
										
											2022-07-11 17:32:29 +00:00
										 |  |  |         return Error::from_syscall("accept"sv, -errno); | 
					
						
							| 
									
										
										
										
											2019-07-27 10:21:25 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-02 15:12:51 +00:00
										 |  |  |     return Stream::LocalSocket::adopt_fd(accepted_fd, Stream::PreventSIGPIPE::Yes); | 
					
						
							| 
									
										
										
										
											2020-02-02 12:34:39 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-27 10:21:25 +02:00
										 |  |  | } |