1 /*
2 * Copyright (c) 2021-2024 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 "ipc_unix_socket.h"
17
18 #include "os/failure_retry.h"
19 #include "utils/logger.h"
20 #include "securec.h"
21
22 #include <poll.h>
23 #include <sys/types.h>
24 #include <sys/socket.h>
25 #include <sys/un.h>
26 #include <unistd.h>
27
28 namespace ark::dprof::ipc {
29 constexpr char SOCKET_NAME[] = "\0dprof.socket"; // NOLINT(modernize-avoid-c-arrays)
30 static_assert(sizeof(static_cast<sockaddr_un *>(nullptr)->sun_path) >= sizeof(SOCKET_NAME), "Socket name too large");
31
CreateUnixServerSocket(int backlog)32 os::unique_fd::UniqueFd CreateUnixServerSocket(int backlog)
33 {
34 os::unique_fd::UniqueFd sock(PANDA_FAILURE_RETRY(::socket(AF_UNIX, SOCK_STREAM, 0)));
35
36 int opt = 1;
37 if (PANDA_FAILURE_RETRY(::setsockopt(sock.Get(), SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt))) == -1) {
38 PLOG(ERROR, DPROF) << "setsockopt() failed";
39 return os::unique_fd::UniqueFd();
40 }
41
42 struct sockaddr_un serverAddr {};
43 if (memset_s(&serverAddr, sizeof(serverAddr), 0, sizeof(serverAddr)) != EOK) {
44 PLOG(ERROR, DPROF) << "CreateUnixServerSocket memset_s failed";
45 UNREACHABLE();
46 }
47 serverAddr.sun_family = AF_UNIX;
48 if (memcpy_s(serverAddr.sun_path, sizeof(SOCKET_NAME), SOCKET_NAME, sizeof(SOCKET_NAME)) != EOK) {
49 PLOG(ERROR, DPROF) << "CreateUnixServerSocket memcpy_s failed";
50 UNREACHABLE();
51 }
52 auto *sockAddr = reinterpret_cast<sockaddr *>(&serverAddr);
53 if (PANDA_FAILURE_RETRY(::bind(sock.Get(), sockAddr, sizeof(serverAddr))) == -1) {
54 PLOG(ERROR, DPROF) << "bind() failed";
55 return os::unique_fd::UniqueFd();
56 }
57
58 if (::listen(sock.Get(), backlog) == -1) {
59 PLOG(ERROR, DPROF) << "listen() failed";
60 return os::unique_fd::UniqueFd();
61 }
62
63 return sock;
64 }
65
CreateUnixClientSocket()66 os::unique_fd::UniqueFd CreateUnixClientSocket()
67 {
68 os::unique_fd::UniqueFd sock(PANDA_FAILURE_RETRY(::socket(AF_UNIX, SOCK_STREAM, 0)));
69 if (!sock.IsValid()) {
70 PLOG(ERROR, DPROF) << "socket() failed";
71 return os::unique_fd::UniqueFd();
72 }
73
74 struct sockaddr_un serverAddr {};
75 if (memset_s(&serverAddr, sizeof(serverAddr), 0, sizeof(serverAddr)) != EOK) {
76 PLOG(ERROR, DPROF) << "CreateUnixClientSocket memset_s failed";
77 UNREACHABLE();
78 }
79 serverAddr.sun_family = AF_UNIX;
80 if (memcpy_s(serverAddr.sun_path, sizeof(SOCKET_NAME), SOCKET_NAME, sizeof(SOCKET_NAME)) != EOK) {
81 PLOG(ERROR, DPROF) << "CreateUnixClientSocket memcpy_s failed";
82 UNREACHABLE();
83 }
84 auto *sockAddr = reinterpret_cast<sockaddr *>(&serverAddr);
85 if (PANDA_FAILURE_RETRY(::connect(sock.Get(), sockAddr, sizeof(serverAddr))) == -1) {
86 PLOG(ERROR, DPROF) << "connect() failed";
87 return os::unique_fd::UniqueFd();
88 }
89
90 return sock;
91 }
92
SendAll(int fd,const void * buf,int len)93 bool SendAll(int fd, const void *buf, int len)
94 {
95 const char *p = reinterpret_cast<const char *>(buf);
96 int total = 0;
97 while (total < len) {
98 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
99 int n = PANDA_FAILURE_RETRY(::send(fd, p + total, len, 0));
100 if (n == -1) {
101 PLOG(ERROR, DPROF) << "send() failed";
102 return false;
103 }
104 total += n;
105 len -= n;
106 }
107 return true;
108 }
109
WaitDataTimeout(int fd,int timeoutMs)110 bool WaitDataTimeout(int fd, int timeoutMs)
111 {
112 struct pollfd pfd {};
113 pfd.fd = fd;
114 pfd.events = POLLIN;
115
116 int rc = PANDA_FAILURE_RETRY(::poll(&pfd, 1, timeoutMs));
117 if (rc == 1) {
118 // Success
119 return true;
120 }
121 if (rc == -1) {
122 // Error
123 PLOG(ERROR, DPROF) << "poll() failed";
124 return false;
125 }
126 if (rc == 0) {
127 // Timeout
128 LOG(ERROR, DPROF) << "Timeout, cannot recv data";
129 return false;
130 }
131
132 UNREACHABLE();
133 return false;
134 }
135
RecvTimeout(int fd,void * buf,int len,int timeoutMs)136 int RecvTimeout(int fd, void *buf, int len, int timeoutMs)
137 {
138 if (!WaitDataTimeout(fd, timeoutMs)) {
139 LOG(ERROR, DPROF) << "Cannot get access to data";
140 return -1;
141 }
142
143 int n = PANDA_FAILURE_RETRY(::recv(fd, buf, len, 0));
144 if (n == -1) {
145 PLOG(ERROR, DPROF) << "Cannot recv data, len=" << len;
146 return -1;
147 }
148 if (n == 0) {
149 // socket was closed
150 return 0;
151 }
152 if (n != len) {
153 LOG(ERROR, DPROF) << "Cannot recv data id, len=" << len << " n=" << n;
154 return -1;
155 }
156
157 return len;
158 }
159 } // namespace ark::dprof::ipc
160