• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef FOUNDATION_APPEXECFWK_SERVICES_APPMGR_INCLUDE_APP_SPAWN_MSG_WRAPPER_H
17 #define FOUNDATION_APPEXECFWK_SERVICES_APPMGR_INCLUDE_APP_SPAWN_MSG_WRAPPER_H
18 
19 #include <string>
20 #include <vector>
21 #include <unistd.h>
22 
23 #include "nocopyable.h"
24 #include "client_socket.h"
25 
26 namespace OHOS {
27 namespace AppExecFwk {
28 struct AppSpawnStartMsg {
29     int32_t uid;
30     int32_t gid;
31     std::vector<int32_t> gids;
32     std::string procName;
33     std::string soPath;
34 };
35 
36 using AppSpawnMsg = AppSpawn::ClientSocket::AppProperty;
37 
38 constexpr auto LEN_PID = sizeof(pid_t);
39 
40 union AppSpawnPidMsg {
41     pid_t pid = 0;
42     char pidBuf[LEN_PID];
43 };
44 
45 class AppSpawnMsgWrapper {
46 public:
47     /**
48      * Constructor.
49      */
50     AppSpawnMsgWrapper() = default;
51 
52     /**
53      * Destructor
54      */
55     ~AppSpawnMsgWrapper();
56 
57     /**
58      * Disable copy.
59      */
60     DISALLOW_COPY_AND_MOVE(AppSpawnMsgWrapper);
61 
62     /**
63      * Verify message and assign to member variable.
64      *
65      * @param startMsg, request message.
66      */
67     bool AssembleMsg(const AppSpawnStartMsg &startMsg);
68 
69     /**
70      * Get function, return isValid_.
71      */
IsValid()72     bool IsValid() const
73     {
74         return isValid_;
75     }
76 
77     /**
78      * Get function, return member variable message.
79      */
GetMsgBuf()80     const void *GetMsgBuf() const
81     {
82         return reinterpret_cast<void *>(msg_);
83     }
84 
85     /**
86      * Get function, return message length.
87      */
GetMsgLength()88     int32_t GetMsgLength() const
89     {
90         return isValid_ ? sizeof(AppSpawnMsg) : 0;
91     }
92 
93 private:
94     /**
95      * Verify message.
96      *
97      * @param startMsg, request message.
98      */
99     bool VerifyMsg(const AppSpawnStartMsg &startMsg) const;
100 
101     /**
102      * Print message.
103      *
104      * @param startMsg, request message.
105      */
106     void DumpMsg() const;
107 
108     /**
109      * Release message.
110      */
111     void FreeMsg();
112 
113 private:
114     bool isValid_ = false;
115     // because AppSpawnMsg's size is uncertain, so should use raw pointer.
116     AppSpawnMsg *msg_ = nullptr;
117 };
118 }  // namespace AppExecFwk
119 }  // namespace OHOS
120 #endif  // FOUNDATION_APPEXECFWK_SERVICES_APPMGR_INCLUDE_APP_SPAWN_MSG_WRAPPER_H
121