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 "pkg_request.hh"
19 #include "utils/logging.hh"
21 #include "pkgmgrinfo_debug.h"
23 namespace pkgmgr_server {
25 PkgRequest::PkgRequest()
26 : request_type_(pkgmgr_common::REQ_TYPE_NONE), data_size_(-1) {}
28 PkgRequest::PkgRequest(int fd)
29 : request_type_(pkgmgr_common::REQ_TYPE_NONE), data_size_(-1) {
30 socket_ = std::unique_ptr<pkgmgr_common::socket::DataSocket>(
31 new (std::nothrow) pkgmgr_common::socket::DataSocket(fd));
32 if (socket_ == nullptr)
33 LOG(ERROR) << "Out of memory";
36 PkgRequest::~PkgRequest() {
41 unsigned char* PkgRequest::GetData() {
45 int PkgRequest::GetSize() {
49 int PkgRequest::GetFd() {
50 return socket_->GetFd();
53 pid_t PkgRequest::GetSenderPID() {
54 return socket_->GetPID();
57 uid_t PkgRequest::GetSenderUID() {
58 return socket_->GetUID();
61 pkgmgr_common::ReqType PkgRequest::GetRequestType() {
65 bool PkgRequest::ReceiveData() {
66 int ret = socket_->ReceiveData(&request_type_, sizeof(request_type_));
67 if (ret < 0 || request_type_ == pkgmgr_common::REQ_TYPE_NONE) {
68 LOG(ERROR) << "Failed to ReceiveData";
71 ret = socket_->ReceiveData(&data_size_, sizeof(data_size_));
73 LOG(ERROR) << "Failed to ReceiveData";
74 request_type_ = pkgmgr_common::REQ_TYPE_NONE;
77 if (data_size_ <= 0) {
78 LOG(ERROR) << "Invalid data";
79 request_type_ = pkgmgr_common::REQ_TYPE_NONE;
83 data_ = new (std::nothrow) unsigned char[data_size_];
84 if (data_ == nullptr) {
85 LOG(ERROR) << "Out of memory";
86 request_type_ = pkgmgr_common::REQ_TYPE_NONE;
89 ret = socket_->ReceiveData(reinterpret_cast<void*>(data_), data_size_);
91 LOG(ERROR) << "Failed to ReceiveData";
94 request_type_ = pkgmgr_common::REQ_TYPE_NONE;
101 bool PkgRequest::SendData(unsigned char* data, int size) {
102 if (socket_->SendData(&size, sizeof(size)) < 0)
105 return (socket_->SendData(data, size) == 0);
108 } // namespace pkgmgr_server