init_service_socket.c 8.7 KB
Newer Older
1
/*
Z
zhong_ning 已提交
2
 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "init_service_socket.h"

#include <errno.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/uio.h>

C
cheng_jinsong 已提交
25
#include "init_cmdexecutor.h"
26 27
#include "init_log.h"
#include "init_service.h"
28
#include "loop_event.h"
29
#include "securec.h"
30
#define SOCKET_BUFF_SIZE (256 * 1024)
31 32 33 34 35 36 37 38

#define HOS_SOCKET_DIR "/dev/unix/socket"
#define HOS_SOCKET_ENV_PREFIX "OHOS_SOCKET_"
#define MAX_SOCKET_ENV_PREFIX_LEN 64
#define MAX_SOCKET_FD_LEN 16

static int GetSocketAddr(struct sockaddr_un *addr, const char *name)
{
X
xionglei6 已提交
39
    (void)memset_s(addr, sizeof(struct sockaddr_un), 0x0, sizeof(struct sockaddr_un));
40 41 42 43 44 45 46
    addr->sun_family = AF_UNIX;
    size_t addrLen = sizeof(addr->sun_path);
    int ret = snprintf_s(addr->sun_path, addrLen, addrLen - 1, HOS_SOCKET_DIR "/%s", name);
    INIT_ERROR_CHECK(ret >= 0, return -1, "Failed to format addr %s", name);
    return 0;
}

47
static int SetSocketAddr(ServiceSocket *sockopt, sockaddr_union *addr)
48
{
49 50
    int ret = 0;
    if (sockopt->family == AF_NETLINK) {
M
Mupceet 已提交
51
#ifndef __LITEOS_A__
52
        if (memset_s(&(addr->addrnl), sizeof(addr->addrnl), 0, sizeof(addr->addrnl)) != EOK) {
53
            INIT_LOGE("Failed to clear socket address");
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
            return -1;
        }
        addr->addrnl.nl_family = AF_NETLINK;
        addr->addrnl.nl_pid = getpid();
        addr->addrnl.nl_groups = 0xffffffff;
#else
        INIT_LOGE("No support in liteos kernel");
        return -1;
#endif
    } else {
        ret = GetSocketAddr(&(addr->addrun), sockopt->name);
        INIT_ERROR_CHECK(ret == 0, return -1, "Failed to format addr %s", sockopt->name);
        if (access(addr->addrun.sun_path, F_OK) == 0) {
            INIT_LOGI("%s already exist, remove it", addr->addrun.sun_path);
            unlink(addr->addrun.sun_path);
        }
70
    }
71 72
    return ret;
}
73

74 75 76 77 78 79 80
static int SetSocketOptionAndBind(ServiceSocket *sockopt)
{
    if (sockopt->option & SOCKET_OPTION_PASSCRED) {
        int on = 1;
        if (setsockopt(sockopt->sockFd, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on))) {
            INIT_LOGE("Failed to setsockopt");
            return -1;
81
        }
82 83 84 85 86 87 88 89 90 91 92 93 94 95
    }
    if (sockopt->option & SOCKET_OPTION_RCVBUFFORCE) {
        int buffSize = SOCKET_BUFF_SIZE;
        if (setsockopt(sockopt->sockFd, SOL_SOCKET, SO_RCVBUFFORCE, &buffSize, sizeof(buffSize))) {
            INIT_LOGE("Failed to setsockopt");
            return -1;
        }
    }
    sockaddr_union addr = {};
    if (SetSocketAddr(sockopt, &addr) != 0) {
        INIT_LOGE("Failed to set socket addr");
        return -1;
    }
    if (sockopt->family == AF_NETLINK) {
M
Mupceet 已提交
96
#ifndef __LITEOS_A__
97 98 99
        if (bind(sockopt->sockFd, (struct sockaddr *)&(addr.addrnl), sizeof(addr.addrnl))) {
            INIT_LOGE("Create socket for service %s failed: %d", sockopt->name, errno);
            return -1;
100
        }
101 102 103 104 105 106
#else
        INIT_LOGE("No support in liteos kernel");
        return -1;
#endif
    } else {
        if (bind(sockopt->sockFd, (struct sockaddr *)&(addr.addrun), sizeof(addr.addrun))) {
107
            INIT_LOGE("Create socket for service %s failed: %d", sockopt->name, errno);
108
            return -1;
109
        }
110
        if (lchown(addr.addrun.sun_path, sockopt->uid, sockopt->gid)) {
111
            INIT_LOGE("lchown fail %d ", errno);
112 113
            unlink(addr.addrun.sun_path);
            return -1;
114
        }
115
        if (fchmodat(AT_FDCWD, addr.addrun.sun_path, sockopt->perm, AT_SYMLINK_NOFOLLOW)) {
116
            INIT_LOGE("fchmodat fail %d ", errno);
117 118
            unlink(addr.addrun.sun_path);
            return -1;
119
        }
120 121 122 123 124 125
    }
    return 0;
}

static int CreateSocket(ServiceSocket *sockopt)
{
C
fix log  
cheng_jinsong 已提交
126 127
    INIT_ERROR_CHECK(sockopt != NULL, return SERVICE_FAILURE, "Invalid socket options");
    INIT_LOGV("Socket name: %s, family: %d, type: %u, protocol: %d, perm: %u, uid: %u, gid: %u, option: %u",
128 129 130 131 132 133 134 135 136 137
        sockopt->name, sockopt->family, sockopt->type, sockopt->protocol,
        sockopt->perm, sockopt->uid,    sockopt->gid,  sockopt->option);
    if (sockopt->sockFd >= 0) {
        close(sockopt->sockFd);
        sockopt->sockFd = -1;
    }
    sockopt->sockFd = socket(sockopt->family, sockopt->type, sockopt->protocol);
    INIT_ERROR_CHECK(sockopt->sockFd >= 0, return -1, "socket fail %d ", errno);

    int ret = SetSocketOptionAndBind(sockopt);
138 139 140 141 142
    if (ret != 0) {
        close(sockopt->sockFd);
        return -1;
    }
    INIT_LOGI("CreateSocket %s success", sockopt->name);
R
renwei 已提交
143

C
cheng_jinsong 已提交
144
    PluginExecCmdByName("restoreContentRecurse", HOS_SOCKET_DIR);
145 146 147 148 149 150 151 152
    return sockopt->sockFd;
}

static int SetSocketEnv(int fd, const char *name)
{
    INIT_ERROR_CHECK(name != NULL, return SERVICE_FAILURE, "Invalid name");
    char pubName[MAX_SOCKET_ENV_PREFIX_LEN] = { 0 };
    char val[MAX_SOCKET_FD_LEN] = { 0 };
X
add ut  
xionglei6 已提交
153 154 155
    INIT_CHECK_RETURN_VALUE(snprintf_s(pubName, sizeof(pubName), sizeof(pubName) - 1, HOS_SOCKET_ENV_PREFIX "%s",
        name) >= 0, -1);
    INIT_CHECK_RETURN_VALUE(snprintf_s(val, sizeof(val), sizeof(val) - 1, "%d", fd) >= 0, -1);
156
    int ret = setenv(pubName, val, 1);
X
add ut  
xionglei6 已提交
157
    INIT_ERROR_CHECK(ret >= 0, return -1, "setenv fail %d ", errno);
158 159 160 161
    fcntl(fd, F_SETFD, 0);
    return 0;
}

162 163 164 165
static void ProcessWatchEvent_(const WatcherHandle watcherHandle, int fd, uint32_t *events, const void *context)
{
    *events = 0;
    Service *service = (Service *)context;
166
    ServiceSocket *tmpSock = service->socketCfg;
167 168 169 170 171 172 173 174 175 176 177 178 179
    while (tmpSock != NULL) {
        if (tmpSock->sockFd == fd) {
            tmpSock->watcher = NULL;
            break;
        }
        tmpSock = tmpSock->next;
    }
    if (tmpSock == NULL) { // not found socket
        INIT_LOGE("Service %s not match socket fd %d!", service->name, fd);
        close(fd);
        return;
    }
    INIT_LOGI("Socket information detected, fd:%d service name:%s", fd, service->name);
C
cheng_jinsong 已提交
180
    RemoveSocketWatcher(watcherHandle);
181 182 183 184 185
    if (ServiceStart(service) != SERVICE_SUCCESS) {
        INIT_LOGE("Service %s start failed!", service->name);
    }
}

C
cheng_jinsong 已提交
186
int AddSocketWatcher(ServiceWatcher *watcherHandle, Service *service, int fd)
187 188 189 190 191 192 193 194
{
    WatcherHandle handle;
    LE_WatchInfo info = {};
    info.fd = fd;
    info.flags = WATCHER_ONCE;
    info.events = Event_Read;
    info.processEvent = ProcessWatchEvent_;
    int ret = LE_StartWatcher(LE_GetDefaultLoop(), &handle, &info, service);
C
fix log  
cheng_jinsong 已提交
195
    INIT_LOGI("Watcher socket fd %d for service %s", fd, service->name);
196 197 198 199
    *watcherHandle = (ServiceWatcher)handle;
    return ret;
}

C
cheng_jinsong 已提交
200
void RemoveSocketWatcher(ServiceWatcher watcherHandle)
201 202 203 204
{
    LE_RemoveWatcher(LE_GetDefaultLoop(), (WatcherHandle)watcherHandle);
}

X
xionglei6 已提交
205
int CreateServiceSocket(Service *service)
206
{
X
xionglei6 已提交
207
    INIT_CHECK(service != NULL && service->socketCfg != NULL, return 0);
X
xionglei6 已提交
208
    INIT_CHECK(service->socketCfg->sockFd == -1, return 0);
X
xionglei6 已提交
209 210
    int ret = 0;
    ServiceSocket *tmpSock = service->socketCfg;
211
    while (tmpSock != NULL) {
R
renwei 已提交
212
        PluginExecCmdByName("setSockCreateCon", service->name);
213
        int fd = CreateSocket(tmpSock);
R
renwei 已提交
214
        PluginExecCmdByName("setSockCreateCon", "");
X
add ut  
xionglei6 已提交
215
        INIT_CHECK_RETURN_VALUE(fd >= 0, -1);
X
xionglei6 已提交
216
        if (IsOnDemandService(service)) {
217 218 219 220
            if (IsConnectionBasedSocket(tmpSock)) {
                ret = listen(tmpSock->sockFd, MAX_SOCKET_FD_LEN);
                INIT_CHECK_RETURN_VALUE(ret == 0, -1);
            }
221
            if (strcmp(service->name, "ueventd") != 0) {
C
cheng_jinsong 已提交
222
                ret = AddSocketWatcher(&tmpSock->watcher, service, tmpSock->sockFd);
223 224
                INIT_CHECK_RETURN_VALUE(ret == 0, -1);
            }
X
xionglei6 已提交
225 226
        }
        ret = SetSocketEnv(fd, tmpSock->name);
X
add ut  
xionglei6 已提交
227
        INIT_CHECK_RETURN_VALUE(ret >= 0, -1);
228 229 230 231 232
        tmpSock = tmpSock->next;
    }
    return 0;
}

X
xionglei6 已提交
233
void CloseServiceSocket(Service *service)
234
{
X
xionglei6 已提交
235
    INIT_CHECK(service != NULL && service->socketCfg != NULL, return);
236
    struct sockaddr_un addr;
X
xionglei6 已提交
237 238 239
    ServiceSocket *sockopt = service->socketCfg;
    while (sockopt != NULL) {
        if (sockopt->watcher != NULL) {
C
cheng_jinsong 已提交
240
            RemoveSocketWatcher(sockopt->watcher);
X
xionglei6 已提交
241
        }
242 243 244 245 246
        if (sockopt->sockFd >= 0) {
            close(sockopt->sockFd);
            sockopt->sockFd = -1;
        }
        if (GetSocketAddr(&addr, sockopt->name) == 0) {
C
cheng_jinsong 已提交
247
#ifndef STARTUP_INIT_TEST
248
            unlink(addr.sun_path);
C
cheng_jinsong 已提交
249
#endif
250
        }
X
xionglei6 已提交
251
        sockopt = sockopt->next;
252 253
    }
    return;
Z
zhong_ning 已提交
254
}