ueventd_socket.c 2.8 KB
Newer Older
S
sun_fan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * 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 <poll.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <ctype.h>
#include <signal.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <linux/netlink.h>
#include <sys/stat.h>
#include <sys/sysmacros.h>
#include "securec.h"
#define INIT_LOG_TAG "ueventd"
#include "init_log.h"

S
sun_fan 已提交
33 34 35
#define UEVENT_SOCKET_BUFF_SIZE (256 * 1024)

int UeventdSocketInit(void)
S
sun_fan 已提交
36 37
{
    struct sockaddr_nl addr;
S
sun_fan 已提交
38
    int buffSize = UEVENT_SOCKET_BUFF_SIZE;
S
sun_fan 已提交
39 40 41 42 43 44 45 46 47 48
    int on = 1;

    if (memset_s(&addr, sizeof(addr), 0, sizeof(addr) != EOK)) {
        INIT_LOGE("Faild to clear socket address");
        return -1;
    }
    addr.nl_family = AF_NETLINK;
    addr.nl_pid = getpid();
    addr.nl_groups = 0xffffffff;

S
sun_fan 已提交
49
    int sockfd = socket(PF_NETLINK, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, NETLINK_KOBJECT_UEVENT);
S
sun_fan 已提交
50 51 52 53 54 55 56 57
    if (sockfd < 0) {
        INIT_LOGE("Create socket failed, err = %d", errno);
        return -1;
    }

    setsockopt(sockfd, SOL_SOCKET, SO_RCVBUFFORCE, &buffSize, sizeof(buffSize));
    setsockopt(sockfd, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on));

S
sun_fan 已提交
58
    if (bind(sockfd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
S
sun_fan 已提交
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
        INIT_LOGE("Bind socket failed, err = %d", errno);
        close(sockfd);
        return -1;
    }
    return sockfd;
}

ssize_t ReadUeventMessage(int sockFd, char *buffer, size_t length)
{
    ssize_t n = -1;
    struct msghdr msghdr;
    struct iovec iov;
    struct sockaddr_nl addr;
    char credMsg[CMSG_SPACE(sizeof(struct ucred))];

    // sanity check
    if (sockFd < 0 || buffer == NULL) {
        return n;
    }

    iov.iov_base = buffer;
    iov.iov_len = length;
    msghdr.msg_name = &addr;
    msghdr.msg_namelen = sizeof(addr);
    msghdr.msg_iov = &iov;
    msghdr.msg_iovlen = 1;
    msghdr.msg_control = credMsg;
    msghdr.msg_controllen = sizeof(credMsg);

    n = recvmsg(sockFd, &msghdr, 0);
    if (n <= 0) {
        return n;
    }
S
sun_fan 已提交
92
    struct cmsghdr *cmsghdr = CMSG_FIRSTHDR(&msghdr);
S
sun_fan 已提交
93 94 95 96 97 98 99 100
    if (cmsghdr == NULL || cmsghdr->cmsg_type != SCM_CREDENTIALS) {
        INIT_LOGE("Unexpected control message, ignored");
        // Drop this message
        *buffer = '\0';
        n = -1;
    }
    return n;
}