File: lifecycle.c

package info (click to toggle)
ifupdown-ng 0.12.1-8
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 964 kB
  • sloc: ansic: 3,572; sh: 980; makefile: 233
file content (604 lines) | stat: -rw-r--r-- 15,903 bytes parent folder | download | duplicates (3)
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
/*
 * libifupdown/lifecycle.c
 * Purpose: management of interface lifecycle (bring up, takedown, reload)
 *
 * Copyright (c) 2020 Ariadne Conill <ariadne@dereferenced.org>
 * Copyright (c) 2020 Maximilian Wilhelm <max@sdn.clinic>
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * This software is provided 'as is' and without any warranty, express or
 * implied.  In no event shall the authors be liable for any damages arising
 * from the use of this software.
 */

#include <ctype.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

#include "libifupdown/environment.h"
#include "libifupdown/execute.h"
#include "libifupdown/interface.h"
#include "libifupdown/lifecycle.h"
#include "libifupdown/state.h"
#include "libifupdown/tokenize.h"
#include "libifupdown/config-file.h"

#define BUFFER_LEN 4096

static bool
handle_commands_for_phase(const struct lif_execute_opts *opts, char *const envp[], const struct lif_interface *iface, const char *phase)
{
	const struct lif_node *iter;

	LIF_DICT_FOREACH(iter, &iface->vars)
	{
		const struct lif_dict_entry *entry = iter->data;

		if (strcmp(entry->key, phase))
			continue;

		const char *cmd = entry->data;
		if (!lif_execute_fmt(opts, envp, "%s", cmd))
			return false;
	}

	return true;
}

static inline bool
handle_single_executor_for_phase(const struct lif_dict_entry *entry, const struct lif_execute_opts *opts, char *const envp[], const char *phase, const char *lifname)
{
	if (strcmp(entry->key, "use"))
		return true;

	const char *cmd = entry->data;
	if (!lif_maybe_run_executor(opts, envp, cmd, phase, lifname))
		return false;

	return true;
}

static bool
handle_executors_for_phase(const struct lif_execute_opts *opts, char *const envp[], const struct lif_interface *iface, bool up, const char *phase)
{
	bool ret = true;
	const struct lif_node *iter;

	if (up)
	{
		LIF_DICT_FOREACH(iter, &iface->vars) {
			if (!handle_single_executor_for_phase(iter->data, opts, envp, phase, iface->ifname))
				ret = false;
		}
	}
	else
	{
		LIF_DICT_FOREACH_REVERSE(iter, &iface->vars) {
			if (!handle_single_executor_for_phase(iter->data, opts, envp, phase, iface->ifname))
				ret = false;
		}
	}

	return ret;
}

static bool
query_dependents_from_executors(const struct lif_execute_opts *opts, char *const envp[], const struct lif_interface *iface, char *buf, size_t bufsize, const char *phase)
{
	const struct lif_node *iter;

	LIF_DICT_FOREACH(iter, &iface->vars)
	{
		char resbuf[1024] = {};
		const struct lif_dict_entry *entry = iter->data;
		struct lif_execute_opts exec_opts = {
			.verbose = opts->verbose,
			.executor_path = opts->executor_path,
			.interfaces_file = opts->interfaces_file,
			.timeout = opts->timeout,
		};

		if (strcmp(entry->key, "use"))
			continue;

		const char *cmd = entry->data;
		if (!lif_maybe_run_executor_with_result(&exec_opts, envp, cmd, resbuf, sizeof resbuf, phase, iface->ifname))
			return false;

		if (!*resbuf)
			continue;

		strlcat(buf, " ", bufsize);
		strlcat(buf, resbuf, bufsize);
	}

	return true;
}

static bool
append_to_buffer(char **buffer, size_t *buffer_len, char **end, const char *value)
{
	size_t value_len = strlen (value);

	/* Make sure there is enough room to add the value to the buffer */
	if (*buffer_len < strlen (*buffer) + value_len + 2)
	{
		size_t end_offset = *end - *buffer;
		char *tmp = realloc (*buffer, *buffer_len * 2);

		if (tmp != NULL)
		{
			*buffer = tmp;
			*end = tmp + end_offset;
			*buffer_len = *buffer_len * 2;
		}
		else
			return false;
	}

	/* Append value to buffer */
	size_t printed = snprintf (*end, value_len + 2, "%s ", value);
	if (printed < value_len + 1)
		/* Here be dragons */
		return false;

	/* Move end pointer to last printed byte */
	*end += printed;

	return true;
}

