src/server/daemon/event_linux.c

Mon, 12 Aug 2024 21:20:17 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Mon, 12 Aug 2024 21:20:17 +0200
changeset 544
27684460629f
parent 543
3335f431a91b
child 545
720893ec7d48
permissions
-rw-r--r--

fix memory leak in case handle_request fails (some illegal requests)

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2013 Olaf Wintermann. All rights reserved.
 *
 * 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.
 *
 * 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 <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/epoll.h>
#include <sys/eventfd.h>

#include "../util/systhr.h"
#include "../util/atomic.h"

#include "../util/io.h"

#include "event.h"
#include "event_linux.h"

#include "httprequest.h"


EVHandler* evhandler_create(EventHandlerConfig *cfg) {
    EVHandler *ev = malloc(sizeof(EVHandler));
    ev->current = 0;
    ev->instances = calloc(cfg->nthreads, sizeof(void*));
    ev->numins = cfg->nthreads;
       
    for(int i=0;i<cfg->nthreads;i++) {
        EventHandlerLinux *handler = malloc(sizeof(EventHandlerLinux));
        memset(handler, 0, sizeof(EventHandlerLinux));
        ev->instances[i] = (EventHandler*)handler;
        
        handler->ep = epoll_create(64);
        if(handler->ep < 0) {
            log_ereport(LOG_FAILURE, "evhandler_create: epoll_create: %s", strerror(errno));
            return NULL;
        }
        
        int eventpipe[2];
        if(pipe(eventpipe)) {
            log_ereport(LOG_FAILURE, "evhandler_create: pipe: %s", strerror(errno));
            return NULL;
        }
        handler->eventin = eventpipe[0];
        handler->eventout = eventpipe[1];
        
        struct epoll_event epev;
        epev.events = EPOLLIN | EPOLLET; // input event, edge triggered
        epev.data.ptr = NULL;
        if(epoll_ctl(handler->ep, EPOLL_CTL_ADD, handler->eventin, &epev)) {
            log_ereport(LOG_FAILURE, "evhandler_create: epoll_ctl: %s", strerror(errno));
            return NULL;
        }
        
        SYS_THREAD t = systhread_start(
                0,
                0,
                (thrstartfunc)ev_handle_events,
                handler);
        systhread_detach(t);
    }   
    
    return ev;
}

void ev_handle_events(EventHandlerLinux *ev) {
    EventHandler *h = (EventHandler*)ev;
    int ep = ev->ep;
    
    struct epoll_event events[EV_MAX_EVENTS];
    Event* finished[EV_MAX_EVENTS];
    
    int loop_ctn = 0;
    for(;;) {
        /* wait for events */
        int ret = epoll_wait(ep, events, 16, EV_IDLE_TIMEOUT * 1000);
        if(ret == -1 && errno != EINTR) {
            log_ereport(LOG_FAILURE, "epoll_wait failed: %s", strerror(errno));
            continue;
        }
              
        int numfinished = 0;
        ev->base.numret = 0;
        for(int i=0;i<ret;i++) {
            Event *event = events[i].data.ptr;
            if(!event) {
                char ebuf[sizeof(Event*)];
                int ebufpos = 0;
                char *b = ebuf;
                while(ebufpos < sizeof(Event*)) {
                    ssize_t r = read(ev->eventin, b + ebufpos, sizeof(Event*)-ebufpos);
                    if(r < 0) {
                        break;
                    }
                    ebufpos += r;
                }
                if(ebufpos == sizeof(Event*)) {
                    intptr_t *p = (intptr_t*)b;
                    *(&event) = (Event*)*p;
                    /*
                    if(event->fn) {
                        if(!event->fn(ev, event) && event->finish) {
                            event->finish(ev, event);
                        }
                    }*/
                } else {
                    continue; // should not happen
                }        
            }
            
            if(event->fn) {
                int saved_ev = event->events;
                if(!event->fn(h, event)) {
                    // event fn returned 0 -> remove event from epoll
                    if(saved_ev && epoll_ctl(ep, EPOLL_CTL_DEL, event->object, NULL)) {
                        event->error = 1;
                        log_ereport(
                                LOG_FAILURE,
                                "epoll_ctl failed: fd: %d error: %s",
                                event->object,
                                strerror(errno));
                    }
                    
                    // if set, remember this event and 
                    // execute event->finish later
                    if(event->finish) {
                        finished[numfinished++] = event;
                        //event->finish(ev, event);
                    }
                } else {
                    if(saved_ev != event->events) {
                        // event type changed
                        struct epoll_event epev;
                        epev.events = EPOLLET;
                        epev.data.ptr = event;
                        
                        // adjust epoll events
                        epev.events = ev_convert2sys_events(event->events);
                        
                        if(epoll_ctl(ep, EPOLL_CTL_MOD, event->object, &epev)) {
                            log_ereport(
                                    LOG_FAILURE,
                                    "epoll_wait failed: %s",
                                    strerror(errno));
                        }
                    }
                }
            }
        }
        // call event finish handlers
        for(int i=0;i<numfinished;i++) {
            Event *event = finished[i];
            // check again if the finish callback is set
            if(finished[i]->finish) {
                finished[i]->finish(h, event);
            }
        }
        // execute return calls
        for(int i=0;i<ev->base.numret;i++) {
            EVReturn ret = ev->base.fnreturn[i];
            nsapi_saf_return(ret.sn, ret.rq, ret.ret);
        }
        
        if(ret == 0 || ++loop_ctn >= EV_IDLE_LOOP_CTN) {
            watchlist_check(&ev->base, 0);
            loop_ctn = 0;
        }
    }
}

