ueventd_main.c 2.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

M
Mupceet 已提交
16
#include <limits.h>
17
#include <poll.h>
C
chengjinsong 已提交
18
#include <stdbool.h>
19 20 21 22 23 24 25
#include "ueventd.h"
#include "ueventd_read_cfg.h"
#include "ueventd_socket.h"
#define INIT_LOG_TAG "ueventd"
#include "init_log.h"
#include "init_socket.h"

C
chengjinsong 已提交
26
static void PollUeventdSocketTimeout(int ueventSockFd, bool ondemand)
27 28 29 30
{
    struct pollfd pfd = {};
    pfd.events = POLLIN;
    pfd.fd = ueventSockFd;
C
chengjinsong 已提交
31
    int timeout = ondemand ? UEVENTD_POLL_TIME : -1;
32 33 34 35
    int ret = -1;

    while (1) {
        pfd.revents = 0;
C
chengjinsong 已提交
36
        ret = poll(&pfd, 1, timeout);
37
        if (ret == 0) {
38 39
            INIT_LOGI("poll ueventd socket timeout, ueventd exit");
            return;
40 41 42 43 44 45 46 47 48 49
        } else if (ret < 0) {
            INIT_LOGE("Failed to poll ueventd socket!");
            return;
        }
        if (pfd.revents & POLLIN) {
            ProcessUevent(ueventSockFd, NULL, 0); // Not require boot devices
        }
    }
}

50 51
int main(int argc, char **argv)
{
M
Mupceet 已提交
52 53
    // start log
    EnableInitLog(INIT_INFO);
M
Mupceet 已提交
54
    char *ueventdConfigs[] = {"/etc/ueventd.config", "/vendor/etc/ueventd.config", NULL};
55 56 57 58
    int i = 0;
    while (ueventdConfigs[i] != NULL) {
        ParseUeventdConfigFile(ueventdConfigs[i++]);
    }
C
chengjinsong 已提交
59
    bool ondemand = true;
60 61 62 63
    int ueventSockFd = GetControlSocket("ueventd");
    if (ueventSockFd < 0) {
        INIT_LOGW("Failed to get uevent socket, try to create");
        ueventSockFd = UeventdSocketInit();
C
chengjinsong 已提交
64
        ondemand = false;
65 66 67 68 69 70 71 72
    }
    if (ueventSockFd < 0) {
        INIT_LOGE("Failed to create uevent socket!");
        return -1;
    }
    if (access(UEVENTD_FLAG, F_OK)) {
        INIT_LOGI("ueventd first start to trigger");
        RetriggerUevent(ueventSockFd, NULL, 0); // Not require boot devices
X
xionglei6 已提交
73
        int fd = open(UEVENTD_FLAG, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
74 75 76 77 78 79 80 81 82
        if (fd < 0) {
            INIT_LOGE("Failed to create ueventd flag!");
            return -1;
        }
        (void)close(fd);
    } else {
        INIT_LOGI("ueventd start to process uevent message");
        ProcessUevent(ueventSockFd, NULL, 0); // Not require boot devices
    }
C
chengjinsong 已提交
83
    PollUeventdSocketTimeout(ueventSockFd, ondemand);
84 85
    return 0;
}