summaryrefslogtreecommitdiff
path: root/src/dumm/ext/dumm.c
blob: a9c7cb8bde26b4a09a6a4aa258944d0a83061687 (plain)
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
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
/*
 * Copyright (C) 2008-2010 Tobias Brunner
 * Copyright (C) 2008 Martin Willi
 * Hochschule fuer Technik Rapperswil
 *
 * This program is free software; you can redistribute it and/or modify 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.  See <http://www.fsf.org/copyleft/gpl.txt>.
 *
 * This program 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 General Public License
 * for more details.
 */

#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <fcntl.h>

#include <library.h>
#include <dumm.h>
#include <debug.h>
#include <utils/linked_list.h>

#undef PACKAGE_NAME
#undef PACKAGE_TARNAME
#undef PACKAGE_VERSION
#undef PACKAGE_STRING
#undef PACKAGE_BUGREPORT
#undef PACKAGE_URL
#include <ruby.h>

static dumm_t *dumm;

static VALUE rbm_dumm;
static VALUE rbc_guest;
static VALUE rbc_bridge;
static VALUE rbc_iface;
static VALUE rbc_template;

/**
 * Guest invocation callback
 */
static pid_t invoke(void *null, guest_t *guest, char *args[], int argc)
{
	pid_t pid;

	pid = fork();
	switch (pid)
	{
		case 0: /* child */
			/* create a new process group in order to prevent signals (e.g.
			 * SIGINT) sent to the parent from terminating the child */
			setpgid(0, 0);
			dup2(open("/dev/null", 0), 1);
			dup2(open("/dev/null", 0), 2);
			execvp(args[0], args);
			/* FALL */
		case -1:
			return 0;
		default:
			return pid;
	}
}

/**
 * SIGCHLD signal handler
 */
static void sigchld_handler(int signal, siginfo_t *info, void* ptr)
{
	enumerator_t *enumerator;
	guest_t *guest;

	enumerator = dumm->create_guest_enumerator(dumm);
	while (enumerator->enumerate(enumerator, &guest))
	{
		if (guest->get_pid(guest) == info->si_pid)
		{
			guest->sigchild(guest);
			break;
		}
	}
	enumerator->destroy(enumerator);
}


/**
 * Global Dumm bindings
 */
static VALUE dumm_add_overlay(VALUE class, VALUE dir)
{
	if (!dumm->add_overlay(dumm, StringValuePtr(dir)))
	{
		rb_raise(rb_eRuntimeError, "loading overlay failed");
	}
	return class;
}

static VALUE dumm_del_overlay(VALUE class, VALUE dir)
{
	return dumm->del_overlay(dumm, StringValuePtr(dir)) ? Qtrue : Qfalse;
}

static VALUE dumm_pop_overlay(VALUE class)
{
	return dumm->pop_overlay(dumm) ? Qtrue : Qfalse;
}

static void dumm_init()
{
	rbm_dumm = rb_define_module("Dumm");

	rb_define_module_function(rbm_dumm, "add_overlay", dumm_add_overlay, 1);
	rb_define_module_function(rbm_dumm, "del_overlay", dumm_del_overlay, 1);
	rb_define_module_function(rbm_dumm, "pop_overlay", dumm_pop_overlay, 0);
}

/**
 * Guest bindings
 */
static VALUE guest_hash_create(VALUE class)
{
	enumerator_t *enumerator;
	guest_t *guest;
	VALUE hash = rb_hash_new();
	enumerator = dumm->create_guest_enumerator(dumm);
	while (enumerator->enumerate(enumerator, &guest))
	{
		rb_hash_aset(hash, rb_str_new2(guest->get_name(guest)),
					 Data_Wrap_Struct(class, NULL, NULL, guest));
	}
	enumerator->destroy(enumerator);
	return hash;
}

