monitord.c 5.3 KB
Newer Older
O
overweight 已提交
1 2
/******************************************************************************
 * Copyright (c) Huawei Technologies Co., Ltd. 2017-2019. All rights reserved.
3 4 5 6
 * iSulad licensed under the Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *     http://license.coscl.org.cn/MulanPSL2
O
overweight 已提交
7 8 9
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
 * PURPOSE.
10
 * See the Mulan PSL v2 for more details.
O
overweight 已提交
11 12 13 14 15 16 17 18 19 20 21 22 23
 * Author: tanyifeng
 * Create: 2017-11-22
 * Description: provide container monitord functions
 ******************************************************************************/
#define _GNU_SOURCE

#include <sys/stat.h>
#include <sys/socket.h>
#include <inttypes.h>
#include <sys/un.h>
#include <sys/epoll.h>
#include <poll.h>
#include <malloc.h>
24
#include <unistd.h>
O
overweight 已提交
25

H
haozi007 已提交
26
#include "isula_libutils/log.h"
O
overweight 已提交
27 28
#include "monitord.h"
#include "mainloop.h"
L
LiuHao 已提交
29
#include "isulad_config.h"
30
#include "events_collector_api.h"
O
overweight 已提交
31
#include "utils.h"
32
#include "events_sender_api.h"
O
overweight 已提交
33 34 35 36 37 38 39 40 41 42

struct monitord_handler {
    struct epoll_descr *pdescr;
    int fifo_fd;
    char *fifo_path;
};

/* monitor event cb */
static int monitor_event_cb(int fd, uint32_t events, void *cbdata, struct epoll_descr *descr)
{
43
    ssize_t len;
O
overweight 已提交
44 45 46 47 48 49 50 51 52
    struct monitord_msg mmsg = { 0 };

    /* first, read message from container monitor. */
    len = util_read_nointr(fd, &mmsg, sizeof(mmsg));
    if ((unsigned int)len != sizeof(mmsg)) {
        ERROR("Invalid message");
        goto out;
    }

D
dogsheng 已提交
53
    /* second, handle events */
O
overweight 已提交
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
    events_handler(&mmsg);
    if (malloc_trim(0) == 0) {
        DEBUG("Malloc trim failed");
    }
out:
    return 0;
}

/* free monitord */
static void free_monitord(struct monitord_handler *mhandler)
{
    if (mhandler->fifo_fd != -1) {
        epoll_loop_del_handler(mhandler->pdescr, mhandler->fifo_fd);
        close(mhandler->fifo_fd);
    }
    if (mhandler->fifo_path != NULL) {
        if (unlink(mhandler->fifo_path) < 0) {
            WARN("Failed to unlink fifo_path");
        }
        free(mhandler->fifo_path);
        mhandler->fifo_path = NULL;
    }

    DEBUG("Clean monitord data...");
}

#define EVENTS_FIFO_SIZE (1024 * 1024)
/* monitord */
static void *monitord(void *arg)
{
    int ret = 0;
    char *fifo_file_path = NULL;
    struct monitord_handler mhandler = { 0 };
    struct flock mlock;
    struct monitord_sync_data *msync = arg;
    struct epoll_descr descr;

    mhandler.fifo_fd = -1;
    ret = pthread_detach(pthread_self());
    if (ret != 0) {
        CRIT("Set thread detach fail");
        goto pexit;
    }

    prctl(PR_SET_NAME, "Monitord");

    ret = epoll_loop_open(&descr);
    if (ret != 0) {
        ERROR("Failed to create epoll_loop");
        goto pexit;
    }
    mhandler.pdescr = &descr;

    /* 1. monitor fifo: to wait container monitor message */
L
lifeng68 已提交
108
    fifo_file_path = conf_get_isulad_monitor_fifo_path();
O
overweight 已提交
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
    if (fifo_file_path == NULL) {
        goto err;
    }
    mhandler.fifo_path = fifo_file_path;

    if (mknod(fifo_file_path, S_IFIFO | S_IRUSR | S_IWUSR, (dev_t)0) && errno != EEXIST) {
        ERROR("Create monitord fifo file failed: %s", strerror(errno));
        goto err;
    }

    mhandler.fifo_fd = util_open(fifo_file_path, O_RDWR | O_NONBLOCK | O_CLOEXEC, 0);
    if (mhandler.fifo_fd == -1) {
        ERROR("Open monitord fifo file failed: %s", strerror(errno));
        goto err;
    }

    if (fcntl(mhandler.fifo_fd, F_SETPIPE_SZ, EVENTS_FIFO_SIZE) == -1) {
        ERROR("Set events fifo buffer size failed: %s\n", strerror(errno));
        goto err;
    }

    mlock.l_type = F_WRLCK;
    mlock.l_whence = SEEK_SET;
    mlock.l_start = 0;
    mlock.l_len = 0;
    if (fcntl(mhandler.fifo_fd, F_SETLK, &mlock)) {
        INFO("Monitord already running on path: %s", fifo_file_path);
        goto err;
    }

    ret = epoll_loop_add_handler(&descr, mhandler.fifo_fd, monitor_event_cb, NULL);
    if (ret != 0) {
        ERROR("Failed to add handler for fifo");
        goto err;
    }

    sem_post(msync->monitord_sem);

    /* loop forever except error occured */
    do {
        ret = epoll_loop(&descr, -1);
    } while (ret == 0);

    ERROR("Mainloop returned an error: %s", strerror(errno));
    goto err2;

err:
    *(msync->exit_code) = -1;
    sem_post(msync->monitord_sem);
err2:
    free_monitord(&mhandler);
    epoll_loop_close(&descr);

pexit:
    return NULL;
}

/* new monitord */
int new_monitord(struct monitord_sync_data *msync)
{
    int ret = 0;
    char *statedir = NULL;
    pthread_t monitord_thread;

    if (msync == NULL || msync->monitord_sem == NULL) {
        ERROR("Monitord sem is NULL");
        ret = -1;
        goto out;
    }

L
LiuHao 已提交
179
    statedir = conf_get_isulad_statedir();
O
overweight 已提交
180
    if (statedir == NULL) {
L
LiuHao 已提交
181
        ERROR("isulad root path is NULL");
O
overweight 已提交
182 183 184 185
        ret = -1;
        goto out;
    }

L
LiuHao 已提交
186
    if (setenv("ISULAD_MONITORD_PATH", statedir, 1)) {
O
overweight 已提交
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
        ERROR("Setenv monitord path failed");
        ret = -1;
        goto out;
    }

    INFO("Starting monitord...");
    if (pthread_create(&monitord_thread, NULL, monitord, msync) != 0) {
        ERROR("Create monitord thread failed");
        ret = -1;
    }

out:
    free(statedir);
    return ret;
}