aboutsummaryrefslogtreecommitdiffstats
path: root/src/dumm/dumm.c
blob: 6432e74a75157babda95a315fb125e1aaa0f163b (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
/*
 * Copyright (C) 2007 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 <sys/stat.h>

#include <debug.h>

#include "dumm.h"

typedef struct private_dumm_t private_dumm_t;

struct private_dumm_t {
	dumm_t public;
	linked_list_t *guests;
	bool destroying;
};

static guest_t* create_guest(private_dumm_t *this, char *name, char *master, int mem)
{
	guest_t *guest;
	
	guest = guest_create(name, master, mem);
	if (guest)
	{
		this->guests->insert_last(this->guests, guest);
	}
	return guest;
}

static iterator_t* create_guest_iterator(private_dumm_t *this)
{
	return this->guests->create_iterator(this->guests, TRUE);
}	
	
/**
 * Implementation of dumm_t.sigchild_handler.
 */
static void sigchild_handler(private_dumm_t *this, siginfo_t *info)
{
	if (this->destroying)
	{
		return;
	}

	switch (info->si_code)
	{
		case CLD_EXITED:
		case CLD_KILLED:
		case CLD_DUMPED:
		case CLD_STOPPED:
		{
			iterator_t *iterator;
			guest_t *guest;
			
			iterator = this->guests->create_iterator(this->guests, TRUE);
			while (iterator->iterate(iterator, (void**)&guest))
			{
				if (guest->get_pid(guest) == info->si_pid)
				{
					guest->sigchild(guest);
					break;
				}
			}
			iterator->destroy(iterator);
			break;
		}
		default:
			break;
	}
}

static void destroy(private_dumm_t *this)
{
	iterator_t *iterator;
	guest_t *guest;

	iterator = this->guests->create_iterator(this->guests, TRUE);
	while (iterator->iterate(iterator, (void**)&guest))
	{
		guest->stop(guest);
	}
	iterator->destroy(iterator);
	
	this->destroying = TRUE;
	this->guests->destroy_offset(this->guests, offsetof(guest_t, destroy));
	free(this);
}

/**
 * check for a directory, create if it does not exist
 */
static bool makedir(char *dir)
{
	struct stat st;
	
	if (stat(dir, &st) != 0)
	{
		return mkdir(dir, S_IRWXU) == 0;
	}
	return S_ISDIR(st.st_mode);
}

dumm_t *dumm_create()
{
	private_dumm_t *this = malloc_thing(private_dumm_t);
	
	this->public.sigchild_handler = (void(*)(dumm_t*, siginfo_t *info))sigchild_handler;
	this->public.create_guest = (void*)create_guest;
	this->public.create_guest_iterator = (void*)create_guest_iterator;
	this->public.destroy = (void*)destroy;
	
	if (!makedir(HOST_DIR) || !makedir(MOUNT_DIR) || !makedir(RUN_DIR))
	{
		free(this);
		return NULL;
	}
	
	this->destroying = FALSE;
	this->guests = linked_list_create();
	return &this->public;
}