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 "fd_holder.h"
17 #include <stdio.h>
18 #include <errno.h>
19
20 #include "beget_ext.h"
21 #include "fd_holder_internal.h"
22 #include "init_utils.h"
23 #include "securec.h"
24
BuildClientSocket(void)25 static int BuildClientSocket(void)
26 {
27 int sockFd;
28 sockFd = socket(AF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC, 0);
29 if (sockFd < 0) {
30 BEGET_LOGE("Failed to build socket, err = %d", errno);
31 return -1;
32 }
33
34 struct sockaddr_un addr;
35 (void)memset_s(&addr, sizeof(addr), 0, sizeof(addr));
36 addr.sun_family = AF_UNIX;
37 if (strncpy_s(addr.sun_path, sizeof(addr.sun_path), INIT_HOLDER_SOCKET_PATH,
38 strlen(INIT_HOLDER_SOCKET_PATH)) != 0) {
39 BEGET_LOGE("Failed to build socket path");
40 close(sockFd);
41 return -1;
42 }
43 socklen_t len = (socklen_t)(offsetof(struct sockaddr_un, sun_path) + strlen(addr.sun_path) + 1);
44 if (connect(sockFd, (struct sockaddr *)&addr, len) < 0) {
45 BEGET_LOGE("Failed to connect to socket, err = %d", errno);
46 close(sockFd);
47 return -1;
48 }
49 return sockFd;
50 }
51
BuildSendData(char * buffer,size_t size,const char * serviceName,bool hold,bool poll)52 STATIC int BuildSendData(char *buffer, size_t size, const char *serviceName, bool hold, bool poll)
53 {
54 if (buffer == NULL || size == 0 || serviceName == 0) {
55 return -1;
56 }
57
58 if (!hold && poll) {
59 BEGET_LOGE("Get fd with poll set, invalid parameter");
60 return -1;
61 }
62
63 char *holdString = ACTION_HOLD;
64 if (!hold) {
65 holdString = ACTION_GET;
66 }
67 char *pollString = WITHPOLL;
68 if (!poll) {
69 pollString = WITHOUTPOLL;
70 }
71
72 if (snprintf_s(buffer, size, size - 1, "%s|%s|%s", serviceName, holdString, pollString) == -1) {
73 BEGET_LOGE("Failed to build send data");
74 return -1;
75 }
76 return 0;
77 }
78
ServiceSendFds(const char * serviceName,int * fds,int fdCount,bool doPoll)79 static int ServiceSendFds(const char *serviceName, int *fds, int fdCount, bool doPoll)
80 {
81 int sock = BuildClientSocket();
82 if (sock < 0) {
83 return -1;
84 }
85 struct iovec iovec = {};
86 struct msghdr msghdr = {
87 .msg_iov = &iovec,
88 .msg_iovlen = 1,
89 };
90
91 char sendBuffer[MAX_FD_HOLDER_BUFFER] = {};
92 if (BuildSendData(sendBuffer, sizeof(sendBuffer), serviceName, true, doPoll) < 0) {
93 BEGET_LOGE("Failed to build send data");
94 close(sock);
95 return -1;
96 }
97
98 BEGET_LOGV("Send data: [%s]", sendBuffer);
99 iovec.iov_base = sendBuffer;
100 iovec.iov_len = strlen(sendBuffer);
101
102 if (BuildControlMessage(&msghdr, fds, fdCount, true) < 0) {
103 BEGET_LOGE("Failed to build control message");
104 if (msghdr.msg_control != NULL) {
105 free(msghdr.msg_control);
106 }
107 msghdr.msg_controllen = 0;
108 close(sock);
109 return -1;
110 }
111
112 if (TEMP_FAILURE_RETRY(sendmsg(sock, &msghdr, MSG_NOSIGNAL)) < 0) {
113 BEGET_LOGE("Failed to send fds to init, err = %d", errno);
114 if (msghdr.msg_control != NULL) {
115 free(msghdr.msg_control);
116 }
117 msghdr.msg_controllen = 0;
118 close(sock);
119 return -1;
120 }
121 if (msghdr.msg_control != NULL) {
122 free(msghdr.msg_control);
123 }
124 msghdr.msg_controllen = 0;
125 BEGET_LOGI("Send fds done");
126 close(sock);
127 return 0;
128 }
129
ServiceSaveFd(const char * serviceName,int * fds,int fdCount)130 int ServiceSaveFd(const char *serviceName, int *fds, int fdCount)
131 {
132 // Sanity checks
133 if (serviceName == NULL || fds == NULL ||
134 fdCount < 0 || fdCount > MAX_HOLD_FDS) {
135 BEGET_LOGE("Invalid parameters");
136 return -1;
137 }
138 return ServiceSendFds(serviceName, fds, fdCount, false);
139 }
140
ServiceSaveFdWithPoll(const char * serviceName,int * fds,int fdCount)141 int ServiceSaveFdWithPoll(const char *serviceName, int *fds, int fdCount)
142 {
143 // Sanity checks
144 if (serviceName == NULL || fds == NULL ||
145 fdCount < 0 || fdCount > MAX_HOLD_FDS) {
146 BEGET_LOGE("Invalid parameters");
147 return -1;
148 }
149 return ServiceSendFds(serviceName, fds, fdCount, true);
150 }
151
ServiceGetFd(const char * serviceName,size_t * outfdCount)152 int *ServiceGetFd(const char *serviceName, size_t *outfdCount)
153 {
154 if (serviceName == NULL || outfdCount == NULL) {
155 BEGET_LOGE("Invalid parameters");
156 return NULL;
157 }
158
159 char path[MAX_FD_HOLDER_BUFFER] = {};
160 int ret = snprintf_s(path, MAX_FD_HOLDER_BUFFER, MAX_FD_HOLDER_BUFFER - 1, ENV_FD_HOLD_PREFIX"%s", serviceName);
161 BEGET_ERROR_CHECK(ret > 0, return NULL, "Failed snprintf_s err=%d", errno);
162 const char *value = getenv(path);
163 if (value == NULL) {
164 BEGET_LOGE("Cannot get env %s\n", path);
165 return NULL;
166 }
167
168 char fdBuffer[MAX_FD_HOLDER_BUFFER] = {};
169 ret = strncpy_s(fdBuffer, MAX_FD_HOLDER_BUFFER - 1, value, strlen(value));
170 BEGET_ERROR_CHECK(ret == 0, return NULL, "Failed strncpy_s err=%d", errno);
171 BEGET_LOGV("fds = %s", fdBuffer);
172 int fdCount = 0;
173 char **fdList = SplitStringExt(fdBuffer, " ", &fdCount, MAX_HOLD_FDS);
174 if (fdList == NULL) {
175 BEGET_LOGE("Cannot get fd list");
176 return NULL;
177 }
178
179 int *fds = calloc((size_t)fdCount, sizeof(int));
180 if (fds == NULL) {
181 BEGET_LOGE("Allocate memory for fd failed. err = %d", errno);
182 FreeStringVector(fdList, fdCount);
183 *outfdCount = 0;
184 return NULL;
185 }
186
187 bool encounterError = false;
188 for (int i = 0; i < fdCount; i++) {
189 errno = 0;
190 fds[i] = (int)strtol(fdList[i], NULL, DECIMAL_BASE);
191 if (errno != 0) {
192 BEGET_LOGE("Failed to convert \' %s \' to fd number", fdList[i]);
193 encounterError = true;
194 break;
195 }
196 }
197
198 if (encounterError) {
199 free(fds);
200 fds = NULL;
201 fdCount = 0;
202 }
203 *outfdCount = fdCount;
204 FreeStringVector(fdList, fdCount);
205 return fds;
206 }
207