summaryrefslogtreecommitdiff
path: root/lib/rdep.c
blob: 2d328dff73716e074ae2ca8bc5c29bd0aa2b5edb (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
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <limits.h>
#include <stdarg.h>
#include "types.h"
#include "rdep.h"

static bool rdep_verbose = false;
static dep_t *add_all_active(const initd_list_t *pool,
			const dep_t *init, initd_sk_t sk);
static bool _recurse_deps(initd_list_t *pool, initd_sk_t sk,
			const dep_t *needed, initd_list_t *all_deps,
			initd_list_t *chain_deps, bool optional,
			const char *parent);
static void msg_fill(FILE *fd, int num, const char *format, ...);
static bool initd_list_verify_level(const initd_list_t *ord,
				initd_rc_t rc, initd_sk_t sk,
				bool required);

/* Given a list of services to add, recurse through their Required and
 * Should fields to determine an ordered list. NULL is returned on
 * errors. */
initd_list_t *initd_add_recurse_deps(initd_list_t *pool, initd_sk_t sk,
			const dep_t *add)
{
	initd_list_t *all = NULL, *chain = NULL;
	dep_t *all_needed = NULL;

	if (!pool)
		goto out;

	/* initialize the returned initd list */
	all = initd_list_new();

	/* just return an empty list if no deps were passed */
	if (!add || !dep_get_num(add))
		goto out;

	/* initialize the chain list */
	chain = initd_list_new();

	/* Add all the currently active services to the needed list so
	 * they are properly reordered. */
	all_needed = add_all_active(pool, add, sk);

	/* recurse over needed */
	if (!_recurse_deps(pool, sk, all_needed, all, chain, false, NULL))
		goto err;

	if (initd_list_verify_deps(all, sk))
		goto out;
	else
		fprintf(stderr, "Failed to verify %s scripts\n",
			(sk == SK_START) ? "start" : "stop");

err:
	initd_list_free(all);
	all = NULL;
out:
	initd_list_free(chain);
	dep_free(all_needed);
	return all;
}

/* Given a list of services to remove, recurse through the Required and
 * Should fields of the other active services to determine a valid,
 * ordered list. Returns NULL on error. */
initd_list_t *initd_remove_recurse_deps(initd_list_t *pool,
				initd_sk_t sk, const dep_t *remove)
{
	initd_list_t *rmlist = NULL, *all = NULL, *chain = NULL;
	initd_t *ip;
	int n;
	dep_t *all_active = NULL;
	char *rm;

	if (!pool)
		goto out;

	/* initialize the returned initd list */
	all = initd_list_new();

	/* just return an empty list if no deps were passed */
	if (!remove || !dep_get_num(remove))
		goto out;

	/* initialize the initd remove list */
	rmlist = initd_list_new();

	/* Check that the specified services to remove are in the pool.
	 * If they are, clear their changed bits to mark them for
	 * removal. */
	for (n = 0; n < dep_get_num(remove); n++) {
		rm = dep_get_dep(remove, n);
		ip = initd_list_find_provides(pool, rm);
		if (!ip) {
			fprintf(stderr, "No init script provides %s\n",
				rm);
			goto err;
		}

		/* continue if already in the rmlist */
		if (initd_list_exists_name(rmlist, ip->name))
			continue;

		/* Set the remove field for all levels */
		if (sk == SK_START)
			initd_set_rc(ip, KEY_RMSTART, RC_ALL);
		else
			initd_set_rc(ip, KEY_RMSTOP, RC_ALL);

		/* Add the initd_t to the rmlist */
		initd_list_add(rmlist, initd_copy(ip));
	}

	/* If none of the specified services are currently active, the
	 * rm list will be empty and we can just return. */
	if (!rmlist->first)
		goto out;

	/* Find all the currently active services */
	all_active = add_all_active(pool, NULL, sk);

	/* initialize the chain recursion lists */
	chain = initd_list_new();

	/* recurse over needed */
	if (!_recurse_deps(pool, sk, all_active, all, chain, false, NULL))
		goto err;

	if (!initd_list_verify_deps(all, sk)) {
		fprintf(stderr, "Failed to verify %s scripts\n",
			(sk == SK_START) ? "start" : "stop");
		goto err;
	}

	/* If we got here, we're successful */
	goto out;
err:
	initd_list_free(all);
	all = NULL;
out:
	initd_list_free(chain);
	dep_free(all_active);
	initd_list_free(rmlist);
	return all;
}

void initd_recurse_set_verbose(bool verbose)
{
	rdep_verbose = verbose;
}

/* Given an ordered initd list (such as determined in initd_*_recurse_deps)
 * determine if the start and stop order is valid in each run level. */
bool initd_list_verify_deps(const initd_list_t *ord, initd_sk_t sk)
{
	initd_rc_t rc;
	bool ret = true;

	if (!ord)
		return false;

	for (rc = RC_S; rc <= RC_6; rc = rc << 1) {
		if (!initd_list_verify_level(ord, rc, sk, true)) {
			fprintf(stderr, "Failed to verify the required "
				"%s scripts for level %c\n",
				(sk == SK_START) ? "start" : "stop",
				initd_rc_level_char(rc));
			ret = false;
			break;
		}
		if (!initd_list_verify_level(ord, rc, sk, false)) {
			fprintf(stderr, "Failed to verify the optional "
				"%s scripts for level %c\n",
				(sk == SK_START) ? "start" : "stop",
				initd_rc_level_char(rc));
			ret = false;
			break;
		}
	}

	return ret;
}

static dep_t *add_all_active(const initd_list_t *pool,
			const dep_t *init, initd_sk_t sk)
{
	dep_t *active;
	initd_t *ip;
	initd_key_t key, rmkey;

	if (!pool)
		return NULL;

	active = dep_copy(init);
	if (sk == SK_START) {
		key = KEY_ASTART;
		rmkey = KEY_RMSTART;
	} else {
		key = KEY_ASTOP;
		rmkey = KEY_RMSTOP;
	}

	for (ip = pool->first; ip; ip = ip->next) {
		/* Add services if they are active on any level and
		 * they are not marked for removal on any levels */
		if (initd_is_active(ip, RC_ALL, key) &&
			!initd_is_active(ip, RC_ALL, rmkey))
			dep_add(active, ip->name);
	}

	return active;
}

static bool _recurse_deps(initd_list_t *pool, initd_sk_t sk,
			const dep_t *needed, initd_list_t *all_deps,
			initd_list_t *chain_deps, bool optional,
			const char *parent)
{
	int n;
	char *cstr;
	initd_t *cip;
	bool success = true;
	static int level = 0;

	if (!(pool || needed || all_deps || chain_deps)) {
		success = false;
		goto out;
	}

	for (n = 0; n < dep_get_num(needed); n++) {
		/* find the initd in pool matching this name */
		cstr = dep_get_dep(needed, n);
		cip = initd_list_find_provides(pool, cstr);
		if (!cip) {
			/* Don't error if the caller said this was an
			 * optional dependency. */
			if (!optional) {
				fprintf(stderr,
					"No init script provides %s\n",
					cstr);
				success = false;
			}
			goto out;
		}

		if (rdep_verbose) {
			if (strcmp(cstr, cip->name) == 0) {
				msg_fill(stderr, level, "Checking %s\n",
					cstr);
			} else {
				msg_fill(stderr, level,
					"Checking %s (%s)\n",
					cstr, cip->name);
			}
		}

		/* if this initd is already in all_deps, continue */
		if (initd_list_exists_name(all_deps, cip->name)) {
			if (rdep_verbose) {
				if (strcmp(cstr, cip->name) == 0) {
					msg_fill(stderr, level,
						"Pruning %s\n",
						cstr);
				} else {
					msg_fill(stderr, level,
						"Pruning %s (%s)\n",
						cstr, cip->name);
				}
			}
			continue;
		}

		/* if this dep is in chain_deps, we have a circular
		 * dependency */
		if (initd_list_exists_name(chain_deps, cip->name)) {
			if (strcmp(cstr, cip->name) == 0) {
				fprintf(stderr,
					"Error: circular dependency"
					" %s -> %s\n",
					parent ? parent : "", cstr);
			} else {
				fprintf(stderr,
					"Error: circular dependency"
					" %s -> %s (%s)\n",
					parent ? parent : "", cstr,
					cip->name);
			}
			success = false;
			goto out;
		}

		/* add it to the chain */
		initd_list_add(chain_deps, initd_copy(cip));

		/* process its required dependencies */
		level++;
		switch (sk) {
		case SK_START:
			success = _recurse_deps(pool, sk, cip->rstart,
						all_deps, chain_deps,
						false, cstr);
			break;
		case SK_STOP:
			success = _recurse_deps(pool, sk, cip->rstop,
						all_deps, chain_deps,
						false, cstr);
			break;
		default:
			fprintf(stderr, "Invalid RC start/stop %d\n", sk);
		}
		level--;

		/* Break from the loop if the recursion was a failure */
		if (!success)
			goto out;

		/* process its optional dependencies */
		level++;
		switch (sk) {
		case SK_START:
			success = _recurse_deps(pool, sk, cip->sstart,
						all_deps, chain_deps,
						true, cstr);
			break;
		case SK_STOP:
			success = _recurse_deps(pool, sk, cip->sstop,
						all_deps, chain_deps,
						true, cstr);
			break;
		default:
			fprintf(stderr, "Invalid RC start/stop %d\n", sk);
		}
		level--;

		/* Break from the loop if the recursion was a failure */
		if (!success)
			goto out;

		/* If we got here, all the subdeps have been processed.
		 * Add this dep to all_deps and remove it from
		 * chain_deps. */
		if (rdep_verbose) {
			if (strcmp(cstr, cip->name) == 0) {
				msg_fill(stderr, level, "Adding %s\n",
					cstr);
			} else {
				msg_fill(stderr, level,
					"Adding %s (%s)\n",
					cstr, cip->name);
			}
		}
		initd_list_add(all_deps, initd_copy(cip));
		initd_list_pop(chain_deps);
	}

out:
	return success;
}

static void msg_fill(FILE *fd, int num, const char *format, ...)
{
	char fill[LINE_MAX];
	va_list args;

	if (num > 0) {
		if (num >= LINE_MAX)
			num = LINE_MAX - 1;

		memset(fill, '.', num);
		fill[num] = '\0';
		fprintf(fd, "%s", fill);
	}

	va_start(args, format);
	vfprintf(fd, format, args);
	va_end(args);
}

static bool initd_list_verify_level(const initd_list_t *ord,
				initd_rc_t rc, initd_sk_t sk,
				bool required)
{
	initd_t *ip, *dep;
	dep_t *iprcdep;
	char *dstr;
	int n;
	bool match;
	initd_key_t dkey, inkey, rmkey;

	if (!ord)
		return false;

	if (sk == SK_START) {
		dkey = KEY_DSTART;
		inkey = KEY_INSTART;
		rmkey = KEY_RMSTART;
	} else {
		dkey = KEY_DSTOP;
		inkey = KEY_INSTOP;
		rmkey = KEY_RMSTOP;
	}

	/* Check the deps are valid in this level */
	for (ip = ord->first; ip; ip = ip->next) {
		if (sk == SK_START) {
			if (required)
				iprcdep = ip->rstart;
			else
				iprcdep = ip->sstart;
		} else {
			if (required)
				iprcdep = ip->rstop;
			else
				iprcdep = ip->sstop;
		}

		/* Skip if the script shoudn't run at this level */
		if (!initd_is_active(ip, rc, dkey))
			continue;

		/* Check the deps */
		for (n = 0; n < dep_get_num(iprcdep); n++) {
			dstr = dep_get_dep(iprcdep, n);
			dep = initd_list_find_provides(ord, dstr);
			if (!dep) {
				if (required) {
					fprintf(stderr,
						"Error: No init script "
						"provides %s\n",
						dstr);
					return false;
				} else {
					continue;
				}
			}

			/* Check if the dep is started in this level
			 * or if it's in a special level */
			if (sk == SK_START) {
				match = dep->dstart & rc;
				if (!match)
					match = dep->dstart & RC_S;
			} else {
				match = dep->dstop & rc;
				if (!match) {
					match = (dep->dstop & RC_0) &&
						(dep->dstop & RC_6);
				}
			}

			if (!match && sk == SK_START) {
				fprintf(stderr,
					"Error: %s dependency %s does "
					"not start in level %c or "
					"sysinit\n",
					ip->name, dstr,
					initd_rc_level_char(rc));
				return false;
			} else if (!match) {
				fprintf(stderr,
					"Error: %s dependency %s does "
					"not stop in level %c or halt "
					"and reboot\n",
					ip->name, dstr,
					initd_rc_level_char(rc));
				return false;
			}

			/* Check if the required dep is marked for
			 * removal. */
			match = initd_is_active(dep, rc, rmkey);
			if (match && required) {
				fprintf(stderr,
					"Error: %s required dependency %s"
					" is marked for removal\n",
					ip->name, dstr);
				return false;
			}
		}

		/* Now that we're successful, mark this script for
		 * installation if it's not marked for removal */
		if (!initd_is_active(ip, rc, rmkey))
			initd_set_rc(ip, inkey, rc);
	}

	/* If we get here, then we were successful */
	return true;
}