xref: /dragonfly/contrib/dhcpcd/src/script.c (revision dda92f98)
1 /* stSPDX-License-Identifier: BSD-2-Clause */
2 /*
3  * dhcpcd - DHCP client daemon
4  * Copyright (c) 2006-2020 Roy Marples <roy@marples.name>
5  * All rights reserved
6 
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
15  *
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
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/stat.h>
30 #include <sys/uio.h>
31 #include <sys/wait.h>
32 
33 #include <netinet/in.h>
34 #include <arpa/inet.h>
35 
36 #include <assert.h>
37 #include <ctype.h>
38 #include <errno.h>
39 #include <pwd.h>
40 #include <signal.h>
41 #include <spawn.h>
42 #include <stdarg.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <unistd.h>
46 
47 #include "config.h"
48 #include "common.h"
49 #include "dhcp.h"
50 #include "dhcp6.h"
51 #include "eloop.h"
52 #include "if.h"
53 #include "if-options.h"
54 #include "ipv4ll.h"
55 #include "ipv6nd.h"
56 #include "logerr.h"
57 #include "privsep.h"
58 #include "script.h"
59 
60 #define DEFAULT_PATH	"/usr/bin:/usr/sbin:/bin:/sbin"
61 
62 static const char * const if_params[] = {
63 	"interface",
64 	"protocol",
65 	"reason",
66 	"pid",
67 	"ifcarrier",
68 	"ifmetric",
69 	"ifwireless",
70 	"ifflags",
71 	"ssid",
72 	"profile",
73 	"interface_order",
74 	NULL
75 };
76 
77 void
78 if_printoptions(void)
79 {
80 	const char * const *p;
81 
82 	for (p = if_params; *p; p++)
83 		printf(" -  %s\n", *p);
84 }
85 
86 pid_t
87 script_exec(char *const *argv, char *const *env)
88 {
89 	pid_t pid = 0;
90 	posix_spawnattr_t attr;
91 	int r;
92 #ifdef USE_SIGNALS
93 	size_t i;
94 	short flags;
95 	sigset_t defsigs;
96 #else
97 	UNUSED(ctx);
98 #endif
99 
100 	/* posix_spawn is a safe way of executing another image
101 	 * and changing signals back to how they should be. */
102 	if (posix_spawnattr_init(&attr) == -1)
103 		return -1;
104 #ifdef USE_SIGNALS
105 	flags = POSIX_SPAWN_SETSIGMASK | POSIX_SPAWN_SETSIGDEF;
106 	posix_spawnattr_setflags(&attr, flags);
107 	sigemptyset(&defsigs);
108 	posix_spawnattr_setsigmask(&attr, &defsigs);
109 	for (i = 0; i < dhcpcd_signals_len; i++)
110 		sigaddset(&defsigs, dhcpcd_signals[i]);
111 	posix_spawnattr_setsigdefault(&attr, &defsigs);
112 #endif
113 	errno = 0;
114 	r = posix_spawn(&pid, argv[0], NULL, &attr, argv, env);
115 	posix_spawnattr_destroy(&attr);
116 	if (r) {
117 		errno = r;
118 		return -1;
119 	}
120 	return pid;
121 }
122 
123 #ifdef INET
124 static int
125 append_config(FILE *fp, const char *prefix, const char *const *config)
126 {
127 	size_t i;
128 
129 	if (config == NULL)
130 		return 0;
131 
132 	/* Do we need to replace existing config rather than append? */
133 	for (i = 0; config[i] != NULL; i++) {
134 		if (efprintf(fp, "%s_%s", prefix, config[i]) == -1)
135 			return -1;
136 	}
137 	return 1;
138 }
139 
140 #endif
141 
142 #define	PROTO_LINK	0
143 #define	PROTO_DHCP	1
144 #define	PROTO_IPV4LL	2
145 #define	PROTO_RA	3
146 #define	PROTO_DHCP6	4
147 #define	PROTO_STATIC6	5
148 static const char *protocols[] = {
149 	"link",
150 	"dhcp",
151 	"ipv4ll",
152 	"ra",
153 	"dhcp6",
154 	"static6"
155 };
156 
157 int
158 efprintf(FILE *fp, const char *fmt, ...)
159 {
160 	va_list args;
161 	int r;
162 
163 	va_start(args, fmt);
164 	r = vfprintf(fp, fmt, args);
165 	va_end(args);
166 	if (r == -1)
167 		return -1;
168 	/* Write a trailing NULL so we can easily create env strings. */
169 	if (fputc('\0', fp) == EOF)
170 		return -1;
171 	return r;
172 }
173 
174 char **
175 script_buftoenv(struct dhcpcd_ctx *ctx, char *buf, size_t len)
176 {
177 	char **env, **envp, *bufp, *endp;
178 	size_t nenv;
179 
180 	/* Count the terminated env strings.
181 	 * Assert that the terminations are correct. */
182 	nenv = 0;
183 	endp = buf + len;
184 	for (bufp = buf; bufp < endp; bufp++) {
185 		if (*bufp == '\0') {
186 #ifndef NDEBUG
187 			if (bufp + 1 < endp)
188 				assert(*(bufp + 1) != '\0');
189 #endif
190 			nenv++;
191 		}
192 	}
193 	assert(*(bufp - 1) == '\0');
194 
195 	if (ctx->script_envlen < nenv) {
196 		env = reallocarray(ctx->script_env, nenv + 1, sizeof(*env));
197 		if (env == NULL)
198 			return NULL;
199 		ctx->script_env = env;
200 		ctx->script_envlen = nenv;
201 	}
202 
203 	bufp = buf;
204 	envp = ctx->script_env;
205 	*envp++ = bufp++;
206 	endp--; /* Avoid setting the last \0 to an invalid pointer */
207 	for (; bufp < endp; bufp++) {
208 		if (*bufp == '\0')
209 			*envp++ = bufp + 1;
210 	}
211 	*envp = NULL;
212 
213 	return ctx->script_env;
214 }
215 
216 static long
217 make_env(struct dhcpcd_ctx *ctx, const struct interface *ifp,
218     const char *reason)
219 {
220 	FILE *fp;
221 	long buf_pos, i;
222 	char *path;
223 	int protocol = PROTO_LINK;
224 	const struct if_options *ifo;
225 	const struct interface *ifp2;
226 	int af;
227 #ifdef INET
228 	const struct dhcp_state *state;
229 #ifdef IPV4LL
230 	const struct ipv4ll_state *istate;
231 #endif
232 #endif
233 #ifdef DHCP6
234 	const struct dhcp6_state *d6_state;
235 #endif
236 
237 #ifdef HAVE_OPEN_MEMSTREAM
238 	if (ctx->script_fp == NULL) {
239 		fp = open_memstream(&ctx->script_buf, &ctx->script_buflen);
240 		if (fp == NULL)
241 			goto eexit;
242 		ctx->script_fp = fp;
243 	} else {
244 		fp = ctx->script_fp;
245 		rewind(fp);
246 	}
247 #else
248 	char tmpfile[] = "/tmp/dhcpcd-script-env-XXXXXX";
249 	int tmpfd;
250 
251 	fp = NULL;
252 	tmpfd = mkstemp(tmpfile);
253 	if (tmpfd == -1)
254 		goto eexit;
255 	unlink(tmpfile);
256 	fp = fdopen(tmpfd, "w+");
257 	if (fp == NULL) {
258 		close(tmpfd);
259 		goto eexit;
260 	}
261 #endif
262 
263 	/* Needed for scripts */
264 	path = getenv("PATH");
265 	if (efprintf(fp, "PATH=%s", path == NULL ? DEFAULT_PATH:path) == -1)
266 		goto eexit;
267 	if (efprintf(fp, "reason=%s", reason) == -1)
268 		goto eexit;
269 	if (efprintf(fp, "pid=%d", getpid()) == -1)
270 		goto eexit;
271 
272 #ifdef PRIVSEP
273 	if (ctx->options & DHCPCD_PRIVSEP && ctx->ps_user != NULL) {
274 		if (efprintf(fp, "chroot=%s", ctx->ps_user->pw_dir) == -1)
275 			goto eexit;
276 	}
277 	if (strcmp(reason, "CHROOT") == 0)
278 		goto make;
279 #endif
280 
281 	ifo = ifp->options;
282 #ifdef INET
283 	state = D_STATE(ifp);
284 #ifdef IPV4LL
285 	istate = IPV4LL_CSTATE(ifp);
286 #endif
287 #endif
288 #ifdef DHCP6
289 	d6_state = D6_CSTATE(ifp);
290 #endif
291 	if (strcmp(reason, "TEST") == 0) {
292 		if (1 == 2) {
293 			/* This space left intentionally blank
294 			 * as all the below statements are optional. */
295 		}
296 #ifdef INET6
297 #ifdef DHCP6
298 		else if (d6_state && d6_state->new)
299 			protocol = PROTO_DHCP6;
300 #endif
301 		else if (ipv6nd_hasra(ifp))
302 			protocol = PROTO_RA;
303 #endif
304 #ifdef INET
305 #ifdef IPV4LL
306 		else if (istate && istate->addr != NULL)
307 			protocol = PROTO_IPV4LL;
308 #endif
309 		else
310 			protocol = PROTO_DHCP;
311 #endif
312 	}
313 #ifdef INET6
314 	else if (strcmp(reason, "STATIC6") == 0)
315 		protocol = PROTO_STATIC6;
316 #ifdef DHCP6
317 	else if (reason[strlen(reason) - 1] == '6')
318 		protocol = PROTO_DHCP6;
319 #endif
320 	else if (strcmp(reason, "ROUTERADVERT") == 0)
321 		protocol = PROTO_RA;
322 #endif
323 	else if (strcmp(reason, "PREINIT") == 0 ||
324 	    strcmp(reason, "CARRIER") == 0 ||
325 	    strcmp(reason, "NOCARRIER") == 0 ||
326 	    strcmp(reason, "UNKNOWN") == 0 ||
327 	    strcmp(reason, "DEPARTED") == 0 ||
328 	    strcmp(reason, "STOPPED") == 0)
329 		protocol = PROTO_LINK;
330 #ifdef INET
331 #ifdef IPV4LL
332 	else if (strcmp(reason, "IPV4LL") == 0)
333 		protocol = PROTO_IPV4LL;
334 #endif
335 	else
336 		protocol = PROTO_DHCP;
337 #endif
338 
339 
340 	if (efprintf(fp, "interface=%s", ifp->name) == -1)
341 		goto eexit;
342 	if (ifp->ctx->options & DHCPCD_DUMPLEASE)
343 		goto dumplease;
344 	if (efprintf(fp, "ifcarrier=%s",
345 	    ifp->carrier == LINK_UNKNOWN ? "unknown" :
346 	    ifp->carrier == LINK_UP ? "up" : "down") == -1)
347 		goto eexit;
348 	if (efprintf(fp, "ifmetric=%d", ifp->metric) == -1)
349 		goto eexit;
350 	if (efprintf(fp, "ifwireless=%d", ifp->wireless) == -1)
351 		goto eexit;
352 	if (efprintf(fp, "ifflags=%u", ifp->flags) == -1)
353 		goto eexit;
354 	if (efprintf(fp, "ifmtu=%d", if_getmtu(ifp)) == -1)
355 		goto eexit;
356 
357 	if (fprintf(fp, "interface_order=") == -1)
358 		goto eexit;
359 	TAILQ_FOREACH(ifp2, ifp->ctx->ifaces, next) {
360 		if (ifp2 != TAILQ_FIRST(ifp->ctx->ifaces)) {
361 			if (fputc(' ', fp) == EOF)
362 				return -1;
363 		}
364 		if (fprintf(fp, "%s", ifp2->name) == -1)
365 			return -1;
366 	}
367 	if (fputc('\0', fp) == EOF)
368 		return -1;
369 
370 	if (strcmp(reason, "STOPPED") == 0) {
371 		if (efprintf(fp, "if_up=false") == -1)
372 			goto eexit;
373 		if (efprintf(fp, "if_down=%s",
374 		    ifo->options & DHCPCD_RELEASE ? "true" : "false") == -1)
375 			goto eexit;
376 	} else if (strcmp(reason, "TEST") == 0 ||
377 	    strcmp(reason, "PREINIT") == 0 ||
378 	    strcmp(reason, "CARRIER") == 0 ||
379 	    strcmp(reason, "UNKNOWN") == 0)
380 	{
381 		if (efprintf(fp, "if_up=false") == -1)
382 			goto eexit;
383 		if (efprintf(fp, "if_down=false") == -1)
384 			goto eexit;
385 	} else if (1 == 2 /* appease ifdefs */
386 #ifdef INET
387 	    || (protocol == PROTO_DHCP && state && state->new)
388 #ifdef IPV4LL
389 	    || (protocol == PROTO_IPV4LL && IPV4LL_STATE_RUNNING(ifp))
390 #endif
391 #endif
392 #ifdef INET6
393 	    || (protocol == PROTO_STATIC6 && IPV6_STATE_RUNNING(ifp))
394 #ifdef DHCP6
395 	    || (protocol == PROTO_DHCP6 && d6_state && d6_state->new)
396 #endif
397 	    || (protocol == PROTO_RA && ipv6nd_hasra(ifp))
398 #endif
399 	    )
400 	{
401 		if (efprintf(fp, "if_up=true") == -1)
402 			goto eexit;
403 		if (efprintf(fp, "if_down=false") == -1)
404 			goto eexit;
405 	} else {
406 		if (efprintf(fp, "if_up=false") == -1)
407 			goto eexit;
408 		if (efprintf(fp, "if_down=true") == -1)
409 			goto eexit;
410 	}
411 	if (protocols[protocol] != NULL) {
412 		if (efprintf(fp, "protocol=%s", protocols[protocol]) == -1)
413 			goto eexit;
414 	}
415 	if ((af = dhcpcd_ifafwaiting(ifp)) != AF_MAX) {
416 		if (efprintf(fp, "if_afwaiting=%d", af) == -1)
417 			goto eexit;
418 	}
419 	if ((af = dhcpcd_afwaiting(ifp->ctx)) != AF_MAX) {
420 		TAILQ_FOREACH(ifp2, ifp->ctx->ifaces, next) {
421 			if ((af = dhcpcd_ifafwaiting(ifp2)) != AF_MAX)
422 				break;
423 		}
424 	}
425 	if (af != AF_MAX) {
426 		if (efprintf(fp, "af_waiting=%d", af) == -1)
427 			goto eexit;
428 	}
429 	if (ifo->options & DHCPCD_DEBUG) {
430 		if (efprintf(fp, "syslog_debug=true") == -1)
431 			goto eexit;
432 	}
433 	if (*ifp->profile != '\0') {
434 		if (efprintf(fp, "profile=%s", ifp->profile) == -1)
435 			goto eexit;
436 	}
437 	if (ifp->wireless) {
438 		char pssid[IF_SSIDLEN * 4];
439 
440 		if (print_string(pssid, sizeof(pssid), OT_ESCSTRING,
441 		    ifp->ssid, ifp->ssid_len) != -1)
442 		{
443 			if (efprintf(fp, "ifssid=%s", pssid) == -1)
444 				goto eexit;
445 		}
446 	}
447 #ifdef INET
448 	if (protocol == PROTO_DHCP && state && state->old) {
449 		if (dhcp_env(fp, "old", ifp,
450 		    state->old, state->old_len) == -1)
451 			goto eexit;
452 		if (append_config(fp, "old",
453 		    (const char *const *)ifo->config) == -1)
454 			goto eexit;
455 	}
456 #endif
457 #ifdef DHCP6
458 	if (protocol == PROTO_DHCP6 && d6_state && d6_state->old) {
459 		if (dhcp6_env(fp, "old", ifp,
460 		    d6_state->old, d6_state->old_len) == -1)
461 			goto eexit;
462 	}
463 #endif
464 
465 dumplease:
466 #ifdef INET
467 #ifdef IPV4LL
468 	if (protocol == PROTO_IPV4LL && istate) {
469 		if (ipv4ll_env(fp, istate->down ? "old" : "new", ifp) == -1)
470 			goto eexit;
471 	}
472 #endif
473 	if (protocol == PROTO_DHCP && state && state->new) {
474 		if (dhcp_env(fp, "new", ifp,
475 		    state->new, state->new_len) == -1)
476 			goto eexit;
477 		if (append_config(fp, "new",
478 		    (const char *const *)ifo->config) == -1)
479 			goto eexit;
480 	}
481 #endif
482 #ifdef INET6
483 	if (protocol == PROTO_STATIC6) {
484 		if (ipv6_env(fp, "new", ifp) == -1)
485 			goto eexit;
486 	}
487 #ifdef DHCP6
488 	if (protocol == PROTO_DHCP6 && D6_STATE_RUNNING(ifp)) {
489 		if (dhcp6_env(fp, "new", ifp,
490 		    d6_state->new, d6_state->new_len) == -1)
491 			goto eexit;
492 	}
493 #endif
494 	if (protocol == PROTO_RA) {
495 		if (ipv6nd_env(fp, ifp) == -1)
496 			goto eexit;
497 	}
498 #endif
499 
500 	/* Add our base environment */
501 	if (ifo->environ) {
502 		for (i = 0; ifo->environ[i] != NULL; i++)
503 			if (efprintf(fp, "%s", ifo->environ[i]) == -1)
504 				goto eexit;
505 	}
506 
507 #ifdef PRIVSEP
508 make:
509 #endif
510 	/* Convert buffer to argv */
511 	fflush(fp);
512 
513 	buf_pos = ftell(fp);
514 	if (buf_pos == -1) {
515 		logerr(__func__);
516 		goto eexit;
517 	}
518 
519 #ifndef HAVE_OPEN_MEMSTREAM
520 	size_t buf_len = (size_t)buf_pos;
521 	if (ctx->script_buflen < buf_len) {
522 		char *buf = realloc(ctx->script_buf, buf_len);
523 		if (buf == NULL)
524 			goto eexit;
525 		ctx->script_buf = buf;
526 		ctx->script_buflen = buf_len;
527 	}
528 	rewind(fp);
529 	if (fread(ctx->script_buf, sizeof(char), buf_len, fp) != buf_len)
530 		goto eexit;
531 	fclose(fp);
532 	fp = NULL;
533 #endif
534 
535 	if (script_buftoenv(ctx, ctx->script_buf, (size_t)buf_pos) == NULL)
536 		goto eexit;
537 
538 	return buf_pos;
539 
540 eexit:
541 	logerr(__func__);
542 #ifndef HAVE_OPEN_MEMSTREAM
543 	if (fp != NULL)
544 		fclose(fp);
545 #endif
546 	return -1;
547 }
548 
549 static int
550 send_interface1(struct fd_list *fd, const struct interface *ifp,
551     const char *reason)
552 {
553 	struct dhcpcd_ctx *ctx = ifp->ctx;
554 	long len;
555 
556 	len = make_env(ifp->ctx, ifp, reason);
557 	if (len == -1)
558 		return -1;
559 	return control_queue(fd, ctx->script_buf, (size_t)len, 1);
560 }
561 
562 int
563 send_interface(struct fd_list *fd, const struct interface *ifp, int af)
564 {
565 	int retval = 0;
566 #ifdef INET
567 	const struct dhcp_state *d;
568 #endif
569 #ifdef DHCP6
570 	const struct dhcp6_state *d6;
571 #endif
572 
573 #ifndef AF_LINK
574 #define	AF_LINK	AF_PACKET
575 #endif
576 
577 	if (af == AF_UNSPEC || af == AF_LINK) {
578 		const char *reason;
579 
580 		switch (ifp->carrier) {
581 		case LINK_UP:
582 			reason = "CARRIER";
583 			break;
584 		case LINK_DOWN:
585 		case LINK_DOWN_IFFUP:
586 			reason = "NOCARRIER";
587 			break;
588 		default:
589 			reason = "UNKNOWN";
590 			break;
591 		}
592 		if (fd != NULL) {
593 			if (send_interface1(fd, ifp, reason) == -1)
594 				retval = -1;
595 		} else
596 			retval++;
597 	}
598 
599 #ifdef INET
600 	if (af == AF_UNSPEC || af == AF_INET) {
601 		if (D_STATE_RUNNING(ifp)) {
602 			d = D_CSTATE(ifp);
603 			if (fd != NULL) {
604 				if (send_interface1(fd, ifp, d->reason) == -1)
605 					retval = -1;
606 			} else
607 				retval++;
608 		}
609 #ifdef IPV4LL
610 		if (IPV4LL_STATE_RUNNING(ifp)) {
611 			if (fd != NULL) {
612 				if (send_interface1(fd, ifp, "IPV4LL") == -1)
613 					retval = -1;
614 			} else
615 				retval++;
616 		}
617 #endif
618 	}
619 #endif
620 
621 #ifdef INET6
622 	if (af == AF_UNSPEC || af == AF_INET6) {
623 		if (IPV6_STATE_RUNNING(ifp)) {
624 			if (fd != NULL) {
625 				if (send_interface1(fd, ifp, "STATIC6") == -1)
626 					retval = -1;
627 			} else
628 				retval++;
629 		}
630 		if (RS_STATE_RUNNING(ifp)) {
631 			if (fd != NULL) {
632 				if (send_interface1(fd, ifp,
633 				    "ROUTERADVERT") == -1)
634 					retval = -1;
635 			} else
636 				retval++;
637 		}
638 #ifdef DHCP6
639 		if (D6_STATE_RUNNING(ifp)) {
640 			d6 = D6_CSTATE(ifp);
641 			if (fd != NULL) {
642 				if (send_interface1(fd, ifp, d6->reason) == -1)
643 					retval = -1;
644 			} else
645 				retval++;
646 		}
647 #endif
648 	}
649 #endif
650 
651 	return retval;
652 }
653 
654 static int
655 script_run(struct dhcpcd_ctx *ctx, char **argv)
656 {
657 	pid_t pid;
658 	int status = 0;
659 
660 	pid = script_exec(argv, ctx->script_env);
661 	if (pid == -1)
662 		logerr("%s: %s", __func__, argv[0]);
663 	else if (pid != 0) {
664 		/* Wait for the script to finish */
665 		while (waitpid(pid, &status, 0) == -1) {
666 			if (errno != EINTR) {
667 				logerr("%s: waitpid", __func__);
668 				status = 0;
669 				break;
670 			}
671 		}
672 		if (WIFEXITED(status)) {
673 			if (WEXITSTATUS(status))
674 				logerrx("%s: %s: WEXITSTATUS %d",
675 				    __func__, argv[0], WEXITSTATUS(status));
676 		} else if (WIFSIGNALED(status))
677 			logerrx("%s: %s: %s",
678 			    __func__, argv[0], strsignal(WTERMSIG(status)));
679 	}
680 
681 	return WEXITSTATUS(status);
682 }
683 
684 int
685 script_runreason(const struct interface *ifp, const char *reason)
686 {
687 	struct dhcpcd_ctx *ctx = ifp->ctx;
688 	char *argv[2];
689 	int status = 0;
690 	struct fd_list *fd;
691 
692 	if (ifp->options->script == NULL &&
693 	    TAILQ_FIRST(&ifp->ctx->control_fds) == NULL)
694 		return 0;
695 
696 	/* Make our env */
697 	if (make_env(ifp->ctx, ifp, reason) == -1) {
698 		logerr(__func__);
699 		return -1;
700 	}
701 
702 	if (ifp->options->script == NULL)
703 		goto send_listeners;
704 
705 	argv[0] = ifp->options->script;
706 	argv[1] = NULL;
707 	logdebugx("%s: executing `%s' %s", ifp->name, argv[0], reason);
708 
709 #ifdef PRIVSEP
710 	if (ctx->options & DHCPCD_PRIVSEP) {
711 		if (ps_root_script(ifp,
712 		    ctx->script_buf, ctx->script_buflen) == -1)
713 			logerr(__func__);
714 		goto send_listeners;
715 	}
716 #endif
717 
718 	status = script_run(ctx, argv);
719 
720 send_listeners:
721 	/* Send to our listeners */
722 	status = 0;
723 	TAILQ_FOREACH(fd, &ctx->control_fds, next) {
724 		if (!(fd->flags & FD_LISTEN))
725 			continue;
726 		if (control_queue(fd, ctx->script_buf, ctx->script_buflen,
727 		    true) == -1)
728 			logerr("%s: control_queue", __func__);
729 		else
730 			status = 1;
731 	}
732 
733 	return status;
734 }
735 
736 #ifdef PRIVSEP
737 int
738 script_runchroot(struct dhcpcd_ctx *ctx, char *script)
739 {
740 	char *argv[2];
741 
742 	/* Make our env */
743 	if (make_env(ctx, NULL, "CHROOT") == -1) {
744 		logerr(__func__);
745 		return -1;
746 	}
747 
748 	argv[0] = script;
749 	argv[1] = NULL;
750 	logdebugx("executing `%s' %s", argv[0], "CHROOT");
751 
752 	return script_run(ctx, argv);
753 }
754 #endif
755