2010-09-01 Marcus Brinkmann <marcus@g10code.de>
[gpgme.git] / src / wait-private.c
1 /* wait-private.c 
2    Copyright (C) 2000 Werner Koch (dd9jn)
3    Copyright (C) 2001, 2002, 2003, 2004, 2005 g10 Code GmbH
4  
5    This file is part of GPGME.
6  
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.
11    
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.
16    
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
20    02111-1307, USA.  */
21
22 #if HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25 #include <assert.h>
26 #include <errno.h>
27
28 #include "gpgme.h"
29 #include "context.h"
30 #include "wait.h"
31 #include "ops.h"
32 #include "priv-io.h"
33 #include "util.h"
34
35 \f
36 /* The private event loops are used for all blocking operations, and
37    for the key and trust item listing operations.  They are completely
38    separated from each other.  */
39
40 \f
41 /* Internal I/O callback functions.  */
42
43 /* The add_io_cb and remove_io_cb handlers are shared with the global
44    event loops.  */
45
46 void
47 _gpgme_wait_private_event_cb (void *data, gpgme_event_io_t type,
48                               void *type_data)
49 {
50   switch (type)
51     {
52     case GPGME_EVENT_START:
53       /* Nothing to do here, as the wait routine is called after the
54          initialization is finished.  */
55       break;
56
57     case GPGME_EVENT_DONE:
58       break;
59
60     case GPGME_EVENT_NEXT_KEY:
61       _gpgme_op_keylist_event_cb (data, type, type_data);
62       break;
63
64     case GPGME_EVENT_NEXT_TRUSTITEM:
65       _gpgme_op_trustlist_event_cb (data, type, type_data);
66       break;
67     }
68 }
69
70 \f
71 /* If COND is a null pointer, wait until the blocking operation in CTX
72    finished and return its error value.  Otherwise, wait until COND is
73    satisfied or the operation finished.  */
74 gpgme_error_t
75 _gpgme_wait_on_condition (gpgme_ctx_t ctx, volatile int *cond,
76                           gpgme_error_t *op_err_p)
77 {
78   gpgme_error_t err = 0;
79   int hang = 1;
80
81   if (op_err_p)
82     *op_err_p = 0;
83
84   do
85     {
86       int nr = _gpgme_io_select (ctx->fdt.fds, ctx->fdt.size, 0);
87       unsigned int i;
88
89       if (nr < 0)
90         {
91           /* An error occured.  Close all fds in this context, and
92              signal it.  */
93           err = gpg_error_from_errno (errno);
94           _gpgme_cancel_with_err (ctx, err, 0);
95
96           return err;
97         }
98       
99       for (i = 0; i < ctx->fdt.size && nr; i++)
100         {
101           if (ctx->fdt.fds[i].fd != -1 && ctx->fdt.fds[i].signaled)
102             {
103               gpgme_error_t op_err = 0;
104
105               ctx->fdt.fds[i].signaled = 0;
106               assert (nr);
107               nr--;
108
109               LOCK (ctx->lock);
110               if (ctx->canceled)
111                 err = gpg_error (GPG_ERR_CANCELED);
112               UNLOCK (ctx->lock);
113               
114               if (!err)
115                 err = _gpgme_run_io_cb (&ctx->fdt.fds[i], 0, &op_err);  
116               if (err)
117                 {
118                   /* An error occured.  Close all fds in this context,
119                      and signal it.  */
120                   _gpgme_cancel_with_err (ctx, err, 0);
121
122                   return err;
123                 }
124               else if (op_err)
125                 {
126                   /* An operational error occured.  Cancel the current
127                      operation but not the session, and signal it.  */
128                   _gpgme_cancel_with_err (ctx, 0, op_err);
129
130                   /* NOTE: This relies on the operational error being
131                      generated after the operation really has
132                      completed, for example after no further status
133                      line output is generated.  Otherwise the
134                      following I/O will spill over into the next
135                      operation.  */
136                   if (op_err_p)
137                     *op_err_p = op_err;
138                   return 0;
139                 }
140             }
141         }
142
143       for (i = 0; i < ctx->fdt.size; i++)
144         if (ctx->fdt.fds[i].fd != -1)
145           break;
146       if (i == ctx->fdt.size)
147         {
148           struct gpgme_io_event_done_data data;
149           data.err = 0;
150           data.op_err = 0;
151           _gpgme_engine_io_event (ctx->engine, GPGME_EVENT_DONE, &data);
152           hang = 0;
153         }
154       if (cond && *cond)
155         hang = 0;
156     }
157   while (hang);
158
159   return 0;
160 }
161
162
163 /* Wait until the blocking operation in context CTX has finished and
164    return the error value.  This variant can not be used for
165    session-based protocols.  */
166 gpgme_error_t
167 _gpgme_wait_one (gpgme_ctx_t ctx)
168 {
169   return _gpgme_wait_on_condition (ctx, NULL, NULL);
170 }
171
172 /* Wait until the blocking operation in context CTX has finished and
173    return the error value.  This is the right variant to use for
174    sesion-based protocols.  */
175 gpgme_error_t
176 _gpgme_wait_one_ext (gpgme_ctx_t ctx, gpgme_error_t *op_err)
177 {
178   return _gpgme_wait_on_condition (ctx, NULL, op_err);
179 }