Receive client's request at each thread
[platform/core/appfw/pkgmgr-info.git] / src / server / pkg_request.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 "pkg_request.hh"
18
19 #include "utils/logging.hh"
20
21 #include "pkgmgrinfo_debug.h"
22
23 namespace pkgmgr_server {
24
25 PkgRequest::PkgRequest()
26     : request_type_(pkgmgr_common::REQ_TYPE_NONE), data_size_(-1),
27         privilege_checked_(false) {}
28
29 PkgRequest::PkgRequest(int fd)
30     : request_type_(pkgmgr_common::REQ_TYPE_NONE), data_size_(-1),
31         privilege_checked_(false) {
32   socket_ = std::unique_ptr<pkgmgr_common::socket::DataSocket>(
33       new (std::nothrow) pkgmgr_common::socket::DataSocket(fd));
34   if (socket_ == nullptr)
35     LOG(ERROR) << "Out of memory";
36 }
37
38 PkgRequest::~PkgRequest() {
39   if (data_ != nullptr)
40     delete[] data_;
41 }
42
43 const unsigned char* PkgRequest::GetData() {
44   return data_;
45 }
46
47 int PkgRequest::GetSize() {
48   return data_size_;
49 }
50
51 int PkgRequest::GetFd() {
52   return socket_->GetFd();
53 }
54
55 pid_t PkgRequest::GetSenderPID() {
56   return socket_->GetPID();
57 }
58
59 uid_t PkgRequest::GetSenderUID() {
60   return socket_->GetUID();
61 }
62
63 pkgmgr_common::ReqType PkgRequest::GetRequestType() {
64   return request_type_;
65 }
66
67 bool PkgRequest::ReceiveData() {
68   int ret = socket_->ReceiveData(&request_type_, sizeof(request_type_));
69   if (ret < 0 || request_type_ == pkgmgr_common::REQ_TYPE_NONE) {
70     LOG(ERROR) << "Failed to ReceiveData";
71     return false;
72   }
73   ret = socket_->ReceiveData(&data_size_, sizeof(data_size_));
74   if (ret < 0) {
75     LOG(ERROR) << "Failed to ReceiveData";
76     request_type_ = pkgmgr_common::REQ_TYPE_NONE;
77     return false;
78   }
79   if (data_size_ <= 0) {
80     LOG(ERROR) << "Invalid data";
81     request_type_ = pkgmgr_common::REQ_TYPE_NONE;
82     return false;
83   }
84
85   data_ = new (std::nothrow) unsigned char[data_size_];
86   if (data_ == nullptr) {
87     LOG(ERROR) << "Out of memory";
88     request_type_ = pkgmgr_common::REQ_TYPE_NONE;
89     return false;
90   }
91   ret = socket_->ReceiveData(reinterpret_cast<void*>(data_), data_size_);
92   if (ret < 0) {
93     LOG(ERROR) << "Failed to ReceiveData";
94     delete[] data_;
95     data_ = nullptr;
96     request_type_ = pkgmgr_common::REQ_TYPE_NONE;
97     return false;
98   }
99
100   return true;
101 }
102
103 bool PkgRequest::SendData(unsigned char* data, int size) {
104   if (socket_->SendData(&size, sizeof(size)) < 0)
105     return false;
106
107   return (socket_->SendData(data, size) == 0);
108 }
109
110 bool PkgRequest::GetPrivilegeChecked() {
111   return privilege_checked_;
112 }
113
114 void PkgRequest::SetPrivilegeChecked(bool checked) {
115   privilege_checked_ = checked;
116 }
117
118 }  // namespace pkgmgr_server