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)
40 char bootDevice[CMDLINE_VALUE_LEN_MAX] = { 0 };
41 #define WRITE_SIZE 4
42
43 static 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
GetSubsystemType(const char * subsystem)55 static 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
ActionString(ACTION action)72 const char *ActionString(ACTION action)
73 {
74 return actions[action];
75 }
76
GetUeventAction(const char * action)77 static 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
HandleUevent(const struct Uevent * uevent)104 static 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
128 typedef 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
136 static 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
HandleRequiredDynamicDeviceNodes(const struct Uevent * uevent)143 static 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(uevent->major, 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
HandleRequiredBlockDeviceNodes(const struct Uevent * uevent,char ** devices,int num)175 static 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, "boot") != NULL ||
188 strstr(uevent->partitionName, "ramdisk") != NULL ||
189 strstr(uevent->partitionName, "rvt") != NULL) {
190 INIT_LOGI("Handle required partitionName %s", uevent->partitionName);
191 HandleBlockDeviceEvent(uevent);
192 return;
193 } else {
194 INIT_LOGI("Handle required partitionName %s", uevent->partitionName);
195 INIT_LOGE("Handle required device %s", devices[i]);
196 }
197 }
198 }
199
HandleUeventRequired(const struct Uevent * uevent,char ** devices,int num)200 static 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
AddUevent(struct Uevent * uevent,const char * event,size_t len)216 static void AddUevent(struct Uevent *uevent, const char *event, size_t len)
217 {
218 if (uevent == NULL || event == NULL || len == 0) {
219 return;
220 }
221
222 if (STARTSWITH(event, "DEVPATH=")) {
223 uevent->syspath = event + strlen("DEVPATH=");
224 } else if (STARTSWITH(event, "SUBSYSTEM=")) {
225 uevent->subsystem = event + strlen("SUBSYSTEM=");
226 } else if (STARTSWITH(event, "ACTION=")) {
227 uevent->action = GetUeventAction(event + strlen("ACTION="));
228 } else if (STARTSWITH(event, "DEVNAME=")) {
229 uevent->deviceName = event + strlen("DEVNAME=");
230 } else if (STARTSWITH(event, "PARTNAME=")) {
231 uevent->partitionName = event + strlen("PARTNAME=");
232 } else if (STARTSWITH(event, "PARTN=")) {
233 uevent->partitionNum = StringToInt(event + strlen("PARTN="), -1);
234 } else if (STARTSWITH(event, "MAJOR=")) {
235 uevent->major = StringToInt(event + strlen("MAJOR="), -1);
236 } else if (STARTSWITH(event, "MINOR=")) {
237 uevent->minor = StringToInt(event + strlen("MINOR="), -1);
238 } else if (STARTSWITH(event, "DEVUID")) {
239 uevent->ug.uid = (uid_t)StringToInt(event + strlen("DEVUID="), 0);
240 } else if (STARTSWITH(event, "DEVGID")) {
241 uevent->ug.gid = (gid_t)StringToInt(event + strlen("DEVGID="), 0);
242 } else if (STARTSWITH(event, "FIRMWARE=")) {
243 uevent->firmware = event + strlen("FIRMWARE=");
244 } else if (STARTSWITH(event, "BUSNUM=")) {
245 uevent->busNum = StringToInt(event + strlen("BUSNUM="), -1);
246 } else if (STARTSWITH(event, "DEVNUM=")) {
247 uevent->devNum = StringToInt(event + strlen("DEVNUM="), -1);
248 }
249 // Ignore other events
250 }
251
ParseUeventMessage(const char * buffer,ssize_t length,struct Uevent * uevent)252 void ParseUeventMessage(const char *buffer, ssize_t length, struct Uevent *uevent)
253 {
254 if (buffer == NULL || uevent == NULL || length == 0) {
255 // Ignore invalid buffer
256 return;
257 }
258
259 // reset partition number, major and minor.
260 uevent->partitionNum = -1;
261 uevent->major = -1;
262 uevent->minor = -1;
263 uevent->busNum = -1;
264 uevent->devNum = -1;
265 ssize_t pos = 0;
266 while (pos < length) {
267 const char *event = buffer + pos;
268 size_t len = strlen(event);
269 if (len == 0) {
270 break;
271 }
272 AddUevent(uevent, event, len);
273 pos += (ssize_t)len + 1;
274 }
275 }
276
ProcessUevent(int sockFd,char ** devices,int num)277 void ProcessUevent(int sockFd, char **devices, int num)
278 {
279 // One more bytes for '\0'
280 char ueventBuffer[UEVENT_BUFFER_SIZE] = {};
281 ssize_t n = 0;
282 struct Uevent uevent = {};
283 while ((n = ReadUeventMessage(sockFd, ueventBuffer, sizeof(ueventBuffer) - 1)) > 0) {
284 ParseUeventMessage(ueventBuffer, n, &uevent);
285 if (uevent.syspath == NULL) {
286 INIT_LOGV("Ignore unexpected uevent");
287 return;
288 }
289 if (devices != NULL && num > 0) {
290 HandleUeventRequired(&uevent, devices, num);
291 } else {
292 HandleUevent(&uevent);
293 }
294 }
295 }
296
DoTrigger(const char * ueventPath,int sockFd,char ** devices,int num)297 static void DoTrigger(const char *ueventPath, int sockFd, char **devices, int num)
298 {
299 if (ueventPath == NULL || ueventPath[0] == '\0') {
300 return;
301 }
302
303 int fd = open(ueventPath, O_WRONLY | O_CLOEXEC);
304 if (fd < 0) {
305 INIT_LOGE("Open \" %s \" failed, err = %d", ueventPath, errno);
306 } else {
307 ssize_t n = write(fd, "add\n", WRITE_SIZE);
308 if (n < 0) {
309 INIT_LOGE("Write \" %s \" failed, err = %d", ueventPath, errno);
310 close(fd);
311 } else {
312 close(fd);
313 // uevent triggered, now handle it.
314 if (sockFd >= 0) {
315 ProcessUevent(sockFd, devices, num);
316 }
317 }
318 }
319 }
320
Trigger(const char * path,int sockFd,char ** devices,int num)321 static void Trigger(const char *path, int sockFd, char **devices, int num)
322 {
323 if (path == NULL) {
324 return;
325 }
326 DIR *dir = opendir(path);
327 if (dir == NULL) {
328 return;
329 }
330 struct dirent *dirent = NULL;
331 while ((dirent = readdir(dir)) != NULL) {
332 if (dirent->d_name[0] == '.') {
333 continue;
334 }
335 if (dirent->d_type == DT_DIR) {
336 char pathBuffer[PATH_MAX];
337 if (snprintf_s(pathBuffer, PATH_MAX, PATH_MAX - 1, "%s/%s", path, dirent->d_name) == -1) {
338 continue;
339 }
340 Trigger(pathBuffer, sockFd, devices, num);
341 } else {
342 if (strcmp(dirent->d_name, "uevent") != 0) {
343 continue;
344 }
345 char ueventBuffer[PATH_MAX];
346 if (snprintf_s(ueventBuffer, PATH_MAX, PATH_MAX - 1, "%s/%s", path, "uevent") == -1) {
347 INIT_LOGW("Cannot build uevent path under %s", path);
348 continue;
349 }
350 DoTrigger(ueventBuffer, sockFd, devices, num);
351 }
352 }
353 closedir(dir);
354 }
355
RetriggerUeventByPath(int sockFd,char * path)356 void RetriggerUeventByPath(int sockFd, char *path)
357 {
358 Trigger(path, sockFd, NULL, 0);
359 }
360
RetriggerUevent(int sockFd,char ** devices,int num)361 void RetriggerUevent(int sockFd, char **devices, int num)
362 {
363 char *buffer = ReadFileData("/proc/cmdline");
364 int ret = GetProcCmdlineValue("default_boot_device", buffer, bootDevice, CMDLINE_VALUE_LEN_MAX);
365 INIT_CHECK_ONLY_ELOG(ret == 0, "Failed get default_boot_device value from cmdline");
366 Trigger("/sys/block", sockFd, devices, num);
367 Trigger("/sys/class", sockFd, devices, num);
368 Trigger("/sys/devices", sockFd, devices, num);
369 if (buffer != NULL) {
370 free(buffer);
371 }
372 }
373