ueventd_main.c 2.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * 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.
 */

#include <poll.h>
#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"

24
static void PollUeventdSocketTimeout(int ueventSockFd)
25 26 27 28 29 30 31 32 33 34
{
    struct pollfd pfd = {};
    pfd.events = POLLIN;
    pfd.fd = ueventSockFd;
    int ret = -1;

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

47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
int main(int argc, char **argv)
{
    char *ueventdConfigs[] = {"/etc/ueventd.config", NULL};
    int i = 0;
    while (ueventdConfigs[i] != NULL) {
        ParseUeventdConfigFile(ueventdConfigs[i++]);
    }
    int ueventSockFd = GetControlSocket("ueventd");
    if (ueventSockFd < 0) {
        INIT_LOGW("Failed to get uevent socket, try to create");
        ueventSockFd = UeventdSocketInit();
    }
    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 已提交
66
        int fd = open(UEVENTD_FLAG, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
67 68 69 70 71 72 73 74 75
        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
    }
76
    PollUeventdSocketTimeout(ueventSockFd);
77 78
    return 0;
}