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 <limits.h>
17 #include <poll.h>
18 #include <stdbool.h>
19 #include "ueventd.h"
20 #include "ueventd_read_cfg.h"
21 #include "ueventd_socket.h"
22 #define INIT_LOG_TAG "ueventd"
23 #include "init_log.h"
24 #include "init_socket.h"
25
PollUeventdSocketTimeout(int ueventSockFd,bool ondemand)26 static void PollUeventdSocketTimeout(int ueventSockFd, bool ondemand)
27 {
28 struct pollfd pfd = {};
29 pfd.events = POLLIN;
30 pfd.fd = ueventSockFd;
31 int timeout = ondemand ? UEVENTD_POLL_TIME : -1;
32 int ret = -1;
33
34 while (1) {
35 pfd.revents = 0;
36 ret = poll(&pfd, 1, timeout);
37 if (ret == 0) {
38 INIT_LOGI("poll ueventd socket timeout, ueventd exit");
39 return;
40 } else if (ret < 0) {
41 INIT_LOGE("Failed to poll ueventd socket!");
42 return;
43 }
44 if (pfd.revents & POLLIN) {
45 ProcessUevent(ueventSockFd, NULL, 0); // Not require boot devices
46 }
47 }
48 }
49
main(int argc,char ** argv)50 int main(int argc, char **argv)
51 {
52 // start log
53 EnableInitLog(INIT_INFO);
54 char *ueventdConfigs[] = {"/etc/ueventd.config", "/vendor/etc/ueventd.config", NULL};
55 int i = 0;
56 while (ueventdConfigs[i] != NULL) {
57 ParseUeventdConfigFile(ueventdConfigs[i++]);
58 }
59 bool ondemand = true;
60 int ueventSockFd = GetControlSocket("ueventd");
61 if (ueventSockFd < 0) {
62 INIT_LOGW("Failed to get uevent socket, try to create");
63 ueventSockFd = UeventdSocketInit();
64 ondemand = false;
65 }
66 if (ueventSockFd < 0) {
67 INIT_LOGE("Failed to create uevent socket!");
68 return -1;
69 }
70 if (access(UEVENTD_FLAG, F_OK)) {
71 INIT_LOGI("Ueventd started, trigger uevent");
72 RetriggerUevent(ueventSockFd, NULL, 0); // Not require boot devices
73 int fd = open(UEVENTD_FLAG, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
74 if (fd < 0) {
75 INIT_LOGE("Failed to create ueventd flag!");
76 return -1;
77 }
78 (void)close(fd);
79 } else {
80 INIT_LOGI("ueventd start to process uevent message");
81 ProcessUevent(ueventSockFd, NULL, 0); // Not require boot devices
82 }
83 PollUeventdSocketTimeout(ueventSockFd, ondemand);
84 return 0;
85 }
86