int ev_convert2sys_events(int events) {
    int e = EPOLLET;
    if((events & EVENT_POLLIN) == EVENT_POLLIN) {
        e |= EPOLLIN;
    }
    if((events & EVENT_POLLOUT) == EVENT_POLLOUT) {
        e |= EPOLLOUT;
    }
    return e;
}

int ev_pollin(EventHandler *h, int fd, Event *event) {
    EventHandlerLinux *ev = (EventHandlerLinux*)h;
    event->object = (intptr_t)fd;
    event->events = EVENT_POLLIN;
    struct epoll_event epev;
    epev.events = EPOLLIN | EPOLLRDHUP | EPOLLET; // input event, edge triggered
    epev.data.ptr = event;
    return epoll_ctl(ev->ep, EPOLL_CTL_ADD, fd, &epev);
}

int ev_pollout(EventHandler *h, int fd, Event *event) {
    EventHandlerLinux *ev = (EventHandlerLinux*)h;
    event->object = (intptr_t)fd;
    event->events = EVENT_POLLOUT;
    struct epoll_event epev;
    epev.events = EPOLLOUT | EPOLLRDHUP | EPOLLET; // input event, edge triggered
    epev.data.ptr = event;
    return epoll_ctl(ev->ep, EPOLL_CTL_ADD, fd, &epev);
}

int ev_remove_poll(EventHandler *h, int fd) {
    EventHandlerLinux *ev = (EventHandlerLinux*)h;
    return epoll_ctl(ev->ep, EPOLL_CTL_DEL, fd, NULL);
}

int event_send(EventHandler *h, Event *event) {
    EventHandlerLinux *ev = (EventHandlerLinux*)h;
    event->object = 0;
    event->events = 0;
    ssize_t r = write(ev->eventout, &event, sizeof(Event*));
    if(r < sizeof(Event*)) {
        log_ereport(LOG_FAILURE, "failed to send event: %s", strerror(errno));
    }
    return r > 0 ? 0 : 1;
}

// TODO: remove this fake aio
int ev_aioread(int fd, aiocb_s *cb) {
    ssize_t result = pread(fd, cb->buf, cb->nbytes, cb->offset);
    cb->result = result;
    if(result < 0) {
        cb->result_errno = errno;
    }
    return event_send(cb->evhandler, cb->event);
}

int ev_aiowrite(int fd, aiocb_s *cb) {
    ssize_t result = pwrite(fd, cb->buf, cb->nbytes, cb->offset);
    cb->result = result;
    if(result < 0) {
        cb->result_errno = errno;
    }
    return event_send(cb->evhandler, cb->event);
}


int event_pollin(EventHandler *ev, SYS_NETFD fd, Event *event) {
    return ((IOStream*)fd)->poll(fd, ev, IO_POLL_IN, event);
}

int event_pollout(EventHandler *ev, SYS_NETFD fd, Event *event) {
    return ((IOStream*)fd)->poll(fd, ev, IO_POLL_OUT, event);
}

int event_removepoll(EventHandler *ev, SYS_NETFD fd) {
    return ((IOStream*)fd)->poll(fd, ev, IO_POLL_NONE, NULL);
}

mercurial