fs_epoll.c 8.7 KB
Newer Older
L
lnlan 已提交
1
/*
Y
yinjiaming 已提交
2
 * Copyright (c) 2021-2022 Huawei Device Co., Ltd. All rights reserved.
L
lnlan 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this list of
 *    conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
 *    of conditions and the following disclaimer in the documentation and/or other materials
 *    provided with the distribution.
 *
 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
 *    to endorse or promote products derived from this software without specific prior written
 *    permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "epoll.h"
#include <stdint.h>
#include <poll.h>
#include <errno.h>
#include <string.h>
#include "pthread.h"

/* 100, the number of fd one epollfd can control */
#define EPOLL_DEFAULT_SIZE 100

/* Internal data, used to manage each epoll fd */
struct epoll_head {
    int size;
    int nodeCount;
    struct epoll_event *evs;
};

STATIC pthread_mutex_t g_epollMutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;

#ifndef MAX_EPOLL_FD
#define MAX_EPOLL_FD CONFIG_EPOLL_DESCRIPTORS
#endif

/* Record the kernel fd of epoll  */
STATIC fd_set g_epollFdSet;

/* Record the private data of epoll  */
STATIC struct epoll_head *g_epPrivBuf[MAX_EPOLL_FD];

/**
 * Alloc sysFd, storage epoll private data, set using bit.
 *
 * @param maxfdp: Maximum allowed application of sysFd.
 * @param head: Private data.
 * @return the index of the new fd; -1 on error
 */
static int EpollAllocSysFd(int maxfdp, struct epoll_head *head)
{
    int i;

    fd_set *fdset = &g_epollFdSet;

    for (i = 0; i < maxfdp; i++) {
L
lnlan 已提交
74 75
        if (fdset && !(FD_ISSET(i, fdset))) {
            FD_SET(i, fdset);
L
lnlan 已提交
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
            if (!g_epPrivBuf[i]) {
                g_epPrivBuf[i] = head;
                return i + EPOLL_FD_OFFSET;
            }
        }
    }

    set_errno(EMFILE);
    return -1;
}

/**
 * free sysFd, delete epoll private data, clear using bit.
 *
 * @param fd: epoll fd.
 * @return 0 or -1
 */
static int EpollFreeSysFd(int fd)
{
L
lnlan 已提交
95
    int efd = fd - EPOLL_FD_OFFSET;
L
lnlan 已提交
96

L
lnlan 已提交
97
    if ((efd < 0) || (efd >= MAX_EPOLL_FD)) {
L
lnlan 已提交
98 99 100 101 102
        set_errno(EMFILE);
        return -1;
    }

    fd_set *fdset = &g_epollFdSet;
L
lnlan 已提交
103 104 105
    if (fdset && FD_ISSET(efd, fdset)) {
        FD_CLR(efd, fdset);
        g_epPrivBuf[efd] = NULL;
L
lnlan 已提交
106 107 108 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
    }

    return 0;
}

/**
 * get private data by epoll fd
 *
 * @param fd: epoll fd.
 * @return point to epoll_head
 */
static struct epoll_head *EpollGetDataBuff(int fd)
{
    int id = fd - EPOLL_FD_OFFSET;

    if ((id < 0) || (id >= MAX_EPOLL_FD)) {
        return NULL;
    }

    return g_epPrivBuf[id];
}

/**
 * when do EPOLL_CTL_ADD, need check if fd exist
 *
 * @param epHead: epoll control head, find by epoll id .
 * @param fd: ctl add fd.
 * @return 0 or -1
 */
static int CheckFdExist(struct epoll_head *epHead, int fd)
{
    int i;
    for (i = 0; i < epHead->nodeCount; i++) {
        if (epHead->evs[i].data.fd == fd) {
            return -1;
        }
    }

    return 0;
}

/**
 * close epoll
 *
 * @param epHead: epoll control head.
 * @return void
 */
static VOID DoEpollClose(struct epoll_head *epHead)
{
    if (epHead != NULL) {
        if (epHead->evs != NULL) {
            free(epHead->evs);
        }

        free(epHead);
    }
L
lnlan 已提交
162 163

    return;
L
lnlan 已提交
164 165 166
}

/**
167 168 169 170 171
 * epoll_create unsupported api
 *
 * epoll_create is implemented by calling epoll_create1, it's parameter 'size' is useless.
 *
 * epoll_create1,
L
lnlan 已提交
172 173 174 175
 * The simple version of epoll does not use red-black trees,
 * so when fd is normal value (greater than 0),
 * actually allocated epoll can manage num of EPOLL_DEFAULT_SIZE
 *
176
 * @param flags: not actually used
L
lnlan 已提交
177 178
 * @return epoll fd
 */
