• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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_SOCKET_H
17 #define APPSPAWN_SOCKET_H
18 
19 #include <string>
20 #include <sys/socket.h>
21 #include <sys/time.h>
22 #include <sys/un.h>
23 #include <unistd.h>
24 
25 #include "nocopyable.h"
26 
27 namespace OHOS {
28 namespace AppSpawn {
29 class AppSpawnSocket {
30 public:
31     /**
32      * Constructor used to delete the default constructor.
33      */
34     AppSpawnSocket() = delete;
35 
36     /**
37      * Constructor used to create a AppSpawnSocket.
38      */
39     explicit AppSpawnSocket(const std::string &name);
40 
41     /**
42      * Destructor used to destroy a AppSpawnSocket
43      */
44     virtual ~AppSpawnSocket();
45 
46     /**
47      * Disables copying and moving for the AppSpawnSocket.
48      */
49     DISALLOW_COPY_AND_MOVE(AppSpawnSocket);
50 
51     /**
52      * Gets the socket's file descriptor (FD).
53      */
54     int GetSocketFd() const;
55 
56     /**
57      * Reads messages from the AppSpawn socket.
58      *
59      * @param socketFd Indicates the socket's FD.
60      * @param buf Indicates the message buffer.
61      * @param len Indicates the message size.
62      *
63      * @return -1:failed;other means message size.
64      */
65     virtual int ReadSocketMessage(int socketFd, void *buf, int len);
66 
67     /**
68      * Writes messages to the AppSpawn socket.
69      *
70      * @param socketFd Indicates the socket's FD.
71      * @param buf  Indicates the message buffer.
72      * @param len Indicates the message size.
73      *
74      * @return -1:failed;other means message size.
75      */
76     virtual int WriteSocketMessage(int socketFd, const void *buf, int len);
77 
78     /**
79      * Closes an AppSpawn socket.
80      *
81      * @param socketFd Indicates the socket's FD.
82      */
83     static void CloseSocket(int &socketFd);
84 
85     /**
86      * Creates an AppSpawn socket.
87      *
88      * @return -1:failed;other means connection FD.
89      */
90     static int CreateSocket();
91 
92 protected:
93     /**
94      * Sets the socket name to the socket address.
95      *
96      * @return -1:failed; 0:success
97      */
98     int PackSocketAddr();
99 
100 protected:
101     int socketFd_ = -1;
102     std::string socketName_ {};
103     struct sockaddr_un socketAddr_ {};
104     socklen_t socketAddrLen_ = 0;
105 #ifdef __MUSL__
106     const std::string socketDir_ = "/dev/unix/socket/";
107 #else
108     const std::string socketDir_ = "/dev/socket/";
109 #endif
110     const unsigned int listenBacklog_ = 50;                   // 50: max num of clients
111 };
112 }  // namespace AppSpawn
113 }  // namespace OHOS
114 #endif
115