• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "init_service_socket.h"
16 
17 #include <errno.h>
18 #include <fcntl.h>
19 #include <netinet/in.h>
20 #include <string.h>
21 #include <sys/socket.h>
22 #include <sys/stat.h>
23 #include <sys/uio.h>
24 
25 #include "init_cmdexecutor.h"
26 #include "init_log.h"
27 #include "init_service.h"
28 #include "loop_event.h"
29 #include "securec.h"
30 #define SOCKET_BUFF_SIZE (256 * 1024)
31 
32 #define HOS_SOCKET_DIR "/dev/unix/socket"
33 #define HOS_SOCKET_PATH 64
34 #define HOS_SOCKET_ENV_PREFIX "OHOS_SOCKET_"
35 #define MAX_SOCKET_ENV_PREFIX_LEN 64
36 #define MAX_SOCKET_FD_LEN 16
37 
GetSocketAddr(struct sockaddr_un * addr,const char * name)38 static int GetSocketAddr(struct sockaddr_un *addr, const char *name)
39 {
40     (void)memset_s(addr, sizeof(struct sockaddr_un), 0x0, sizeof(struct sockaddr_un));
41     addr->sun_family = AF_UNIX;
42     size_t addrLen = sizeof(addr->sun_path);
43     int ret = snprintf_s(addr->sun_path, addrLen, addrLen - 1, HOS_SOCKET_DIR "/%s", name);
44     INIT_ERROR_CHECK(ret >= 0, return -1, "Failed to format addr %s", name);
45     return 0;
46 }
47 
SetSocketAddr(ServiceSocket * sockopt,sockaddr_union * addr)48 static int SetSocketAddr(ServiceSocket *sockopt, sockaddr_union *addr)
49 {
50     int ret = 0;
51     if (sockopt->family == AF_NETLINK) {
52 #ifndef __LITEOS_A__
53         if (memset_s(&(addr->addrnl), sizeof(addr->addrnl), 0, sizeof(addr->addrnl)) != EOK) {
54             INIT_LOGE("Failed to clear socket address");
55             return -1;
56         }
57         addr->addrnl.nl_family = AF_NETLINK;
58         addr->addrnl.nl_pid = getpid();
59         addr->addrnl.nl_groups = 0xffffffff;
60 #else
61         INIT_LOGE("No support in liteos kernel");
62         return -1;
63 #endif
64     } else {
65         ret = GetSocketAddr(&(addr->addrun), sockopt->name);
66         INIT_ERROR_CHECK(ret == 0, return -1, "Failed to format addr %s", sockopt->name);
67         if (access(addr->addrun.sun_path, F_OK) == 0) {
68             INIT_LOGI("%s already exist, remove it", addr->addrun.sun_path);
69             unlink(addr->addrun.sun_path);
70         }
71     }
72     return ret;
73 }
74 
SetSocketOptionAndBind(ServiceSocket * sockopt)75 static int SetSocketOptionAndBind(ServiceSocket *sockopt)
76 {
77     if (sockopt->option & SOCKET_OPTION_PASSCRED) {
78         int on = 1;
79         if (setsockopt(sockopt->sockFd, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on))) {
80             INIT_LOGE("Failed to setsockopt");
81             return -1;
82         }
83     }
84     if (sockopt->option & SOCKET_OPTION_RCVBUFFORCE) {
85         int buffSize = SOCKET_BUFF_SIZE;
86         if (setsockopt(sockopt->sockFd, SOL_SOCKET, SO_RCVBUFFORCE, &buffSize, sizeof(buffSize))) {
87             INIT_LOGE("Failed to setsockopt");
88             return -1;
89         }
90     }
91     sockaddr_union addr = {};
92     if (SetSocketAddr(sockopt, &addr) != 0) {
93         INIT_LOGE("Failed to set socket addr");
94         return -1;
95     }
96     if (sockopt->family == AF_NETLINK) {
97 #ifndef __LITEOS_A__
98         if (bind(sockopt->sockFd, (struct sockaddr *)&(addr.addrnl), sizeof(addr.addrnl))) {
99             INIT_LOGE("Create socket for service %s failed: %d", sockopt->name, errno);
100             return -1;
101         }
102 #else
103         INIT_LOGE("No support in liteos kernel");
104         return -1;
105 #endif
106     } else {
107         if (bind(sockopt->sockFd, (struct sockaddr *)&(addr.addrun), sizeof(addr.addrun))) {
108             INIT_LOGE("Create socket for service %s failed: %d", sockopt->name, errno);
109             return -1;
110         }
111         if (lchown(addr.addrun.sun_path, sockopt->uid, sockopt->gid)) {
112             INIT_LOGE("lchown fail %d ", errno);
113             unlink(addr.addrun.sun_path);
114             return -1;
115         }
116         if (fchmodat(AT_FDCWD, addr.addrun.sun_path, sockopt->perm, AT_SYMLINK_NOFOLLOW)) {
117             INIT_LOGE("fchmodat fail %d ", errno);
118             unlink(addr.addrun.sun_path);
119             return -1;
120         }
121     }
122     return 0;
123 }
124 
CreateSocket(ServiceSocket * sockopt)125 static int CreateSocket(ServiceSocket *sockopt)
126 {
127     INIT_ERROR_CHECK(sockopt != NULL, return SERVICE_FAILURE, "Invalid socket options");
128     INIT_LOGV("Socket name: %s, family: %d, type: %u, protocol: %d, perm: %u, uid: %u, gid: %u, option: %u",
129         sockopt->name, sockopt->family, sockopt->type, sockopt->protocol,
130         sockopt->perm, sockopt->uid,    sockopt->gid,  sockopt->option);
131     if (sockopt->sockFd >= 0) {
132         close(sockopt->sockFd);
133         sockopt->sockFd = -1;
134     }
135     sockopt->sockFd = socket(sockopt->family, sockopt->type, sockopt->protocol);
136     INIT_ERROR_CHECK(sockopt->sockFd >= 0, return -1, "socket fail %d ", errno);
137 
138     int ret = SetSocketOptionAndBind(sockopt);
139     if (ret != 0) {
140         close(sockopt->sockFd);
141         return -1;
142     }
143     INIT_LOGI("CreateSocket %s success", sockopt->name);
144 
145     char path[HOS_SOCKET_PATH] = { 0 };
146     if (snprintf_s(path, sizeof(path), sizeof(path) - 1, HOS_SOCKET_DIR"/%s", sockopt->name) < 0) {
147         close(sockopt->sockFd);
148         return -1;
149     }
150     PluginExecCmdByName("restoreContentRecurse", path);
151     INIT_LOGI("CreateSocket restoreContentRecurse %s success", path);
152     return sockopt->sockFd;
153 }
154 
SetSocketEnv(int fd,const char * name)155 static int SetSocketEnv(int fd, const char *name)
156 {
157     INIT_ERROR_CHECK(name != NULL, return SERVICE_FAILURE, "Invalid name");
158     char pubName[MAX_SOCKET_ENV_PREFIX_LEN] = { 0 };
159     char val[MAX_SOCKET_FD_LEN] = { 0 };
160     INIT_CHECK_RETURN_VALUE(snprintf_s(pubName, sizeof(pubName), sizeof(pubName) - 1, HOS_SOCKET_ENV_PREFIX "%s",
161         name) >= 0, -1);
162     INIT_CHECK_RETURN_VALUE(snprintf_s(val, sizeof(val), sizeof(val) - 1, "%d", fd) >= 0, -1);
163     int ret = setenv(pubName, val, 1);
164     INIT_ERROR_CHECK(ret >= 0, return -1, "setenv fail %d ", errno);
165     fcntl(fd, F_SETFD, 0);
166     return 0;
167 }
168 
ProcessWatchEvent_(const WatcherHandle watcherHandle,int fd,uint32_t * events,const void * context)169 static void ProcessWatchEvent_(const WatcherHandle watcherHandle, int fd, uint32_t *events, const void *context)
170 {
171     *events = 0;
172     Service *service = (Service *)context;
173     ServiceSocket *tmpSock = service->socketCfg;
174     while (tmpSock != NULL) {
175         if (tmpSock->sockFd == fd) {
176             tmpSock->watcher = NULL;
177             break;
178         }
179         tmpSock = tmpSock->next;
180     }
181     if (tmpSock == NULL) { // not found socket
182         INIT_LOGE("Service %s not match socket fd %d!", service->name, fd);
183         close(fd);
184         return;
185     }
186     INIT_LOGI("Socket information detected, fd:%d service name:%s", fd, service->name);
187     RemoveSocketWatcher(watcherHandle);
188     if (ServiceStart(service, &service->pathArgs) != SERVICE_SUCCESS) {
189         INIT_LOGE("Service %s start failed!", service->name);
190     }
191 }
192 
AddSocketWatcher(ServiceWatcher * watcherHandle,Service * service,int fd)193 int AddSocketWatcher(ServiceWatcher *watcherHandle, Service *service, int fd)
194 {
195     WatcherHandle handle;
196     LE_WatchInfo info = {};
197     info.fd = fd;
198     info.flags = WATCHER_ONCE;
199     info.events = Event_Read;
200     info.processEvent = ProcessWatchEvent_;
201     int ret = LE_StartWatcher(LE_GetDefaultLoop(), &handle, &info, service);
202     INIT_LOGI("Watcher socket fd %d for service %s", fd, service->name);
203     *watcherHandle = (ServiceWatcher)handle;
204     return ret;
205 }
206 
RemoveSocketWatcher(ServiceWatcher watcherHandle)207 void RemoveSocketWatcher(ServiceWatcher watcherHandle)
208 {
209     LE_RemoveWatcher(LE_GetDefaultLoop(), (WatcherHandle)watcherHandle);
210 }
211 
CreateServiceSocket(Service * service)212 int CreateServiceSocket(Service *service)
213 {
214     INIT_CHECK(service != NULL && service->socketCfg != NULL, return 0);
215     INIT_CHECK(service->socketCfg->sockFd == -1, return 0);
216     int ret = 0;
217     ServiceSocket *tmpSock = service->socketCfg;
218     while (tmpSock != NULL) {
219         PluginExecCmdByName("setSockCreateCon", service->name);
220         int fd = CreateSocket(tmpSock);
221         PluginExecCmdByName("setSockCreateCon", "");
222         INIT_CHECK_RETURN_VALUE(fd >= 0, -1);
223         if (IsOnDemandService(service)) {
224             if (IsConnectionBasedSocket(tmpSock)) {
225                 ret = listen(tmpSock->sockFd, MAX_SOCKET_FD_LEN);
226                 INIT_CHECK_RETURN_VALUE(ret == 0, -1);
227             }
228             if (strcmp(service->name, "ueventd") != 0) {
229                 ret = AddSocketWatcher(&tmpSock->watcher, service, tmpSock->sockFd);
230                 INIT_CHECK_RETURN_VALUE(ret == 0, -1);
231             }
232         }
233         ret = SetSocketEnv(fd, tmpSock->name);
234         INIT_CHECK_RETURN_VALUE(ret >= 0, -1);
235         tmpSock = tmpSock->next;
236     }
237     return 0;
238 }
239 
CloseServiceSocket(Service * service)240 void CloseServiceSocket(Service *service)
241 {
242     INIT_CHECK(service != NULL && service->socketCfg != NULL, return);
243     struct sockaddr_un addr;
244     ServiceSocket *sockopt = service->socketCfg;
245     while (sockopt != NULL) {
246         if (sockopt->watcher != NULL) {
247             RemoveSocketWatcher(sockopt->watcher);
248         }
249         if (sockopt->sockFd >= 0) {
250             close(sockopt->sockFd);
251             sockopt->sockFd = -1;
252         }
253         if (GetSocketAddr(&addr, sockopt->name) == 0) {
254 #ifndef STARTUP_INIT_TEST
255             unlink(addr.sun_path);
256 #endif
257         }
258         sockopt = sockopt->next;
259     }
260     return;
261 }
262