179
int epoll_create1(int flags)
L
lnlan 已提交
180
{
181
    (void)flags;
L
lnlan 已提交
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
    int fd = -1;

    struct epoll_head *epHead = (struct epoll_head *)malloc(sizeof(struct epoll_head));
    if (epHead == NULL) {
        set_errno(ENOMEM);
        return fd;
    }

    /* actually allocated epoll can manage num is EPOLL_DEFAULT_SIZE */
    epHead->size = EPOLL_DEFAULT_SIZE;
    epHead->nodeCount = 0;
    epHead->evs = malloc(sizeof(struct epoll_event) * EPOLL_DEFAULT_SIZE);
    if (epHead->evs == NULL) {
        free(epHead);
        set_errno(ENOMEM);
        return fd;
    }

    /* fd set, get sysfd, for close */
    (VOID)pthread_mutex_lock(&g_epollMutex);
    fd = EpollAllocSysFd(MAX_EPOLL_FD, epHead);
    if (fd == -1) {
        (VOID)pthread_mutex_unlock(&g_epollMutex);
        DoEpollClose(epHead);
        set_errno(EMFILE);
        return fd;
    }
    (VOID)pthread_mutex_unlock(&g_epollMutex);
    return fd;
}

/**
 * epoll_close,
 * called by close
 * @param epfd: epoll fd
 * @return 0 or -1
 */
int epoll_close(int epfd)
{
    struct epoll_head *epHead = NULL;

    epHead = EpollGetDataBuff(epfd);
    if (epHead == NULL) {
        set_errno(EBADF);
        return -1;
    }

    DoEpollClose(epHead);
    return EpollFreeSysFd(epfd);
}

int epoll_ctl(int epfd, int op, int fd, struct epoll_event *ev)
{
    struct epoll_head *epHead = NULL;
    int i;
    int ret = -1;

    epHead = EpollGetDataBuff(epfd);
L
lnlan 已提交
240
    if (epHead == NULL) {
L
lnlan 已提交
241 242 243 244
        set_errno(EBADF);
        return ret;
    }

W
wangchen 已提交
245 246 247 248 249
    if (ev == NULL) {
        set_errno(EINVAL);
        return -1;
    }

L
lnlan 已提交
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
    switch (op) {
        case EPOLL_CTL_ADD:
            ret = CheckFdExist(epHead, fd);
            if (ret == -1) {
                set_errno(EEXIST);
                return -1;
            }

            if (epHead->nodeCount == EPOLL_DEFAULT_SIZE) {
                set_errno(ENOMEM);
                return -1;
            }

            epHead->evs[epHead->nodeCount].events = ev->events | POLLERR | POLLHUP;
            epHead->evs[epHead->nodeCount].data.fd = fd;
            epHead->nodeCount++;
            return 0;
        case EPOLL_CTL_DEL:
            for (i = 0; i < epHead->nodeCount; i++) {
                if (epHead->evs[i].data.fd != fd) {
                    continue;
                }

                if (i != epHead->nodeCount - 1) {
                    memmove_s(&epHead->evs[i], epHead->nodeCount - i, &epHead->evs[i + 1],
                              epHead->nodeCount - i);
                }
                epHead->nodeCount--;
                return 0;
            }
            set_errno(ENOENT);
            return -1;
        case EPOLL_CTL_MOD:
            for (i = 0; i < epHead->nodeCount; i++) {
                if (epHead->evs[i].data.fd == fd) {
                    epHead->evs[i].events = ev->events | POLLERR | POLLHUP;
                    return 0;
                }
            }
            set_errno(ENOENT);
            return -1;
        default:
            set_errno(EINVAL);
            return -1;
    }
}

int epoll_wait(int epfd, FAR struct epoll_event *evs, int maxevents, int timeout)
{
    struct epoll_head *epHead = NULL;
    int ret;
    int counter;
    int i;
    struct pollfd *pFd = NULL;
    int pollSize;

    epHead = EpollGetDataBuff(epfd);
Y
yinjiaming 已提交
307
    if (epHead == NULL) {
L
lnlan 已提交
308 309 310 311
        set_errno(EBADF);
        return -1;
    }

W
wangchen 已提交
312
    if ((maxevents <= 0) || (evs == NULL)) {
L
lnlan 已提交
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
        set_errno(EINVAL);
        return -1;
    }

    if (maxevents > epHead->nodeCount) {
        pollSize = epHead->nodeCount;
    } else {
        pollSize = maxevents;
    }

    pFd = malloc(sizeof(struct pollfd) * pollSize);
    if (pFd == NULL) {
        set_errno(EINVAL);
        return -1;
    }

    for (i = 0; i < epHead->nodeCount; i++) {
        pFd[i].fd = epHead->evs[i].data.fd;
        pFd[i].events = (short)epHead->evs[i].events;
    }


    ret = poll(pFd, pollSize, timeout);
    if (ret <= 0) {
L
lnlan 已提交
337
        free(pFd);
L
lnlan 已提交
338 339 340 341 342 343 344 345 346 347 348
        return 0;
    }

    for (i = 0, counter = 0; i < ret && counter < pollSize; counter++) {
        if (pFd[counter].revents != 0) {
            evs[i].data.fd = pFd[counter].fd;
            evs[i].events  = pFd[counter].revents;
            i++;
        }
    }

L
lnlan 已提交
349
    free(pFd);
L
lnlan 已提交
350 351 352
    return i;
}