Fix static analysis issue
[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), sender_tid_(0) {}
28
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";
36 }
37
38 PkgRequest::~PkgRequest() {
39   free(data_);
40 }
41
42 const unsigned char* PkgRequest::GetData() {
43   return data_;
44 }
45
46 size_t PkgRequest::GetSize() {
47   return data_size_;
48 }
49
50 int PkgRequest::GetFd() {
51   return socket_->GetFd();
52 }
53
54 pid_t PkgRequest::GetSenderPID() {
55   return socket_->GetPID();
56 }
57
58 pid_t PkgRequest::GetSenderTID() {
59   return sender_tid_;
60 }
61
62 uid_t PkgRequest::GetSenderUID() {
63   return socket_->GetUID();
64 }
65
66 pkgmgr_common::ReqType PkgRequest::GetRequestType() {
67   return request_type_;
68 }
69
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";
74     return false;
75   }
76   ret = socket_->ReceiveData(&sender_tid_, sizeof(sender_tid_));
77   if (ret < 0) {
78     LOG(ERROR) << "Failed to ReceiveData";
79     request_type_ = pkgmgr_common::REQ_TYPE_NONE;
80     return false;
81   }
82   ret = socket_->ReceiveData(&data_size_, sizeof(data_size_));
83   if (ret < 0) {
84     LOG(ERROR) << "Failed to ReceiveData";
85     request_type_ = pkgmgr_common::REQ_TYPE_NONE;
86     return false;
87   }
88   if (data_size_ <= 0) {
89     LOG(ERROR) << "Invalid data";
90     request_type_ = pkgmgr_common::REQ_TYPE_NONE;
91     return false;
92   }
93
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;
98     return false;
99   }
100   ret = socket_->ReceiveData(reinterpret_cast<void*>(data_), data_size_);
101   if (ret < 0) {
102     LOG(ERROR) << "Failed to ReceiveData";
103     free(data_);
104     data_ = nullptr;
105     request_type_ = pkgmgr_common::REQ_TYPE_NONE;
106     return false;
107   }
108
109   return true;
110 }
111
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)
116     return false;
117
118   return (socket_->SendData(raw, size) == 0);
119 }
120
121 bool PkgRequest::GetPrivilegeChecked() {
122   return privilege_checked_;
123 }
124
125 void PkgRequest::SetPrivilegeChecked(bool checked) {
126   privilege_checked_ = checked;
127 }
128
129 unsigned char* PkgRequest::DetachData() {
130   unsigned char* data = data_;
131   data_ = nullptr;
132   return data;
133 }
134
135 }  // namespace pkgmgr_server