1/* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16#include "ueventd.h" 17#include <dirent.h> 18#include <limits.h> 19#include <fcntl.h> 20#include <errno.h> 21#include <stdlib.h> 22#include <string.h> 23#include <sys/stat.h> 24#include <sys/sysmacros.h> 25#include <fcntl.h> 26#include <unistd.h> 27 28#include "ueventd_device_handler.h" 29#include "ueventd_firmware_handler.h" 30#include "ueventd_read_cfg.h" 31#include "ueventd_socket.h" 32#include "ueventd_utils.h" 33#include "securec.h" 34#define INIT_LOG_TAG "ueventd" 35#include "init_log.h" 36#include "init_utils.h" 37 38// buffer size refer to kernel kobject uevent 39#define UEVENT_BUFFER_SIZE (2048 + 1) 40char bootDevice[CMDLINE_VALUE_LEN_MAX] = { 0 }; 41#define WRITE_SIZE 4 42 43static const char *actions[] = { 44 [ACTION_ADD] = "add", 45 [ACTION_REMOVE] = "remove", 46 [ACTION_CHANGE] = "change", 47 [ACTION_MOVE] = "move", 48 [ACTION_ONLINE] = "online", 49 [ACTION_OFFLINE] = "offline", 50 [ACTION_BIND] = "bind", 51 [ACTION_UNBIND] = "unbind", 52 [ACTION_UNKNOWN] = "unknown", 53}; 54 55static SUBSYSTEMTYPE GetSubsystemType(const char *subsystem) 56{ 57 if (subsystem == NULL || *subsystem == '\0') { 58 return SUBSYSTEM_EMPTY; 59 } 60 61 if (strcmp(subsystem, "block") == 0) { 62 return SUBSYSTEM_BLOCK; 63 } else if (strcmp(subsystem, "platform") == 0) { 64 return SUBSYSTEM_PLATFORM; 65 } else if (strcmp(subsystem, "firmware") == 0) { 66 return SUBSYSTEM_FIRMWARE; 67 } else { 68 return SUBSYSTEM_OTHERS; 69 } 70} 71 72const char *ActionString(ACTION action) 73{ 74 return actions[action]; 75} 76 77static ACTION GetUeventAction(const char *action) 78{ 79 if (action == NULL || *action == '\0') { 80 return ACTION_UNKNOWN; 81 } 82 83 if (STRINGEQUAL(action, "add")) { 84 return ACTION_ADD; 85 } else if (STRINGEQUAL(action, "remove")) { 86 return ACTION_REMOVE; 87 } else if (STRINGEQUAL(action, "change")) { 88 return ACTION_CHANGE; 89 } else if (STRINGEQUAL(action, "move")) { 90 return ACTION_MOVE; 91 } else if (STRINGEQUAL(action, "online")) { 92 return ACTION_ONLINE; 93 } else if (STRINGEQUAL(action, "offline")) { 94 return ACTION_OFFLINE; 95 } else if (STRINGEQUAL(action, "bind")) { 96 return ACTION_BIND; 97 } else if (STRINGEQUAL(action, "unbind")) { 98 return ACTION_UNBIND; 99 } else { 100 return ACTION_UNKNOWN; 101 } 102} 103 104STATIC void HandleUevent(const struct Uevent *uevent) 105{ 106 if (uevent->action == ACTION_ADD || uevent->action == ACTION_CHANGE || uevent->action == ACTION_ONLINE) { 107 ChangeSysAttributePermissions(uevent->syspath); 108 } 109 110 SUBSYSTEMTYPE type = GetSubsystemType(uevent->subsystem); 111 switch (type) { 112 case SUBSYSTEM_BLOCK: 113 HandleBlockDeviceEvent(uevent); 114 break; 115 case SUBSYSTEM_FIRMWARE: 116 HandleFimwareDeviceEvent(uevent); 117 break; 118 case SUBSYSTEM_OTHERS: 119 HandleOtherDeviceEvent(uevent); 120 break; 121 default: 122 break; 123 } 124} 125 126#define DEFAULT_RW_MODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH) 127 128typedef struct { 129 const char *dev; 130 mode_t mode; 131} DYNAMIC_DEVICE_NODE; 132 133#define DEV_NODE_PATH_PREFIX "/dev/" 134#define DEV_NODE_PATH_PREFIX_LEN 5 135 136static const DYNAMIC_DEVICE_NODE DYNAMIC_DEVICES[] = { 137 { DEV_NODE_PATH_PREFIX"tty", S_IFCHR | DEFAULT_RW_MODE }, 138 { DEV_NODE_PATH_PREFIX"binder", S_IFCHR | DEFAULT_RW_MODE }, 139 { DEV_NODE_PATH_PREFIX"console", S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP }, 140 { DEV_NODE_PATH_PREFIX"mapper/control", S_IFCHR | DEFAULT_RW_MODE } 141}; 142 143static void HandleRequiredDynamicDeviceNodes(const struct Uevent *uevent) 144{ 145 mode_t mask; 146 size_t idx = 0; 147 148 if (uevent->deviceName == NULL) { 149 return; 150 } 151 152 while (idx < sizeof(DYNAMIC_DEVICES) / sizeof(DYNAMIC_DEVICES[0])) { 153 if (strcmp(uevent->deviceName, DYNAMIC_DEVICES[idx].dev + DEV_NODE_PATH_PREFIX_LEN) != 0) { 154 idx++; 155 continue; 156 } 157 158 if (strcmp(uevent->deviceName, "mapper/control") == 0) { 159 HandleOtherDeviceEvent(uevent); 160 return; 161 } 162 163 // Matched 164 mask = umask(0); 165 if (mknod(DYNAMIC_DEVICES[idx].dev, DYNAMIC_DEVICES[idx].mode, 166 makedev((unsigned int)uevent->major, (unsigned int)uevent->minor)) != 0) { 167 INIT_LOGE("Create device node %s failed. %s", DYNAMIC_DEVICES[idx].dev, strerror(errno)); 168 } 169 // Restore umask 170 umask(mask); 171 break; 172 } 173} 174 175static void HandleRequiredBlockDeviceNodes(const struct Uevent *uevent, char **devices, int num) 176{ 177 for (int i = 0; i < num; i++) { 178 if (uevent->partitionName == NULL) { 179 if (strstr(devices[i], uevent->deviceName) != NULL) { 180 INIT_LOGI("%s match with required partition %s success, now handle it", devices[i], uevent->deviceName); 181 HandleBlockDeviceEvent(uevent); 182 return; 183 } 184 } else if (strstr(devices[i], uevent->partitionName) != NULL || 185 strstr(uevent->partitionName, "vendor") != NULL || 186 strstr(uevent->partitionName, "system") != NULL || 187 strstr(uevent->partitionName, "chipset") != NULL || 188 strstr(uevent->partitionName, "boot") != NULL || 189 strstr(uevent->partitionName, "ramdisk") != NULL || 190 strstr(uevent->partitionName, "rvt") != NULL || 191 strstr(uevent->partitionName, "dtbo") != NULL) { 192 INIT_LOGI("Handle required partitionName %s", uevent->partitionName); 193 HandleBlockDeviceEvent(uevent); 194 return; 195 } 196 } 197 INIT_LOGW("Not found device for partitionName %s ", uevent->partitionName); 198} 199 200static void HandleUeventRequired(const struct Uevent *uevent, char **devices, int num) 201{ 202 INIT_ERROR_CHECK(devices != NULL && num > 0, return, "Fault parameters"); 203 if (uevent->action == ACTION_ADD) { 204 ChangeSysAttributePermissions(uevent->syspath); 205 } 206 SUBSYSTEMTYPE type = GetSubsystemType(uevent->subsystem); 207 if (type == SUBSYSTEM_BLOCK) { 208 HandleRequiredBlockDeviceNodes(uevent, devices, num); 209 } else if (type == SUBSYSTEM_OTHERS) { 210 HandleRequiredDynamicDeviceNodes(uevent); 211 } else { 212 return; 213 } 214} 215 216static void AddUevent(struct Uevent *uevent, const char *event, size_t len) 217{ 218 if (STARTSWITH(event, "DEVPATH=")) { 219 uevent->syspath = event + strlen("DEVPATH="); 220 } else if (STARTSWITH(event, "SUBSYSTEM=")) { 221 uevent->subsystem = event + strlen("SUBSYSTEM="); 222 } else if (STARTSWITH(event, "ACTION=")) { 223 uevent->action = GetUeventAction(event + strlen("ACTION=")); 224 } else if (STARTSWITH(event, "DEVNAME=")) { 225 uevent->deviceName = event + strlen("DEVNAME="); 226 } else if (STARTSWITH(event, "PARTNAME=")) { 227 uevent->partitionName = event + strlen("PARTNAME="); 228 } else if (STARTSWITH(event, "PARTN=")) { 229 uevent->partitionNum = StringToInt(event + strlen("PARTN="), -1); 230 } else if (STARTSWITH(event, "MAJOR=")) { 231 uevent->major = StringToInt(event + strlen("MAJOR="), -1); 232 } else if (STARTSWITH(event, "MINOR=")) { 233 uevent->minor = StringToInt(event + strlen("MINOR="), -1); 234 } else if (STARTSWITH(event, "DEVUID")) { 235 uevent->ug.uid = (uid_t)StringToInt(event + strlen("DEVUID="), 0); 236 } else if (STARTSWITH(event, "DEVGID")) { 237 uevent->ug.gid = (gid_t)StringToInt(event + strlen("DEVGID="), 0); 238 } else if (STARTSWITH(event, "FIRMWARE=")) { 239 uevent->firmware = event + strlen("FIRMWARE="); 240 } else if (STARTSWITH(event, "BUSNUM=")) { 241 uevent->busNum = StringToInt(event + strlen("BUSNUM="), -1); 242 } else if (STARTSWITH(event, "DEVNUM=")) { 243 uevent->devNum = StringToInt(event + strlen("DEVNUM="), -1); 244 } 245 246 // Ignore other events 247 INIT_LOGV("got uevent message:\n" 248 "subsystem: %s\n" 249 "parition: %s:%d\n" 250 "action: %s\n" 251 "devpath: %s\n" 252 "devname: %s\n" 253 "devnode: %d:%d\n" 254 "id: %d:%d", 255 uevent->subsystem, 256 uevent->partitionName, uevent->partitionNum, 257 uevent->action, 258 uevent->syspath, 259 uevent->deviceName, 260 uevent->major, uevent->minor, 261 uevent->ug.uid, uevent->ug.gid); 262} 263 264void ParseUeventMessage(const char *buffer, ssize_t length, struct Uevent *uevent) 265{ 266 if (buffer == NULL || uevent == NULL || length == 0) { 267 // Ignore invalid buffer 268 return; 269 } 270 271 // reset partition number, major and minor. 272 uevent->partitionName = NULL; 273 uevent->partitionNum = -1; 274 uevent->major = -1; 275 uevent->minor = -1; 276 uevent->busNum = -1; 277 uevent->devNum = -1; 278 ssize_t pos = 0; 279 while (pos < length) { 280 const char *event = buffer + pos; 281 size_t len = strlen(event); 282 if (len == 0) { 283 break; 284 } 285 AddUevent(uevent, event, len); 286 pos += (ssize_t)len + 1; 287 } 288} 289 290void ProcessUevent(int sockFd, char **devices, int num) 291{ 292 // One more bytes for '\0' 293 char ueventBuffer[UEVENT_BUFFER_SIZE] = {}; 294 ssize_t n = 0; 295 struct Uevent uevent = {}; 296 while ((n = ReadUeventMessage(sockFd, ueventBuffer, sizeof(ueventBuffer) - 1)) > 0) { 297 ParseUeventMessage(ueventBuffer, n, &uevent); 298 if (uevent.syspath == NULL) { 299 INIT_LOGV("Ignore unexpected uevent"); 300 return; 301 } 302 if (devices != NULL && num > 0) { 303 HandleUeventRequired(&uevent, devices, num); 304 } else { 305 HandleUevent(&uevent); 306 } 307 } 308} 309 310static void DoTrigger(const char *ueventPath, int sockFd, char **devices, int num) 311{ 312 if (ueventPath == NULL || ueventPath[0] == '\0') { 313 return; 314 } 315 316 INIT_LOGV("------------------------\n" 317 "\nTry to trigger \" %s \" now ...", ueventPath); 318 int fd = open(ueventPath, O_WRONLY | O_CLOEXEC); 319 if (fd < 0) { 320 INIT_LOGE("Open \" %s \" failed, err = %d", ueventPath, errno); 321 return; 322 } 323 324 ssize_t n = write(fd, "add\n", WRITE_SIZE); 325 close(fd); 326 if (n < 0) { 327 INIT_LOGE("Write \" %s \" failed, err = %d", ueventPath, errno); 328 return; 329 } 330 331 // uevent triggered, now handle it. 332 if (sockFd >= 0) { 333 ProcessUevent(sockFd, devices, num); 334 } 335} 336 337static void Trigger(const char *path, int sockFd, char **devices, int num) 338{ 339 if (path == NULL) { 340 return; 341 } 342 DIR *dir = opendir(path); 343 if (dir == NULL) { 344 return; 345 } 346 struct dirent *dirent = NULL; 347 while ((dirent = readdir(dir)) != NULL) { 348 if (dirent->d_name[0] == '.') { 349 continue; 350 } 351 if (dirent->d_type == DT_DIR) { 352 char pathBuffer[PATH_MAX]; 353 if (snprintf_s(pathBuffer, PATH_MAX, PATH_MAX - 1, "%s/%s", path, dirent->d_name) == -1) { 354 continue; 355 } 356 Trigger(pathBuffer, sockFd, devices, num); 357 } else { 358 if (strcmp(dirent->d_name, "uevent") != 0) { 359 continue; 360 } 361 char ueventBuffer[PATH_MAX]; 362 if (snprintf_s(ueventBuffer, PATH_MAX, PATH_MAX - 1, "%s/%s", path, "uevent") == -1) { 363 INIT_LOGW("Cannot build uevent path under %s", path); 364 continue; 365 } 366 DoTrigger(ueventBuffer, sockFd, devices, num); 367 } 368 } 369 closedir(dir); 370} 371 372void RetriggerUeventByPath(int sockFd, char *path) 373{ 374 Trigger(path, sockFd, NULL, 0); 375} 376 377void RetriggerUevent(int sockFd, char **devices, int num) 378{ 379 int ret = GetParameterFromCmdLine("default_boot_device", bootDevice, CMDLINE_VALUE_LEN_MAX); 380 INIT_CHECK_ONLY_ELOG(ret == 0, "Failed get default_boot_device value from cmdline"); 381 Trigger("/sys/block", sockFd, devices, num); 382 Trigger("/sys/class", sockFd, devices, num); 383 Trigger("/sys/devices", sockFd, devices, num); 384} 385