| 
									
										
										
										
											2020-01-18 09:38:21 +01:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> | 
					
						
							|  |  |  |  * All rights reserved. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Redistribution and use in source and binary forms, with or without | 
					
						
							|  |  |  |  * modification, are permitted provided that the following conditions are met: | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * 1. Redistributions of source code must retain the above copyright notice, this | 
					
						
							|  |  |  |  *    list of conditions and the following disclaimer. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * 2. Redistributions in binary form must reproduce the above copyright notice, | 
					
						
							|  |  |  |  *    this list of conditions and the following disclaimer in the documentation | 
					
						
							|  |  |  |  *    and/or other materials provided with the distribution. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | 
					
						
							|  |  |  |  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 
					
						
							|  |  |  |  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | 
					
						
							|  |  |  |  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | 
					
						
							|  |  |  |  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | 
					
						
							|  |  |  |  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | 
					
						
							|  |  |  |  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | 
					
						
							|  |  |  |  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | 
					
						
							|  |  |  |  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 
					
						
							|  |  |  |  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-06 15:04:03 +01:00
										 |  |  | #include <LibCore/LocalSocket.h>
 | 
					
						
							| 
									
										
										
										
											2019-07-26 22:39:16 +02:00
										 |  |  | #include <errno.h>
 | 
					
						
							| 
									
										
										
										
											2020-06-21 21:18:45 +02:00
										 |  |  | #include <stdio.h>
 | 
					
						
							| 
									
										
										
										
											2020-02-02 12:34:39 +01:00
										 |  |  | #include <sys/socket.h>
 | 
					
						
							| 
									
										
										
										
											2020-06-21 21:18:45 +02:00
										 |  |  | #include <sys/stat.h>
 | 
					
						
							| 
									
										
										
										
											2019-07-13 19:42:03 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-25 17:09:52 +01:00
										 |  |  | #ifndef SOCK_NONBLOCK
 | 
					
						
							| 
									
										
										
										
											2020-02-02 12:34:39 +01:00
										 |  |  | #    include <sys/ioctl.h>
 | 
					
						
							| 
									
										
										
										
											2019-12-25 17:09:52 +01:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 12:34:39 +01:00
										 |  |  | namespace Core { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | LocalSocket::LocalSocket(int fd, Object* parent) | 
					
						
							|  |  |  |     : Socket(Socket::Type::Local, parent) | 
					
						
							| 
									
										
										
										
											2019-07-27 10:21:25 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-03-07 11:37:51 +13:00
										 |  |  |     // NOTE: This constructor is used by LocalServer::accept(), so the socket is already connected.
 | 
					
						
							| 
									
										
										
										
											2019-09-22 21:46:46 +02:00
										 |  |  |     m_connected = true; | 
					
						
							| 
									
										
										
										
											2019-07-27 10:21:25 +02:00
										 |  |  |     set_fd(fd); | 
					
						
							| 
									
										
										
										
											2020-02-02 12:34:39 +01:00
										 |  |  |     set_mode(IODevice::ReadWrite); | 
					
						
							| 
									
										
										
										
											2019-07-27 10:21:25 +02:00
										 |  |  |     set_error(0); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 12:34:39 +01:00
										 |  |  | LocalSocket::LocalSocket(Object* parent) | 
					
						
							|  |  |  |     : Socket(Socket::Type::Local, parent) | 
					
						
							| 
									
										
										
										
											2019-07-13 19:42:03 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-02-02 12:34:39 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-25 17:09:52 +01:00
										 |  |  | #ifdef SOCK_NONBLOCK
 | 
					
						
							| 
									
										
										
										
											2019-07-16 18:00:08 +02:00
										 |  |  |     int fd = socket(AF_LOCAL, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0); | 
					
						
							| 
									
										
										
										
											2019-12-25 17:09:52 +01:00
										 |  |  | #else
 | 
					
						
							|  |  |  |     int fd = socket(AF_LOCAL, SOCK_STREAM, 0); | 
					
						
							|  |  |  |     int option = 1; | 
					
						
							|  |  |  |     ioctl(fd, FIONBIO, &option); | 
					
						
							|  |  |  |     fcntl(fd, F_SETFD, FD_CLOEXEC); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-13 19:42:03 +02:00
										 |  |  |     if (fd < 0) { | 
					
						
							| 
									
										
										
										
											2019-08-17 11:07:15 +02:00
										 |  |  |         set_error(errno); | 
					
						
							| 
									
										
										
										
											2019-07-13 19:42:03 +02:00
										 |  |  |     } else { | 
					
						
							|  |  |  |         set_fd(fd); | 
					
						
							| 
									
										
										
										
											2020-02-02 12:34:39 +01:00
										 |  |  |         set_mode(IODevice::ReadWrite); | 
					
						
							| 
									
										
										
										
											2019-07-13 19:42:03 +02:00
										 |  |  |         set_error(0); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 12:34:39 +01:00
										 |  |  | LocalSocket::~LocalSocket() | 
					
						
							| 
									
										
										
										
											2019-07-13 19:42:03 +02:00
										 |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2020-02-02 12:34:39 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-21 21:18:45 +02:00
										 |  |  | RefPtr<LocalSocket> LocalSocket::take_over_accepted_socket_from_system_server() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     constexpr auto socket_takeover = "SOCKET_TAKEOVER"; | 
					
						
							|  |  |  |     if (!getenv(socket_takeover)) | 
					
						
							|  |  |  |         return nullptr; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // The SystemServer has passed us the socket as fd 3,
 | 
					
						
							|  |  |  |     // so use that instead of creating our own.
 | 
					
						
							|  |  |  |     constexpr int fd = 3; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Sanity check: it has to be a socket.
 | 
					
						
							|  |  |  |     struct stat stat; | 
					
						
							|  |  |  |     int rc = fstat(fd, &stat); | 
					
						
							|  |  |  |     if (rc < 0 || !S_ISSOCK(stat.st_mode)) { | 
					
						
							|  |  |  |         if (rc != 0) | 
					
						
							|  |  |  |             perror("fstat"); | 
					
						
							| 
									
										
										
										
											2021-01-09 18:51:44 +01:00
										 |  |  |         dbgln("ERROR: The fd we got from SystemServer is not a socket"); | 
					
						
							| 
									
										
										
										
											2020-06-21 21:18:45 +02:00
										 |  |  |         return nullptr; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     auto socket = LocalSocket::construct(fd); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 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(fd, F_SETFD, FD_CLOEXEC); | 
					
						
							|  |  |  |     // We wouldn't want our children to think we're passing
 | 
					
						
							|  |  |  |     // them a socket either, so unset the env variable.
 | 
					
						
							|  |  |  |     unsetenv(socket_takeover); | 
					
						
							|  |  |  |     return socket; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 12:34:39 +01:00
										 |  |  | } |