1d9f0492fSopenharmony_ci/* 2d9f0492fSopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd. 3d9f0492fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4d9f0492fSopenharmony_ci * you may not use this file except in compliance with the License. 5d9f0492fSopenharmony_ci * You may obtain a copy of the License at 6d9f0492fSopenharmony_ci * 7d9f0492fSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8d9f0492fSopenharmony_ci * 9d9f0492fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10d9f0492fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11d9f0492fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12d9f0492fSopenharmony_ci * See the License for the specific language governing permissions and 13d9f0492fSopenharmony_ci * limitations under the License. 14d9f0492fSopenharmony_ci */ 15d9f0492fSopenharmony_ci 16d9f0492fSopenharmony_ci#include <poll.h> 17d9f0492fSopenharmony_ci#include <stdlib.h> 18d9f0492fSopenharmony_ci#include <errno.h> 19d9f0492fSopenharmony_ci#include <unistd.h> 20d9f0492fSopenharmony_ci#include <sys/socket.h> 21d9f0492fSopenharmony_ci#include <linux/netlink.h> 22d9f0492fSopenharmony_ci#include "securec.h" 23d9f0492fSopenharmony_ci#define INIT_LOG_TAG "ueventd" 24d9f0492fSopenharmony_ci#include "init_log.h" 25d9f0492fSopenharmony_ci 26d9f0492fSopenharmony_ci#define UEVENT_SOCKET_BUFF_SIZE (256 * 1024) 27d9f0492fSopenharmony_ci 28d9f0492fSopenharmony_ciint UeventdSocketInit(void) 29d9f0492fSopenharmony_ci{ 30d9f0492fSopenharmony_ci struct sockaddr_nl addr; 31d9f0492fSopenharmony_ci int buffSize = UEVENT_SOCKET_BUFF_SIZE; 32d9f0492fSopenharmony_ci int on = 1; 33d9f0492fSopenharmony_ci 34d9f0492fSopenharmony_ci if (memset_s(&addr, sizeof(addr), 0, sizeof(addr)) != EOK) { 35d9f0492fSopenharmony_ci INIT_LOGE("Failed to clear socket address"); 36d9f0492fSopenharmony_ci return -1; 37d9f0492fSopenharmony_ci } 38d9f0492fSopenharmony_ci addr.nl_family = AF_NETLINK; 39d9f0492fSopenharmony_ci addr.nl_pid = getpid(); 40d9f0492fSopenharmony_ci addr.nl_groups = 0xffffffff; 41d9f0492fSopenharmony_ci 42d9f0492fSopenharmony_ci int sockfd = socket(PF_NETLINK, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, NETLINK_KOBJECT_UEVENT); 43d9f0492fSopenharmony_ci if (sockfd < 0) { 44d9f0492fSopenharmony_ci INIT_LOGE("Create socket failed, err = %d", errno); 45d9f0492fSopenharmony_ci return -1; 46d9f0492fSopenharmony_ci } 47d9f0492fSopenharmony_ci 48d9f0492fSopenharmony_ci setsockopt(sockfd, SOL_SOCKET, SO_RCVBUFFORCE, &buffSize, sizeof(buffSize)); 49d9f0492fSopenharmony_ci setsockopt(sockfd, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on)); 50d9f0492fSopenharmony_ci 51d9f0492fSopenharmony_ci if (bind(sockfd, (struct sockaddr *)&addr, sizeof(addr)) < 0) { 52d9f0492fSopenharmony_ci INIT_LOGE("Bind socket failed, err = %d", errno); 53d9f0492fSopenharmony_ci#ifndef STARTUP_INIT_TEST 54d9f0492fSopenharmony_ci close(sockfd); 55d9f0492fSopenharmony_ci return -1; 56d9f0492fSopenharmony_ci#endif 57d9f0492fSopenharmony_ci } 58d9f0492fSopenharmony_ci return sockfd; 59d9f0492fSopenharmony_ci} 60d9f0492fSopenharmony_ci 61d9f0492fSopenharmony_cissize_t ReadUeventMessage(int sockFd, char *buffer, size_t length) 62d9f0492fSopenharmony_ci{ 63d9f0492fSopenharmony_ci ssize_t n = -1; 64d9f0492fSopenharmony_ci struct msghdr msghdr = {}; 65d9f0492fSopenharmony_ci struct iovec iov; 66d9f0492fSopenharmony_ci struct sockaddr_nl addr; 67d9f0492fSopenharmony_ci char credMsg[CMSG_SPACE(sizeof(struct ucred))]; 68d9f0492fSopenharmony_ci 69d9f0492fSopenharmony_ci // sanity check 70d9f0492fSopenharmony_ci if (sockFd < 0 || buffer == NULL) { 71d9f0492fSopenharmony_ci return n; 72d9f0492fSopenharmony_ci } 73d9f0492fSopenharmony_ci 74d9f0492fSopenharmony_ci iov.iov_base = buffer; 75d9f0492fSopenharmony_ci iov.iov_len = length; 76d9f0492fSopenharmony_ci msghdr.msg_name = &addr; 77d9f0492fSopenharmony_ci msghdr.msg_namelen = sizeof(addr); 78d9f0492fSopenharmony_ci msghdr.msg_iov = &iov; 79d9f0492fSopenharmony_ci msghdr.msg_iovlen = 1; 80d9f0492fSopenharmony_ci msghdr.msg_control = credMsg; 81d9f0492fSopenharmony_ci msghdr.msg_controllen = sizeof(credMsg); 82d9f0492fSopenharmony_ci 83d9f0492fSopenharmony_ci n = recvmsg(sockFd, &msghdr, 0); 84d9f0492fSopenharmony_ci if (n <= 0) { 85d9f0492fSopenharmony_ci return n; 86d9f0492fSopenharmony_ci } 87d9f0492fSopenharmony_ci struct cmsghdr *cmsghdr = CMSG_FIRSTHDR(&msghdr); 88d9f0492fSopenharmony_ci if (cmsghdr == NULL || cmsghdr->cmsg_type != SCM_CREDENTIALS) { 89d9f0492fSopenharmony_ci INIT_LOGE("Unexpected control message, ignored"); 90d9f0492fSopenharmony_ci // Drop this message 91d9f0492fSopenharmony_ci *buffer = '\0'; 92d9f0492fSopenharmony_ci n = -1; 93d9f0492fSopenharmony_ci } 94d9f0492fSopenharmony_ci return n; 95d9f0492fSopenharmony_ci} 96