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