1 /*
2 * Copyright (c) 2021-2023 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 #include "faultloggerd_socket.h"
17
18 #include <cstddef>
19 #include <cstdio>
20 #include <securec.h>
21 #include <string>
22 #include <unistd.h>
23
24 #include <sys/socket.h>
25 #include <sys/stat.h>
26 #include <sys/time.h>
27 #include <sys/un.h>
28
29 #include "dfx_define.h"
30 #include "dfx_log.h"
31 #include "init_socket.h"
32
StartConnect(int & sockfd,const char * path,const int timeout)33 bool StartConnect(int& sockfd, const char* path, const int timeout)
34 {
35 bool ret = false;
36 if ((sockfd = socket(AF_LOCAL, SOCK_STREAM, 0)) < 0) {
37 DFXLOG_ERROR("%s :: Failed to socket\n", __func__);
38 return ret;
39 }
40
41 do {
42 if (timeout > 0) {
43 struct timeval timev = {
44 timeout,
45 0
46 };
47 void* pTimev = &timev;
48 if (setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, \
49 static_cast<const char*>(pTimev), sizeof(timev)) != 0) {
50 DFXLOG_ERROR("setsockopt SO_RCVTIMEO error");
51 }
52 }
53
54 std::string fullPath = std::string(FAULTLOGGERD_SOCK_BASE_PATH) + std::string(path);
55 struct sockaddr_un server;
56 (void)memset_s(&server, sizeof(server), 0, sizeof(server));
57 server.sun_family = AF_LOCAL;
58 errno_t err = strncpy_s(server.sun_path, sizeof(server.sun_path), fullPath.c_str(),
59 sizeof(server.sun_path) - 1);
60 if (err != EOK) {
61 DFXLOG_ERROR("%s :: strncpy failed, err = %d.", __func__, (int)err);
62 break;
63 }
64
65 int len = static_cast<int>(offsetof(struct sockaddr_un, sun_path) + strlen(server.sun_path) + 1);
66 int connected = connect(sockfd, reinterpret_cast<struct sockaddr *>(&server), len);
67 if (connected < 0) {
68 DFXLOG_ERROR("%s :: connect failed, errno = %d.", __func__, errno);
69 break;
70 }
71
72 ret = true;
73 } while (false);
74
75 if (!ret) {
76 close(sockfd);
77 }
78 return ret;
79 }
80
GetServerSocket(int & sockfd,const char * name)81 static bool GetServerSocket(int& sockfd, const char* name)
82 {
83 sockfd = OHOS_TEMP_FAILURE_RETRY(socket(AF_LOCAL, SOCK_STREAM, 0));
84 if (sockfd < 0) {
85 DFXLOG_ERROR("%s :: Failed to create socket, errno(%d)", __func__, errno);
86 return false;
87 }
88
89 std::string path = std::string(FAULTLOGGERD_SOCK_BASE_PATH) + std::string(name);
90 struct sockaddr_un server;
91 (void)memset_s(&server, sizeof(server), 0, sizeof(server));
92 server.sun_family = AF_LOCAL;
93 if (strncpy_s(server.sun_path, sizeof(server.sun_path), path.c_str(), sizeof(server.sun_path) - 1) != 0) {
94 DFXLOG_ERROR("%s :: strncpy failed.", __func__);
95 return false;
96 }
97
98 chmod(path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IWOTH);
99 unlink(path.c_str());
100
101 int optval = 1;
102 int ret = setsockopt(sockfd, SOL_SOCKET, SO_PASSCRED, &optval, sizeof(optval));
103 if (ret < 0) {
104 DFXLOG_ERROR("%s :: Failed to set socket option, errno(%d)", __func__, errno);
105 return false;
106 }
107
108 if (bind(sockfd, (struct sockaddr *)&server,
109 offsetof(struct sockaddr_un, sun_path) + strlen(server.sun_path)) < 0) {
110 DFXLOG_ERROR("%s :: Failed to bind socket, errno(%d)", __func__, errno);
111 return false;
112 }
113
114 return true;
115 }
116
StartListen(int & sockfd,const char * name,const int listenCnt)117 bool StartListen(int& sockfd, const char* name, const int listenCnt)
118 {
119 if (name == nullptr) {
120 return false;
121 }
122 sockfd = GetControlSocket(name);
123 if (sockfd < 0) {
124 DFXLOG_WARN("%s :: Failed to get socket fd by cfg", __func__);
125 if (GetServerSocket(sockfd, name) == false) {
126 DFXLOG_ERROR("%s :: Failed to get socket fd by path", __func__);
127 return false;
128 }
129 }
130
131 if (listen(sockfd, listenCnt) < 0) {
132 DFXLOG_ERROR("%s :: Failed to listen socket, errno(%d)", __func__, errno);
133 close(sockfd);
134 sockfd = -1;
135 return false;
136 }
137
138 DFXLOG_INFO("%s :: success to listen socket", __func__);
139 return true;
140 }
141
RecvMsgFromSocket(int sockfd,unsigned char * data,size_t & len)142 static bool RecvMsgFromSocket(int sockfd, unsigned char* data, size_t& len)
143 {
144 bool ret = false;
145 if ((sockfd < 0) || (data == nullptr)) {
146 return ret;
147 }
148
149 do {
150 struct msghdr msgh = { 0 };
151 char msgBuffer[SOCKET_BUFFER_SIZE] = { 0 };
152 struct iovec iov = {
153 .iov_base = msgBuffer,
154 .iov_len = sizeof(msgBuffer)
155 };
156 msgh.msg_iov = &iov;
157 msgh.msg_iovlen = 1;
158
159 char ctlBuffer[SOCKET_BUFFER_SIZE] = { 0 };
160 msgh.msg_control = ctlBuffer;
161 msgh.msg_controllen = sizeof(ctlBuffer);
162
163 if (recvmsg(sockfd, &msgh, 0) < 0) {
164 DFXLOG_ERROR("%s :: Failed to recv message, errno(%d)\n", __func__, errno);
165 break;
166 }
167
168 struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msgh);
169 if (cmsg == nullptr) {
170 DFXLOG_ERROR("%s :: Invalid message\n", __func__);
171 break;
172 }
173
174 len = cmsg->cmsg_len - sizeof(struct cmsghdr);
175 if (memcpy_s(data, len, CMSG_DATA(cmsg), len) != 0) {
176 DFXLOG_ERROR("%s :: memcpy error\n", __func__);
177 break;
178 }
179
180 ret = true;
181 } while (false);
182 return ret;
183 }
184
RecvMsgCredFromSocket(int sockfd,struct ucred * pucred)185 bool RecvMsgCredFromSocket(int sockfd, struct ucred* pucred)
186 {
187 bool ret = false;
188 if ((sockfd < 0) || (pucred == nullptr)) {
189 return ret;
190 }
191
192 do {
193 struct msghdr msgh = { 0 };
194 union {
195 char buf[CMSG_SPACE(sizeof(struct ucred))];
196
197 /* Space large enough to hold a 'ucred' structure */
198 struct cmsghdr align;
199 } controlMsg;
200
201 msgh.msg_name = nullptr;
202 msgh.msg_namelen = 0;
203
204 int data;
205 struct iovec iov = {
206 .iov_base = &data,
207 .iov_len = sizeof(data)
208 };
209 msgh.msg_iov = &iov;
210 msgh.msg_iovlen = 1;
211
212 msgh.msg_control = controlMsg.buf;
213 msgh.msg_controllen = sizeof(controlMsg.buf);
214
215 if (recvmsg(sockfd, &msgh, 0) < 0) {
216 DFXLOG_ERROR("%s :: Failed to recv message, errno(%d)\n", __func__, errno);
217 break;
218 }
219
220 struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msgh);
221 if (cmsg == nullptr) {
222 DFXLOG_ERROR("%s :: Invalid message\n", __func__);
223 break;
224 }
225
226 if (memcpy_s(pucred, sizeof(struct ucred), CMSG_DATA(cmsg), sizeof(struct ucred)) != 0) {
227 DFXLOG_ERROR("%s :: memcpy error\n", __func__);
228 break;
229 }
230
231 ret = true;
232 } while (false);
233 return ret;
234 }
235
SendMsgIovToSocket(int sockfd,void * iovBase,const int iovLen)236 bool SendMsgIovToSocket(int sockfd, void *iovBase, const int iovLen)
237 {
238 if ((sockfd < 0) || (iovBase == nullptr) || (iovLen == 0)) {
239 return false;
240 }
241
242 struct msghdr msgh = { 0 };
243 msgh.msg_name = nullptr;
244 msgh.msg_namelen = 0;
245
246 struct iovec iov;
247 iov.iov_base = iovBase;
248 iov.iov_len = iovLen;
249 msgh.msg_iov = &iov;
250 msgh.msg_iovlen = 1;
251
252 msgh.msg_control = nullptr;
253 msgh.msg_controllen = 0;
254
255 if (sendmsg(sockfd, &msgh, 0) < 0) {
256 DFXLOG_ERROR("%s :: Failed to send message, errno(%d).", __func__, errno);
257 return false;
258 }
259 return true;
260 }
261
SendMsgCtlToSocket(int sockfd,const void * cmsg,const int cmsgLen)262 static bool SendMsgCtlToSocket(int sockfd, const void *cmsg, const int cmsgLen)
263 {
264 if ((sockfd < 0) || (cmsg == nullptr) || (cmsgLen == 0)) {
265 return false;
266 }
267
268 struct msghdr msgh = { 0 };
269 char iovBase[] = "";
270 struct iovec iov = {
271 .iov_base = reinterpret_cast<void *>(iovBase),
272 .iov_len = 1
273 };
274 msgh.msg_iov = &iov;
275 msgh.msg_iovlen = 1;
276
277 int controlBufLen = CMSG_SPACE(static_cast<unsigned int>(cmsgLen));
278 char controlBuf[controlBufLen];
279 msgh.msg_control = controlBuf;
280 msgh.msg_controllen = sizeof(controlBuf);
281
282 struct cmsghdr *cmsgh = CMSG_FIRSTHDR(&msgh);
283 if (cmsgh != nullptr) {
284 cmsgh->cmsg_level = SOL_SOCKET;
285 cmsgh->cmsg_type = SCM_RIGHTS;
286 cmsgh->cmsg_len = CMSG_LEN(cmsgLen);
287 }
288 if (memcpy_s(CMSG_DATA(cmsgh), cmsgLen, cmsg, cmsgLen) != 0) {
289 DFXLOG_ERROR("%s :: memcpy error\n", __func__);
290 }
291
292 if (sendmsg(sockfd, &msgh, 0) < 0) {
293 DFXLOG_ERROR("%s :: Failed to send message, errno(%d)", __func__, errno);
294 return false;
295 }
296 return true;
297 }
298
SendFileDescriptorToSocket(int sockfd,int fd)299 bool SendFileDescriptorToSocket(int sockfd, int fd)
300 {
301 return SendMsgCtlToSocket(sockfd, reinterpret_cast<void *>(&fd), sizeof(fd));
302 }
303
ReadFileDescriptorFromSocket(int sockfd)304 int ReadFileDescriptorFromSocket(int sockfd)
305 {
306 size_t len = sizeof(int);
307 unsigned char data[len + 1];
308 if (!RecvMsgFromSocket(sockfd, data, len)) {
309 DFXLOG_ERROR("%s :: Failed to recv message", __func__);
310 return -1;
311 }
312
313 if (len != sizeof(int)) {
314 DFXLOG_ERROR("%s :: data is null or len is %d", __func__, len);
315 return -1;
316 }
317 int fd = *(reinterpret_cast<int *>(data));
318 DFXLOG_DEBUG("%s :: fd: %d", __func__, fd);
319 return fd;
320 }