From e4a2c02983d447db49d75c96910e25fbc8b3045f Mon Sep 17 00:00:00 2001 From: zhao_haipeng Date: Wed, 22 Sep 2021 15:11:46 +0800 Subject: [PATCH] ueventd_device_handler moditify adapter sc9830 platform --- ueventd/ueventd_device_handler.c | 128 +++++++++++++------------------ 1 file changed, 55 insertions(+), 73 deletions(-) diff --git a/ueventd/ueventd_device_handler.c b/ueventd/ueventd_device_handler.c index 48a9a4ce4..6afe58f2c 100755 --- a/ueventd/ueventd_device_handler.c +++ b/ueventd/ueventd_device_handler.c @@ -14,16 +14,13 @@ */ #include "ueventd_device_handler.h" - #include #include -#include #include #include #include #include #include -#include "init_utils.h" #include "list.h" #include "ueventd.h" #include "ueventd_read_cfg.h" @@ -107,7 +104,7 @@ static int CreateDeviceNode(const struct Uevent *uevent, const char *deviceNode, INIT_LOGE("Create path \" %s \" failed", devicePath); return rc; } - + GetDeviceNodePermissions(deviceNode, &uid, &gid, &mode); mode |= isBlock ? S_IFBLK : S_IFCHR; dev_t dev = makedev(major, minor); @@ -115,15 +112,15 @@ static int CreateDeviceNode(const struct Uevent *uevent, const char *deviceNode, rc = mknod(deviceNode, mode, dev); if (rc < 0) { if (errno != EEXIST) { - INIT_LOGE("Create device node[%s %d, %d] failed. %d", deviceNode, major, minor, errno); + INIT_LOGE("Create device node[%s %d, %d] failed", deviceNode, major, minor, errno); return rc; } } AdjustDeviceNodePermissions(deviceNode, uid, gid, mode); - if (symLinks != NULL) { + if (symLinks) { CreateSymbolLinks(deviceNode, symLinks); } - // No matter what result the symbol links returns, + // No matter what result the symbol links returns, // as long as create device node done, just returns success. rc = 0; return rc; @@ -131,9 +128,10 @@ static int CreateDeviceNode(const struct Uevent *uevent, const char *deviceNode, static int RemoveDeviceNode(const char *deviceNode, char **symLinks) { + int rc = -1; if (INVALIDSTRING(deviceNode)) { INIT_LOGE("Invalid device node"); - return -1; + return rc; } if (symLinks != NULL) { for (int i = 0; symLinks[i] != NULL; i++) { @@ -151,44 +149,9 @@ static int RemoveDeviceNode(const char *deviceNode, char **symLinks) return unlink(deviceNode); } -static char *FindPlatformDeviceName(char *path) -{ - if (INVALIDSTRING(path)) { - return NULL; - } - - if (STARTSWITH(path, "/sys/devices/platform/")) { - path += strlen("/sys/devices/platform/"); - return path; - } - return NULL; -} - -static void BuildDeviceSymbolLinks(char **links, int linkNum, const char *parent, const char *partitionName) -{ - if (linkNum > BLOCKDEVICE_LINKS - 1) { - INIT_LOGW("Too many links, ignore"); - return; - } - - // If a block device without partition name. - // For now, we will not create symbol link for it. - if (!INVALIDSTRING(partitionName)) { - links[linkNum] = calloc(sizeof(char), DEVICE_FILE_SIZE); - if (links[linkNum] == NULL) { - INIT_LOGE("Failed to allocate memory for link, err = %d", errno); - return; - } - if (snprintf_s(links[linkNum], DEVICE_FILE_SIZE, DEVICE_FILE_SIZE - 1, - "/dev/block/platform/%s/by-name/%s", parent, partitionName) == -1) { - INIT_LOGE("Failed to build link"); - } - } -} - static char **GetBlockDeviceSymbolLinks(const struct Uevent *uevent) { - if (uevent == NULL || uevent->subsystem == NULL || STRINGEQUAL(uevent->subsystem, "block") == 0) { + if (uevent == NULL || !STRINGEQUAL(uevent->subsystem, "block")) { INIT_LOGW("Invalid arguments, Skip to get device symbol links."); return NULL; } @@ -200,7 +163,7 @@ static char **GetBlockDeviceSymbolLinks(const struct Uevent *uevent) // 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. - char sysPath[SYSPATH_SIZE] = {}; + char sysPath[SYSPATH_SIZE] = {}; 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; @@ -210,7 +173,6 @@ static char **GetBlockDeviceSymbolLinks(const struct Uevent *uevent) int linkNum = 0; if (links == NULL) { INIT_LOGE("Failed to allocate memory for links, err = %d", errno); - return NULL; } // Reverse walk through sysPath, and check subystem file under each directory. @@ -222,20 +184,45 @@ static char **GetBlockDeviceSymbolLinks(const struct Uevent *uevent) return NULL; } - char bus[PATH_MAX] = {0}; - if (Realpath(subsystem, bus, sizeof(bus)) != NULL) { - if (STRINGEQUAL(bus, "/sys/bus/platform")) { - INIT_LOGD("Find a platform device: %s", parent); - parent = FindPlatformDeviceName(parent); - if (parent != NULL) { - BuildDeviceSymbolLinks(links, linkNum, parent, uevent->partitionName); + char *bus = realpath(subsystem, NULL); + if (bus == NULL) { + goto loop; + } + + if (STRINGEQUAL(bus, "/sys/bus/platform")) { + INIT_LOGI("Find a platform device: %s", parent); + if (STARTSWITH(parent, "/sys/devices/platform/") || STARTSWITH(parent, "/sys/devices/sdio_emmc")) { + if (STARTSWITH(parent, "/sys/devices/platform/")) { + parent += strlen("/sys/devices/platform/"); + } else { + parent += strlen("/sys/devices/"); + } + + if (linkNum > BLOCKDEVICE_LINKS - 1) { + INIT_LOGW("Too much links, ignore"); + break; + } + links[linkNum] = calloc(sizeof(char), DEVICE_FILE_SIZE); + if (links[linkNum] == NULL) { + INIT_LOGE("Failed to allocate memory for link, err = %d", errno); + break; + } + // If a block device without partition name. + // For now, we will not create symbol link for it. + if (!INVALIDSTRING(uevent->partitionName)) { + if (snprintf_s(links[linkNum], DEVICE_FILE_SIZE, DEVICE_FILE_SIZE - 1, + "/dev/block/platform/%s/by-name/%s", parent, uevent->partitionName) == -1) { + INIT_LOGE("Failed to build link"); + break; + } } linkNum++; } } +loop: parent = dirname(parent); + continue; } - links[linkNum] = NULL; return links; } @@ -273,7 +260,7 @@ static void HandleDeviceNode(const struct Uevent *uevent, const char *deviceNode INIT_LOGE("Remove device \" %s \" failed", deviceNode); } } else if (action == ACTION_CHANGE) { - INIT_LOGI("Device %s changed", uevent->syspath); + INIT_LOGD("Device %s changed", uevent->syspath); } // Ignore other actions FreeSymbolLinks(symLinks); @@ -327,8 +314,6 @@ static const char *GetDeviceBasePath(const char *subsystem) devPath = "/dev/graphics"; } else if (STRINGEQUAL(subsystem, "sound")) { devPath = "/dev/snd"; - } else if (STRINGEQUAL(subsystem, "functionfs")) { - devPath = "/dev/functionfs"; } else { devPath = "/dev"; } @@ -358,9 +343,6 @@ void HandleBlockDeviceEvent(const struct Uevent *uevent) char deviceNode[DEVICE_FILE_SIZE] = {}; char sysPath[SYSPATH_SIZE] = {}; - if (uevent->syspath == NULL) { - return; - } if (strncpy_s(sysPath, SYSPATH_SIZE - 1, uevent->syspath, strlen(uevent->syspath) != EOK)) { INIT_LOGE("Failed to copy sys path"); return; @@ -380,7 +362,7 @@ void HandleBlockDeviceEvent(const struct Uevent *uevent) void HandleOtherDeviceEvent(const struct Uevent *uevent) { - if (uevent == NULL || uevent->subsystem == NULL || uevent->syspath == NULL) { + if (uevent == NULL || uevent->subsystem == NULL) { INIT_LOGE("Invalid uevent received"); return; } @@ -404,10 +386,10 @@ void HandleOtherDeviceEvent(const struct Uevent *uevent) } INIT_LOGD("HandleOtherDeviceEvent, devPath = %s, devName = %s", devPath, devName); - // 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")) { + // 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")) { 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); @@ -419,19 +401,19 @@ void HandleOtherDeviceEvent(const struct Uevent *uevent) INIT_LOGE("usb device with invalid bus number or device number"); return; } - if (snprintf_s(deviceNode, DEVICE_FILE_SIZE, DEVICE_FILE_SIZE - 1, + if (snprintf_s(deviceNode, DEVICE_FILE_SIZE, DEVICE_FILE_SIZE - 1, "/dev/bus/usb/%03d/%03d", uevent->busNum, uevent->devNum) == -1) { - INIT_LOGE("Make usb device node for device [%d : %d]", uevent->busNum, uevent->devNum); + INIT_LOGE("Make usb device node for device [%d : %d]", uevent->busNum, uevent->devNum); } } - } else if (STARTSWITH(uevent->subsystem, "usb")) { - // Other usb devies, do not handle it. - return; - } else { + } else if (STARTSWITH(uevent->subsystem, "usb")) { + // Other usb devies, do not handle it. + return; + } else { 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, false); + } + HandleDeviceNode(uevent, deviceNode, false); } -- Gitee