summaryrefslogtreecommitdiff
path: root/accel-pppd/backup/backup.c
blob: 16349fc7147cf93fd33328ac7655dbd9641d842e (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
#include <stdlib.h>
#include <string.h>

#include "triton.h"
#include "log.h"
#include "events.h"
#include "ap_session.h"
#include "backup.h"

#ifdef USE_BACKUP

static LIST_HEAD(storage_list);
static LIST_HEAD(module_list);

struct backup_tag __export *backup_add_tag(struct backup_mod *m, uint8_t id, int internal, const void *data, size_t size)
{
	struct backup_tag *t;
	
	t = m->data->storage->alloc_tag(m->data, size);
	if (!t)
		return NULL;

	t->id = id;
	t->internal = internal;
	t->size = size;
	memcpy(t->data, data, size);

	list_add_tail(&t->entry, &m->tag_list);

	return t;
}

void backup_add_fd(struct backup_mod *m, int fd)
{
	if (m->data->storage->add_fd)
		m->data->storage->add_fd(m->data, fd);
}

struct backup_mod __export *backup_find_mod(struct backup_data *d, uint8_t mod_id)
{
	struct backup_mod *m;
	
	list_for_each_entry(m, &d->mod_list, entry) {
		if (m->id == mod_id)
			return m;
	}

	return NULL;
}

struct backup_tag __export *backup_find_tag(struct backup_data *d, uint8_t mod_id, uint8_t tag_id, int internal)
{
	struct backup_mod *m = backup_find_mod(d, mod_id);
	struct backup_tag *t;

	if (!m)
		return NULL;

	list_for_each_entry(t, &m->tag_list, entry) {
		if (t->id == tag_id && t->internal == internal)
			return t;
	}

	return NULL;
}

void __export backup_free(struct backup_data *data)
{
	struct backup_mod *m;
	struct backup_tag *t;

	while (!list_empty(&data->mod_list)) {
		m = list_entry(data->mod_list.next, typeof(*m), entry);
		while (!list_empty(&m->tag_list)) {
			t = list_entry(m->tag_list.next, typeof(*t), entry);
			list_del(&t->entry);
			data->storage->free_tag(data, t);
		}
		list_del(&m->entry);
		data->storage->free_mod(m);
	}
	data->storage->free(data);
}

int __export backup_save_session(struct ap_session *ses)
{
	struct backup_storage *storage;
	struct backup_module *module;
	struct backup_data *d;
	struct backup_mod *m;
	int r, f1 = 0, f2;

	list_for_each_entry(storage, &storage_list, entry) {
		d = storage->create(ses);
		if (!d)
			continue;

		//d->ses = ses;

		f2 = 0;

		list_for_each_entry(module, &module_list, entry) {
			if (!module->save)
				continue;

			m = storage->alloc_mod(d);
			if (!m) {
				f2 = 1;
				break;
			}

			m->data = d;
			m->id = module->id;
			r = module->save(ses, m);
			if (r == -2) {
				storage->free_mod(m);
				continue;
			}

			list_add_tail(&m->entry, &d->mod_list);

			if (r == -1) {
				f2 = 1;
				break;
			}
		}

		if (f2)
			backup_free(d);
		else {
			f1 = 1;
			if (storage->commit)
				storage->commit(d);
			ses->backup = d;
		}
	}

	return !f1;
}

/*int backup_restore_internal(void)
{
	struct backup_storage *storage;

	list_for_each_entry(storage, &storage_list, entry) {
		if (storage->restore_internal) {
			if (storage->check_integrity())
				continue;
			storage->restore_internal();
			return 0;
		}
	}

	return -1;
}

void backup_restore_external(void)
{
	struct backup_storage *storage;

	list_for_each_entry(storage, &storage_list, entry) {
		if (storage->restore_external) {
			if (storage->check_integrity())
				continue;
			storage->restore_external();
			return;
		}
	}
}*/

static void __restore_session(struct ap_session *ses)
{
	struct backup_module *module;
	struct backup_mod *m;
	struct backup_module *ctrl = NULL;

	list_for_each_entry(module, &module_list, entry) {
		if (module->ctrl_start)
			ctrl = module;
		if (module->restore) {
			m = backup_find_mod(ses->backup, module->id);
			if (!m)
				continue;
			module->restore(ses, m);
		}
	}

	log_ppp_info1("session restored\n");

	if (ctrl)
		ctrl->ctrl_start(ses);
	else {
		triton_event_fire(EV_CTRL_STARTING, ses);
		triton_event_fire(EV_CTRL_STARTED, ses);

		ap_session_starting(ses);
		ap_session_activate(ses);
	}
}

void __export backup_restore_session(struct backup_data *d)
{
	struct backup_module *module;
	struct backup_mod *m;
	struct ap_session *ses;

	list_for_each_entry(module, &module_list, entry) {
		if (module->ctrl_restore) {
			m = backup_find_mod(d, module->id);
			if (!m)
				continue;
			ses = module->ctrl_restore(m);
			ses->backup = d;
			d->ses = ses;
			ses->state = AP_STATE_RESTORE;
			triton_context_call(ses->ctrl->ctx, (triton_event_func)__restore_session, ses);
			break;
		}
	}
}


void __export backup_register_module(struct backup_module *m)
{
	list_add_tail(&m->entry, &module_list);
}

void __export backup_register_storage(struct backup_storage *s)
{
	list_add_tail(&s->entry, &storage_list);
}

void backup_restore_fd()
{
	
}

void backup_restore(int internal)
{
	struct backup_storage *storage;
	struct backup_module *module;
	
	list_for_each_entry(storage, &storage_list, entry) {
		if (storage->restore)
			storage->restore(internal);
	}
		
	list_for_each_entry(module, &module_list, entry) {
		if (module->restore_complete)
			module->restore_complete();
	}
}

#endif