* limitations under the License.
*/
+#include "abstract_socket.hh"
+
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <sys/types.h>
#include <unistd.h>
+#include "utils/logging.hh"
+
#include "pkgmgrinfo_debug.h"
-#include "abstract_socket.hh"
namespace pkgmgr_common {
namespace socket {
int AbstractSocket::SendData(const void* buf, unsigned int size) {
auto buffer = static_cast<const unsigned char*>(buf);
unsigned int left = size;
+
while (left) {
ssize_t send_byte = send(fd_, buffer, left, MSG_NOSIGNAL);
if (send_byte < 0) {
- LOGE("send() is failed. fd(%d), errno(%d)", fd_, errno);
+ LOG(ERROR) << "send() is failed. fd: " << fd_ << ", errno:" << errno;
return -ECOMM;
}
int AbstractSocket::ReceiveData(void* buf, unsigned int size) {
bool is_blocking = true;
- if (fcntl(fd_, F_GETFL, 0) & O_NONBLOCK) is_blocking = false;
+ int retry_count = 20;
+ int block_retry_count = 5;
+
+ if (fcntl(fd_, F_GETFL, 0) & O_NONBLOCK)
+ is_blocking = false;
auto buffer = static_cast<unsigned char*>(buf);
unsigned int left = size;
while (left) {
ssize_t recv_byte = recv(fd_, buffer, left, 0);
if (recv_byte == 0) {
- LOGW("Socket was disconnected. fd(%d), errno(%d)", fd_, errno);
- return -errno;
+ int err = errno;
+ LOG(WARNING) << "Socket was disconnected. fd: " << fd_
+ << ", errno: " << err;
+ return -err;
} else if (recv_byte < 0) {
if (errno == EINTR) {
+ LOG(WARNING) << "Interrupt occuered, try to receive data continue";
continue;
} else if (errno == EAGAIN) {
if (is_blocking) {
- LOGE("Timed out. fd(%d), errno(%d)", fd_, errno);
- return -errno;
+ LOG(ERROR) << "Timed out. fd: " << fd_ << ", errno: " << EAGAIN
+ << " remaining retry count : " << block_retry_count;
+ if (block_retry_count > 0) {
+ block_retry_count--;
+ continue;
+ }
+ return -EAGAIN;
}
- continue;
+ if (retry_count > 0) {
+ LOG(WARNING) << "Fail to receive data from "
+ << "non-blocking socket retry count : " << retry_count
+ << " left byte : " << left << " receive byte : " << recv_byte;
+ usleep(100 * 1000);
+ retry_count--;
+ continue;
+ }
}
- LOGE("recv() is failed. fd(%d), errno(%d)", fd_, errno);
+ LOG(ERROR) << "recv() is failed. fd: " << fd_ << ", errno: " << errno;
return -ECOMM;
}
return 0;
}
-int AbstractSocket::GetFd() { return fd_; }
+int AbstractSocket::GetFd() {
+ return fd_;
+}
-std::string AbstractSocket::GetPath() { return path_; }
+std::string AbstractSocket::GetPath() {
+ return path_;
+}
-pid_t AbstractSocket::GetPID() { return pid_; }
+pid_t AbstractSocket::GetPID() {
+ return pid_;
+}
-uid_t AbstractSocket::GetUID() { return uid_; }
+uid_t AbstractSocket::GetUID() {
+ return uid_;
+}
void AbstractSocket::SetOption() {
int size = 2048;
int ret = setsockopt(fd_, SOL_SOCKET, SO_SNDBUF, &size, sizeof(size));
+
if (ret < 0) {
- LOGE("setsockopt() is failed. fd(%d), errno(%d)", fd_, errno);
+ LOG(ERROR) << "setsockopt() is failed. fd: " << fd_
+ << ", errno: " << errno;
return;
}
ret = setsockopt(fd_, SOL_SOCKET, SO_RCVBUF, &size, sizeof(size));
- if (ret < 0) LOGE("setsockopt() is failed. fd(%d), errno(%d)", fd_, errno);
+ if (ret < 0)
+ LOG(ERROR) << "setsockopt() is failed. fd: " << fd_
+ << ", errno: " << errno;
}
int AbstractSocket::Create() {
- if (fd_ != -1) return 0;
+ if (fd_ != -1)
+ return 0;
+
fd_ = ::socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
if (fd_ < 0) {
- LOGE("socket() is failed. errno(%d)", errno);
+ LOG(ERROR) << "socket() is failed. errno: " << errno;
return fd_;
}
r = getsockopt(fd_, SOL_SOCKET, SO_PEERCRED, &cred, &len);
if (r < 0) {
- LOGE("getsockopt has failed, errno[%d]", errno);
+ LOG(ERROR) << "getsockopt has failed, errno: " << errno;
return;
}