static void
build_environment(char **envp[], const struct lif_execute_opts *opts, const struct lif_interface *iface, const char *lifname, const char *phase, const char *mode)
{
	if (lifname == NULL)
		lifname = iface->ifname;

	lif_environment_push(envp, "IFACE", lifname);
	lif_environment_push(envp, "PHASE", phase);
	lif_environment_push(envp, "MODE", mode);
	lif_environment_push(envp, "METHOD", "none");

	if (opts->verbose)
		lif_environment_push(envp, "VERBOSE", "1");

	if (opts->interfaces_file)
		lif_environment_push(envp, "INTERFACES_FILE", opts->interfaces_file);

	const struct lif_node *iter;
	bool did_address = false, did_gateway = false;

	/* Allocate a buffer for all possible addresses, if any */
	char *addresses = calloc (BUFFER_LEN, 1);
	size_t addresses_size = BUFFER_LEN;
	char *addresses_end = addresses;

	/* Allocate a buffer for all possible gateways, if any */
	char *gateways = calloc (BUFFER_LEN, 1);
	size_t gateways_size = BUFFER_LEN;
	char *gateways_end = gateways;

	LIF_DICT_FOREACH(iter, &iface->vars)
	{
		struct lif_dict_entry *entry = iter->data;

		if (!strcmp(entry->key, "address"))
		{
			char addrbuf[4096];

			if (!lif_address_format_cidr(iface, entry, addrbuf, sizeof(addrbuf)))
				continue;

			/* Append address to buffer */
			append_to_buffer(&addresses, &addresses_size, &addresses_end, addrbuf);

			/* Only print IF_ADDRESS once */
			if (did_address)
				continue;

			lif_environment_push(envp, "IF_ADDRESS", addrbuf);
			did_address = true;

			continue;
		}
		else if (!strcmp(entry->key, "gateway"))
		{
			/* Append address to buffer */
			append_to_buffer(&gateways, &gateways_size, &gateways_end, entry->data);

			if (did_gateway)
				continue;

			did_gateway = true;
		}
		else if (!strcmp(entry->key, "requires"))
		{
			if (iface->is_bridge)
				lif_environment_push(envp, "IF_BRIDGE_PORTS", (const char *) entry->data);
		}

		char envkey[4096] = "IF_";
		strlcat(envkey, entry->key, sizeof envkey);
		char *ep = envkey + 2;

		while (*ep++)
		{
			*ep = toupper(*ep);

			if (*ep == '-')
				*ep = '_';
		}

		lif_environment_push(envp, envkey, (const char *) entry->data);
	}

	if (addresses != NULL)
		lif_environment_push(envp, "IF_ADDRESSES", addresses);
	if (gateways != NULL)
		lif_environment_push(envp, "IF_GATEWAYS", gateways);

	/* Clean up */
	free (addresses);
	free (gateways);
}

bool
lif_lifecycle_query_dependents(const struct lif_execute_opts *opts, struct lif_interface *iface, const char *lifname)
{
	char deps[4096] = {};
	char final_deps[4096] = {};

	if (lifname == NULL)
		lifname = iface->ifname;

	char **envp = NULL;

	build_environment(&envp, opts, iface, lifname, "depend", "depend");

	struct lif_dict_entry *entry = lif_dict_find(&iface->vars, "requires");
	if (entry != NULL)
		strlcpy(deps, entry->data, sizeof deps);

	if (!query_dependents_from_executors(opts, envp, iface, deps, sizeof deps, "depend"))
		return false;

	char *p = deps;
	while (*p)
	{
		char *token = lif_next_token(&p);

		if (strstr(final_deps, token) != NULL)
			continue;

		strlcat(final_deps, token, sizeof final_deps);
		strlcat(final_deps, " ", sizeof final_deps);
	}

	if (entry != NULL)
	{
		free(entry->data);
		entry->data = strdup(final_deps);
	}
	else if (*final_deps)
		lif_dict_add(&iface->vars, "requires", strdup(final_deps));

	lif_environment_free(&envp);

	return true;
}

