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