6cc9063476d4633eddbcc45f2aad74d1e274429d
[platform/core/appfw/pkgmgr-info.git] / src / common / socket / abstract_socket.cc
1 /*
2  * Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include "abstract_socket.hh"
18
19 #include <errno.h>
20 #include <fcntl.h>
21 #include <limits.h>
22 #include <sys/socket.h>
23 #include <sys/types.h>
24 #include <unistd.h>
25
26 #include "utils/logging.hh"
27
28 #include "pkgmgrinfo_debug.h"
29
30 namespace pkgmgr_common {
31 namespace socket {
32
33 AbstractSocket::AbstractSocket(std::string path)
34     : path_(std::move(path)), fd_(-1), addr_{} {}
35
36 AbstractSocket::AbstractSocket(int fd) : fd_(fd), addr_{} {
37   GetFdInfo();
38 }
39
40 AbstractSocket::~AbstractSocket() {
41   Disconnect();
42 }
43
44 int AbstractSocket::SendData(const void* buf, unsigned int size) {
45   auto buffer = static_cast<const unsigned char*>(buf);
46   unsigned int left = size;
47
48   while (left) {
49     ssize_t send_byte = send(fd_, buffer, left, MSG_NOSIGNAL);
50     if (send_byte < 0) {
51       LOG(ERROR) << "send() is failed. fd: " << fd_ << ", errno:" << errno;
52       return -ECOMM;
53     }
54
55     left -= send_byte;
56     buffer += send_byte;
57   }
58
59   return 0;
60 }
61
62 int AbstractSocket::ReceiveData(void* buf, unsigned int size) {
63   bool is_blocking = true;
64   int retry_count = 20;
65
66   if (fcntl(fd_, F_GETFL, 0) & O_NONBLOCK)
67     is_blocking = false;
68
69   auto buffer = static_cast<unsigned char*>(buf);
70   unsigned int left = size;
71   while (left) {
72     ssize_t recv_byte = recv(fd_, buffer, left, 0);
73     if (recv_byte == 0) {
74       LOG(WARNING) << "Socket was disconnected. fd: " << fd_
75           << ", errno: " << errno;
76       return -errno;
77     } else if (recv_byte < 0) {
78       if (errno == EINTR) {
79         continue;
80       } else if (errno == EAGAIN) {
81         if (is_blocking) {
82           LOG(ERROR) << "Timed out. fd: " << fd_ << ", errno: " << errno;
83           return -errno;
84         }
85
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;
90           usleep(100 * 1000);
91           retry_count--;
92           continue;
93         }
94       }
95
96       LOG(ERROR) << "recv() is failed. fd: " << fd_ << ", errno: " << errno;
97       return -ECOMM;
98     }
99
100     left -= recv_byte;
101     buffer += recv_byte;
102   }
103
104   return 0;
105 }
106
107 int AbstractSocket::GetFd() {
108   return fd_;
109 }
110
111 std::string AbstractSocket::GetPath() {
112   return path_;
113 }
114
115 pid_t AbstractSocket::GetPID() {
116   return pid_;
117 }
118
119 uid_t AbstractSocket::GetUID() {
120   return uid_;
121 }
122
123 void AbstractSocket::SetOption() {
124   int size = 2048;
125   int ret = setsockopt(fd_, SOL_SOCKET, SO_SNDBUF, &size, sizeof(size));
126
127   if (ret < 0) {
128     LOG(ERROR) << "setsockopt() is failed. fd: " << fd_
129         << ", errno: " << errno;
130     return;
131   }
132
133   ret = setsockopt(fd_, SOL_SOCKET, SO_RCVBUF, &size, sizeof(size));
134   if (ret < 0)
135     LOG(ERROR) << "setsockopt() is failed. fd: " << fd_
136         << ", errno: " << errno;
137 }
138
139 int AbstractSocket::Create() {
140   if (fd_ != -1)
141     return 0;
142
143   fd_ = ::socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
144   if (fd_ < 0) {
145     LOG(ERROR) << "socket() is failed. errno: " << errno;
146     return fd_;
147   }
148
149   addr_.sun_family = AF_UNIX;
150   snprintf(addr_.sun_path, sizeof(addr_.sun_path), "%s", path_.c_str());
151   SetOption();
152   GetFdInfo();
153   return 0;
154 }
155
156 void AbstractSocket::GetFdInfo() {
157   int r;
158   struct ucred cred = {};
159   socklen_t len = sizeof(cred);
160
161   r = getsockopt(fd_, SOL_SOCKET, SO_PEERCRED, &cred, &len);
162   if (r < 0) {
163     LOG(ERROR) << "getsockopt has failed, errno: " << errno;
164     return;
165   }
166
167   pid_ = cred.pid;
168   uid_ = cred.uid;
169 }
170
171 void AbstractSocket::Disconnect() {
172   if (fd_ > 0)
173     close(fd_);
174
175   fd_ = -1;
176 }
177
178 }  // namespace socket
179 }  // namespace pkgmgr_common