static VALUE guest_hash(VALUE class)
{
	ID id = rb_intern("@@guests");
	if (!rb_cvar_defined(class, id))
	{
		VALUE hash = guest_hash_create(class);
		rb_cvar_set(class, id, hash, 0);
		return hash;
	}
	return rb_cvar_get(class, id);
}

static VALUE guest_find(VALUE class, VALUE key)
{
	if (TYPE(key) != T_STRING)
	{
		key = rb_convert_type(key, T_STRING, "String", "to_s");
	}
	return rb_hash_aref(guest_hash(class), key);
}

static VALUE guest_get(VALUE class, VALUE key)
{
	return guest_find(class, key);
}

static VALUE guest_each(int argc, VALUE *argv, VALUE class)
{
	if (!rb_block_given_p())
	{
		rb_raise(rb_eArgError, "must be called with a block");
	}
	rb_block_call(guest_hash(class), rb_intern("each_value"), 0, 0,
				  rb_yield, 0);
	return class;
}

static VALUE guest_new(VALUE class, VALUE name, VALUE kernel,
					   VALUE master, VALUE args)
{
	VALUE self;
	guest_t *guest;
	guest = dumm->create_guest(dumm, StringValuePtr(name),
							   StringValuePtr(kernel), StringValuePtr(master),
							   StringValuePtr(args));
	if (!guest)
	{
		rb_raise(rb_eRuntimeError, "creating guest failed");
	}
	self = Data_Wrap_Struct(class, NULL, NULL, guest);
	rb_hash_aset(guest_hash(class), name, self);
	return self;
}

static VALUE guest_to_s(VALUE self)
{
	guest_t *guest;

	Data_Get_Struct(self, guest_t, guest);
	return rb_str_new2(guest->get_name(guest));
}

static VALUE guest_start(VALUE self)
{
	guest_t *guest;

	Data_Get_Struct(self, guest_t, guest);

	if (!guest->start(guest, invoke, NULL, NULL))
	{
		rb_raise(rb_eRuntimeError, "starting guest failed");
	}
	return self;
}

static VALUE guest_stop(VALUE self)
{
	guest_t *guest;

	Data_Get_Struct(self, guest_t, guest);
	guest->stop(guest, NULL);
	return self;
}

static VALUE guest_running(VALUE self)
{
	guest_t *guest;

	Data_Get_Struct(self, guest_t, guest);
	return guest->get_pid(guest) ? Qtrue : Qfalse;
}

static void exec_cb(void *data, char *buf)
{
	rb_yield(rb_str_new2(buf));
}

static VALUE guest_exec(VALUE self, VALUE cmd)
{
	guest_t *guest;
	bool block;
	int ret;

	block = rb_block_given_p();
	Data_Get_Struct(self, guest_t, guest);
	ret = guest->exec_str(guest, block ? (void*)exec_cb : NULL, TRUE, NULL,
						  "exec %s", StringValuePtr(cmd));
	rb_iv_set(self, "@execstatus", INT2NUM(ret));
	return self;
}

static VALUE guest_mconsole(VALUE self, VALUE cmd)
{
	guest_t *guest;
	bool block;
	int ret;

	block = rb_block_given_p();
	Data_Get_Struct(self, guest_t, guest);
	if ((ret = guest->exec_str(guest, block ? (void*)exec_cb : NULL, TRUE, NULL,
					"%s", StringValuePtr(cmd))) != 0)
	{
		rb_raise(rb_eRuntimeError, "executing command failed (%d)", ret);
	}
	return self;
}

static VALUE guest_add_iface(VALUE self, VALUE name)
{
	guest_t *guest;
	iface_t *iface;

	Data_Get_Struct(self, guest_t, guest);
	iface = guest->create_iface(guest, StringValuePtr(name));
	if (!iface)
	{
		rb_raise(rb_eRuntimeError, "adding interface failed");
	}
	return Data_Wrap_Struct(rbc_iface, NULL, NULL, iface);
}

