Merge commit '430be849945688ae107b079db1e216329b1a1f06'
[osmocom-bb.git] / src / shared / libosmocore / src / select.c
index 9517778..4b002ae 100644 (file)
@@ -5,8 +5,9 @@
  * (C) 2000-2009 by Harald Welte <laforge@gnumonks.org>
  *
  *  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
  */
 
 #include <fcntl.h>
-#include <osmocore/select.h>
-#include <osmocore/linuxlist.h>
-#include <osmocore/timer.h>
+#include <stdio.h>
+
+#include <osmocom/core/select.h>
+#include <osmocom/core/linuxlist.h>
+#include <osmocom/core/timer.h>
 
 #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)) {
@@ -121,7 +143,8 @@ restart:
                /* ugly, ugly hack. If more than one filedescriptors were
                 * unregistered, they might have been consecutive and
                 * llist_for_each_entry_safe() is no longer safe */
-               if (unregistered_count > 1)
+               /* this seems to happen with the last element of the list as well */
+               if (unregistered_count >= 1)
                        goto restart;
        }
        return work;