2 * Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include "abstract_socket.hh"
22 #include <sys/socket.h>
23 #include <sys/types.h>
26 #include "utils/logging.hh"
28 #include "pkgmgrinfo_debug.h"
30 namespace pkgmgr_common {
33 AbstractSocket::AbstractSocket(std::string path)
34 : path_(std::move(path)), fd_(-1), addr_{} {}
36 AbstractSocket::AbstractSocket(int fd) : fd_(fd), addr_{} {
40 AbstractSocket::~AbstractSocket() {
44 int AbstractSocket::SendData(const void* buf, unsigned int size) {
45 auto buffer = static_cast<const unsigned char*>(buf);
46 unsigned int left = size;
49 ssize_t send_byte = send(fd_, buffer, left, MSG_NOSIGNAL);
51 LOG(ERROR) << "send() is failed. fd: " << fd_ << ", errno:" << errno;
62 int AbstractSocket::ReceiveData(void* buf, unsigned int size) {
63 bool is_blocking = true;
65 int block_retry_count = 5;
67 if (fcntl(fd_, F_GETFL, 0) & O_NONBLOCK)
70 auto buffer = static_cast<unsigned char*>(buf);
71 unsigned int left = size;
73 ssize_t recv_byte = recv(fd_, buffer, left, 0);
76 LOG(WARNING) << "Socket was disconnected. fd: " << fd_
77 << ", errno: " << err;
79 } else if (recv_byte < 0) {
81 LOG(WARNING) << "Interrupt occuered, try to receive data continue";
83 } else if (errno == EAGAIN) {
85 LOG(ERROR) << "Timed out. fd: " << fd_ << ", errno: " << EAGAIN
86 << " remaining retry count : " << block_retry_count;
87 if (block_retry_count > 0) {
94 if (retry_count > 0) {
95 LOG(WARNING) << "Fail to receive data from "
96 << "non-blocking socket retry count : " << retry_count
97 << " left byte : " << left << " receive byte : " << recv_byte;
104 LOG(ERROR) << "recv() is failed. fd: " << fd_ << ", errno: " << errno;
115 int AbstractSocket::GetFd() {
119 std::string AbstractSocket::GetPath() {
123 pid_t AbstractSocket::GetPID() {
127 uid_t AbstractSocket::GetUID() {
131 void AbstractSocket::SetOption() {
133 int ret = setsockopt(fd_, SOL_SOCKET, SO_SNDBUF, &size, sizeof(size));
136 LOG(ERROR) << "setsockopt() is failed. fd: " << fd_
137 << ", errno: " << errno;
141 ret = setsockopt(fd_, SOL_SOCKET, SO_RCVBUF, &size, sizeof(size));
143 LOG(ERROR) << "setsockopt() is failed. fd: " << fd_
144 << ", errno: " << errno;
147 int AbstractSocket::Create() {
151 fd_ = ::socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
153 LOG(ERROR) << "socket() is failed. errno: " << errno;
157 addr_.sun_family = AF_UNIX;
158 snprintf(addr_.sun_path, sizeof(addr_.sun_path), "%s", path_.c_str());
164 void AbstractSocket::GetFdInfo() {
166 struct ucred cred = {};
167 socklen_t len = sizeof(cred);
169 r = getsockopt(fd_, SOL_SOCKET, SO_PEERCRED, &cred, &len);
171 LOG(ERROR) << "getsockopt has failed, errno: " << errno;
179 void AbstractSocket::Disconnect() {
186 } // namespace socket
187 } // namespace pkgmgr_common