static VALUE guest_find_iface(VALUE self, VALUE key)
{
	enumerator_t *enumerator;
	iface_t *iface, *found = NULL;
	guest_t *guest;

	if (TYPE(key) == T_SYMBOL)
	{
		key = rb_convert_type(key, T_STRING, "String", "to_s");
	}
	Data_Get_Struct(self, guest_t, guest);
	enumerator = guest->create_iface_enumerator(guest);
	while (enumerator->enumerate(enumerator, &iface))
	{
		if (streq(iface->get_guestif(iface), StringValuePtr(key)))
		{
			found = iface;
			break;
		}
	}
	enumerator->destroy(enumerator);
	if (!found)
	{
		return Qnil;
	}
	return Data_Wrap_Struct(rbc_iface, NULL, NULL, iface);
}

static VALUE guest_get_iface(VALUE self, VALUE key)
{
	VALUE iface = guest_find_iface(self, key);
	if (NIL_P(iface))
	{
		rb_raise(rb_eRuntimeError, "interface not found");
	}
	return iface;
}

static VALUE guest_each_iface(int argc, VALUE *argv, VALUE self)
{
	enumerator_t *enumerator;
	linked_list_t *list;
	guest_t *guest;
	iface_t *iface;

	if (!rb_block_given_p())
	{
		rb_raise(rb_eArgError, "must be called with a block");
	}
	Data_Get_Struct(self, guest_t, guest);
	list = linked_list_create();
	enumerator = guest->create_iface_enumerator(guest);
	while (enumerator->enumerate(enumerator, &iface))
	{
		list->insert_last(list, iface);
	}
	enumerator->destroy(enumerator);
	while (list->remove_first(list, (void**)&iface) == SUCCESS)
	{
		rb_yield(Data_Wrap_Struct(rbc_iface, NULL, NULL, iface));
	}
	list->destroy(list);
	return self;
}

static VALUE guest_delete(VALUE self)
{
	guest_t *guest;

	Data_Get_Struct(self, guest_t, guest);
	if (guest->get_pid(guest))
	{
		rb_raise(rb_eRuntimeError, "guest is running");
	}
	dumm->delete_guest(dumm, guest);
	return Qnil;
}

static VALUE guest_add_overlay(VALUE self, VALUE dir)
{
	guest_t *guest;

	Data_Get_Struct(self, guest_t, guest);
	if (!guest->add_overlay(guest, StringValuePtr(dir)))
	{
		rb_raise(rb_eRuntimeError, "loading overlay failed");
	}
	return self;
}

static VALUE guest_del_overlay(VALUE self, VALUE dir)
{
	guest_t *guest;

	Data_Get_Struct(self, guest_t, guest);
	return guest->del_overlay(guest, StringValuePtr(dir)) ? Qtrue : Qfalse;
}

static VALUE guest_pop_overlay(VALUE self)
{
	guest_t *guest;

	Data_Get_Struct(self, guest_t, guest);
	return guest->pop_overlay(guest) ? Qtrue : Qfalse;
}

static void guest_init()
{
	rbc_guest = rb_define_class_under(rbm_dumm , "Guest", rb_cObject);
	rb_include_module(rb_class_of(rbc_guest), rb_mEnumerable);
	rb_include_module(rbc_guest, rb_mEnumerable);

	rb_define_singleton_method(rbc_guest, "[]", guest_get, 1);
	rb_define_singleton_method(rbc_guest, "each", guest_each, -1);
	rb_define_singleton_method(rbc_guest, "new", guest_new, 4);
	rb_define_singleton_method(rbc_guest, "include?", guest_find, 1);
	rb_define_singleton_method(rbc_guest, "guest?", guest_find, 1);

	rb_define_method(rbc_guest, "to_s", guest_to_s, 0);
	rb_define_method(rbc_guest, "start", guest_start, 0);
	rb_define_method(rbc_guest, "stop", guest_stop, 0);
	rb_define_method(rbc_guest, "running?", guest_running, 0);
	rb_define_method(rbc_guest, "exec", guest_exec, 1);
	rb_define_method(rbc_guest, "mconsole", guest_mconsole, 1);
	rb_define_method(rbc_guest, "add", guest_add_iface, 1);
	rb_define_method(rbc_guest, "[]", guest_get_iface, 1);
	rb_define_method(rbc_guest, "each", guest_each_iface, -1);
	rb_define_method(rbc_guest, "include?", guest_find_iface, 1);
	rb_define_method(rbc_guest, "iface?", guest_find_iface, 1);
	rb_define_method(rbc_guest, "delete", guest_delete, 0);
	rb_define_method(rbc_guest, "add_overlay", guest_add_overlay, 1);
	rb_define_method(rbc_guest, "del_overlay", guest_del_overlay, 1);
	rb_define_method(rbc_guest, "pop_overlay", guest_pop_overlay, 0);

	rb_define_attr(rbc_guest, "execstatus", 1, 0);
}