bool
lif_lifecycle_run_phase(const struct lif_execute_opts *opts, struct lif_interface *iface, const char *phase, const char *lifname, bool up)
{
	char **envp = NULL;

	build_environment(&envp, opts, iface, lifname, phase, up ? "start" : "stop");

	if (!handle_executors_for_phase(opts, envp, iface, up, phase))
		goto handle_error;

	if (!handle_commands_for_phase(opts, envp, iface, phase))
		goto handle_error;

	/* if we don't need to support /etc/if-X.d we're done here */
	if (!lif_config.allow_addon_scripts)
		goto out_free;

	/* Check if scripts dir for this phase is present and bail out if it isn't */
	struct stat dir_stat;
	char dir_path[4096];
	snprintf (dir_path, 4096, "/etc/network/if-%s.d", phase);

	if (stat (dir_path, &dir_stat) != 0 || S_ISDIR (dir_stat.st_mode) == 0) {
		goto out_free;
	}

	/* we should do error handling here, but ifupdown1 doesn't */
	lif_execute_fmt(opts, envp, "/bin/run-parts %s", dir_path);

out_free:
	lif_environment_free(&envp);
	return true;

handle_error:
	lif_environment_free(&envp);
	return false;
}

/* this function returns true if we can skip processing the interface for now,
 * otherwise false.
 */
static bool
handle_refcounting(struct lif_dict *state, struct lif_interface *iface, bool up)
{
	size_t orig_refcount = iface->refcount;

	if (up)
		lif_state_ref_if(state, iface->ifname, iface);
	else
		lif_state_unref_if(state, iface->ifname, iface);

#ifdef DEBUG_REFCOUNTING
	fprintf(stderr, "handle_refcounting(): orig_refcount=%zu, refcount=%zu, direction=%s\n",
		orig_refcount, iface->refcount, up ? "UP" : "DOWN");
#endif

	/* if going up and orig_refcount > 0 -- we're already configured. */
	if (up && orig_refcount > 0)
		return true;

	/* if going down and iface->refcount > 1 -- we still have other dependents. */
	if (!up && iface->refcount > 1)
		return true;

	/* we can change this interface -- no blocking dependents. */
	return false;
}

static bool
handle_dependents(const struct lif_execute_opts *opts, struct lif_interface *parent, struct lif_dict *collection, struct lif_dict *state, bool up)
{
	struct lif_dict_entry *requires = lif_dict_find(&parent->vars, "requires");

	/* no dependents, nothing to worry about */
	if (requires == NULL)
		return true;

	/* set the parent's pending flag to break dependency cycles */
	parent->is_pending = true;

	char require_ifs[4096] = {};
	strlcpy(require_ifs, requires->data, sizeof require_ifs);
	char *bufp = require_ifs;

	for (char *tokenp = lif_next_token(&bufp); *tokenp; tokenp = lif_next_token(&bufp))
	{
		struct lif_interface *iface = lif_interface_collection_find(collection, tokenp);

		if (iface->has_config_error)
		{
			if (opts->force)
				fprintf (stderr, "ifupdown: (de)configuring dependent interface %s (of %s) despite config errors\n",
				         iface->ifname, parent->ifname);
			else
			{
				fprintf (stderr, "ifupdown: skipping dependent interface %s (of %s) as it has config errors\n",
			        iface->ifname, parent->ifname);
				continue;
			}
		}

		/* if handle_refcounting returns true, it means we've already
		 * configured the interface, or it is too soon to deconfigure
		 * the interface.
		 */
		if (handle_refcounting(state, iface, up))
		{
			if (opts->verbose)
				fprintf(stderr, "ifupdown: skipping dependent interface %s (of %s) -- %s\n",
					iface->ifname, parent->ifname,
					up ? "already configured" : "transient dependencies still exist");

			continue;
		}

		if (!up && iface->is_explicit)
		{
			if (opts->verbose)
				fprintf(stderr, "ifupdown: skipping dependent interface %s (of %s) -- interface is marked as explicitly configured\n",
					iface->ifname, parent->ifname);

			continue;
		}

		if (opts->verbose)
			fprintf(stderr, "ifupdown: changing state of dependent interface %s (of %s) to %s\n",
				iface->ifname, parent->ifname, up ? "up" : "down");

		if (!lif_lifecycle_run(opts, iface, collection, state, iface->ifname, up))
		{
			parent->is_pending = false;
			return false;
		}
	}

	parent->is_pending = false;
	return true;
}

