2 * Copyright (C) 2016 Simon Fels <morphis@gravedo.de>
4 * This program is free software: you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 3, as published
6 * by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranties of
10 * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
11 * PURPOSE. See the GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program. If not, see <http://www.gnu.org/licenses/>.
18 #include "android/service/local_socket_connection.h"
20 #include <sys/types.h>
21 #include <sys/socket.h>
26 #define LOG_TAG "Anboxd"
27 #include <cutils/log.h>
30 bool socket_error_is_transient(int error_code) {
31 return (error_code == EINTR);
36 LocalSocketConnection::LocalSocketConnection(const std::string &path) :
39 struct sockaddr_un socket_address;
40 memset(&socket_address, 0, sizeof(socket_address));
42 socket_address.sun_family = AF_UNIX;
43 memcpy(socket_address.sun_path, path.data(), path.size());
45 fd_ = Fd{socket(AF_UNIX, SOCK_STREAM, 0)};
46 if (connect(fd_, reinterpret_cast<sockaddr*>(&socket_address), sizeof(socket_address)) < 0)
47 throw std::runtime_error("Failed to connect to server socket");
50 LocalSocketConnection::~LocalSocketConnection() {
55 ssize_t LocalSocketConnection::read_all(std::uint8_t *buffer, const size_t &size) {
56 ssize_t bytes_read = ::recv(fd_, reinterpret_cast<void*>(buffer), size, 0);
60 void LocalSocketConnection::send(char const* data, size_t length) {
61 size_t bytes_written{0};
63 while(bytes_written < length) {
64 ssize_t const result = ::send(fd_,
66 length - bytes_written,
69 if (socket_error_is_transient(errno))
72 throw std::runtime_error("Failed to send message to server");
75 bytes_written += result;
79 ssize_t LocalSocketConnection::send_raw(char const* data, size_t length) {