/**
 * Bridge binding
 */
static VALUE bridge_find(VALUE class, VALUE key)
{
	enumerator_t *enumerator;
	bridge_t *bridge, *found = NULL;

	if (TYPE(key) == T_SYMBOL)
	{
		key = rb_convert_type(key, T_STRING, "String", "to_s");
	}
	enumerator = dumm->create_bridge_enumerator(dumm);
	while (enumerator->enumerate(enumerator, &bridge))
	{
		if (streq(bridge->get_name(bridge), StringValuePtr(key)))
		{
			found = bridge;
			break;
		}
	}
	enumerator->destroy(enumerator);
	if (!found)
	{
		return Qnil;
	}
	return Data_Wrap_Struct(class, NULL, NULL, found);
}

static VALUE bridge_get(VALUE class, VALUE key)
{
	VALUE bridge = bridge_find(class, key);
	if (NIL_P(bridge))
	{
		rb_raise(rb_eRuntimeError, "bridge not found");
	}
	return bridge;
}

static VALUE bridge_each(int argc, VALUE *argv, VALUE class)
{
	enumerator_t *enumerator;
	linked_list_t *list;
	bridge_t *bridge;

	if (!rb_block_given_p())
	{
		rb_raise(rb_eArgError, "must be called with a block");
	}
	list = linked_list_create();
	enumerator = dumm->create_bridge_enumerator(dumm);
	while (enumerator->enumerate(enumerator, &bridge))
	{
		list->insert_last(list, bridge);
	}
	enumerator->destroy(enumerator);
	while (list->remove_first(list, (void**)&bridge) == SUCCESS)
	{
		rb_yield(Data_Wrap_Struct(class, NULL, NULL, bridge));
	}
	list->destroy(list);
	return class;
}

static VALUE bridge_new(VALUE class, VALUE name)

{
	bridge_t *bridge;

	bridge = dumm->create_bridge(dumm, StringValuePtr(name));
	if (!bridge)
	{
		rb_raise(rb_eRuntimeError, "creating bridge failed");
	}
	return Data_Wrap_Struct(class, NULL, NULL, bridge);
}

static VALUE bridge_to_s(VALUE self)
{
	bridge_t *bridge;

	Data_Get_Struct(self, bridge_t, bridge);
	return rb_str_new2(bridge->get_name(bridge));
}

static VALUE bridge_each_iface(int argc, VALUE *argv, VALUE self)
{
	enumerator_t *enumerator;
	linked_list_t *list;
	bridge_t *bridge;
	iface_t *iface;

	if (!rb_block_given_p())
	{
		rb_raise(rb_eArgError, "must be called with a block");
	}
	Data_Get_Struct(self, bridge_t, bridge);
	list = linked_list_create();
	enumerator = bridge->create_iface_enumerator(bridge);
	while (enumerator->enumerate(enumerator, &iface))
	{
		list->insert_last(list, iface);
	}
	enumerator->destroy(enumerator);
	while (list->remove_first(list, (void**)&iface) == SUCCESS)
	{
		rb_yield(Data_Wrap_Struct(rbc_iface, NULL, NULL, iface));
	}
	list->destroy(list);
	return self;
}

