X-Git-Url: http://git.rot13.org/?a=blobdiff_plain;f=src%2Fshared%2Flibosmocore%2Fsrc%2Fselect.c;h=4b002ae5cf8c31eb984c52016721cacd47c49351;hb=7a9b3f45b278c5664bd8f76b692febb84dee6a71;hp=2f6afa7f53c2b7dabb339ed918436aabacc4f72e;hpb=9dfbfe907213ce1042dcbc0b77bfbf804452ccf2;p=osmocom-bb.git diff --git a/src/shared/libosmocore/src/select.c b/src/shared/libosmocore/src/select.c index 2f6afa7..4b002ae 100644 --- a/src/shared/libosmocore/src/select.c +++ b/src/shared/libosmocore/src/select.c @@ -5,8 +5,9 @@ * (C) 2000-2009 by Harald Welte * * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License version 2 - * as published by the Free Software Foundation + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of @@ -19,19 +20,21 @@ */ #include -#include -#include -#include +#include + +#include +#include +#include #include "../config.h" #ifdef HAVE_SYS_SELECT_H static int maxfd = 0; -static LLIST_HEAD(bsc_fds); +static LLIST_HEAD(osmo_fds); static int unregistered_count; -int bsc_register_fd(struct bsc_fd *fd) +int osmo_fd_register(struct osmo_fd *fd) { int flags; @@ -44,24 +47,43 @@ int bsc_register_fd(struct bsc_fd *fd) if (flags < 0) return flags; + /* set close-on-exec flag */ + flags = fcntl(fd->fd, F_GETFD); + if (flags < 0) + return flags; + flags |= FD_CLOEXEC; + flags = fcntl(fd->fd, F_SETFD, flags); + if (flags < 0) + return flags; + /* Register FD */ if (fd->fd > maxfd) maxfd = fd->fd; - llist_add_tail(&fd->list, &bsc_fds); +#ifdef BSC_FD_CHECK + struct osmo_fd *entry; + llist_for_each_entry(entry, &osmo_fds, list) { + if (entry == fd) { + fprintf(stderr, "Adding a osmo_fd that is already in the list.\n"); + return 0; + } + } +#endif + + llist_add_tail(&fd->list, &osmo_fds); return 0; } -void bsc_unregister_fd(struct bsc_fd *fd) +void osmo_fd_unregister(struct osmo_fd *fd) { unregistered_count++; llist_del(&fd->list); } -int bsc_select_main(int polling) +int osmo_select_main(int polling) { - struct bsc_fd *ufd, *tmp; + struct osmo_fd *ufd, *tmp; fd_set readset, writeset, exceptset; int work = 0, rc; struct timeval no_time = {0, 0}; @@ -71,7 +93,7 @@ int bsc_select_main(int polling) FD_ZERO(&exceptset); /* prepare read and write fdsets */ - llist_for_each_entry(ufd, &bsc_fds, list) { + llist_for_each_entry(ufd, &osmo_fds, list) { if (ufd->when & BSC_FD_READ) FD_SET(ufd->fd, &readset); @@ -82,21 +104,21 @@ int bsc_select_main(int polling) FD_SET(ufd->fd, &exceptset); } - bsc_timer_check(); + osmo_timers_check(); if (!polling) - bsc_prepare_timers(); - rc = select(maxfd+1, &readset, &writeset, &exceptset, polling ? &no_time : bsc_nearest_timer()); + osmo_timers_prepare(); + rc = select(maxfd+1, &readset, &writeset, &exceptset, polling ? &no_time : osmo_timers_nearest()); if (rc < 0) return 0; /* fire timers */ - bsc_update_timers(); + osmo_timers_update(); /* call registered callback functions */ restart: unregistered_count = 0; - llist_for_each_entry_safe(ufd, tmp, &bsc_fds, list) { + llist_for_each_entry_safe(ufd, tmp, &osmo_fds, list) { int flags = 0; if (FD_ISSET(ufd->fd, &readset)) {