Increse socket timeout for client
[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   int block_retry_count = 5;
66
67   if (fcntl(fd_, F_GETFL, 0) & O_NONBLOCK)
68     is_blocking = false;
69
70   auto buffer = static_cast<unsigned char*>(buf);
71   unsigned int left = size;
72   while (left) {
73     ssize_t recv_byte = recv(fd_, buffer, left, 0);
74     if (recv_byte == 0) {
75       int err = errno;
76       LOG(WARNING) << "Socket was disconnected. fd: " << fd_
77           << ", errno: " << err;
78       return -err;
79     } else if (recv_byte < 0) {
80       if (errno == EINTR) {
81         LOG(WARNING) << "Interrupt occuered, try to receive data continue";
82         continue;
83       } else if (errno == EAGAIN) {
84         if (is_blocking) {
85           LOG(ERROR) << "Timed out. fd: " << fd_ << ", errno: " << EAGAIN
86               << " remaining retry count : " << block_retry_count;
87           if (block_retry_count > 0) {
88             block_retry_count--;
89             continue;
90           }
91           return -EAGAIN;
92         }
93
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;
98           usleep(100 * 1000);
99           retry_count--;
100           continue;
101         }
102       }
103
104       LOG(ERROR) << "recv() is failed. fd: " << fd_ << ", errno: " << errno;
105       return -ECOMM;
106     }
107
108     left -= recv_byte;
109     buffer += recv_byte;
110   }
111
112   return 0;
113 }
114
115 int AbstractSocket::GetFd() {
116   return fd_;
117 }
118
119 std::string AbstractSocket::GetPath() {
120   return path_;
121 }
122
123 pid_t AbstractSocket::GetPID() {
124   return pid_;
125 }
126
127 uid_t AbstractSocket::GetUID() {
128   return uid_;
129 }
130
131 void AbstractSocket::SetOption() {
132   int size = 2048;
133   int ret = setsockopt(fd_, SOL_SOCKET, SO_SNDBUF, &size, sizeof(size));
134
135   if (ret < 0) {
136     LOG(ERROR) << "setsockopt() is failed. fd: " << fd_
137         << ", errno: " << errno;
138     return;
139   }
140
141   ret = setsockopt(fd_, SOL_SOCKET, SO_RCVBUF, &size, sizeof(size));
142   if (ret < 0)
143     LOG(ERROR) << "setsockopt() is failed. fd: " << fd_
144         << ", errno: " << errno;
145 }
146
147 int AbstractSocket::Create() {
148   if (fd_ != -1)
149     return 0;
150
151   fd_ = ::socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
152   if (fd_ < 0) {
153     LOG(ERROR) << "socket() is failed. errno: " << errno;
154     return fd_;
155   }
156
157   addr_.sun_family = AF_UNIX;
158   snprintf(addr_.sun_path, sizeof(addr_.sun_path), "%s", path_.c_str());
159   SetOption();
160   GetFdInfo();
161   return 0;
162 }
163
164 void AbstractSocket::GetFdInfo() {
165   int r;
166   struct ucred cred = {};
167   socklen_t len = sizeof(cred);
168
169   r = getsockopt(fd_, SOL_SOCKET, SO_PEERCRED, &cred, &len);
170   if (r < 0) {
171     LOG(ERROR) << "getsockopt has failed, errno: " << errno;
172     return;
173   }
174
175   pid_ = cred.pid;
176   uid_ = cred.uid;
177 }
178
179 void AbstractSocket::Disconnect() {
180   if (fd_ > 0)
181     close(fd_);
182
183   fd_ = -1;
184 }
185
186 }  // namespace socket
187 }  // namespace pkgmgr_common