static VALUE bridge_delete(VALUE self)
{
	bridge_t *bridge;

	Data_Get_Struct(self, bridge_t, bridge);
	dumm->delete_bridge(dumm, bridge);
	return Qnil;
}

static void bridge_init()
{
	rbc_bridge = rb_define_class_under(rbm_dumm , "Bridge", rb_cObject);
	rb_include_module(rb_class_of(rbc_bridge), rb_mEnumerable);
	rb_include_module(rbc_bridge, rb_mEnumerable);

	rb_define_singleton_method(rbc_bridge, "[]", bridge_get, 1);
	rb_define_singleton_method(rbc_bridge, "each", bridge_each, -1);
	rb_define_singleton_method(rbc_bridge, "new", bridge_new, 1);
	rb_define_singleton_method(rbc_bridge, "include?", bridge_find, 1);
	rb_define_singleton_method(rbc_bridge, "bridge?", bridge_find, 1);

	rb_define_method(rbc_bridge, "to_s", bridge_to_s, 0);
	rb_define_method(rbc_bridge, "each", bridge_each_iface, -1);
	rb_define_method(rbc_bridge, "delete", bridge_delete, 0);
}

/**
 * Iface wrapper
 */
static VALUE iface_to_s(VALUE self)
{
	iface_t *iface;

	Data_Get_Struct(self, iface_t, iface);
	return rb_str_new2(iface->get_hostif(iface));
}

static VALUE iface_connect(VALUE self, VALUE vbridge)
{
	iface_t *iface;
	bridge_t *bridge;

	Data_Get_Struct(self, iface_t, iface);
	Data_Get_Struct(vbridge, bridge_t, bridge);
	if (!bridge->connect_iface(bridge, iface))
	{
		rb_raise(rb_eRuntimeError, "connecting iface failed");
	}
	return self;
}

static VALUE iface_disconnect(VALUE self)
{
	iface_t *iface;
	bridge_t *bridge;

	Data_Get_Struct(self, iface_t, iface);
	bridge = iface->get_bridge(iface);
	if (!bridge || !bridge->disconnect_iface(bridge, iface))
	{
		rb_raise(rb_eRuntimeError, "disconnecting iface failed");
	}
	return self;
}

static VALUE iface_add_addr(VALUE self, VALUE name)
{
	iface_t *iface;
	host_t *addr;

	addr = host_create_from_string(StringValuePtr(name), 0);
	if (!addr)
	{
		rb_raise(rb_eArgError, "invalid IP address");
	}
	Data_Get_Struct(self, iface_t, iface);
	if (!iface->add_address(iface, addr))
	{
		addr->destroy(addr);
		rb_raise(rb_eRuntimeError, "adding address failed");
	}
	if (rb_block_given_p()) {
		rb_yield(self);
		iface->delete_address(iface, addr);
	}
	addr->destroy(addr);
	return self;
}

static VALUE iface_each_addr(int argc, VALUE *argv, VALUE self)
{
	enumerator_t *enumerator;
	linked_list_t *list;
	iface_t *iface;
	host_t *addr;
	char buf[64];

	if (!rb_block_given_p())
	{
		rb_raise(rb_eArgError, "must be called with a block");
	}
	Data_Get_Struct(self, iface_t, iface);
	enumerator = iface->create_address_enumerator(iface);
	while (enumerator->enumerate(enumerator, &addr))
	{
		list->insert_last(list, addr->clone(addr));
	}
	enumerator->destroy(enumerator);
	while (list->remove_first(list, (void**)&addr) == SUCCESS)
	{
		snprintf(buf, sizeof(buf), "%H", addr);
		addr->destroy(addr);
		rb_yield(rb_str_new2(buf));
	}
	list->destroy(list);
	return self;
}

