• 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 APPSPAWN_SERVER_H
17 #define APPSPAWN_SERVER_H
18 
19 #include <queue>
20 #include <string>
21 #include <map>
22 
23 #include "appspawn_msg_peer.h"
24 #include "server_socket.h"
25 #include "nocopyable.h"
26 
27 namespace OHOS {
28 namespace AppSpawn {
29 class AppSpawnServer {
30 public:
31     /**
32      * Constructor used to delete the default constructor.
33      */
34     AppSpawnServer() = delete;
35 
36     /**
37      * Constructor used to create a AppSpawnServer.
38      */
39     explicit AppSpawnServer(const std::string &socketName);
40 
41     /**
42      * Destructor used to destory a AppSpawnServer
43      */
44     ~AppSpawnServer() = default;
45 
46     /**
47      * Disables copying and moving for the AppSpawnServer.
48      */
49     DISALLOW_COPY_AND_MOVE(AppSpawnServer);
50 
51     /**
52      * Provides the AppSpawn core function for the server to receive messages from ability manager service.
53      *
54      * @param longProcName Indicates the long process name.
55      * @param longProcNameLen Indicates the length of long process name.
56      */
57     bool ServerMain(char *longProcName, int64_t longProcNameLen);
58 
59     /**
60      * Controls the server listening socket.
61      *
62      * @param isRunning Indicates whether the server is running. Value false means to stop the server and exit.
63      */
64     void SetRunning(bool isRunning);
65 
66     /**
67      * Set its value to the member variable socket_.
68      *
69      * @param serverSocket Indicates the server socket.
70      */
71     void SetServerSocket(const std::shared_ptr<ServerSocket> &serverSocket);
72 
73     int AppColdStart(char *longProcName,
74         int64_t longProcNameLen, const ClientSocket::AppProperty *appProperty, int fd);
75 private:
76     int DoColdStartApp(ClientSocket::AppProperty *appProperty, int fd);
77 
78     static constexpr uint8_t BITLEN32 = 32;
79     static constexpr uint8_t FDLEN2 = 2;
80     static constexpr uint8_t FD_INIT_VALUE = 0;
81 
82     /**
83      * Use the MsgPeer method in the AppSpawnMsgPeer class to read message from socket, and notify the ServerMain to
84      * unlock.
85      *
86      * @param connectFd Indicates the connect FDs.
87      */
88     void MsgPeer(int connectFd);
89 
90     /**
91      * Gets the connect fd and creates a thread to receive messages.
92      */
93     void ConnectionPeer();
94 
95     /**
96      * Sets a name for an applicaiton process.
97      *
98      * @param longProcName Indicates the length of long process name.
99      * @param longProcNameLen Indicates the long process name.
100      * @param processName Indicates the process name from the ability manager service.
101      * @param len Indicates the size of processName.
102      */
103     int32_t SetProcessName(char *longProcName, int64_t longProcNameLen, const char *processName, int32_t len);
104 
105     /**
106      * Sets keep capabilities.
107      */
108     int32_t SetKeepCapabilities(uint32_t uid);
109 
110     /**
111      * Sets the uid and gid of an application process.
112      *
113      * @param uid Indicates the uid of the application process.
114      * @param gid Indicates the gid of the application process.
115      * @param gidTable Indicates an array of application processes.
116      * @param gidCount Indicates the number of GIDs.
117      */
118     int32_t SetUidGid(const uint32_t uid, const uint32_t gid, const uint32_t *gidTable, const uint32_t gidCount);
119 
120     /**
121      * Sets FDs in an application process.
122      */
123     int32_t SetFileDescriptors();
124 
125     /**
126      * Sets capabilities of an application process.
127      */
128     int32_t SetCapabilities();
129 
130     /**
131      * Create sandbox root folder file
132      */
133     int32_t DoSandboxRootFolderCreate(std::string sandboxPackagePath);
134 
135     /**
136      * Create sandbox root folder file with wargnar device
137      */
138     int32_t DoSandboxRootFolderCreateAdapt(std::string sandboxPackagePath);
139 
140     /**
141      * Do app sandbox original path mount common
142      */
143     int32_t DoAppSandboxMountOnce(const std::string originPath, const std::string destinationPath);
144 
145     /**
146      * Do app sandbox original path mount
147      */
148     int32_t DoAppSandboxMount(const ClientSocket::AppProperty *appProperty, std::string rootPath);
149 
150     /**
151      * Do app sandbox original path mount for some customized packages
152      */
153     int32_t DoAppSandboxMountCustomized(const ClientSocket::AppProperty *appProperty, std::string rootPath);
154 
155     /**
156      * Do app sandbox mkdir /mnt/sandbox/<packagename>/
157      */
158     void DoAppSandboxMkdir(std::string rootPath, const ClientSocket::AppProperty *appProperty);
159 
160     /**
161      * Sets app sandbox property.
162      */
163     int32_t SetAppSandboxProperty(const ClientSocket::AppProperty *appProperty);
164 
165     /**
166      * Sets app process property.
167      */
168     bool SetAppProcProperty(const ClientSocket::AppProperty *appProperty, char *longProcName,
169         int64_t longProcNameLen, const int32_t fd);
170 
171     /**
172      * Notify
173      */
174     void NotifyResToParentProc(const int32_t fd, const int32_t value);
175 
176     /**
177      * Special app process property.
178      */
179     void SpecialHandle(ClientSocket::AppProperty *appProperty);
180 
181     /**
182      * Check app process property.
183      */
184     bool CheckAppProperty(const ClientSocket::AppProperty *appProperty);
185 
186     void LoadAceLib();
187 
188     void SetAppAccessToken(const ClientSocket::AppProperty *appProperty);
189 
190     int StartApp(char *longProcName, int64_t longProcNameLen,
191         ClientSocket::AppProperty *appProperty, int connectFd, pid_t &pid);
192 
193     void WaitRebootEvent();
194 
195     void HandleSignal();
196 
197     void QuickExitMain();
198 
199     void ProcessAppSpawnMsg(char *longProcName, int64_t longProcNameLen,
200                             const std::unique_ptr<AppSpawnMsgPeer> &msg);
201 
202 private:
203     const std::string deviceNull_ = "/dev/null";
204     std::string socketName_ {};
205     std::shared_ptr<ServerSocket> socket_ = nullptr;
206     std::mutex mut_ {};
207     mutable std::condition_variable dataCond_ {};
208     std::queue<std::unique_ptr<AppSpawnMsgPeer>> appQueue_ {};
209     std::function<int(const ClientSocket::AppProperty &)> propertyHandler_ = nullptr;
210     std::function<void(const std::string &)> errHandlerHook_ = nullptr;
211     bool isRunning_ {};
212     bool isStop_ { false };
213     bool isChildDie_ { false };
214     pid_t childPid_ {};
215     std::map<pid_t, std::string> appMap_;
216 #ifdef NWEB_SPAWN
217     void *nwebHandle = nullptr;
218 #endif
219 };
220 }  // namespace AppSpawn
221 }  // namespace OHOS
222 
223 #endif
224