1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
|
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* gck-call.c - the GObject PKCS#11 wrapper library
Copyright (C) 2008, Stefan Walter
The Gnome Keyring Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The Gnome Keyring Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the Gnome Library; see the file COPYING.LIB. If not,
write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
Author: Stef Walter <nielsen@memberwebs.com>
*/
#include "config.h"
#include "gck-private.h"
#include <string.h>
typedef struct _GckCallSource GckCallSource;
static gpointer _gck_call_parent_class = NULL;
struct _GckCall {
GObject parent;
GckModule *module;
/* For making the call */
GckPerformFunc perform;
GckCompleteFunc complete;
GckArguments *args;
GCancellable *cancellable;
GDestroyNotify destroy;
CK_RV rv;
/* For result callback only */
gpointer object;
GAsyncReadyCallback callback;
gpointer user_data;
};
struct _GckCallClass {
GObjectClass parent;
GThreadPool *thread_pool;
GAsyncQueue *completed_queue;
guint completed_id;
};
struct _GckCallSource {
GSource source;
GckCallClass *klass;
};
/* ----------------------------------------------------------------------------
* HELPER FUNCTIONS
*/
static CK_RV
perform_call (GckPerformFunc func, GCancellable *cancellable, GckArguments *args)
{
CK_RV rv;
/* Double check a few things */
g_assert (func);
g_assert (args);
if (cancellable) {
if (g_cancellable_is_cancelled (cancellable)) {
return CKR_FUNCTION_CANCELED;
}
/* Push for the notify callback */
g_object_ref (cancellable);
g_cancellable_push_current (cancellable);
}
rv = (func) (args);
if (cancellable) {
g_cancellable_pop_current (cancellable);
g_object_unref (cancellable);
}
return rv;
}
static gboolean
complete_call (GckCompleteFunc func, GckArguments *args, CK_RV result)
{
/* Double check a few things */
g_assert (args);
/* If no complete function, then just ignore */
if (!func)
return TRUE;
return (func) (args, result);
}
static void
process_async_call (gpointer data, GckCallClass *klass)
{
GckCall *call = GCK_CALL (data);
g_assert (GCK_IS_CALL (call));
call->rv = perform_call (call->perform, call->cancellable, call->args);
g_async_queue_push (klass->completed_queue, call);
/* Wakeup main thread if on a separate thread */
g_main_context_wakeup (NULL);
}
static void
process_result (GckCall *call, gpointer unused)
{
gboolean stop = FALSE;
/* Double check a few things */
g_assert (GCK_IS_CALL (call));
if (call->cancellable) {
/* Don't call the callback when cancelled */
if (g_cancellable_is_cancelled (call->cancellable)) {
call->rv = CKR_FUNCTION_CANCELED;
stop = TRUE;
}
}
/*
* Hmmm, does the function want to actually be done?
* If not, then queue this call again.
*/
if (!stop && !complete_call (call->complete, call->args, call->rv)) {
g_object_ref (call);
g_thread_pool_push (GCK_CALL_GET_CLASS (call)->thread_pool, call, NULL);
/* All done, finish processing */
} else if (call->callback) {
g_assert (G_IS_OBJECT (call->object));
(call->callback) (G_OBJECT (call->object), G_ASYNC_RESULT (call),
call->user_data);
}
}
static gboolean
process_completed (GckCallClass *klass)
{
gpointer call;
g_assert (klass->completed_queue);
call = g_async_queue_try_pop (klass->completed_queue);
if (call) {
process_result (call, NULL);
g_object_unref (call);
return TRUE;
}
return FALSE;
}
static gboolean
completed_prepare(GSource* base, gint *timeout)
{
GckCallSource *source = (GckCallSource*)base;
gboolean have;
g_assert (source->klass->completed_queue);
have = g_async_queue_length (source->klass->completed_queue) > 0;
*timeout = have ? 0 : -1;
return have;
}
static gboolean
completed_check(GSource* base)
{
GckCallSource *source = (GckCallSource*)base;
g_assert (source->klass->completed_queue);
return g_async_queue_length (source->klass->completed_queue) > 0;
}
static gboolean
completed_dispatch(GSource* base, GSourceFunc callback, gpointer user_data)
{
GckCallSource *source = (GckCallSource*)base;
process_completed (source->klass);
return TRUE;
}
static void
completed_finalize(GSource* base)
{
}
static GSourceFuncs completed_functions = {
completed_prepare,
completed_check,
completed_dispatch,
completed_finalize
};
/* ----------------------------------------------------------------------------
* OBJECT
*/
static void
_gck_call_init (GckCall *call)
{
call->rv = CKR_OK;
}
static void
_gck_call_finalize (GObject *obj)
{
GckCall *call = GCK_CALL (obj);
if (call->module)
g_object_unref (call->module);
call->module = NULL;
if (call->object)
g_object_unref (call->object);
call->object = NULL;
if (call->cancellable)
g_object_unref (call->cancellable);
call->cancellable = NULL;
if (call->destroy)
(call->destroy) (call->args);
call->destroy = NULL;
call->args = NULL;
G_OBJECT_CLASS (_gck_call_parent_class)->finalize (obj);
}
static gpointer
_gck_call_get_user_data (GAsyncResult *async_result)
{
g_return_val_if_fail (GCK_IS_CALL (async_result), NULL);
return GCK_CALL (async_result)->user_data;
}
static GObject*
_gck_call_get_source_object (GAsyncResult *async_result)
{
g_return_val_if_fail (GCK_IS_CALL (async_result), NULL);
return GCK_CALL (async_result)->object;
}
static void
_gck_call_implement_async_result (GAsyncResultIface *iface)
{
iface->get_user_data = _gck_call_get_user_data;
iface->get_source_object = _gck_call_get_source_object;
}
static void
_gck_call_class_init (GckCallClass *klass)
{
GObjectClass *gobject_class = (GObjectClass*)klass;
_gck_call_parent_class = g_type_class_peek_parent (klass);
gobject_class->finalize = _gck_call_finalize;
}
static void
_gck_call_base_init (GckCallClass *klass)
{
GckCallSource *source;
GMainContext *context;
GError *err = NULL;
klass->thread_pool = g_thread_pool_new ((GFunc)process_async_call, klass, 16, FALSE, &err);
if (!klass->thread_pool) {
g_critical ("couldn't create thread pool: %s",
err && err->message ? err->message : "");
return;
}
klass->completed_queue = g_async_queue_new_full (g_object_unref);
g_assert (klass->completed_queue);
context = g_main_context_default ();
g_assert (context);
/* Add our idle handler which processes other tasks */
source = (GckCallSource*)g_source_new (&completed_functions, sizeof (GckCallSource));
source->klass = klass;
klass->completed_id = g_source_attach ((GSource*)source, context);
g_source_set_callback ((GSource*)source, NULL, NULL, NULL);
g_source_unref ((GSource*)source);
}
static void
_gck_call_base_finalize (GckCallClass *klass)
{
GMainContext *context;
GSource *src;
if (klass->thread_pool) {
g_assert (g_thread_pool_unprocessed (klass->thread_pool) == 0);
g_thread_pool_free (klass->thread_pool, FALSE, TRUE);
klass->thread_pool = NULL;
}
if (klass->completed_id) {
context = g_main_context_default ();
g_return_if_fail (context);
src = g_main_context_find_source_by_id (context, klass->completed_id);
g_assert (src);
g_source_destroy (src);
klass->completed_id = 0;
}
if (klass->completed_queue) {
g_assert (g_async_queue_length (klass->completed_queue));
g_async_queue_unref (klass->completed_queue);
klass->completed_queue = NULL;
}
}
GType
_gck_call_get_type (void)
{
static volatile gsize type_id__volatile = 0;
if (g_once_init_enter (&type_id__volatile)) {
static const GTypeInfo type_info = {
sizeof (GckCallClass),
(GBaseInitFunc)_gck_call_base_init,
(GBaseFinalizeFunc)_gck_call_base_finalize,
(GClassInitFunc)_gck_call_class_init,
(GClassFinalizeFunc)NULL,
NULL, // class_data
sizeof (GckCall),
0, // n_preallocs
(GInstanceInitFunc)_gck_call_init,
};
static const GInterfaceInfo interface_info = {
(GInterfaceInitFunc)_gck_call_implement_async_result
};
GType type_id = g_type_register_static (G_TYPE_OBJECT, "_GckCall", &type_info, 0);
g_type_add_interface_static (type_id, G_TYPE_ASYNC_RESULT, &interface_info);
g_once_init_leave (&type_id__volatile, type_id);
}
return type_id__volatile;
}
/* ----------------------------------------------------------------------------
* PUBLIC
*/
void
_gck_call_uninitialize (void)
{
}
gboolean
_gck_call_sync (gpointer object, gpointer perform, gpointer complete,
gpointer data, GCancellable *cancellable, GError **err)
{
GckArguments *args = (GckArguments*)data;
GckModule *module = NULL;
CK_RV rv;
g_assert (G_IS_OBJECT (object));
g_assert (perform);
g_assert (args);
g_object_get (object, "module", &module, "handle", &args->handle, NULL);
g_assert (GCK_IS_MODULE (module));
/* We now hold a reference to module until below */
args->pkcs11 = gck_module_get_functions (module);
g_assert (args->pkcs11);
do {
rv = perform_call (perform, cancellable, args);
if (rv == CKR_FUNCTION_CANCELED)
break;
} while (!complete_call (complete, args, rv));
g_object_unref (module);
if (rv == CKR_OK)
return TRUE;
g_set_error (err, GCK_ERROR, rv, "%s", gck_message_from_rv (rv));
return FALSE;
}
gpointer
_gck_call_async_prep (gpointer object, gpointer cb_object, gpointer perform,
gpointer complete, gsize args_size, gpointer destroy)
{
GckArguments *args;
GckCall *call;
g_assert (!object || G_IS_OBJECT (object));
g_assert (perform);
if (!destroy)
destroy = g_free;
if (args_size == 0)
args_size = sizeof (GckArguments);
g_assert (args_size >= sizeof (GckArguments));
args = g_malloc0 (args_size);
call = g_object_new (GCK_TYPE_CALL, NULL);
call->destroy = (GDestroyNotify)destroy;
call->perform = (GckPerformFunc)perform;
call->complete = (GckCompleteFunc)complete;
call->object = cb_object;
g_object_ref (cb_object);
/* Hook the two together */
call->args = args;
call->args->call = call;
/* Setup call object if available */
if (object != NULL)
_gck_call_async_object (call, object);
return args;
}
void
_gck_call_async_object (GckCall *call, gpointer object)
{
g_assert (GCK_IS_CALL (call));
g_assert (call->args);
if (call->module)
g_object_unref (call->module);
call->module = NULL;
g_object_get (object, "module", &call->module, "handle", &call->args->handle, NULL);
g_assert (GCK_IS_MODULE (call->module));
call->args->pkcs11 = gck_module_get_functions (call->module);
/* We now hold a reference on module until finalize */
}
GckCall*
_gck_call_async_ready (gpointer data, GCancellable *cancellable,
GAsyncReadyCallback callback, gpointer user_data)
{
GckArguments *args = (GckArguments*)data;
g_assert (GCK_IS_CALL (args->call));
args->call->cancellable = cancellable;
if (cancellable) {
g_assert (G_IS_CANCELLABLE (cancellable));
g_object_ref (cancellable);
}
args->call->callback = callback;
args->call->user_data = user_data;
return args->call;
}
void
_gck_call_async_go (GckCall *call)
{
g_assert (GCK_IS_CALL (call));
g_assert (call->args->pkcs11);
/* To keep things balanced, process at one completed event */
process_completed(GCK_CALL_GET_CLASS (call));
g_assert (GCK_CALL_GET_CLASS (call)->thread_pool);
g_thread_pool_push (GCK_CALL_GET_CLASS (call)->thread_pool, call, NULL);
}
void
_gck_call_async_ready_go (gpointer data, GCancellable *cancellable,
GAsyncReadyCallback callback, gpointer user_data)
{
GckCall *call = _gck_call_async_ready (data, cancellable, callback, user_data);
_gck_call_async_go (call);
}
gboolean
_gck_call_basic_finish (GAsyncResult *result, GError **err)
{
CK_RV rv;
g_return_val_if_fail (GCK_IS_CALL (result), FALSE);
rv = GCK_CALL (result)->rv;
if (rv == CKR_OK)
return TRUE;
g_set_error (err, GCK_ERROR, rv, "%s", gck_message_from_rv (rv));
return FALSE;
}
void
_gck_call_async_short (GckCall *call, CK_RV rv)
{
g_assert (GCK_IS_CALL (call));
call->rv = rv;
/* Already complete, so just push it for processing in main loop */
g_assert (GCK_CALL_GET_CLASS (call)->completed_queue);
g_async_queue_push (GCK_CALL_GET_CLASS (call)->completed_queue, call);
g_main_context_wakeup (NULL);
}
gpointer
_gck_call_get_arguments (GckCall *call)
{
g_assert (GCK_IS_CALL (call));
return call->args;
}
|