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;
66 if (fcntl(fd_, F_GETFL, 0) & O_NONBLOCK)
69 auto buffer = static_cast<unsigned char*>(buf);
70 unsigned int left = size;
72 ssize_t recv_byte = recv(fd_, buffer, left, 0);
74 LOG(WARNING) << "Socket was disconnected. fd: " << fd_
75 << ", errno: " << errno;
77 } else if (recv_byte < 0) {
80 } else if (errno == EAGAIN) {
82 LOG(ERROR) << "Timed out. fd: " << fd_ << ", errno: " << errno;
86 if (retry_count > 0) {
87 LOG(WARNING) << "Fail to receive data from "
88 << "non-blocking socket retry count : " << retry_count
89 << " left byte : " << left << " receive byte : " << recv_byte;
96 LOG(ERROR) << "recv() is failed. fd: " << fd_ << ", errno: " << errno;
107 int AbstractSocket::GetFd() {
111 std::string AbstractSocket::GetPath() {
115 pid_t AbstractSocket::GetPID() {
119 uid_t AbstractSocket::GetUID() {
123 void AbstractSocket::SetOption() {
125 int ret = setsockopt(fd_, SOL_SOCKET, SO_SNDBUF, &size, sizeof(size));
128 LOG(ERROR) << "setsockopt() is failed. fd: " << fd_
129 << ", errno: " << errno;
133 ret = setsockopt(fd_, SOL_SOCKET, SO_RCVBUF, &size, sizeof(size));
135 LOG(ERROR) << "setsockopt() is failed. fd: " << fd_
136 << ", errno: " << errno;
139 int AbstractSocket::Create() {
143 fd_ = ::socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
145 LOG(ERROR) << "socket() is failed. errno: " << errno;
149 addr_.sun_family = AF_UNIX;
150 snprintf(addr_.sun_path, sizeof(addr_.sun_path), "%s", path_.c_str());
156 void AbstractSocket::GetFdInfo() {
158 struct ucred cred = {};
159 socklen_t len = sizeof(cred);
161 r = getsockopt(fd_, SOL_SOCKET, SO_PEERCRED, &cred, &len);
163 LOG(ERROR) << "getsockopt has failed, errno: " << errno;
171 void AbstractSocket::Disconnect() {
178 } // namespace socket
179 } // namespace pkgmgr_common