init_service_socket.c 8.6 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 25 26
 * 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>

#include "init_log.h"
#include "init_service.h"
27
#include "loop_event.h"
R
renwei 已提交
28 29 30
#ifdef WITH_SELINUX
#include "policycoreutils.h"
#endif
31
#include "securec.h"
32
#define SOCKET_BUFF_SIZE (256 * 1024)
33 34 35 36 37 38 39 40

#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 已提交
41
    (void)memset_s(addr, sizeof(struct sockaddr_un), 0x0, sizeof(struct sockaddr_un));
42 43 44 45 46 47 48
    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;
}

49
static int SetSocketAddr(ServiceSocket *sockopt, sockaddr_union *addr)
50
{
51 52
    int ret = 0;
    if (sockopt->family == AF_NETLINK) {
M
Mupceet 已提交
53
#ifndef __LITEOS_A__
54
        if (memset_s(&(addr->addrnl), sizeof(addr->addrnl), 0, sizeof(addr->addrnl)) != EOK) {
55
            INIT_LOGE("Failed to clear socket address");
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
            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);
        }
72
    }
73 74
    return ret;
}
75

76 77 78 79 80 81 82
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;
83
        }
84 85 86 87 88 89 90 91 92 93 94 95 96 97
    }
    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 已提交
98
#ifndef __LITEOS_A__
99 100 101
        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;
102
        }
103 104 105 106 107 108
#else
        INIT_LOGE("No support in liteos kernel");
        return -1;
#endif
    } else {
        if (bind(sockopt->sockFd, (struct sockaddr *)&(addr.addrun), sizeof(addr.addrun))) {
109
            INIT_LOGE("Create socket for service %s failed: %d", sockopt->name, errno);
110
            return -1;
111
        }
112
        if (lchown(addr.addrun.sun_path, sockopt->uid, sockopt->gid)) {
113
            INIT_LOGE("lchown fail %d ", errno);
114 115
            unlink(addr.addrun.sun_path);
            return -1;
116
        }
117
        if (fchmodat(AT_FDCWD, addr.addrun.sun_path, sockopt->perm, AT_SYMLINK_NOFOLLOW)) {
118
            INIT_LOGE("fchmodat fail %d ", errno);
119 120
            unlink(addr.addrun.sun_path);
            return -1;
121
        }
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
    }
    return 0;
}

static int CreateSocket(ServiceSocket *sockopt)
{
    INIT_ERROR_CHECK(sockopt != NULL, return SERVICE_FAILURE, "Invalid socket opt");
    INIT_LOGI("name: %s, family: %d, type: %u, protocol: %d, perm: %u, uid: %u, gid: %u, option: %u",
        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);
140 141 142 143 144
    if (ret != 0) {
        close(sockopt->sockFd);
        return -1;
    }
    INIT_LOGI("CreateSocket %s success", sockopt->name);
R
renwei 已提交
145 146 147 148 149 150

#ifdef WITH_SELINUX
    if (RestoreconRecurse(HOS_SOCKET_DIR)) {
        INIT_LOGE("DoRestorecon failed for '%s', err %d.", sockopt->name, errno);
    }
#endif
151 152 153 154 155 156 157 158
    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 已提交
159 160 161
    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);
162
    int ret = setenv(pubName, val, 1);
X
add ut  
xionglei6 已提交
163
    INIT_ERROR_CHECK(ret >= 0, return -1, "setenv fail %d ", errno);
164 165 166 167
    fcntl(fd, F_SETFD, 0);
    return 0;
}

168 169 170 171
static void ProcessWatchEvent_(const WatcherHandle watcherHandle, int fd, uint32_t *events, const void *context)
{
    *events = 0;
    Service *service = (Service *)context;
172
    ServiceSocket *tmpSock = service->socketCfg;
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
    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);
    SocketDelWatcher(watcherHandle);
    if (ServiceStart(service) != SERVICE_SUCCESS) {
        INIT_LOGE("Service %s start failed!", service->name);
    }
}

int SocketAddWatcher(ServiceWatcher *watcherHandle, Service *service, int fd)
{
    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);
    INIT_LOGI("Start to monitor socket, fd:%d service name:%s", fd, service->name);
    *watcherHandle = (ServiceWatcher)handle;
    return ret;
}

void SocketDelWatcher(ServiceWatcher watcherHandle)
{
    LE_RemoveWatcher(LE_GetDefaultLoop(), (WatcherHandle)watcherHandle);
}

X
xionglei6 已提交
211
int CreateServiceSocket(Service *service)
212
{
X
xionglei6 已提交
213
    INIT_CHECK(service != NULL && service->socketCfg != NULL, return 0);
X
xionglei6 已提交
214
    INIT_CHECK(service->socketCfg->sockFd == -1, return 0);
X
xionglei6 已提交
215 216
    int ret = 0;
    ServiceSocket *tmpSock = service->socketCfg;
217 218
    while (tmpSock != NULL) {
        int fd = CreateSocket(tmpSock);
X
add ut  
xionglei6 已提交
219
        INIT_CHECK_RETURN_VALUE(fd >= 0, -1);
X
xionglei6 已提交
220
        if (IsOnDemandService(service)) {
221 222 223 224
            if (IsConnectionBasedSocket(tmpSock)) {
                ret = listen(tmpSock->sockFd, MAX_SOCKET_FD_LEN);
                INIT_CHECK_RETURN_VALUE(ret == 0, -1);
            }
225 226 227 228
            if (strcmp(service->name, "ueventd") != 0) {
                ret = SocketAddWatcher(&tmpSock->watcher, service, tmpSock->sockFd);
                INIT_CHECK_RETURN_VALUE(ret == 0, -1);
            }
X
xionglei6 已提交
229 230
        }
        ret = SetSocketEnv(fd, tmpSock->name);
X
add ut  
xionglei6 已提交
231
        INIT_CHECK_RETURN_VALUE(ret >= 0, -1);
232 233 234 235 236
        tmpSock = tmpSock->next;
    }
    return 0;
}

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