2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2011 James Gritton
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
40 struct cfjails ready = TAILQ_HEAD_INITIALIZER(ready);
41 struct cfjails depend = TAILQ_HEAD_INITIALIZER(depend);
43 static void dep_add(struct cfjail *from, struct cfjail *to, unsigned flags);
44 static int cmp_jailptr(const void *a, const void *b);
45 static int cmp_jailptr_name(const void *a, const void *b);
46 static struct cfjail *find_jail(const char *name);
47 static int running_jid(const char *name, int flags);
49 static struct cfjail **jails_byname;
53 * Set up jail dependency lists.
58 struct cfjail *j, *dj;
69 * With no config file, let "depend" for a single jail
70 * look at currently running jails.
72 if ((j = TAILQ_FIRST(&cfjails)) &&
73 (p = j->intparams[IP_DEPEND])) {
74 TAILQ_FOREACH(s, &p->val, tq) {
75 if (running_jid(s->s, 0) < 0) {
76 warnx("depends on nonexistent jail "
78 j->flags |= JF_FAILED;
86 TAILQ_FOREACH(j, &cfjails, tq)
88 jails_byname = emalloc(njails * sizeof(struct cfjail *));
90 TAILQ_FOREACH(j, &cfjails, tq)
91 jails_byname[njails++] = j;
92 qsort(jails_byname, njails, sizeof(struct cfjail *), cmp_jailptr);
97 TAILQ_FOREACH(j, &cfjails, tq) {
98 if (j->flags & JF_FAILED)
100 if ((p = j->intparams[IP_DEPEND])) {
101 TAILQ_FOREACH(s, &p->val, tq) {
102 dj = find_jail(s->s);
108 "depends on undefined jail \"%s\"",
110 j->flags |= JF_FAILED;
114 /* A jail has an implied dependency on its parent. */
115 if ((cs = strrchr(j->name, '.')))
117 if (plen < (size_t)(cs - j->name + 1)) {
118 plen = (cs - j->name) + 1;
119 pname = erealloc(pname, plen);
121 strlcpy(pname, j->name, plen);
122 dj = find_jail(pname);
125 dep_add(j, dj, DF_LIGHT);
130 /* Look for dependency loops. */
131 if (deps && (deps > 1 || ldeps)) {
132 (void)start_state(NULL, 0, 0, 0);
133 while ((j = TAILQ_FIRST(&ready))) {
134 requeue(j, &cfjails);
135 dep_done(j, DF_NOFAIL);
137 while ((j = TAILQ_FIRST(&depend)) != NULL) {
138 jail_warnx(j, "dependency loop");
139 j->flags |= JF_FAILED;
141 requeue(j, &cfjails);
142 dep_done(j, DF_NOFAIL);
143 } while ((j = TAILQ_FIRST(&ready)));
145 TAILQ_FOREACH(j, &cfjails, tq)
146 STAILQ_FOREACH(d, &j->dep[DEP_FROM], tq[DEP_FROM])
147 d->flags &= ~DF_SEEN;
154 * Return if a jail has dependencies.
157 dep_check(struct cfjail *j)
159 int reset, depfrom, depto, ndeps, rev;
163 static int bits[] = { 0, 1, 1, 2, 1, 2, 2, 3 };
168 if ((rev = JF_DO_STOP(j->flags))) {
175 STAILQ_FOREACH(d, &j->dep[depfrom], tq[depfrom]) {
176 if (d->flags & DF_SEEN)
179 if (dj->flags & JF_FAILED) {
180 if (!(j->flags & (JF_DEPEND | JF_FAILED)) &&
182 jail_warnx(j, "skipped");
183 j->flags |= JF_FAILED;
187 * The dependee's state may be set (or changed) as a result of
188 * being in a dependency it wasn't in earlier.
191 if (bits[dj->flags & JF_OP_MASK] <= 1) {
192 if (!(dj->flags & JF_OP_MASK)) {
194 dj->flags |= JF_DEPEND;
197 /* Set or change the dependee's state. */
198 switch (j->flags & JF_OP_MASK) {
200 dj->flags |= JF_START;
203 if (!(dj->flags & JF_OP_MASK))
205 else if (dj->flags & JF_STOP)
206 dj->flags |= JF_START;
210 if (!(dj->flags & JF_STOP))
212 dj->flags |= JF_STOP;
213 if (dj->flags & JF_SET)
214 dj->flags ^= (JF_START | JF_SET);
220 if (!((d->flags & DF_LIGHT) &&
221 (rev ? dj->jid < 0 : dj->jid > 0)))
231 * Resolve any dependencies from a finished jail.
234 dep_done(struct cfjail *j, unsigned flags)
240 if (JF_DO_STOP(j->flags)) {
247 STAILQ_FOREACH(d, &j->dep[depto], tq[depto]) {
248 if ((d->flags & DF_SEEN) | (flags & ~d->flags & DF_LIGHT))
252 if (!(flags & DF_NOFAIL) && (j->flags & JF_FAILED) &&
253 (j->flags & (JF_OP_MASK | JF_DEPEND)) !=
254 (JF_SET | JF_DEPEND)) {
255 if (!(dj->flags & (JF_DEPEND | JF_FAILED)) &&
257 jail_warnx(dj, "skipped");
258 dj->flags |= JF_FAILED;
260 if (!--dj->ndeps && dj->queue == &depend)
266 * Count a jail's dependencies and mark them as unseen.
269 dep_reset(struct cfjail *j)
274 depfrom = JF_DO_STOP(j->flags) ? DEP_TO : DEP_FROM;
276 STAILQ_FOREACH(d, &j->dep[depfrom], tq[depfrom])
281 * Find the next jail ready to do something.
288 if (!(j = next_proc(!TAILQ_EMPTY(&ready))) &&
289 (j = TAILQ_FIRST(&ready)) && JF_DO_STOP(j->flags) &&
290 (j = TAILQ_LAST(&ready, cfjails)) && !JF_DO_STOP(j->flags)) {
291 TAILQ_FOREACH_REVERSE(j, &ready, cfjails, tq)
292 if (JF_DO_STOP(j->flags))
296 requeue(j, &cfjails);
301 * Set jails to the proper start state.
304 start_state(const char *target, int docf, unsigned state, int running)
306 struct iovec jiov[6];
307 struct cfjail *j, *tj;
309 char namebuf[MAXHOSTNAMELEN];
311 if (!target || (!docf && state != JF_STOP) ||
312 (!running && !strcmp(target, "*"))) {
314 * For a global wildcard (including no target specified),
315 * set the state on all jails and start with those that
316 * have no dependencies.
318 TAILQ_FOREACH_SAFE(j, &cfjails, tq, tj) {
319 j->flags = (j->flags & JF_FAILED) | state |
320 (docf ? JF_WILD : 0);
322 requeue(j, j->ndeps ? &depend : &ready);
324 } else if (wild_jail_name(target)) {
326 * For targets specified singly, or with a non-global wildcard,
327 * set their state and call them ready (even if there are
328 * dependencies). Leave everything else unqueued for now.
332 * -R matches its wildcards against currently running
333 * jails, not against the config file.
335 jiov[0].iov_base = __DECONST(char *, "lastjid");
336 jiov[0].iov_len = sizeof("lastjid");
337 jiov[1].iov_base = &jid;
338 jiov[1].iov_len = sizeof(jid);
339 jiov[2].iov_base = __DECONST(char *, "jid");
340 jiov[2].iov_len = sizeof("jid");
341 jiov[3].iov_base = &jid;
342 jiov[3].iov_len = sizeof(jid);
343 jiov[4].iov_base = __DECONST(char *, "name");
344 jiov[4].iov_len = sizeof("name");
345 jiov[5].iov_base = &namebuf;
346 jiov[5].iov_len = sizeof(namebuf);
347 for (jid = 0; jail_get(jiov, 6, 0) > 0; ) {
348 if (wild_jail_match(namebuf, target)) {
350 j->name = estrdup(namebuf);
352 j->flags = (j->flags & JF_FAILED) |
359 TAILQ_FOREACH_SAFE(j, &cfjails, tq, tj) {
360 if (wild_jail_match(j->name, target)) {
361 j->flags = (j->flags & JF_FAILED) |
369 j = find_jail(target);
370 if (j == NULL && state == JF_STOP) {
371 /* Allow -[rR] to specify a currently running jail. */
372 if ((jid = running_jid(target, JAIL_DYING)) > 0) {
374 j->name = estrdup(target);
379 warnx("\"%s\" not found", target);
382 j->flags = (j->flags & JF_FAILED) | state;
390 * Move a jail to a new list.
393 requeue(struct cfjail *j, struct cfjails *queue)
395 if (j->queue != queue) {
396 TAILQ_REMOVE(j->queue, j, tq);
397 TAILQ_INSERT_TAIL(queue, j, tq);
403 requeue_head(struct cfjail *j, struct cfjails *queue)
405 TAILQ_REMOVE(j->queue, j, tq);
406 TAILQ_INSERT_HEAD(queue, j, tq);
411 * Add a dependency edge between two jails.
414 dep_add(struct cfjail *from, struct cfjail *to, unsigned flags)
418 d = emalloc(sizeof(struct cfdepend));
420 d->j[DEP_FROM] = from;
422 STAILQ_INSERT_TAIL(&from->dep[DEP_FROM], d, tq[DEP_FROM]);
423 STAILQ_INSERT_TAIL(&to->dep[DEP_TO], d, tq[DEP_TO]);
427 * Compare jail pointers for qsort/bsearch.
430 cmp_jailptr(const void *a, const void *b)
432 return strcmp((*((struct cfjail * const *)a))->name,
433 ((*(struct cfjail * const *)b))->name);
437 cmp_jailptr_name(const void *a, const void *b)
439 return strcmp((const char *)a, ((*(struct cfjail * const *)b))->name);
443 * Find a jail object by name.
445 static struct cfjail *
446 find_jail(const char *name)
450 jp = bsearch(name, jails_byname, njails, sizeof(struct cfjail *),
452 return jp ? *jp : NULL;
456 * Return the named jail's jid if it is running, and -1 if it isn't.
459 running_jid(const char *name, int flags)
461 struct iovec jiov[2];
465 if ((jid = strtol(name, &ep, 10)) && !*ep) {
466 jiov[0].iov_base = __DECONST(char *, "jid");
467 jiov[0].iov_len = sizeof("jid");
468 jiov[1].iov_base = &jid;
469 jiov[1].iov_len = sizeof(jid);
471 jiov[0].iov_base = __DECONST(char *, "name");
472 jiov[0].iov_len = sizeof("name");
473 jiov[1].iov_len = strlen(name) + 1;
474 jiov[1].iov_base = alloca(jiov[1].iov_len);
475 strcpy(jiov[1].iov_base, name);
477 return jail_get(jiov, 2, flags);