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