Change log format
[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(int fd)
26     : request_type_(pkgmgr_common::REQ_TYPE_NONE), data_size_(-1) {
27   socket_ = std::unique_ptr<pkgmgr_common::socket::DataSocket>(
28       new (std::nothrow) pkgmgr_common::socket::DataSocket(fd));
29   if (socket_ == nullptr)
30     LOG(ERROR) << "Out of memory";
31 }
32
33 PkgRequest::~PkgRequest() {
34   if (data_ != nullptr)
35     delete[] data_;
36 }
37
38 unsigned char* PkgRequest::GetData() {
39   return data_;
40 }
41
42 int PkgRequest::GetSize() {
43   return data_size_;
44 }
45
46 int PkgRequest::GetFd() {
47   return socket_->GetFd();
48 }
49
50 pid_t PkgRequest::GetSenderPID() {
51   return socket_->GetPID();
52 }
53
54 uid_t PkgRequest::GetSenderUID() {
55   return socket_->GetUID();
56 }
57
58 pkgmgr_common::ReqType PkgRequest::GetRequestType() {
59   return request_type_;
60 }
61
62 bool PkgRequest::ReceiveData() {
63   int ret = socket_->ReceiveData(&request_type_, sizeof(request_type_));
64   if (ret < 0 || request_type_ == pkgmgr_common::REQ_TYPE_NONE) {
65     LOG(ERROR) << "Failed to ReceiveData";
66     return false;
67   }
68   ret = socket_->ReceiveData(&data_size_, sizeof(data_size_));
69   if (ret < 0) {
70     LOG(ERROR) << "Failed to ReceiveData";
71     request_type_ = pkgmgr_common::REQ_TYPE_NONE;
72     return false;
73   }
74   if (data_size_ <= 0) {
75     LOG(ERROR) << "Invalid data";
76     request_type_ = pkgmgr_common::REQ_TYPE_NONE;
77     return false;
78   }
79
80   data_ = new (std::nothrow) unsigned char[data_size_];
81   if (data_ == nullptr) {
82     LOG(ERROR) << "Out of memory";
83     request_type_ = pkgmgr_common::REQ_TYPE_NONE;
84     return false;
85   }
86   ret = socket_->ReceiveData(reinterpret_cast<void*>(data_), data_size_);
87   if (ret < 0) {
88     LOG(ERROR) << "Failed to ReceiveData";
89     delete[] data_;
90     data_ = nullptr;
91     request_type_ = pkgmgr_common::REQ_TYPE_NONE;
92     return false;
93   }
94
95   return true;
96 }
97
98 bool PkgRequest::SendData(unsigned char* data, int size) {
99   if (socket_->SendData(&size, sizeof(size)) < 0)
100     return false;
101
102   return (socket_->SendData(data, size) == 0);
103 }
104
105 }  // namespace pkgmgr_server