2 Copyright (C) 2000 Werner Koch (dd9jn)
3 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2007 g10 Code GmbH
5 This file is part of GPGME.
7 GPGME is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as
9 published by the Free Software Foundation; either version 2.1 of
10 the License, or (at your option) any later version.
12 GPGME is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public
18 License along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
29 #ifdef HAVE_SYS_TYPES_H
30 # include <sys/types.h>
44 _gpgme_fd_table_init (fd_table_t fdt)
51 _gpgme_fd_table_deinit (fd_table_t fdt)
58 /* XXX We should keep a marker and roll over for speed. */
60 fd_table_put (fd_table_t fdt, int fd, int dir, void *opaque, int *idx)
63 struct io_select_fd_s *new_fds;
65 for (i = 0; i < fdt->size; i++)
67 if (fdt->fds[i].fd == -1)
72 #define FDT_ALLOCSIZE 10
73 new_fds = realloc (fdt->fds, (fdt->size + FDT_ALLOCSIZE)
76 return gpg_error_from_errno (errno);
79 fdt->size += FDT_ALLOCSIZE;
80 for (j = 0; j < FDT_ALLOCSIZE; j++)
81 fdt->fds[i + j].fd = -1;
85 fdt->fds[i].for_read = (dir == 1);
86 fdt->fds[i].for_write = (dir == 0);
87 fdt->fds[i].signaled = 0;
88 fdt->fds[i].opaque = opaque;
94 /* Register the file descriptor FD with the handler FNC (which gets
95 FNC_DATA as its first argument) for the direction DIR. DATA should
96 be the context for which the fd is added. R_TAG will hold the tag
97 that can be used to remove the fd. */
99 _gpgme_add_io_cb (void *data, int fd, int dir, gpgme_io_cb_t fnc,
100 void *fnc_data, void **r_tag)
103 gpgme_ctx_t ctx = (gpgme_ctx_t) data;
105 struct wait_item_s *item;
114 tag = malloc (sizeof *tag);
116 return gpg_error_from_errno (errno);
119 /* Allocate a structure to hold information about the handler. */
120 item = calloc (1, sizeof *item);
123 int saved_errno = errno;
125 return gpg_error_from_errno (saved_errno);
130 item->handler_value = fnc_data;
132 err = fd_table_put (fdt, fd, dir, item, &tag->idx);
140 TRACE3 (DEBUG_CTX, "_gpgme_add_io_cb", ctx,
141 "fd %d, dir=%d -> tag=%p", fd, dir, tag);
149 _gpgme_remove_io_cb (void *data)
151 struct tag *tag = data;
163 TRACE2 (DEBUG_CTX, "_gpgme_remove_io_cb", data,
164 "setting fd 0x%x (item=%p) done", fdt->fds[idx].fd,
165 fdt->fds[idx].opaque);
167 free (fdt->fds[idx].opaque);
170 /* Free the table entry. */
171 fdt->fds[idx].fd = -1;
172 fdt->fds[idx].for_read = 0;
173 fdt->fds[idx].for_write = 0;
174 fdt->fds[idx].opaque = NULL;
178 /* This is slightly embarrassing. The problem is that running an I/O
179 callback _may_ influence the status of other file descriptors. Our
180 own event loops could compensate for that, but the external event
181 loops cannot. FIXME: We may still want to optimize this a bit when
182 we are called from our own event loops. So if CHECKED is 1, the
185 _gpgme_run_io_cb (struct io_select_fd_s *an_fds, int checked,
186 gpgme_error_t *op_err)
188 struct wait_item_s *item;
189 struct io_cb_data iocb_data;
192 item = (struct wait_item_s *) an_fds->opaque;
198 struct io_select_fd_s fds;
200 TRACE0 (DEBUG_CTX, "_gpgme_run_io_cb", item, "need to check");
203 /* Just give it a quick poll. */
204 nr = _gpgme_io_select (&fds, 1, 1);
209 /* The status changed in the meantime, there is nothing left
214 TRACE2 (DEBUG_CTX, "_gpgme_run_io_cb", item, "handler (%p, %d)",
215 item->handler_value, an_fds->fd);
217 iocb_data.handler_value = item->handler_value;
218 iocb_data.op_err = 0;
219 err = item->handler (&iocb_data, an_fds->fd);
221 *op_err = iocb_data.op_err;