bool
lif_lifecycle_run(const struct lif_execute_opts *opts, struct lif_interface *iface, struct lif_dict *collection, struct lif_dict *state, const char *lifname, bool up)
{
	/* if we're already pending, exit */
	if (iface->is_pending)
		return true;

	if (iface->is_template)
		return false;

	if (lifname == NULL)
		lifname = iface->ifname;

	if (up)
	{
		/* when going up, dependents go up first. */
		if (!handle_dependents(opts, iface, collection, state, up))
			return false;

		/* XXX: we should try to recover (take the iface down) if bringing it up fails.
		 * but, right now neither debian ifupdown or busybox ifupdown do any recovery,
		 * so we wont right now.
		 */
		if (!lif_lifecycle_run_phase(opts, iface, "create", lifname, up))
			return false;

		if (!lif_lifecycle_run_phase(opts, iface, "pre-up", lifname, up))
			return false;

		if (!lif_lifecycle_run_phase(opts, iface, "up", lifname, up))
			return false;

		if (!lif_lifecycle_run_phase(opts, iface, "post-up", lifname, up))
			return false;

		lif_state_ref_if(state, lifname, iface);
	}
	else
	{
		if (!lif_lifecycle_run_phase(opts, iface, "pre-down", lifname, up))
			return false;

		if (!lif_lifecycle_run_phase(opts, iface, "down", lifname, up))
			return false;

		if (!lif_lifecycle_run_phase(opts, iface, "post-down", lifname, up))
			return false;

		if (!lif_lifecycle_run_phase(opts, iface, "destroy", lifname, up))
			return false;

		/* when going up, dependents go down last. */
		if (!handle_dependents(opts, iface, collection, state, up))
			return false;

		lif_state_unref_if(state, lifname, iface);
	}

	return true;
}

static bool
count_interface_rdepends(const struct lif_execute_opts *opts, struct lif_dict *collection, struct lif_interface *parent, size_t depth)
{
	/* if we have looped, return true immediately to break the loop. */
	if (parent->is_pending)
		return true;

	/* query our dependents if we don't have them already */
	if (!lif_lifecycle_query_dependents(opts, parent, parent->ifname))
		return false;

	/* set rdepends_count to depth, dependents will be depth + 1 */
	parent->is_pending = true;
	parent->rdepends_count = depth;

	struct lif_dict_entry *requires = lif_dict_find(&parent->vars, "requires");

	/* no dependents, nothing to worry about */
	if (requires == NULL)
	{
		parent->is_pending = false;
		return true;
	}

	/* walk any dependents */
	char require_ifs[4096] = {};
	strlcpy(require_ifs, requires->data, sizeof require_ifs);
	char *bufp = require_ifs;

	for (char *tokenp = lif_next_token(&bufp); *tokenp; tokenp = lif_next_token(&bufp))
	{
		struct lif_interface *iface = lif_interface_collection_find(collection, tokenp);

		if (!count_interface_rdepends(opts, collection, iface, depth + 1))
		{
			parent->is_pending = false;
			return false;
		}
	}

	parent->is_pending = false;
	return true;
}

ssize_t
lif_lifecycle_count_rdepends(const struct lif_execute_opts *opts, struct lif_dict *collection)
{
	struct lif_node *iter;

	LIF_DICT_FOREACH(iter, collection)
	{
		struct lif_dict_entry *entry = iter->data;
		struct lif_interface *iface = entry->data;

		/* start depth at interface's rdepends_count, which will be 0 for the root,
		 * but will be more if additional rdepends are found...
		 */
		if (!count_interface_rdepends(opts, collection, iface, iface->rdepends_count))
		{
			fprintf(stderr, "ifupdown: dependency graph is broken for interface %s\n", iface->ifname);
			return -1;
		}
	}

	/* figure out the max depth */
	size_t maxdepth = 0;

	LIF_DICT_FOREACH(iter, collection)
	{
		struct lif_dict_entry *entry = iter->data;
		struct lif_interface *iface = entry->data;

		if (iface->rdepends_count > maxdepth)
			maxdepth = iface->rdepends_count;
	}

	/* move the collection to a temporary list so we can reorder it */
	struct lif_list temp_list = {};
	struct lif_node *iter_next;

	LIF_LIST_FOREACH_SAFE(iter, iter_next, collection->list.head)
	{
		void *data = iter->data;

		lif_node_delete(iter, &collection->list);
		memset(iter, 0, sizeof *iter);

		lif_node_insert(iter, data, &temp_list);
	}

	/* walk backwards from maxdepth to 0, readding nodes */
	for (ssize_t curdepth = maxdepth; curdepth > -1; curdepth--)
	{
		LIF_LIST_FOREACH_SAFE(iter, iter_next, temp_list.head)
		{
			struct lif_dict_entry *entry = iter->data;
			struct lif_interface *iface = entry->data;

			if ((ssize_t) iface->rdepends_count != curdepth)
				continue;

			lif_node_delete(iter, &temp_list);
			memset(iter, 0, sizeof *iter);

			lif_node_insert(iter, entry, &collection->list);
		}
	}

	return maxdepth;
}