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) {
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
HandleRequiredBlockDeviceNodes(const struct Uevent * uevent,char ** devices,int num)174 static void HandleRequiredBlockDeviceNodes(const struct Uevent *uevent, char **devices, int num)
175 {
176 for (int i = 0; i < num; i++) {
177 if (uevent->partitionName == NULL) {
178 if (strstr(devices[i], uevent->deviceName) != NULL) {
179 INIT_LOGI("%s match with required partition %s success, now handle it", devices[i], uevent->deviceName);
180 HandleBlockDeviceEvent(uevent);
181 return;
182 }
183 } else if (strstr(devices[i], uevent->partitionName) != NULL ||
184 strstr(uevent->partitionName, "vendor") != NULL ||
185 strstr(uevent->partitionName, "system") != NULL ||
186 strstr(uevent->partitionName, "chipset") != NULL ||
187 strstr(uevent->partitionName, "boot") != NULL ||
188 strstr(uevent->partitionName, "ramdisk") != NULL ||
189 strstr(uevent->partitionName, "rvt") != NULL ||
190 strstr(uevent->partitionName, "dtbo") != NULL ||
191 strstr(uevent->partitionName, "fw_hifi") != NULL ||
192 strstr(uevent->partitionName, "modem") != NULL) {
193 INIT_LOGI("Handle required partitionName %s", uevent->partitionName);
194 HandleBlockDeviceEvent(uevent);
195 return;
196 }
197 }
198 INIT_LOGW("Not found device for partitionName %s ", uevent->partitionName);
199 }
200
HandleUeventRequired(const struct Uevent * uevent,char ** devices,int num)201 static void HandleUeventRequired(const struct Uevent *uevent, char **devices, int num)
202 {
203 INIT_ERROR_CHECK(devices != NULL && num > 0, return, "Fault parameters");
204 if (uevent->action == ACTION_ADD) {
205 ChangeSysAttributePermissions(uevent->syspath);
206 }
207 SUBSYSTEMTYPE type = GetSubsystemType(uevent->subsystem);
208 if (type == SUBSYSTEM_BLOCK) {
209 HandleRequiredBlockDeviceNodes(uevent, devices, num);
210 } else if (type == SUBSYSTEM_OTHERS) {
211 HandleRequiredDynamicDeviceNodes(uevent);
212 } else {
213 return;
214 }
215 }
216
AddUevent(struct Uevent * uevent,const char * event,size_t len)217 static void AddUevent(struct Uevent *uevent, const char *event, size_t len)
218 {
219 if (STARTSWITH(event, "DEVPATH=")) {
220 uevent->syspath = event + strlen("DEVPATH=");
221 } else if (STARTSWITH(event, "SUBSYSTEM=")) {
222 uevent->subsystem = event + strlen("SUBSYSTEM=");
223 } else if (STARTSWITH(event, "ACTION=")) {
224 uevent->action = GetUeventAction(event + strlen("ACTION="));
225 } else if (STARTSWITH(event, "DEVNAME=")) {
226 uevent->deviceName = event + strlen("DEVNAME=");
227 } else if (STARTSWITH(event, "PARTNAME=")) {
228 uevent->partitionName = event + strlen("PARTNAME=");
229 } else if (STARTSWITH(event, "PARTN=")) {
230 uevent->partitionNum = StringToInt(event + strlen("PARTN="), -1);
231 } else if (STARTSWITH(event, "MAJOR=")) {
232 uevent->major = StringToInt(event + strlen("MAJOR="), -1);
233 } else if (STARTSWITH(event, "MINOR=")) {
234 uevent->minor = StringToInt(event + strlen("MINOR="), -1);
235 } else if (STARTSWITH(event, "DEVUID")) {
236 uevent->ug.uid = (uid_t)StringToInt(event + strlen("DEVUID="), 0);
237 } else if (STARTSWITH(event, "DEVGID")) {
238 uevent->ug.gid = (gid_t)StringToInt(event + strlen("DEVGID="), 0);
239 } else if (STARTSWITH(event, "FIRMWARE=")) {
240 uevent->firmware = event + strlen("FIRMWARE=");
241 } else if (STARTSWITH(event, "BUSNUM=")) {
242 uevent->busNum = StringToInt(event + strlen("BUSNUM="), -1);
243 } else if (STARTSWITH(event, "DEVNUM=")) {
244 uevent->devNum = StringToInt(event + strlen("DEVNUM="), -1);
245 }
246
247 // Ignore other events
248 INIT_LOGV("got uevent message:\n"
249 "subsystem: %s\n"
250 "parition: %s:%d\n"
251 "action: %s\n"
252 "devpath: %s\n"
253 "devname: %s\n"
254 "devnode: %d:%d\n"
255 "id: %d:%d",
256 uevent->subsystem,
257 uevent->partitionName, uevent->partitionNum,
258 uevent->action,
259 uevent->syspath,
260 uevent->deviceName,
261 uevent->major, uevent->minor,
262 uevent->ug.uid, uevent->ug.gid);
263 }
264
ParseUeventMessage(const char * buffer,ssize_t length,struct Uevent * uevent)265 void ParseUeventMessage(const char *buffer, ssize_t length, struct Uevent *uevent)
266 {
267 if (buffer == NULL || uevent == NULL || length == 0) {
268 // Ignore invalid buffer
269 return;
270 }
271
272 // reset partition number, major and minor.
273 uevent->partitionName = NULL;
274 uevent->partitionNum = -1;
275 uevent->major = -1;
276 uevent->minor = -1;
277 uevent->busNum = -1;
278 uevent->devNum = -1;
279 ssize_t pos = 0;
280 while (pos < length) {
281 const char *event = buffer + pos;
282 size_t len = strlen(event);
283 if (len == 0) {
284 break;
285 }
286 AddUevent(uevent, event, len);
287 pos += (ssize_t)len + 1;
288 }
289 }
290
ProcessUevent(int sockFd,char ** devices,int num)291 void ProcessUevent(int sockFd, char **devices, int num)
292 {
293 // One more bytes for '\0'
294 char ueventBuffer[UEVENT_BUFFER_SIZE] = {};
295 ssize_t n = 0;
296 struct Uevent uevent = {};
297 while ((n = ReadUeventMessage(sockFd, ueventBuffer, sizeof(ueventBuffer) - 1)) > 0) {
298 ParseUeventMessage(ueventBuffer, n, &uevent);
299 if (uevent.syspath == NULL) {
300 INIT_LOGV("Ignore unexpected uevent");
301 return;
302 }
303 if (devices != NULL && num > 0) {
304 HandleUeventRequired(&uevent, devices, num);
305 } else {
306 HandleUevent(&uevent);
307 }
308 }
309 }
310
DoTrigger(const char * ueventPath,int sockFd,char ** devices,int num)311 static void DoTrigger(const char *ueventPath, int sockFd, char **devices, int num)
312 {
313 if (ueventPath == NULL || ueventPath[0] == '\0') {
314 return;
315 }
316
317 INIT_LOGV("------------------------\n"
318 "\nTry to trigger \" %s \" now ...", ueventPath);
319 int fd = open(ueventPath, O_WRONLY | O_CLOEXEC);
320 if (fd < 0) {
321 INIT_LOGE("Open \" %s \" failed, err = %d", ueventPath, errno);
322 return;
323 }
324
325 ssize_t n = write(fd, "add\n", WRITE_SIZE);
326 close(fd);
327 if (n < 0) {
328 INIT_LOGE("Write \" %s \" failed, err = %d", ueventPath, errno);
329 return;
330 }
331
332 // uevent triggered, now handle it.
333 if (sockFd >= 0) {
334 ProcessUevent(sockFd, devices, num);
335 }
336 }
337
Trigger(const char * path,int sockFd,char ** devices,int num)338 static void Trigger(const char *path, int sockFd, char **devices, int num)
339 {
340 if (path == NULL) {
341 return;
342 }
343 DIR *dir = opendir(path);
344 if (dir == NULL) {
345 return;
346 }
347 struct dirent *dirent = NULL;
348 while ((dirent = readdir(dir)) != NULL) {
349 if (dirent->d_name[0] == '.') {
350 continue;
351 }
352 if (dirent->d_type == DT_DIR) {
353 char pathBuffer[PATH_MAX];
354 if (snprintf_s(pathBuffer, PATH_MAX, PATH_MAX - 1, "%s/%s", path, dirent->d_name) == -1) {
355 continue;
356 }
357 Trigger(pathBuffer, sockFd, devices, num);
358 } else {
359 if (strcmp(dirent->d_name, "uevent") != 0) {
360 continue;
361 }
362 char ueventBuffer[PATH_MAX];
363 if (snprintf_s(ueventBuffer, PATH_MAX, PATH_MAX - 1, "%s/%s", path, "uevent") == -1) {
364 INIT_LOGW("Cannot build uevent path under %s", path);
365 continue;
366 }
367 DoTrigger(ueventBuffer, sockFd, devices, num);
368 }
369 }
370 closedir(dir);
371 }
372
RetriggerUeventByPath(int sockFd,char * path)373 void RetriggerUeventByPath(int sockFd, char *path)
374 {
375 Trigger(path, sockFd, NULL, 0);
376 }
377
RetriggerUevent(int sockFd,char ** devices,int num)378 void RetriggerUevent(int sockFd, char **devices, int num)
379 {
380 int ret = GetParameterFromCmdLine("default_boot_device", bootDevice, CMDLINE_VALUE_LEN_MAX);
381 INIT_CHECK_ONLY_ELOG(ret == 0, "Failed get default_boot_device value from cmdline");
382 Trigger("/sys/block", sockFd, devices, num);
383 Trigger("/sys/class", sockFd, devices, num);
384 Trigger("/sys/devices", sockFd, devices, num);
385 }
386