static VALUE iface_del_addr(VALUE self, VALUE vaddr)
{
	iface_t *iface;
	host_t *addr;

	addr = host_create_from_string(StringValuePtr(vaddr), 0);
	if (!addr)
	{
		rb_raise(rb_eArgError, "invalid IP address");
	}
	Data_Get_Struct(self, iface_t, iface);
	if (!iface->delete_address(iface, addr))
	{
		addr->destroy(addr);
		rb_raise(rb_eRuntimeError, "address not found");
	}
	if (rb_block_given_p()) {
		rb_yield(self);
		iface->add_address(iface, addr);
	}
	addr->destroy(addr);
	return self;
}

static VALUE iface_delete(VALUE self)
{
	guest_t *guest;
	iface_t *iface;

	Data_Get_Struct(self, iface_t, iface);
	guest = iface->get_guest(iface);
	guest->destroy_iface(guest, iface);
	return Qnil;
}

static void iface_init()
{
	rbc_iface = rb_define_class_under(rbm_dumm , "Iface", rb_cObject);
	rb_include_module(rbc_iface, rb_mEnumerable);

	rb_define_method(rbc_iface, "to_s", iface_to_s, 0);
	rb_define_method(rbc_iface, "connect", iface_connect, 1);
	rb_define_method(rbc_iface, "disconnect", iface_disconnect, 0);
	rb_define_method(rbc_iface, "add", iface_add_addr, 1);
	rb_define_method(rbc_iface, "del", iface_del_addr, 1);
	rb_define_method(rbc_iface, "each", iface_each_addr, -1);
	rb_define_method(rbc_iface, "delete", iface_delete, 0);
}

static VALUE template_load(VALUE class, VALUE dir)
{
	if (!dumm->load_template(dumm, StringValuePtr(dir)))
	{
		rb_raise(rb_eRuntimeError, "loading template failed");
	}
	return class;
}

static VALUE template_unload(VALUE class)
{
	if (!dumm->load_template(dumm, NULL))
	{
		rb_raise(rb_eRuntimeError, "unloading template failed");
	}
	return class;
}

static VALUE template_each(int argc, VALUE *argv, VALUE class)
{
	enumerator_t *enumerator;
	char *template;

	if (!rb_block_given_p())
	{
		rb_raise(rb_eArgError, "must be called with a block");
	}
	enumerator = dumm->create_template_enumerator(dumm);
	while (enumerator->enumerate(enumerator, &template))
	{
		rb_yield(rb_str_new2(template));
	}
	enumerator->destroy(enumerator);
	return class;
}

static void template_init()
{
	rbc_template = rb_define_class_under(rbm_dumm , "Template", rb_cObject);

	rb_define_singleton_method(rbc_template, "load", template_load, 1);
	rb_define_singleton_method(rbc_template, "unload", template_unload, 0);
	rb_define_singleton_method(rbc_template, "each", template_each, -1);
}

/**
 * extension finalization
 */
void Final_dumm()
{
	struct sigaction action;

	dumm->destroy(dumm);

	sigemptyset(&action.sa_mask);
	action.sa_handler = SIG_DFL;
	action.sa_flags = 0;
	sigaction(SIGCHLD, &action, NULL);

	library_deinit();
}

/**
 * extension initialization
 */
void Init_dumm()
{
	struct sigaction action;

	/* there are too many to report, rubyruby... */
	setenv("LEAK_DETECTIVE_DISABLE", "1", 1);

	library_init(NULL);

	dumm = dumm_create(NULL);

	dumm_init();
	guest_init();
	bridge_init();
	iface_init();
	template_init();

	sigemptyset(&action.sa_mask);
	action.sa_sigaction = sigchld_handler;
	action.sa_flags = SA_SIGINFO;
	sigaction(SIGCHLD, &action, NULL);

	rb_set_end_proc(Final_dumm, 0);
}