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),
27 privilege_checked_(false), sender_tid_(0) {}
29 PkgRequest::PkgRequest(int fd)
30 : request_type_(pkgmgr_common::REQ_TYPE_NONE), data_size_(-1),
31 privilege_checked_(false), sender_tid_(0) {
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";
38 PkgRequest::~PkgRequest() {
42 const unsigned char* PkgRequest::GetData() {
46 size_t PkgRequest::GetSize() {
50 int PkgRequest::GetFd() {
51 return socket_->GetFd();
54 pid_t PkgRequest::GetSenderPID() {
55 return socket_->GetPID();
58 pid_t PkgRequest::GetSenderTID() {
62 uid_t PkgRequest::GetSenderUID() {
63 return socket_->GetUID();
66 pkgmgr_common::ReqType PkgRequest::GetRequestType() {
70 bool PkgRequest::ReceiveData() {
71 int ret = socket_->ReceiveData(&request_type_, sizeof(request_type_));
72 if (ret < 0 || request_type_ == pkgmgr_common::REQ_TYPE_NONE) {
73 LOG(ERROR) << "Failed to ReceiveData";
76 ret = socket_->ReceiveData(&sender_tid_, sizeof(sender_tid_));
78 LOG(ERROR) << "Failed to ReceiveData";
79 request_type_ = pkgmgr_common::REQ_TYPE_NONE;
82 ret = socket_->ReceiveData(&data_size_, sizeof(data_size_));
84 LOG(ERROR) << "Failed to ReceiveData";
85 request_type_ = pkgmgr_common::REQ_TYPE_NONE;
88 if (data_size_ <= 0) {
89 LOG(ERROR) << "Invalid data";
90 request_type_ = pkgmgr_common::REQ_TYPE_NONE;
94 data_ = static_cast<unsigned char*>(malloc(data_size_));
95 if (data_ == nullptr) {
96 LOG(ERROR) << "Out of memory";
97 request_type_ = pkgmgr_common::REQ_TYPE_NONE;
100 ret = socket_->ReceiveData(reinterpret_cast<void*>(data_), data_size_);
102 LOG(ERROR) << "Failed to ReceiveData";
105 request_type_ = pkgmgr_common::REQ_TYPE_NONE;
112 bool PkgRequest::SendData(const tizen_base::Parcel& parcel) {
113 auto* raw = parcel.GetData();
114 size_t size = parcel.GetDataSize();
115 if (socket_->SendData(&size, sizeof(size)) < 0)
118 return (socket_->SendData(raw, size) == 0);
121 bool PkgRequest::GetPrivilegeChecked() {
122 return privilege_checked_;
125 void PkgRequest::SetPrivilegeChecked(bool checked) {
126 privilege_checked_ = checked;
129 unsigned char* PkgRequest::DetachData() {
130 unsigned char* data = data_;
135 } // namespace pkgmgr_server