+static int iochan_use(int delta)
+{
+ int iochans;
+ if (!g_mutex)
+ yaz_mutex_create(&g_mutex);
+ yaz_mutex_enter(g_mutex);
+ no_iochans += delta;
+ if (delta > 0)
+ no_iochans_total += delta;
+ iochans = no_iochans;
+ yaz_mutex_leave(g_mutex);
+ yaz_log(YLOG_DEBUG, "%s iochans=%d",
+ delta == 0 ? "" : (delta > 0 ? "INC" : "DEC"), iochans);
+ return iochans;
+}
+
+int iochans_count(void)
+{
+ return iochan_use(0);
+}
+
+int iochans_count_total(void)
+{
+ int total = 0;
+ if (!g_mutex)
+ return 0;
+ yaz_mutex_enter(g_mutex);
+ total = no_iochans_total;
+ yaz_mutex_leave(g_mutex);
+ return total;
+}
+#else
+#define iochan_use(x)
+#define iochans_count(x) 0
+#define iochans_count_total(x) 0
+#endif
+
+struct iochan_man_s {
+ IOCHAN channel_list;
+ sel_thread_t sel_thread;
+ int sel_fd;
+ int no_threads;
+ int log_level;
+ YAZ_MUTEX iochan_mutex;
+ int size_fds;
+ int limit_fd;
+ struct yaz_poll_fd *fds;
+};
+
+iochan_man_t iochan_man_create(int no_threads, int max_sockets)
+{
+ iochan_man_t man = xmalloc(sizeof(*man));
+ man->channel_list = 0;
+ man->sel_thread = 0; /* can't create sel_thread yet because we may fork */
+ man->sel_fd = -1;
+ man->no_threads = no_threads;
+ man->log_level = yaz_log_module_level("iochan");
+ man->iochan_mutex = 0;
+ man->size_fds = 0;
+ man->fds = 0;
+ man->limit_fd = 0;
+#if HAVE_GETRLIMIT
+ {
+ struct rlimit limit_data;
+ getrlimit(RLIMIT_NOFILE, &limit_data);
+ yaz_log(YLOG_LOG, "getrlimit NOFILE cur=%ld max=%ld",
+ (long) limit_data.rlim_cur, (long) limit_data.rlim_max);
+ man->limit_fd = limit_data.rlim_cur - 200;
+ }
+#endif
+ if (max_sockets)
+ man->limit_fd = max_sockets;
+ yaz_log(YLOG_LOG, "iochan threads %d limit fd %d", no_threads,
+ man->limit_fd);
+ yaz_mutex_create(&man->iochan_mutex);
+ return man;
+}
+
+IOCHAN iochan_destroy_real(IOCHAN chan)
+{
+ IOCHAN next = chan->next;
+ if (chan->name)
+ xfree(chan->name);
+ xfree(chan);
+ iochan_use(-1);
+ return next;
+}
+
+void iochan_man_destroy(iochan_man_t *mp)
+{
+ if (*mp)
+ {
+ IOCHAN c;
+ if ((*mp)->sel_thread)
+ sel_thread_destroy((*mp)->sel_thread);
+
+ yaz_mutex_enter((*mp)->iochan_mutex);
+ c = (*mp)->channel_list;
+ (*mp)->channel_list = NULL;
+ yaz_mutex_leave((*mp)->iochan_mutex);
+ while (c)
+ c = iochan_destroy_real(c);
+ yaz_mutex_destroy(&(*mp)->iochan_mutex);
+ xfree((*mp)->fds);
+ xfree(*mp);
+ *mp = 0;
+ }
+}
+
+int iochan_add(iochan_man_t man, IOCHAN chan, int slack)
+{
+ int r = 0, no_fds = 0;
+ IOCHAN p;
+
+ yaz_log(man->log_level, "iochan_add : chan=%p channel list=%p", chan,
+ man->channel_list);
+ yaz_mutex_enter(man->iochan_mutex);
+ for (p = man->channel_list; p; p = p->next)
+ {
+ if (p->fd >= 0)
+ no_fds++;
+ }
+ if (slack >= 0 && man->limit_fd > 0 && no_fds >= man->limit_fd - slack)
+ {
+ r = -1;
+ yaz_log(YLOG_WARN, "max channels %d in use", no_fds);
+ }
+ else
+ {
+ chan->man = man;
+ chan->next = man->channel_list;
+ man->channel_list = chan;
+ }
+ yaz_mutex_leave(man->iochan_mutex);
+ return r;
+}
+
+void iochan_destroy(IOCHAN chan)
+{
+ if (chan->man)
+ chan->destroyed = 1;
+ else
+ iochan_destroy_real(chan);
+}
+
+IOCHAN iochan_create(int fd, IOC_CALLBACK cb, int flags, const char *name)