2021-08-31 12:18:48 +00:00
|
|
|
/*
|
|
|
|
* 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 "ueventd_device_handler.h"
|
2021-09-17 07:43:10 +00:00
|
|
|
|
2021-08-31 12:18:48 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <libgen.h>
|
2021-09-17 07:43:10 +00:00
|
|
|
#include <limits.h>
|
2021-08-31 12:18:48 +00:00
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/sysmacros.h>
|
2021-09-17 07:43:10 +00:00
|
|
|
#include "init_utils.h"
|
2021-08-31 12:18:48 +00:00
|
|
|
#include "ueventd.h"
|
2021-12-30 09:18:32 +00:00
|
|
|
#ifndef __RAMDISK__
|
2021-12-30 08:22:22 +00:00
|
|
|
#include "ueventd_parameter.h"
|
2021-12-30 09:18:32 +00:00
|
|
|
#endif
|
2021-08-31 12:18:48 +00:00
|
|
|
#include "ueventd_read_cfg.h"
|
|
|
|
#include "ueventd_utils.h"
|
|
|
|
#include "securec.h"
|
|
|
|
#define INIT_LOG_TAG "ueventd"
|
|
|
|
#include "init_log.h"
|
2022-02-22 03:25:15 +00:00
|
|
|
#ifdef WITH_SELINUX
|
2023-08-02 07:28:23 +00:00
|
|
|
#include <selinux/selinux.h>
|
2022-02-22 03:25:15 +00:00
|
|
|
#include <policycoreutils.h>
|
|
|
|
#endif
|
2021-08-31 12:18:48 +00:00
|
|
|
|
2023-06-14 06:39:42 +00:00
|
|
|
static inline void AdjustDeviceNodePermissions(const char *deviceNode, uid_t uid, gid_t gid, mode_t mode)
|
|
|
|
{
|
|
|
|
if (INVALIDSTRING(deviceNode)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (chown(deviceNode, uid, gid) != 0) {
|
|
|
|
INIT_LOGW("Failed to change \" %s \" owner, errno %d", deviceNode, errno);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (chmod(deviceNode, mode) != 0) {
|
|
|
|
INIT_LOGW("Failed to change \" %s \" mode, errno %d", deviceNode, errno);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-31 12:18:48 +00:00
|
|
|
static void CreateSymbolLinks(const char *deviceNode, char **symLinks)
|
|
|
|
{
|
|
|
|
if (INVALIDSTRING(deviceNode) || symLinks == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-06-14 06:39:42 +00:00
|
|
|
uid_t uid = 0;
|
|
|
|
gid_t gid = 0;
|
|
|
|
mode_t mode = DEVMODE;
|
|
|
|
|
2021-08-31 12:18:48 +00:00
|
|
|
for (int i = 0; symLinks[i] != NULL; i++) {
|
|
|
|
const char *linkName = symLinks[i];
|
|
|
|
char linkBuf[DEVICE_FILE_SIZE] = {};
|
|
|
|
|
2023-06-14 06:39:42 +00:00
|
|
|
if (strstr(linkName, "/dev/block/by-name") != NULL) {
|
|
|
|
int res = GetDeviceNodePermissions(linkName, &uid, &gid, &mode);
|
|
|
|
INIT_CHECK(res != 0, AdjustDeviceNodePermissions(deviceNode, uid, gid, mode));
|
|
|
|
}
|
|
|
|
|
2021-08-31 12:18:48 +00:00
|
|
|
if (strncpy_s(linkBuf, DEVICE_FILE_SIZE - 1, linkName, strlen(linkName)) != EOK) {
|
|
|
|
INIT_LOGE("Failed to copy link name");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const char *linkDir = dirname(linkBuf);
|
|
|
|
if (MakeDirRecursive(linkDir, DIRMODE) < 0) {
|
2021-11-11 07:37:48 +00:00
|
|
|
INIT_LOGE("[uevent] Failed to create dir \" %s \", err = %d", linkDir, errno);
|
2022-07-13 02:28:27 +00:00
|
|
|
return;
|
|
|
|
}
|
2023-05-17 09:40:41 +00:00
|
|
|
|
2021-08-31 12:18:48 +00:00
|
|
|
errno = 0;
|
2023-07-17 01:25:04 +00:00
|
|
|
INIT_LOGI("symlink %s->%s", deviceNode, linkName);
|
2021-08-31 12:18:48 +00:00
|
|
|
int rc = symlink(deviceNode, linkName);
|
|
|
|
if (rc != 0) {
|
2022-09-16 03:27:53 +00:00
|
|
|
if (errno != EEXIST) {
|
2021-08-31 12:18:48 +00:00
|
|
|
INIT_LOGE("Failed to link \" %s \" to \" %s \", err = %d", deviceNode, linkName, errno);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-08 07:09:35 +00:00
|
|
|
#if defined(WITH_SELINUX) && !defined(__RAMDISK__)
|
2023-08-02 07:28:23 +00:00
|
|
|
static void SetDeviceLable(const char *path, char **symLinks)
|
2022-02-22 03:25:15 +00:00
|
|
|
{
|
|
|
|
int rc = 0;
|
2022-04-22 04:15:21 +00:00
|
|
|
char buffer[PATH_MAX] = {};
|
|
|
|
const char *p = NULL;
|
|
|
|
char *slash = NULL;
|
|
|
|
|
|
|
|
p = path;
|
|
|
|
slash = strchr(path, '/');
|
|
|
|
while (slash != NULL) {
|
|
|
|
int gap = slash - p;
|
|
|
|
p = slash + 1;
|
|
|
|
if (gap == 0) {
|
|
|
|
slash = strchr(p, '/');
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (gap < 0) { // end with '/'
|
2022-09-16 03:15:52 +00:00
|
|
|
return;
|
2022-04-22 04:15:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (memcpy_s(buffer, PATH_MAX, path, p - path - 1) != EOK) {
|
|
|
|
INIT_LOGE("[uevent] Failed to memcpy path %s", path);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
rc += Restorecon(buffer);
|
|
|
|
slash = strchr(p, '/');
|
2022-02-22 03:25:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rc += Restorecon(path);
|
|
|
|
if (rc != 0) {
|
2022-04-22 04:15:21 +00:00
|
|
|
INIT_LOGE("[uevent] Failed to Restorecon \" %s \"", path);
|
2022-02-22 03:25:15 +00:00
|
|
|
}
|
2022-04-22 04:15:21 +00:00
|
|
|
|
2023-08-02 07:28:23 +00:00
|
|
|
INIT_CHECK_ONLY_RETURN(symLinks != NULL);
|
|
|
|
char *context = NULL;
|
|
|
|
for (int i = 0; symLinks[i] != NULL; i++) {
|
|
|
|
const char *linkName = symLinks[i];
|
|
|
|
const char *byNamePath = "/dev/block/by-name";
|
|
|
|
if (strncmp(linkName, byNamePath, strlen(byNamePath)) == 0) {
|
|
|
|
(void)Restorecon(linkName);
|
|
|
|
lgetfilecon(linkName, &context);
|
2023-08-11 09:46:23 +00:00
|
|
|
if (context != NULL) {
|
|
|
|
setfilecon(path, context);
|
|
|
|
}
|
2023-08-02 07:28:23 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-22 04:15:21 +00:00
|
|
|
return;
|
2022-02-22 03:25:15 +00:00
|
|
|
}
|
2023-05-08 07:09:35 +00:00
|
|
|
#endif
|
2022-02-22 03:25:15 +00:00
|
|
|
|
2021-08-31 12:18:48 +00:00
|
|
|
static int CreateDeviceNode(const struct Uevent *uevent, const char *deviceNode, char **symLinks, bool isBlock)
|
|
|
|
{
|
|
|
|
int rc = -1;
|
2021-10-14 08:43:33 +00:00
|
|
|
int major = uevent->major;
|
|
|
|
int minor = uevent->minor;
|
2021-08-31 12:18:48 +00:00
|
|
|
uid_t uid = uevent->ug.uid;
|
|
|
|
gid_t gid = uevent->ug.gid;
|
|
|
|
mode_t mode = DEVMODE;
|
|
|
|
|
|
|
|
if (deviceNode == NULL || *deviceNode == '\0') {
|
|
|
|
INIT_LOGE("Invalid device file");
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
char deviceNodeBuffer[DEVICE_FILE_SIZE] = {};
|
|
|
|
if (strncpy_s(deviceNodeBuffer, DEVICE_FILE_SIZE - 1, deviceNode, strlen(deviceNode)) != EOK) {
|
|
|
|
INIT_LOGE("Failed to copy device node");
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
const char *devicePath = dirname(deviceNodeBuffer);
|
|
|
|
// device node always installed in /dev, should not be other locations.
|
|
|
|
if (STRINGEQUAL(devicePath, ".") || STRINGEQUAL(devicePath, "/")) {
|
|
|
|
INIT_LOGE("device path is not valid. should be starts with /dev");
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = MakeDirRecursive(devicePath, DIRMODE);
|
|
|
|
if (rc < 0) {
|
|
|
|
INIT_LOGE("Create path \" %s \" failed", devicePath);
|
|
|
|
return rc;
|
|
|
|
}
|
2021-09-13 11:58:03 +00:00
|
|
|
|
2023-06-14 06:39:42 +00:00
|
|
|
(void)GetDeviceNodePermissions(deviceNode, &uid, &gid, &mode);
|
2021-08-31 12:18:48 +00:00
|
|
|
mode |= isBlock ? S_IFBLK : S_IFCHR;
|
|
|
|
dev_t dev = makedev(major, minor);
|
|
|
|
setegid(0);
|
|
|
|
rc = mknod(deviceNode, mode, dev);
|
|
|
|
if (rc < 0) {
|
|
|
|
if (errno != EEXIST) {
|
2021-09-13 11:58:03 +00:00
|
|
|
INIT_LOGE("Create device node[%s %d, %d] failed. %d", deviceNode, major, minor, errno);
|
2021-08-31 12:18:48 +00:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
AdjustDeviceNodePermissions(deviceNode, uid, gid, mode);
|
2021-09-13 11:58:03 +00:00
|
|
|
if (symLinks != NULL) {
|
2021-08-31 12:18:48 +00:00
|
|
|
CreateSymbolLinks(deviceNode, symLinks);
|
|
|
|
}
|
2023-05-08 07:09:35 +00:00
|
|
|
#if defined(WITH_SELINUX) && !defined(__RAMDISK__)
|
2023-08-02 07:28:23 +00:00
|
|
|
SetDeviceLable(deviceNode, symLinks);
|
2023-05-08 07:09:35 +00:00
|
|
|
#endif
|
2021-09-13 11:58:03 +00:00
|
|
|
// No matter what result the symbol links returns,
|
2021-08-31 12:18:48 +00:00
|
|
|
// as long as create device node done, just returns success.
|
|
|
|
rc = 0;
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int RemoveDeviceNode(const char *deviceNode, char **symLinks)
|
|
|
|
{
|
|
|
|
if (INVALIDSTRING(deviceNode)) {
|
|
|
|
INIT_LOGE("Invalid device node");
|
2021-09-13 11:58:03 +00:00
|
|
|
return -1;
|
2021-08-31 12:18:48 +00:00
|
|
|
}
|
|
|
|
if (symLinks != NULL) {
|
|
|
|
for (int i = 0; symLinks[i] != NULL; i++) {
|
2022-04-28 09:04:39 +00:00
|
|
|
char realPath[DEVICE_FILE_SIZE] = {0};
|
2021-08-31 12:18:48 +00:00
|
|
|
const char *linkName = symLinks[i];
|
|
|
|
ssize_t ret = readlink(linkName, realPath, DEVICE_FILE_SIZE - 1);
|
|
|
|
if (ret < 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (STRINGEQUAL(deviceNode, realPath)) {
|
2023-07-24 08:54:14 +00:00
|
|
|
INIT_LOGI("unlink %s", linkName);
|
2021-08-31 12:18:48 +00:00
|
|
|
unlink(linkName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-07-24 08:54:14 +00:00
|
|
|
INIT_LOGI("unlink %s", deviceNode);
|
2021-08-31 12:18:48 +00:00
|
|
|
return unlink(deviceNode);
|
|
|
|
}
|
|
|
|
|
2021-09-13 12:53:53 +00:00
|
|
|
static char *FindPlatformDeviceName(char *path)
|
|
|
|
{
|
|
|
|
if (INVALIDSTRING(path)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (STARTSWITH(path, "/sys/devices/platform/")) {
|
|
|
|
path += strlen("/sys/devices/platform/");
|
|
|
|
return path;
|
|
|
|
}
|
2021-10-14 08:43:33 +00:00
|
|
|
|
|
|
|
// Some platform devices may not be registered under platform device.
|
|
|
|
if (STARTSWITH(path, "/sys/devices/")) {
|
|
|
|
path += strlen("/sys/devices/");
|
|
|
|
return path;
|
|
|
|
}
|
2021-09-13 12:53:53 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2023-05-17 09:40:41 +00:00
|
|
|
static int BuildDeviceSymbolLinks(char **links, int linkNum, const char *parent,
|
2021-09-28 15:05:14 +00:00
|
|
|
const char *partitionName, const char *deviceName)
|
2021-09-13 12:53:53 +00:00
|
|
|
{
|
2023-05-17 09:40:41 +00:00
|
|
|
int num = linkNum;
|
|
|
|
links[num] = calloc(DEVICE_FILE_SIZE, sizeof(char));
|
|
|
|
if (links[num] == NULL) {
|
2021-09-28 15:05:14 +00:00
|
|
|
INIT_LOGE("Failed to allocate memory for link, err = %d", errno);
|
2023-05-17 09:40:41 +00:00
|
|
|
return num;
|
2021-09-13 12:53:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If a block device without partition name.
|
|
|
|
// For now, we will not create symbol link for it.
|
|
|
|
if (!INVALIDSTRING(partitionName)) {
|
2023-05-17 09:40:41 +00:00
|
|
|
if (snprintf_s(links[num], DEVICE_FILE_SIZE, DEVICE_FILE_SIZE - 1,
|
2021-09-29 07:06:52 +00:00
|
|
|
"/dev/block/platform/%s/by-name/%s", parent, partitionName) == -1) {
|
2021-09-28 15:05:14 +00:00
|
|
|
INIT_LOGE("Failed to build link");
|
2021-09-13 12:53:53 +00:00
|
|
|
}
|
2023-05-17 09:40:41 +00:00
|
|
|
if (STRINGEQUAL(parent, bootDevice)) {
|
|
|
|
num = linkNum + 1;
|
|
|
|
links[num] = calloc(DEVICE_FILE_SIZE, sizeof(char));
|
|
|
|
if (links[num] == NULL) {
|
|
|
|
INIT_LOGE("Failed to allocate memory for link, err = %d", errno);
|
|
|
|
return linkNum;
|
|
|
|
}
|
|
|
|
if (snprintf_s(links[num], DEVICE_FILE_SIZE, DEVICE_FILE_SIZE - 1,
|
|
|
|
"/dev/block/by-name/%s", partitionName) == -1) {
|
|
|
|
INIT_LOGE("Failed to build link");
|
|
|
|
}
|
|
|
|
}
|
2021-09-28 15:05:14 +00:00
|
|
|
} else if (!INVALIDSTRING(deviceName)) {
|
2023-05-17 09:40:41 +00:00
|
|
|
if (snprintf_s(links[num], DEVICE_FILE_SIZE, DEVICE_FILE_SIZE - 1,
|
2021-11-03 01:35:08 +00:00
|
|
|
"/dev/block/platform/%s/%s", parent, deviceName) == -1) {
|
2021-09-13 12:53:53 +00:00
|
|
|
INIT_LOGE("Failed to build link");
|
|
|
|
}
|
|
|
|
}
|
2023-05-17 09:40:41 +00:00
|
|
|
|
|
|
|
return num;
|
2021-09-13 12:53:53 +00:00
|
|
|
}
|
|
|
|
|
2022-03-29 08:31:22 +00:00
|
|
|
static void FreeSymbolLinks(char **links, int length)
|
|
|
|
{
|
|
|
|
if (links != NULL) {
|
|
|
|
for (int i = 0; i < length && links[i] != NULL; i++) {
|
|
|
|
free(links[i]);
|
|
|
|
links[i] = NULL;
|
|
|
|
}
|
|
|
|
free(links);
|
|
|
|
links = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-31 12:18:48 +00:00
|
|
|
static char **GetBlockDeviceSymbolLinks(const struct Uevent *uevent)
|
|
|
|
{
|
2021-10-14 08:43:33 +00:00
|
|
|
if (uevent == NULL || uevent->subsystem == NULL || STRINGEQUAL(uevent->subsystem, "block") == 0) {
|
2021-08-31 12:18:48 +00:00
|
|
|
INIT_LOGW("Invalid arguments, Skip to get device symbol links.");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only if current uevent is for real device.
|
|
|
|
if (!STARTSWITH(uevent->syspath, "/devices")) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
// For block device under one platform device.
|
|
|
|
// check subsystem file under directory, see if it links to bus/platform.
|
|
|
|
// For now, only support platform device.
|
2021-09-13 11:58:03 +00:00
|
|
|
char sysPath[SYSPATH_SIZE] = {};
|
2021-08-31 12:18:48 +00:00
|
|
|
if (snprintf_s(sysPath, SYSPATH_SIZE, SYSPATH_SIZE - 1, "/sys%s", uevent->syspath) == -1) {
|
|
|
|
INIT_LOGE("Failed to build sys path for device %s", uevent->syspath);
|
|
|
|
return NULL;
|
|
|
|
}
|
2022-11-18 09:11:27 +00:00
|
|
|
char **links = calloc(BLOCKDEVICE_LINKS, sizeof(char *));
|
2021-08-31 12:18:48 +00:00
|
|
|
int linkNum = 0;
|
|
|
|
if (links == NULL) {
|
|
|
|
INIT_LOGE("Failed to allocate memory for links, err = %d", errno);
|
2021-09-13 12:53:53 +00:00
|
|
|
return NULL;
|
2021-08-31 12:18:48 +00:00
|
|
|
}
|
|
|
|
|
2022-06-18 14:57:25 +00:00
|
|
|
// Reverse walk through sysPath, and check subsystem file under each directory.
|
2021-08-31 12:18:48 +00:00
|
|
|
char *parent = dirname(sysPath);
|
|
|
|
while (parent != NULL && !STRINGEQUAL(parent, "/") && !STRINGEQUAL(parent, ".")) {
|
2021-10-14 08:43:33 +00:00
|
|
|
char subsystem[SYSPATH_SIZE];
|
2021-08-31 12:18:48 +00:00
|
|
|
if (snprintf_s(subsystem, SYSPATH_SIZE, SYSPATH_SIZE - 1, "%s/subsystem", parent) == -1) {
|
|
|
|
INIT_LOGE("Failed to build subsystem path for device \" %s \"", uevent->syspath);
|
2022-03-29 08:31:22 +00:00
|
|
|
FreeSymbolLinks(links, BLOCKDEVICE_LINKS);
|
2021-08-31 12:18:48 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2021-10-14 08:43:33 +00:00
|
|
|
char *bus = GetRealPath(subsystem);
|
|
|
|
if (bus == NULL) {
|
2021-09-29 04:43:30 +00:00
|
|
|
parent = dirname(parent);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (STRINGEQUAL(bus, "/sys/bus/platform")) {
|
2022-01-19 08:08:48 +00:00
|
|
|
INIT_LOGV("Find a platform device: %s", parent);
|
2021-09-29 04:43:30 +00:00
|
|
|
parent = FindPlatformDeviceName(parent);
|
|
|
|
if (parent != NULL) {
|
2023-05-17 09:40:41 +00:00
|
|
|
INIT_WARNING_CHECK(linkNum < BLOCKDEVICE_LINKS - 1, links[linkNum] = NULL;
|
|
|
|
return links, "Too many links, ignore");
|
|
|
|
linkNum = BuildDeviceSymbolLinks(links, linkNum, parent, uevent->partitionName, uevent->deviceName);
|
2021-11-11 07:37:48 +00:00
|
|
|
linkNum++;
|
|
|
|
}
|
2021-08-31 12:18:48 +00:00
|
|
|
}
|
2021-10-14 08:43:33 +00:00
|
|
|
free(bus);
|
2021-08-31 12:18:48 +00:00
|
|
|
parent = dirname(parent);
|
|
|
|
}
|
2021-09-13 12:53:53 +00:00
|
|
|
|
2021-08-31 12:18:48 +00:00
|
|
|
links[linkNum] = NULL;
|
|
|
|
return links;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void HandleDeviceNode(const struct Uevent *uevent, const char *deviceNode, bool isBlock)
|
|
|
|
{
|
|
|
|
ACTION action = uevent->action;
|
|
|
|
char **symLinks = NULL;
|
|
|
|
|
|
|
|
// Block device path and name maybe not human readable.
|
|
|
|
// Consider to create symbol links for them.
|
|
|
|
// Make block device more readable.
|
|
|
|
if (isBlock) {
|
|
|
|
symLinks = GetBlockDeviceSymbolLinks(uevent);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (action == ACTION_ADD) {
|
|
|
|
if (CreateDeviceNode(uevent, deviceNode, symLinks, isBlock) < 0) {
|
|
|
|
INIT_LOGE("Create device \" %s \" failed", deviceNode);
|
2021-12-30 08:22:22 +00:00
|
|
|
} else {
|
2021-12-30 09:18:32 +00:00
|
|
|
#ifndef __RAMDISK__
|
2022-01-19 08:08:48 +00:00
|
|
|
if (SetUeventDeviceParameter(deviceNode, action) != 0) {
|
2021-12-30 08:22:22 +00:00
|
|
|
INIT_LOGE("Set device parameter added failed");
|
|
|
|
}
|
2021-12-30 09:18:32 +00:00
|
|
|
#endif
|
2021-08-31 12:18:48 +00:00
|
|
|
}
|
|
|
|
} else if (action == ACTION_REMOVE) {
|
|
|
|
if (RemoveDeviceNode(deviceNode, symLinks) < 0) {
|
|
|
|
INIT_LOGE("Remove device \" %s \" failed", deviceNode);
|
2021-12-30 08:22:22 +00:00
|
|
|
} else {
|
2021-12-30 09:18:32 +00:00
|
|
|
#ifndef __RAMDISK__
|
2022-01-19 08:08:48 +00:00
|
|
|
if (SetUeventDeviceParameter(deviceNode, action) != 0) {
|
2021-12-30 08:22:22 +00:00
|
|
|
INIT_LOGE("Set device parameter removed failed");
|
|
|
|
}
|
2021-12-30 09:18:32 +00:00
|
|
|
#endif
|
2021-08-31 12:18:48 +00:00
|
|
|
}
|
|
|
|
} else if (action == ACTION_CHANGE) {
|
2022-01-27 07:51:30 +00:00
|
|
|
INIT_LOGV("Device %s changed", uevent->syspath);
|
2021-08-31 12:18:48 +00:00
|
|
|
}
|
|
|
|
// Ignore other actions
|
2022-03-29 08:31:22 +00:00
|
|
|
FreeSymbolLinks(symLinks, BLOCKDEVICE_LINKS);
|
2021-08-31 12:18:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static const char *GetDeviceName(char *sysPath, const char *deviceName)
|
|
|
|
{
|
|
|
|
const char *devName = NULL;
|
|
|
|
if (INVALIDSTRING(sysPath)) {
|
|
|
|
INIT_LOGE("Invalid sys path");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (deviceName != NULL && deviceName[0] != '\0') {
|
|
|
|
// if device name reported by kernel includes '/', skip it.
|
2021-10-15 11:23:39 +00:00
|
|
|
// use entire device name reported by kernel
|
2021-08-31 12:18:48 +00:00
|
|
|
devName = basename((char *)deviceName);
|
|
|
|
char *p = strrchr(deviceName, '/');
|
|
|
|
if (p != NULL) { // device name includes slash
|
|
|
|
p++;
|
|
|
|
if (p == NULL || *p == '\0') {
|
|
|
|
// device name ends with '/', which should never happen.
|
|
|
|
// Get name from sys path.
|
|
|
|
devName = basename(sysPath);
|
|
|
|
} else {
|
|
|
|
devName = p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2021-09-07 09:45:06 +00:00
|
|
|
// kernel does not report DEVNAME, which is possible. use base name of syspath instead.
|
2021-08-31 12:18:48 +00:00
|
|
|
devName = basename(sysPath);
|
|
|
|
}
|
|
|
|
return devName;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *GetDeviceBasePath(const char *subsystem)
|
|
|
|
{
|
|
|
|
char *devPath = NULL;
|
|
|
|
if (INVALIDSTRING(subsystem)) {
|
|
|
|
return devPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (STRINGEQUAL(subsystem, "block")) {
|
|
|
|
devPath = "/dev/block";
|
|
|
|
} else if (STRINGEQUAL(subsystem, "input")) {
|
|
|
|
devPath = "/dev/input";
|
|
|
|
} else if (STRINGEQUAL(subsystem, "drm")) {
|
|
|
|
devPath = "/dev/dri";
|
|
|
|
} else if (STRINGEQUAL(subsystem, "graphics")) {
|
|
|
|
devPath = "/dev/graphics";
|
|
|
|
} else if (STRINGEQUAL(subsystem, "sound")) {
|
|
|
|
devPath = "/dev/snd";
|
2021-09-06 02:52:23 +00:00
|
|
|
} else if (STRINGEQUAL(subsystem, "functionfs")) {
|
|
|
|
devPath = "/dev/functionfs";
|
2022-01-13 01:13:24 +00:00
|
|
|
} else if (STRINGEQUAL(subsystem, "dma_heap")) {
|
|
|
|
devPath = "/dev/dma_heap";
|
2021-08-31 12:18:48 +00:00
|
|
|
} else {
|
|
|
|
devPath = "/dev";
|
|
|
|
}
|
|
|
|
return devPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
void HandleBlockDeviceEvent(const struct Uevent *uevent)
|
|
|
|
{
|
|
|
|
// Sanity checks
|
|
|
|
if (uevent == NULL || uevent->subsystem == NULL) {
|
|
|
|
INIT_LOGE("Invalid uevent message received");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp(uevent->subsystem, "block") != 0) {
|
2022-10-31 06:28:17 +00:00
|
|
|
INIT_LOGE("Unexpected uevent subsystem \" %s \" received in block device handler", uevent->subsystem);
|
2021-08-31 12:18:48 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (uevent->major < 0 || uevent->minor < 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isBlock = true;
|
|
|
|
// Block device always installed into /dev/block
|
|
|
|
const char *devPath = GetDeviceBasePath(uevent->subsystem);
|
|
|
|
char deviceNode[DEVICE_FILE_SIZE] = {};
|
|
|
|
char sysPath[SYSPATH_SIZE] = {};
|
|
|
|
|
2021-09-13 11:58:03 +00:00
|
|
|
if (uevent->syspath == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
2021-08-31 12:18:48 +00:00
|
|
|
if (strncpy_s(sysPath, SYSPATH_SIZE - 1, uevent->syspath, strlen(uevent->syspath) != EOK)) {
|
|
|
|
INIT_LOGE("Failed to copy sys path");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const char *devName = GetDeviceName(sysPath, uevent->deviceName);
|
|
|
|
|
|
|
|
if (devPath == NULL || devName == NULL) {
|
|
|
|
INIT_LOGE("Cannot get device path or device name");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (snprintf_s(deviceNode, DEVICE_FILE_SIZE, DEVICE_FILE_SIZE - 1, "%s/%s", devPath, devName) == -1) {
|
|
|
|
INIT_LOGE("Make device file for device [%d : %d]", uevent->major, uevent->minor);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
HandleDeviceNode(uevent, deviceNode, isBlock);
|
|
|
|
}
|
|
|
|
|
|
|
|
void HandleOtherDeviceEvent(const struct Uevent *uevent)
|
|
|
|
{
|
2021-09-13 11:58:03 +00:00
|
|
|
if (uevent == NULL || uevent->subsystem == NULL || uevent->syspath == NULL) {
|
2021-08-31 12:18:48 +00:00
|
|
|
INIT_LOGE("Invalid uevent received");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (uevent->major < 0 || uevent->minor < 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
char deviceNode[DEVICE_FILE_SIZE] = {};
|
|
|
|
char sysPath[SYSPATH_SIZE] = {};
|
2022-11-17 10:12:20 +00:00
|
|
|
if (strncpy_s(sysPath, SYSPATH_SIZE - 1, uevent->syspath, strlen(uevent->syspath)) != EOK) {
|
2021-08-31 12:18:48 +00:00
|
|
|
INIT_LOGE("Failed to copy sys path");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const char *devName = GetDeviceName(sysPath, uevent->deviceName);
|
|
|
|
const char *devPath = GetDeviceBasePath(uevent->subsystem);
|
|
|
|
|
|
|
|
if (devPath == NULL || devName == NULL) {
|
|
|
|
INIT_LOGE("Cannot get device path or device name");
|
|
|
|
return;
|
|
|
|
}
|
2022-01-19 08:08:48 +00:00
|
|
|
INIT_LOGV("HandleOtherDeviceEvent, devPath = %s, devName = %s", devPath, devName);
|
2021-08-31 12:18:48 +00:00
|
|
|
|
2021-09-13 11:58:03 +00:00
|
|
|
// For usb devices, should take care of it specially.
|
|
|
|
// if usb devices report DEVNAME, just create device node.
|
|
|
|
// otherwise, create deviceNode with bus number and device number.
|
|
|
|
if (STRINGEQUAL(uevent->subsystem, "usb")) {
|
2021-08-31 12:18:48 +00:00
|
|
|
if (uevent->deviceName != NULL) {
|
|
|
|
if (snprintf_s(deviceNode, DEVICE_FILE_SIZE, DEVICE_FILE_SIZE - 1, "/dev/%s", uevent->deviceName) == -1) {
|
|
|
|
INIT_LOGE("Make device file for device [%d : %d]", uevent->major, uevent->minor);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (uevent->busNum < 0 || uevent->devNum < 0) {
|
|
|
|
// usb device should always report bus number and device number.
|
|
|
|
INIT_LOGE("usb device with invalid bus number or device number");
|
|
|
|
return;
|
|
|
|
}
|
2021-09-13 11:58:03 +00:00
|
|
|
if (snprintf_s(deviceNode, DEVICE_FILE_SIZE, DEVICE_FILE_SIZE - 1,
|
2021-08-31 12:18:48 +00:00
|
|
|
"/dev/bus/usb/%03d/%03d", uevent->busNum, uevent->devNum) == -1) {
|
2021-09-13 11:58:03 +00:00
|
|
|
INIT_LOGE("Make usb device node for device [%d : %d]", uevent->busNum, uevent->devNum);
|
2021-08-31 12:18:48 +00:00
|
|
|
}
|
|
|
|
}
|
2021-09-13 11:58:03 +00:00
|
|
|
} else if (STARTSWITH(uevent->subsystem, "usb")) {
|
|
|
|
// Other usb devies, do not handle it.
|
|
|
|
return;
|
|
|
|
} else {
|
2023-01-09 08:19:17 +00:00
|
|
|
if (strcmp(uevent->deviceName, "mapper/control") == 0) {
|
|
|
|
devName = "mapper/control";
|
|
|
|
}
|
2021-08-31 12:18:48 +00:00
|
|
|
if (snprintf_s(deviceNode, DEVICE_FILE_SIZE, DEVICE_FILE_SIZE - 1, "%s/%s", devPath, devName) == -1) {
|
|
|
|
INIT_LOGE("Make device file for device [%d : %d]", uevent->major, uevent->minor);
|
|
|
|
return;
|
|
|
|
}
|
2021-09-13 11:58:03 +00:00
|
|
|
}
|
|
|
|
HandleDeviceNode(uevent, deviceNode, false);
|
2021-08-31 12:18:48 +00:00
|
|
|
}
|