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 #ifdef USING_EXISTING_SOCKET
17 #include <string.h>
18 #include <stdio.h>
19 #include <dirent.h>
20 #include <limits.h>
21 #include <fcntl.h>
22 #include <unistd.h>
23 #include <errno.h>
24 #include <sys/socket.h>
25 #include <sys/un.h>
26 #include <string>
27 #include <vector>
28
29 #define OHOS_PROC_FD_DIR "/proc/self/fd"
30 #define OHOS_SOCKET_DIR "/dev/socket/"
31 #define OHOS_SOCKET_FILE_PREFIX "socket"
32
GetFileNameByFd(const int fd)33 static std::string GetFileNameByFd(const int fd)
34 {
35 if (fd <= 0) {
36 return std::string();
37 }
38
39 std::string fdPath = "/proc/self/fd/" + std::to_string(fd);
40 char fileName[PATH_MAX + 1] = {0};
41
42 int ret = readlink(fdPath.c_str(), fileName, PATH_MAX);
43 if (ret < 0 || ret > PATH_MAX) {
44 return std::string();
45 }
46 fileName[ret] = '\0';
47 return std::string(fileName);
48 }
49
GetSocketFds(std::vector<int> & fds)50 static void GetSocketFds(std::vector<int> &fds)
51 {
52 struct dirent *dp = nullptr;
53 DIR *dir = opendir(OHOS_PROC_FD_DIR);
54 if (!dir) {
55 return;
56 }
57
58 while ((dp = readdir(dir)) != nullptr) {
59 int fd = -1;
60 if (dp->d_name[0] == '.') {
61 continue;
62 }
63 fd = atoi(dp->d_name);
64 auto name = GetFileNameByFd(fd);
65 if (strncmp(name.c_str(), OHOS_SOCKET_FILE_PREFIX, strlen(OHOS_SOCKET_FILE_PREFIX))) {
66 continue;
67 }
68 if (TEMP_FAILURE_RETRY(fcntl(fd, F_GETFD)) >= 0) {
69 fds.push_back(fd);
70 }
71 }
72 closedir(dir);
73 }
74
CheckSocketType(int fd,int type)75 static bool CheckSocketType(int fd, int type)
76 {
77 int soType = -1;
78 socklen_t optlen = static_cast<socklen_t>(sizeof(soType));
79 if (getsockopt(fd, SOL_SOCKET,SO_TYPE, &soType, &optlen)) {
80 return false;
81 }
82
83 return (type == soType);
84 }
85
GetExistingSocketServer(const char * name,int type)86 int GetExistingSocketServer(const char *name, int type)
87 {
88 std::vector<int> fds;
89 if (name == nullptr) {
90 return -1;
91 }
92
93 GetSocketFds(fds);
94 std::string socketPath(OHOS_SOCKET_DIR);
95 socketPath += name;
96
97 for (auto fd : fds) {
98 struct sockaddr_un addr;
99 socklen_t addrLen = static_cast<socklen_t>(sizeof(addr));
100
101 if (CheckSocketType(fd, type) == false) {
102 continue;
103 }
104
105 int ret = getsockname(fd, (struct sockaddr*)&addr, &addrLen);
106 if (ret < 0) {
107 return -1;
108 }
109 if (socketPath.compare(addr.sun_path) == 0) {
110 return fd;
111 }
112 }
113
114 return -1;
115 }
116 #else
GetExistingSocketServer(const char *,int)117 int GetExistingSocketServer(const char *, int)
118 {
119 return -1;
120 }
121 #endif
122