1 /* SPDX-License-Identifier: BSD-2-Clause */
3 * dhcpcd - DHCP client daemon
4 * Copyright (c) 2006-2023 Roy Marples <roy@marples.name>
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 static const char dhcpcd_copyright
[] = "Copyright (c) 2006-2023 Roy Marples";
32 #include <sys/ioctl.h>
33 #include <sys/socket.h>
36 #include <sys/types.h>
59 #include "dhcp-common.h"
66 #include "if-options.h"
76 #include <sys/capsicum.h>
83 const int dhcpcd_signals
[] = {
92 const size_t dhcpcd_signals_len
= __arraycount(dhcpcd_signals
);
94 const int dhcpcd_signals_ignore
[] = {
97 const size_t dhcpcd_signals_ignore_len
= __arraycount(dhcpcd_signals_ignore
);
100 const char *dhcpcd_default_script
= SCRIPT
;
106 printf("usage: "PACKAGE
"\t[-146ABbDdEGgHJKLMNPpqTV]\n"
107 "\t\t[-C, --nohook hook] [-c, --script script]\n"
108 "\t\t[-e, --env value] [-F, --fqdn FQDN] [-f, --config file]\n"
109 "\t\t[-h, --hostname hostname] [-I, --clientid clientid]\n"
110 "\t\t[-i, --vendorclassid vendorclassid] [-j, --logfile logfile]\n"
111 "\t\t[-l, --leasetime seconds] [-m, --metric metric]\n"
112 "\t\t[-O, --nooption option] [-o, --option option]\n"
113 "\t\t[-Q, --require option] [-r, --request address]\n"
114 "\t\t[-S, --static value]\n"
115 "\t\t[-s, --inform address[/cidr[/broadcast_address]]]\n [--inform6]"
116 "\t\t[-t, --timeout seconds] [-u, --userclass class]\n"
117 "\t\t[-v, --vendor code, value] [-W, --whitelist address[/cidr]] [-w]\n"
118 "\t\t[--waitip [4 | 6]] [-y, --reboot seconds]\n"
119 "\t\t[-X, --blacklist address[/cidr]] [-Z, --denyinterfaces pattern]\n"
120 "\t\t[-z, --allowinterfaces pattern] [--inactive] [interface] [...]\n"
121 " "PACKAGE
"\t-n, --rebind [interface]\n"
122 " "PACKAGE
"\t-k, --release [interface]\n"
123 " "PACKAGE
"\t-U, --dumplease interface\n"
124 " "PACKAGE
"\t--version\n"
125 " "PACKAGE
"\t-x, --exit [interface]\n");
129 free_globals(struct dhcpcd_ctx
*ctx
)
131 struct dhcp_opt
*opt
;
134 for (; ctx
->ifac
> 0; ctx
->ifac
--)
135 free(ctx
->ifav
[ctx
->ifac
- 1]);
140 for (; ctx
->ifdc
> 0; ctx
->ifdc
--)
141 free(ctx
->ifdv
[ctx
->ifdc
- 1]);
146 for (; ctx
->ifcc
> 0; ctx
->ifcc
--)
147 free(ctx
->ifcv
[ctx
->ifcc
- 1]);
153 if (ctx
->dhcp_opts
) {
154 for (opt
= ctx
->dhcp_opts
;
155 ctx
->dhcp_opts_len
> 0;
156 opt
++, ctx
->dhcp_opts_len
--)
157 free_dhcp_opt_embenc(opt
);
158 free(ctx
->dhcp_opts
);
159 ctx
->dhcp_opts
= NULL
;
164 for (opt
= ctx
->nd_opts
;
165 ctx
->nd_opts_len
> 0;
166 opt
++, ctx
->nd_opts_len
--)
167 free_dhcp_opt_embenc(opt
);
172 if (ctx
->dhcp6_opts
) {
173 for (opt
= ctx
->dhcp6_opts
;
174 ctx
->dhcp6_opts_len
> 0;
175 opt
++, ctx
->dhcp6_opts_len
--)
176 free_dhcp_opt_embenc(opt
);
177 free(ctx
->dhcp6_opts
);
178 ctx
->dhcp6_opts
= NULL
;
183 for (opt
= ctx
->vivso
;
185 opt
++, ctx
->vivso_len
--)
186 free_dhcp_opt_embenc(opt
);
193 handle_exit_timeout(void *arg
)
195 struct dhcpcd_ctx
*ctx
;
198 logerrx("timed out");
199 if (!(ctx
->options
& DHCPCD_MANAGER
)) {
200 struct interface
*ifp
;
202 TAILQ_FOREACH(ifp
, ctx
->ifaces
, next
) {
203 if (ifp
->active
== IF_ACTIVE_USER
)
204 script_runreason(ifp
, "STOPPED");
206 eloop_exit(ctx
->eloop
, EXIT_FAILURE
);
209 ctx
->options
|= DHCPCD_NOWAITIP
;
210 dhcpcd_daemonise(ctx
);
230 dhcpcd_ifafwaiting(const struct interface
*ifp
)
232 unsigned long long opts
;
233 bool foundany
= false;
235 if (ifp
->active
!= IF_ACTIVE_USER
)
238 #define DHCPCD_WAITALL (DHCPCD_WAITIP4 | DHCPCD_WAITIP6)
239 opts
= ifp
->options
->options
;
241 if (opts
& DHCPCD_WAITIP4
||
242 (opts
& DHCPCD_WAITIP
&& !(opts
& DHCPCD_WAITALL
)))
244 bool foundaddr
= ipv4_hasaddr(ifp
);
246 if (opts
& DHCPCD_WAITIP4
&& !foundaddr
)
253 if (opts
& DHCPCD_WAITIP6
||
254 (opts
& DHCPCD_WAITIP
&& !(opts
& DHCPCD_WAITALL
)))
256 bool foundaddr
= ipv6_hasaddr(ifp
);
258 if (opts
& DHCPCD_WAITIP6
&& !foundaddr
)
265 if (opts
& DHCPCD_WAITIP
&& !(opts
& DHCPCD_WAITALL
) && !foundany
)
271 dhcpcd_afwaiting(const struct dhcpcd_ctx
*ctx
)
273 unsigned long long opts
;
274 const struct interface
*ifp
;
277 if (!(ctx
->options
& DHCPCD_WAITOPTS
))
281 TAILQ_FOREACH(ifp
, ctx
->ifaces
, next
) {
283 if (opts
& (DHCPCD_WAITIP
| DHCPCD_WAITIP4
) &&
285 opts
&= ~(DHCPCD_WAITIP
| DHCPCD_WAITIP4
);
288 if (opts
& (DHCPCD_WAITIP
| DHCPCD_WAITIP6
) &&
290 opts
&= ~(DHCPCD_WAITIP
| DHCPCD_WAITIP6
);
292 if (!(opts
& DHCPCD_WAITOPTS
))
295 if (opts
& DHCPCD_WAITIP
)
297 else if (opts
& DHCPCD_WAITIP4
)
299 else if (opts
& DHCPCD_WAITIP6
)
307 dhcpcd_ipwaited(struct dhcpcd_ctx
*ctx
)
309 struct interface
*ifp
;
312 TAILQ_FOREACH(ifp
, ctx
->ifaces
, next
) {
313 if ((af
= dhcpcd_ifafwaiting(ifp
)) != AF_MAX
) {
314 logdebugx("%s: waiting for an %s address",
315 ifp
->name
, dhcpcd_af(af
));
320 if ((af
= dhcpcd_afwaiting(ctx
)) != AF_MAX
) {
321 logdebugx("waiting for an %s address",
329 /* Returns the pid of the child, otherwise 0. */
331 dhcpcd_daemonise(struct dhcpcd_ctx
*ctx
)
333 #ifdef THERE_IS_NO_FORK
334 eloop_timeout_delete(ctx
->eloop
, handle_exit_timeout
, ctx
);
339 unsigned int logopts
= loggetopts();
341 if (ctx
->options
& DHCPCD_DAEMONISE
&&
342 !(ctx
->options
& (DHCPCD_DAEMONISED
| DHCPCD_NOWAITIP
)))
344 if (!dhcpcd_ipwaited(ctx
))
348 if (ctx
->options
& DHCPCD_ONESHOT
) {
349 loginfox("exiting due to oneshot");
350 eloop_exit(ctx
->eloop
, EXIT_SUCCESS
);
354 eloop_timeout_delete(ctx
->eloop
, handle_exit_timeout
, ctx
);
355 if (ctx
->options
& DHCPCD_DAEMONISED
||
356 !(ctx
->options
& DHCPCD_DAEMONISE
))
359 /* Don't use loginfo because this makes no sense in a log. */
360 if (!(logopts
& LOGERR_QUIET
) && ctx
->stderr_valid
)
361 (void)fprintf(stderr
,
362 "forked to background, child pid %d\n", getpid());
364 if (write(ctx
->fork_fd
, &i
, sizeof(i
)) == -1)
366 ctx
->options
|= DHCPCD_DAEMONISED
;
367 eloop_event_delete(ctx
->eloop
, ctx
->fork_fd
);
372 * Stop writing to stderr.
373 * On the happy path, only the manager process writes to stderr,
374 * so this just stops wasting fprintf calls to nowhere.
375 * All other calls - ie errors in privsep processes or script output,
376 * will error when printing.
377 * If we *really* want to fix that, then we need to suck
378 * stderr/stdout in the manager process and either disacrd it or pass
379 * it to the launcher process and then to stderr.
381 logopts
&= ~LOGERR_ERR
;
387 dhcpcd_drop(struct interface
*ifp
, int stop
)
391 dhcp6_drop(ifp
, stop
? NULL
: "EXPIRE6");
401 dhcp_drop(ifp
, stop
? "STOP" : "EXPIRE");
406 #if !defined(DHCP6) && !defined(DHCP)
412 stop_interface(struct interface
*ifp
, const char *reason
)
414 struct dhcpcd_ctx
*ctx
;
417 loginfox("%s: removing interface", ifp
->name
);
418 ifp
->options
->options
|= DHCPCD_STOPPING
;
421 script_runreason(ifp
, reason
== NULL
? "STOPPED" : reason
);
423 /* Delete all timeouts for the interfaces */
424 eloop_q_timeout_delete(ctx
->eloop
, ELOOP_QUEUE_ALL
, NULL
, ifp
);
426 /* De-activate the interface */
427 ifp
->active
= IF_INACTIVE
;
428 ifp
->options
->options
&= ~DHCPCD_STOPPING
;
430 if (!(ctx
->options
& (DHCPCD_MANAGER
| DHCPCD_TEST
)))
431 eloop_exit(ctx
->eloop
, EXIT_FAILURE
);
435 configure_interface1(struct interface
*ifp
)
437 struct if_options
*ifo
= ifp
->options
;
439 /* Do any platform specific configuration */
442 /* If we want to release a lease, we can't really persist the
444 if (ifo
->options
& DHCPCD_RELEASE
)
445 ifo
->options
&= ~DHCPCD_PERSISTENT
;
447 if (ifp
->flags
& (IFF_POINTOPOINT
| IFF_LOOPBACK
)) {
448 ifo
->options
&= ~DHCPCD_ARP
;
449 if (!(ifp
->flags
& IFF_MULTICAST
))
450 ifo
->options
&= ~DHCPCD_IPV6RS
;
451 if (!(ifo
->options
& (DHCPCD_INFORM
| DHCPCD_WANTDHCP
)))
452 ifo
->options
|= DHCPCD_STATIC
;
455 if (ifo
->metric
!= -1)
456 ifp
->metric
= (unsigned int)ifo
->metric
;
459 /* We want to setup INET6 on the interface as soon as possible. */
460 if (ifp
->active
== IF_ACTIVE_USER
&&
461 ifo
->options
& DHCPCD_IPV6
&&
462 !(ifp
->ctx
->options
& (DHCPCD_DUMPLEASE
| DHCPCD_TEST
)))
464 /* If not doing any DHCP, disable the RDNSS requirement. */
465 if (!(ifo
->options
& (DHCPCD_DHCP
| DHCPCD_DHCP6
)))
466 ifo
->options
&= ~DHCPCD_IPV6RA_REQRDNSS
;
471 if (!(ifo
->options
& DHCPCD_IAID
)) {
473 * An IAID is for identifying a unqiue interface within
474 * the client. It is 4 bytes long. Working out a default
475 * value is problematic.
477 * Interface name and number are not stable
478 * between different OS's. Some OS's also cannot make
479 * up their mind what the interface should be called
480 * (yes, udev, I'm looking at you).
481 * Also, the name could be longer than 4 bytes.
482 * Also, with pluggable interfaces the name and index
483 * could easily get swapped per actual interface.
485 * The MAC address is 6 bytes long, the final 3
486 * being unique to the manufacturer and the initial 3
487 * being unique to the organisation which makes it.
488 * We could use the last 4 bytes of the MAC address
489 * as the IAID as it's the most stable part given the
490 * above, but equally it's not guaranteed to be
493 * Given the above, and our need to reliably work
494 * between reboots without persitent storage,
495 * generating the IAID from the MAC address is the only
497 * Saying that, if a VLANID has been specified then we
498 * can use that. It's possible that different interfaces
499 * can have the same VLANID, but this is no worse than
500 * generating the IAID from the duplicate MAC address.
502 * dhclient uses the last 4 bytes of the MAC address.
503 * dibbler uses an increamenting counter.
504 * wide-dhcpv6 uses 0 or a configured value.
506 * Windows 7 uses the first 3 bytes of the MAC address
507 * and an unknown byte.
508 * dhcpcd-6.1.0 and earlier used the interface name,
509 * falling back to interface index if name > 4.
511 if (ifp
->vlanid
!= 0) {
514 /* Maximal VLANID is 4095, so prefix with 0xff
515 * so we don't conflict with an interface index. */
516 vlanid
= htonl(ifp
->vlanid
| 0xff000000);
517 memcpy(ifo
->iaid
, &vlanid
, sizeof(vlanid
));
518 } else if (ifo
->options
& DHCPCD_ANONYMOUS
)
519 memset(ifo
->iaid
, 0, sizeof(ifo
->iaid
));
520 else if (ifp
->hwlen
>= sizeof(ifo
->iaid
)) {
522 ifp
->hwaddr
+ ifp
->hwlen
- sizeof(ifo
->iaid
),
527 len
= (uint32_t)strlen(ifp
->name
);
528 if (len
<= sizeof(ifo
->iaid
)) {
529 memcpy(ifo
->iaid
, ifp
->name
, len
);
530 if (len
< sizeof(ifo
->iaid
))
531 memset(ifo
->iaid
+ len
, 0,
532 sizeof(ifo
->iaid
) - len
);
534 /* IAID is the same size as a uint32_t */
535 len
= htonl(ifp
->index
);
536 memcpy(ifo
->iaid
, &len
, sizeof(ifo
->iaid
));
539 ifo
->options
|= DHCPCD_IAID
;
543 if (ifo
->ia_len
== 0 && ifo
->options
& DHCPCD_IPV6
&&
544 ifp
->name
[0] != '\0')
546 ifo
->ia
= malloc(sizeof(*ifo
->ia
));
551 ifo
->ia
->ia_type
= D6_OPTION_IA_NA
;
552 memcpy(ifo
->ia
->iaid
, ifo
->iaid
, sizeof(ifo
->iaid
));
553 memset(&ifo
->ia
->addr
, 0, sizeof(ifo
->ia
->addr
));
556 ifo
->ia
->sla_len
= 0;
562 for (i
= 0; i
< ifo
->ia_len
; i
++) {
563 if (!ifo
->ia
[i
].iaid_set
) {
564 memcpy(&ifo
->ia
[i
].iaid
, ifo
->iaid
,
565 sizeof(ifo
->ia
[i
].iaid
));
566 ifo
->ia
[i
].iaid_set
= 1;
572 /* If root is network mounted, we don't want to kill the connection
573 * if the DHCP server goes the way of the dodo OR dhcpcd is rebooting
574 * and the lease file has expired. */
575 if (is_root_local() == 0)
576 ifo
->options
|= DHCPCD_LASTLEASE_EXTEND
;
580 dhcpcd_selectprofile(struct interface
*ifp
, const char *profile
)
582 struct if_options
*ifo
;
583 char pssid
[PROFILE_LEN
];
588 r
= print_string(pssid
, sizeof(pssid
), OT_ESCSTRING
,
589 ifp
->ssid
, ifp
->ssid_len
);
596 ifo
= read_config(ifp
->ctx
, ifp
->name
, pssid
, profile
);
598 logdebugx("%s: no profile %s", ifp
->name
, profile
);
601 if (profile
!= NULL
) {
602 strlcpy(ifp
->profile
, profile
, sizeof(ifp
->profile
));
603 loginfox("%s: selected profile %s", ifp
->name
, profile
);
605 *ifp
->profile
= '\0';
607 free_options(ifp
->ctx
, ifp
->options
);
610 add_options(ifp
->ctx
, ifp
->name
, ifp
->options
,
611 ifp
->ctx
->argc
, ifp
->ctx
->argv
);
612 configure_interface1(ifp
);
618 configure_interface(struct interface
*ifp
, int argc
, char **argv
,
619 unsigned long long options
)
623 old
= ifp
->options
? ifp
->options
->mtime
: 0;
624 dhcpcd_selectprofile(ifp
, NULL
);
625 if (ifp
->options
== NULL
) {
626 /* dhcpcd cannot continue with this interface. */
627 ifp
->active
= IF_INACTIVE
;
630 add_options(ifp
->ctx
, ifp
->name
, ifp
->options
, argc
, argv
);
631 ifp
->options
->options
|= options
;
632 configure_interface1(ifp
);
634 /* If the mtime has changed drop any old lease */
635 if (old
!= 0 && ifp
->options
->mtime
!= old
) {
636 logwarnx("%s: config file changed, expiring leases",
643 dhcpcd_initstate2(struct interface
*ifp
, unsigned long long options
)
645 struct if_options
*ifo
;
648 if ((ifo
= default_config(ifp
->ctx
)) == NULL
) {
652 ifo
->options
|= options
;
659 if (ifo
->options
& DHCPCD_IPV6
&& ipv6_init(ifp
->ctx
) == -1) {
661 ifo
->options
&= ~DHCPCD_IPV6
;
667 dhcpcd_initstate1(struct interface
*ifp
, int argc
, char **argv
,
668 unsigned long long options
)
671 configure_interface(ifp
, argc
, argv
, options
);
673 dhcpcd_initstate2(ifp
, 0);
677 dhcpcd_initstate(struct interface
*ifp
, unsigned long long options
)
680 dhcpcd_initstate1(ifp
, ifp
->ctx
->argc
, ifp
->ctx
->argv
, options
);
684 dhcpcd_reportssid(struct interface
*ifp
)
686 char pssid
[IF_SSIDLEN
* 4];
688 if (print_string(pssid
, sizeof(pssid
), OT_ESCSTRING
,
689 ifp
->ssid
, ifp
->ssid_len
) == -1)
695 loginfox("%s: connected to Access Point: %s", ifp
->name
, pssid
);
699 dhcpcd_nocarrier_roaming(struct interface
*ifp
)
702 loginfox("%s: carrier lost - roaming", ifp
->name
);
714 rt_build(ifp
->ctx
, AF_UNSPEC
);
715 script_runreason(ifp
, "NOCARRIER_ROAMING");
719 dhcpcd_handlecarrier(struct interface
*ifp
, int carrier
, unsigned int flags
)
721 bool was_link_up
= if_is_link_up(ifp
);
722 bool was_roaming
= if_roaming(ifp
);
724 ifp
->carrier
= carrier
;
727 if (!if_is_link_up(ifp
)) {
728 if (!ifp
->active
|| (!was_link_up
&& !was_roaming
))
732 * If the interface is roaming (generally on wireless)
733 * then while we are not up, we are not down either.
734 * Preserve the network state until we either disconnect
737 if (!ifp
->options
->randomise_hwaddr
&& if_roaming(ifp
)) {
738 dhcpcd_nocarrier_roaming(ifp
);
742 loginfox("%s: carrier lost", ifp
->name
);
743 script_runreason(ifp
, "NOCARRIER");
746 if (ifp
->options
->randomise_hwaddr
) {
747 bool is_up
= ifp
->flags
& IFF_UP
;
751 if (if_randomisemac(ifp
) == -1 && errno
!= ENXIO
)
761 * At this point carrier is NOT DOWN and we have IFF_UP.
762 * We should treat LINK_UNKNOWN as up as the driver may not support
763 * link state changes.
764 * The consideration of any other information about carrier should
765 * be handled in the OS specific if_carrier() function.
771 if (carrier
== LINK_UNKNOWN
)
772 loginfox("%s: carrier unknown, assuming up", ifp
->name
);
774 loginfox("%s: carrier acquired", ifp
->name
);
777 #if !defined(__linux__) && !defined(__NetBSD__)
778 /* BSD does not emit RTM_NEWADDR or RTM_CHGADDR when the
779 * hardware address changes so we have to go
780 * through the disovery process to work it out. */
781 dhcpcd_handleinterface(ifp
->ctx
, 0, ifp
->name
);
785 uint8_t ossid
[IF_SSIDLEN
];
788 olen
= ifp
->ssid_len
;
789 memcpy(ossid
, ifp
->ssid
, ifp
->ssid_len
);
792 /* If we changed SSID network, drop leases */
793 if ((ifp
->ssid_len
!= olen
||
794 memcmp(ifp
->ssid
, ossid
, ifp
->ssid_len
)) && ifp
->active
)
796 dhcpcd_reportssid(ifp
);
807 dhcpcd_initstate(ifp
, 0);
808 script_runreason(ifp
, "CARRIER");
811 /* Set any IPv6 Routers we remembered to expire faster than they
812 * would normally as we maybe on a new network. */
813 ipv6nd_startexpire(ifp
);
814 #ifdef IPV6_MANAGETEMPADDR
815 /* RFC4941 Section 3.5 */
816 ipv6_regentempaddrs(ifp
);
820 dhcpcd_startinterface(ifp
);
824 warn_iaid_conflict(struct interface
*ifp
, uint16_t ia_type
, uint8_t *iaid
)
826 struct interface
*ifn
;
832 TAILQ_FOREACH(ifn
, ifp
->ctx
->ifaces
, next
) {
833 if (ifn
== ifp
|| !ifn
->active
)
835 if (ifn
->options
->options
& DHCPCD_ANONYMOUS
)
838 memcmp(ifn
->options
->iaid
, iaid
,
839 sizeof(ifn
->options
->iaid
)) == 0)
842 for (i
= 0; i
< ifn
->options
->ia_len
; i
++) {
843 ia
= &ifn
->options
->ia
[i
];
844 if (ia
->ia_type
== ia_type
&&
845 memcmp(ia
->iaid
, iaid
, sizeof(ia
->iaid
)) == 0)
851 /* This is only a problem if the interfaces are on the same network. */
853 logerrx("%s: IAID conflicts with one assigned to %s",
854 ifp
->name
, ifn
->name
);
858 dhcpcd_initduid(struct dhcpcd_ctx
*ctx
, struct interface
*ifp
)
860 char buf
[DUID_LEN
* 3];
862 if (ctx
->duid
!= NULL
) {
869 if (ctx
->duid
== NULL
)
874 hwaddr_ntoa(ctx
->duid
, ctx
->duid_len
, buf
, sizeof(buf
)));
878 dhcpcd_startinterface(void *arg
)
880 struct interface
*ifp
= arg
;
881 struct if_options
*ifo
= ifp
->options
;
883 if (ifo
->options
& DHCPCD_LINK
&& !if_is_link_up(ifp
)) {
884 loginfox("%s: waiting for carrier", ifp
->name
);
888 if (ifo
->options
& (DHCPCD_DUID
| DHCPCD_IPV6
) &&
889 !(ifo
->options
& DHCPCD_ANONYMOUS
))
891 char buf
[sizeof(ifo
->iaid
) * 3];
897 /* Try and init DUID from the interface hardware address */
898 dhcpcd_initduid(ifp
->ctx
, ifp
);
901 loginfox("%s: IAID %s", ifp
->name
,
902 hwaddr_ntoa(ifo
->iaid
, sizeof(ifo
->iaid
),
904 warn_iaid_conflict(ifp
, 0, ifo
->iaid
);
907 for (i
= 0; i
< ifo
->ia_len
; i
++) {
909 if (memcmp(ifo
->iaid
, ia
->iaid
, sizeof(ifo
->iaid
))) {
910 loginfox("%s: IA type %u IAID %s",
911 ifp
->name
, ia
->ia_type
,
912 hwaddr_ntoa(ia
->iaid
, sizeof(ia
->iaid
),
914 warn_iaid_conflict(ifp
, ia
->ia_type
, ia
->iaid
);
921 if (ifo
->options
& DHCPCD_IPV6
&& ipv6_start(ifp
) == -1) {
922 logerr("%s: ipv6_start", ifp
->name
);
923 ifo
->options
&= ~DHCPCD_IPV6
;
926 if (ifo
->options
& DHCPCD_IPV6
) {
927 if (ifp
->active
== IF_ACTIVE_USER
) {
928 ipv6_startstatic(ifp
);
930 if (ifo
->options
& DHCPCD_IPV6RS
)
935 /* DHCPv6 could be turned off, but the interface
936 * is still delegated to. */
938 dhcp6_find_delegates(ifp
);
940 if (ifo
->options
& DHCPCD_DHCP6
) {
941 if (ifp
->active
== IF_ACTIVE_USER
) {
944 if (ifo
->options
& DHCPCD_IA_FORCED
)
945 d6_state
= DH6S_INIT
;
946 else if (ifo
->options
& DHCPCD_INFORM6
)
947 d6_state
= DH6S_INFORM
;
949 d6_state
= DH6S_CONFIRM
;
950 if (dhcp6_start(ifp
, d6_state
) == -1)
951 logerr("%s: dhcp6_start", ifp
->name
);
959 if (ifo
->options
& DHCPCD_IPV4
&& ifp
->active
== IF_ACTIVE_USER
) {
960 /* Ensure we have an IPv4 state before starting DHCP */
961 if (ipv4_getstate(ifp
) != NULL
)
968 dhcpcd_prestartinterface(void *arg
)
970 struct interface
*ifp
= arg
;
971 struct dhcpcd_ctx
*ctx
= ifp
->ctx
;
974 if (ifp
->carrier
<= LINK_DOWN
&&
975 ifp
->options
->randomise_hwaddr
&&
981 randmac_down
= false;
983 if ((!(ctx
->options
& DHCPCD_MANAGER
) ||
984 ifp
->options
->options
& DHCPCD_IF_UP
|| randmac_down
) &&
985 !(ifp
->flags
& IFF_UP
))
987 if (ifp
->options
->randomise_hwaddr
&&
988 if_randomisemac(ifp
) == -1)
990 if (if_up(ifp
) == -1)
994 dhcpcd_startinterface(ifp
);
998 run_preinit(struct interface
*ifp
)
1001 if (ifp
->ctx
->options
& DHCPCD_TEST
)
1004 script_runreason(ifp
, "PREINIT");
1005 if (ifp
->wireless
&& if_is_link_up(ifp
))
1006 dhcpcd_reportssid(ifp
);
1007 if (ifp
->options
->options
& DHCPCD_LINK
&& ifp
->carrier
!= LINK_UNKNOWN
)
1008 script_runreason(ifp
,
1009 ifp
->carrier
== LINK_UP
? "CARRIER" : "NOCARRIER");
1013 dhcpcd_activateinterface(struct interface
*ifp
, unsigned long long options
)
1019 ifp
->active
= IF_ACTIVE
;
1020 dhcpcd_initstate2(ifp
, options
);
1022 /* It's possible we might not have been able to load
1027 configure_interface1(ifp
);
1029 dhcpcd_prestartinterface(ifp
);
1033 dhcpcd_handleinterface(void *arg
, int action
, const char *ifname
)
1035 struct dhcpcd_ctx
*ctx
= arg
;
1036 struct ifaddrs
*ifaddrs
;
1037 struct if_head
*ifs
;
1038 struct interface
*ifp
, *iff
;
1039 const char * const argv
[] = { ifname
};
1043 ifp
= if_find(ctx
->ifaces
, ifname
);
1049 logdebugx("%s: interface departed", ifp
->name
);
1050 stop_interface(ifp
, "DEPARTED");
1052 TAILQ_REMOVE(ctx
->ifaces
, ifp
, next
);
1057 ifs
= if_discover(ctx
, &ifaddrs
, -1, UNCONST(argv
));
1063 ifp
= if_find(ifs
, ifname
);
1065 /* This can happen if an interface is quickly added
1066 * and then removed. */
1073 /* Check if we already have the interface */
1074 iff
= if_find(ctx
->ifaces
, ifp
->name
);
1078 logdebugx("%s: interface updated", iff
->name
);
1079 /* The flags and hwaddr could have changed */
1080 iff
->flags
= ifp
->flags
;
1081 iff
->hwlen
= ifp
->hwlen
;
1082 if (ifp
->hwlen
!= 0)
1083 memcpy(iff
->hwaddr
, ifp
->hwaddr
, iff
->hwlen
);
1085 TAILQ_REMOVE(ifs
, ifp
, next
);
1086 TAILQ_INSERT_TAIL(ctx
->ifaces
, ifp
, next
);
1088 logdebugx("%s: interface added", ifp
->name
);
1089 dhcpcd_initstate(ifp
, 0);
1096 if_learnaddrs(ctx
, ifs
, &ifaddrs
);
1098 dhcpcd_prestartinterface(iff
);
1102 /* Free our discovered list */
1103 while ((ifp
= TAILQ_FIRST(ifs
))) {
1104 TAILQ_REMOVE(ifs
, ifp
, next
);
1108 if_freeifaddrs(ctx
, &ifaddrs
);
1114 dhcpcd_handlelink(void *arg
, unsigned short events
)
1116 struct dhcpcd_ctx
*ctx
= arg
;
1118 if (events
!= ELE_READ
)
1119 logerrx("%s: unexpected event 0x%04x", __func__
, events
);
1121 if (if_handlelink(ctx
) == -1) {
1122 if (errno
== ENOBUFS
|| errno
== ENOMEM
) {
1123 dhcpcd_linkoverflow(ctx
);
1126 if (errno
!= ENOTSUP
)
1132 dhcpcd_checkcarrier(void *arg
)
1134 struct interface
*ifp0
= arg
, *ifp
;
1136 ifp
= if_find(ifp0
->ctx
->ifaces
, ifp0
->name
);
1137 if (ifp
== NULL
|| ifp
->carrier
== ifp0
->carrier
)
1140 dhcpcd_handlecarrier(ifp
, ifp0
->carrier
, ifp0
->flags
);
1146 dhcpcd_setlinkrcvbuf(struct dhcpcd_ctx
*ctx
)
1150 if (ctx
->link_rcvbuf
== 0)
1153 logdebugx("setting route socket receive buffer size to %d bytes",
1156 socklen
= sizeof(ctx
->link_rcvbuf
);
1157 if (setsockopt(ctx
->link_fd
, SOL_SOCKET
,
1158 SO_RCVBUF
, &ctx
->link_rcvbuf
, socklen
) == -1)
1164 dhcpcd_runprestartinterface(void *arg
)
1166 struct interface
*ifp
= arg
;
1169 dhcpcd_prestartinterface(ifp
);
1173 dhcpcd_linkoverflow(struct dhcpcd_ctx
*ctx
)
1180 struct if_head
*ifaces
;
1181 struct ifaddrs
*ifaddrs
;
1182 struct interface
*ifp
, *ifn
, *ifp1
;
1184 socklen
= sizeof(rcvbuflen
);
1185 if (getsockopt(ctx
->link_fd
, SOL_SOCKET
,
1186 SO_RCVBUF
, &rcvbuflen
, &socklen
) == -1) {
1187 logerr("%s: getsockopt", __func__
);
1195 logerrx("route socket overflowed (rcvbuflen %d)"
1196 " - learning interface state", rcvbuflen
);
1198 /* Drain the socket.
1199 * We cannot open a new one due to privsep. */
1202 rlen
= read(ctx
->link_fd
, buf
, sizeof(buf
));
1203 if (++rcnt
% 1000 == 0)
1204 logwarnx("drained %zu messages", rcnt
);
1205 } while (rlen
!= -1 || errno
== ENOBUFS
|| errno
== ENOMEM
);
1206 if (rcnt
% 1000 != 0)
1207 logwarnx("drained %zu messages", rcnt
);
1209 /* Work out the current interfaces. */
1210 ifaces
= if_discover(ctx
, &ifaddrs
, ctx
->ifc
, ctx
->ifv
);
1211 if (ifaces
== NULL
) {
1216 /* Punt departed interfaces */
1217 TAILQ_FOREACH_SAFE(ifp
, ctx
->ifaces
, next
, ifn
) {
1218 if (if_find(ifaces
, ifp
->name
) != NULL
)
1220 dhcpcd_handleinterface(ctx
, -1, ifp
->name
);
1223 /* Add new interfaces */
1224 while ((ifp
= TAILQ_FIRST(ifaces
)) != NULL
) {
1225 TAILQ_REMOVE(ifaces
, ifp
, next
);
1226 ifp1
= if_find(ctx
->ifaces
, ifp
->name
);
1228 /* If the interface already exists,
1229 * check carrier state.
1230 * dhcpcd_checkcarrier will free ifp. */
1231 eloop_timeout_add_sec(ctx
->eloop
, 0,
1232 dhcpcd_checkcarrier
, ifp
);
1235 TAILQ_INSERT_TAIL(ctx
->ifaces
, ifp
, next
);
1237 dhcpcd_initstate(ifp
, 0);
1238 eloop_timeout_add_sec(ctx
->eloop
, 0,
1239 dhcpcd_runprestartinterface
, ifp
);
1244 /* Update address state. */
1245 if_markaddrsstale(ctx
->ifaces
);
1246 if_learnaddrs(ctx
, ctx
->ifaces
, &ifaddrs
);
1247 if_deletestaleaddrs(ctx
->ifaces
);
1248 if_freeifaddrs(ctx
, &ifaddrs
);
1252 dhcpcd_handlehwaddr(struct interface
*ifp
,
1253 uint16_t hwtype
, const void *hwaddr
, uint8_t hwlen
)
1255 char buf
[sizeof(ifp
->hwaddr
) * 3];
1257 if (hwaddr
== NULL
|| !if_valid_hwaddr(hwaddr
, hwlen
))
1260 if (hwlen
> sizeof(ifp
->hwaddr
)) {
1262 logerr("%s: %s", __func__
, ifp
->name
);
1266 if (ifp
->hwtype
!= hwtype
) {
1268 loginfox("%s: hardware address type changed"
1269 " from %d to %d", ifp
->name
, ifp
->hwtype
, hwtype
);
1270 ifp
->hwtype
= hwtype
;
1273 if (ifp
->hwlen
== hwlen
&&
1274 (hwlen
== 0 || memcmp(ifp
->hwaddr
, hwaddr
, hwlen
) == 0))
1278 loginfox("%s: old hardware address: %s", ifp
->name
,
1279 hwaddr_ntoa(ifp
->hwaddr
, ifp
->hwlen
, buf
, sizeof(buf
)));
1280 loginfox("%s: new hardware address: %s", ifp
->name
,
1281 hwaddr_ntoa(hwaddr
, hwlen
, buf
, sizeof(buf
)));
1285 memcpy(ifp
->hwaddr
, hwaddr
, hwlen
);
1289 if_reboot(struct interface
*ifp
, int argc
, char **argv
)
1292 unsigned long long oldopts
;
1294 oldopts
= ifp
->options
->options
;
1296 script_runreason(ifp
, "RECONFIGURE");
1297 dhcpcd_initstate1(ifp
, argc
, argv
, 0);
1299 dhcp_reboot_newopts(ifp
, oldopts
);
1304 dhcpcd_prestartinterface(ifp
);
1308 reload_config(struct dhcpcd_ctx
*ctx
)
1310 struct if_options
*ifo
;
1313 if ((ifo
= read_config(ctx
, NULL
, NULL
, NULL
)) == NULL
)
1315 add_options(ctx
, NULL
, ifo
, ctx
->argc
, ctx
->argv
);
1316 /* We need to preserve these options. */
1317 if (ctx
->options
& DHCPCD_STARTED
)
1318 ifo
->options
|= DHCPCD_STARTED
;
1319 if (ctx
->options
& DHCPCD_MANAGER
)
1320 ifo
->options
|= DHCPCD_MANAGER
;
1321 if (ctx
->options
& DHCPCD_DAEMONISED
)
1322 ifo
->options
|= DHCPCD_DAEMONISED
;
1323 if (ctx
->options
& DHCPCD_PRIVSEP
)
1324 ifo
->options
|= DHCPCD_PRIVSEP
;
1325 ctx
->options
= ifo
->options
;
1326 free_options(ctx
, ifo
);
1330 reconf_reboot(struct dhcpcd_ctx
*ctx
, int action
, int argc
, char **argv
, int oi
)
1333 struct interface
*ifp
;
1335 TAILQ_FOREACH(ifp
, ctx
->ifaces
, next
) {
1336 for (i
= oi
; i
< argc
; i
++) {
1337 if (strcmp(ifp
->name
, argv
[i
]) == 0)
1340 if (oi
!= argc
&& i
== argc
)
1342 if (ifp
->active
== IF_ACTIVE_USER
) {
1344 if_reboot(ifp
, argc
, argv
);
1347 ipv4_applyaddr(ifp
);
1349 } else if (i
!= argc
) {
1350 ifp
->active
= IF_ACTIVE_USER
;
1351 dhcpcd_initstate1(ifp
, argc
, argv
, 0);
1353 dhcpcd_prestartinterface(ifp
);
1359 stop_all_interfaces(struct dhcpcd_ctx
*ctx
, unsigned long long opts
)
1361 struct interface
*ifp
;
1363 ctx
->options
|= DHCPCD_EXITING
;
1364 if (ctx
->ifaces
== NULL
)
1367 /* Drop the last interface first */
1368 TAILQ_FOREACH_REVERSE(ifp
, ctx
->ifaces
, if_head
, next
) {
1371 ifp
->options
->options
|= opts
;
1372 if (ifp
->options
->options
& DHCPCD_RELEASE
)
1373 ifp
->options
->options
&= ~DHCPCD_PERSISTENT
;
1374 ifp
->options
->options
|= DHCPCD_EXITING
;
1375 stop_interface(ifp
, NULL
);
1380 dhcpcd_ifrenew(struct interface
*ifp
)
1386 if (ifp
->options
->options
& DHCPCD_LINK
&& !if_is_link_up(ifp
))
1393 #define DHCPCD_RARENEW (DHCPCD_IPV6 | DHCPCD_IPV6RS)
1394 if ((ifp
->options
->options
& DHCPCD_RARENEW
) == DHCPCD_RARENEW
)
1395 ipv6nd_startrs(ifp
);
1403 dhcpcd_renew(struct dhcpcd_ctx
*ctx
)
1405 struct interface
*ifp
;
1407 TAILQ_FOREACH(ifp
, ctx
->ifaces
, next
) {
1408 dhcpcd_ifrenew(ifp
);
1413 #define sigmsg "received %s, %s"
1415 dhcpcd_signal_cb(int sig
, void *arg
)
1417 struct dhcpcd_ctx
*ctx
= arg
;
1418 unsigned long long opts
;
1421 if (ctx
->options
& DHCPCD_DUMPLEASE
) {
1422 eloop_exit(ctx
->eloop
, EXIT_FAILURE
);
1426 if (sig
!= SIGCHLD
&& ctx
->options
& DHCPCD_FORKED
) {
1427 if (sig
!= SIGHUP
&&
1428 write(ctx
->fork_fd
, &sig
, sizeof(sig
)) == -1)
1429 logerr("%s: write", __func__
);
1434 exit_code
= EXIT_FAILURE
;
1437 loginfox(sigmsg
, "SIGINT", "stopping");
1440 loginfox(sigmsg
, "SIGTERM", "stopping");
1441 exit_code
= EXIT_SUCCESS
;
1444 loginfox(sigmsg
, "SIGALRM", "releasing");
1445 opts
|= DHCPCD_RELEASE
;
1446 exit_code
= EXIT_SUCCESS
;
1449 loginfox(sigmsg
, "SIGHUP", "rebinding");
1451 /* Preserve any options passed on the commandline
1452 * when we were started. */
1453 reconf_reboot(ctx
, 1, ctx
->argc
, ctx
->argv
,
1454 ctx
->argc
- ctx
->ifc
);
1457 loginfox(sigmsg
, "SIGUSR1", "renewing");
1461 loginfox(sigmsg
, "SIGUSR2", "reopening log");
1463 if (IN_PRIVSEP(ctx
)) {
1464 if (ps_root_logreopen(ctx
) == -1)
1465 logerr("ps_root_logreopen");
1469 if (logopen(ctx
->logfile
) == -1)
1474 ps_root_signalcb(sig
, ctx
);
1476 while (waitpid(-1, NULL
, WNOHANG
) > 0)
1481 logerrx("received signal %d but don't know what to do with it",
1486 if (!(ctx
->options
& DHCPCD_TEST
))
1487 stop_all_interfaces(ctx
, opts
);
1488 eloop_exit(ctx
->eloop
, exit_code
);
1493 dhcpcd_handleargs(struct dhcpcd_ctx
*ctx
, struct fd_list
*fd
,
1494 int argc
, char **argv
)
1496 struct interface
*ifp
;
1497 unsigned long long opts
;
1498 int opt
, oi
, do_reboot
, do_renew
, af
= AF_UNSPEC
;
1499 size_t len
, l
, nifaces
;
1502 /* Special commands for our control socket
1503 * as the other end should be blocking until it gets the
1504 * expected reply we should be safely able just to change the
1505 * write callback on the fd */
1506 /* Make any change here in privsep-control.c as well. */
1507 if (strcmp(*argv
, "--version") == 0) {
1508 return control_queue(fd
, UNCONST(VERSION
),
1509 strlen(VERSION
) + 1);
1510 } else if (strcmp(*argv
, "--getconfigfile") == 0) {
1511 return control_queue(fd
, UNCONST(fd
->ctx
->cffile
),
1512 strlen(fd
->ctx
->cffile
) + 1);
1513 } else if (strcmp(*argv
, "--getinterfaces") == 0) {
1516 } else if (strcmp(*argv
, "--listen") == 0) {
1517 fd
->flags
|= FD_LISTEN
;
1521 /* Log the command */
1523 for (opt
= 0; opt
< argc
; opt
++)
1524 len
+= strlen(argv
[opt
]) + 1;
1529 for (opt
= 0; opt
< argc
; opt
++) {
1530 l
= strlen(argv
[opt
]);
1531 strlcpy(p
, argv
[opt
], len
);
1537 loginfox("control command: %s", tmp
);
1543 do_reboot
= do_renew
= 0;
1544 while ((opt
= getopt_long(argc
, argv
, IF_OPTS
, cf_options
, &oi
)) != -1)
1548 /* Assumed if below not set */
1551 opts
|= DHCPCD_RELEASE
;
1557 opts
|= DHCPCD_PERSISTENT
;
1560 opts
|= DHCPCD_EXITING
;
1566 opts
|= DHCPCD_DUMPLEASE
;
1577 if (opts
& DHCPCD_DUMPLEASE
) {
1578 ctx
->options
|= DHCPCD_DUMPLEASE
;
1581 TAILQ_FOREACH(ifp
, ctx
->ifaces
, next
) {
1584 for (oi
= optind
; oi
< argc
; oi
++) {
1585 if (strcmp(ifp
->name
, argv
[oi
]) == 0)
1588 if (optind
== argc
|| oi
< argc
) {
1589 opt
= send_interface(NULL
, ifp
, af
);
1592 nifaces
+= (size_t)opt
;
1595 if (write(fd
->fd
, &nifaces
, sizeof(nifaces
)) != sizeof(nifaces
))
1597 TAILQ_FOREACH(ifp
, ctx
->ifaces
, next
) {
1600 for (oi
= optind
; oi
< argc
; oi
++) {
1601 if (strcmp(ifp
->name
, argv
[oi
]) == 0)
1604 if (optind
== argc
|| oi
< argc
) {
1605 if (send_interface(fd
, ifp
, af
) == -1)
1609 ctx
->options
&= ~DHCPCD_DUMPLEASE
;
1612 ctx
->options
&= ~DHCPCD_DUMPLEASE
;
1616 /* Only privileged users can control dhcpcd via the socket. */
1617 if (fd
->flags
& FD_UNPRIV
) {
1622 if (opts
& (DHCPCD_EXITING
| DHCPCD_RELEASE
)) {
1623 if (optind
== argc
) {
1624 stop_all_interfaces(ctx
, opts
);
1625 eloop_exit(ctx
->eloop
, EXIT_SUCCESS
);
1628 for (oi
= optind
; oi
< argc
; oi
++) {
1629 if ((ifp
= if_find(ctx
->ifaces
, argv
[oi
])) == NULL
)
1633 ifp
->options
->options
|= opts
;
1634 if (opts
& DHCPCD_RELEASE
)
1635 ifp
->options
->options
&= ~DHCPCD_PERSISTENT
;
1636 stop_interface(ifp
, NULL
);
1642 if (optind
== argc
) {
1646 for (oi
= optind
; oi
< argc
; oi
++) {
1647 if ((ifp
= if_find(ctx
->ifaces
, argv
[oi
])) == NULL
)
1649 dhcpcd_ifrenew(ifp
);
1655 /* XXX: Respect initial commandline options? */
1656 reconf_reboot(ctx
, do_reboot
, argc
, argv
, optind
- 1);
1660 static void dhcpcd_readdump1(void *, unsigned short);
1663 dhcpcd_readdump2(void *arg
, unsigned short events
)
1665 struct dhcpcd_ctx
*ctx
= arg
;
1667 int exit_code
= EXIT_FAILURE
;
1669 if (events
!= ELE_READ
)
1670 logerrx("%s: unexpected event 0x%04x", __func__
, events
);
1672 len
= read(ctx
->control_fd
, ctx
->ctl_buf
+ ctx
->ctl_bufpos
,
1673 ctx
->ctl_buflen
- ctx
->ctl_bufpos
);
1677 } else if (len
== 0)
1679 if ((size_t)len
+ ctx
->ctl_bufpos
!= ctx
->ctl_buflen
) {
1680 ctx
->ctl_bufpos
+= (size_t)len
;
1684 if (ctx
->ctl_buf
[ctx
->ctl_buflen
- 1] != '\0') /* unlikely */
1685 ctx
->ctl_buf
[ctx
->ctl_buflen
- 1] = '\0';
1686 script_dump(ctx
->ctl_buf
, ctx
->ctl_buflen
);
1688 if (--ctx
->ctl_extra
!= 0) {
1690 if (eloop_event_add(ctx
->eloop
, ctx
->control_fd
, ELE_READ
,
1691 dhcpcd_readdump1
, ctx
) == -1)
1692 logerr("%s: eloop_event_add", __func__
);
1695 exit_code
= EXIT_SUCCESS
;
1698 shutdown(ctx
->control_fd
, SHUT_RDWR
);
1699 eloop_exit(ctx
->eloop
, exit_code
);
1703 dhcpcd_readdump1(void *arg
, unsigned short events
)
1705 struct dhcpcd_ctx
*ctx
= arg
;
1708 if (events
!= ELE_READ
)
1709 logerrx("%s: unexpected event 0x%04x", __func__
, events
);
1711 len
= read(ctx
->control_fd
, &ctx
->ctl_buflen
, sizeof(ctx
->ctl_buflen
));
1712 if (len
!= sizeof(ctx
->ctl_buflen
)) {
1717 if (ctx
->ctl_buflen
> SSIZE_MAX
) {
1723 ctx
->ctl_buf
= malloc(ctx
->ctl_buflen
);
1724 if (ctx
->ctl_buf
== NULL
)
1727 ctx
->ctl_bufpos
= 0;
1728 if (eloop_event_add(ctx
->eloop
, ctx
->control_fd
, ELE_READ
,
1729 dhcpcd_readdump2
, ctx
) == -1)
1730 logerr("%s: eloop_event_add", __func__
);
1735 eloop_exit(ctx
->eloop
, EXIT_FAILURE
);
1739 dhcpcd_readdump0(void *arg
, unsigned short events
)
1741 struct dhcpcd_ctx
*ctx
= arg
;
1744 if (events
!= ELE_READ
)
1745 logerrx("%s: unexpected event 0x%04x", __func__
, events
);
1747 len
= read(ctx
->control_fd
, &ctx
->ctl_extra
, sizeof(ctx
->ctl_extra
));
1748 if (len
!= sizeof(ctx
->ctl_extra
)) {
1752 eloop_exit(ctx
->eloop
, EXIT_FAILURE
);
1756 if (ctx
->ctl_extra
== 0) {
1757 eloop_exit(ctx
->eloop
, EXIT_SUCCESS
);
1761 if (eloop_event_add(ctx
->eloop
, ctx
->control_fd
, ELE_READ
,
1762 dhcpcd_readdump1
, ctx
) == -1)
1763 logerr("%s: eloop_event_add", __func__
);
1767 dhcpcd_readdumptimeout(void *arg
)
1769 struct dhcpcd_ctx
*ctx
= arg
;
1772 eloop_exit(ctx
->eloop
, EXIT_FAILURE
);
1776 dhcpcd_readdump(struct dhcpcd_ctx
*ctx
)
1779 ctx
->options
|= DHCPCD_FORKED
;
1780 if (eloop_timeout_add_sec(ctx
->eloop
, 5,
1781 dhcpcd_readdumptimeout
, ctx
) == -1)
1783 return eloop_event_add(ctx
->eloop
, ctx
->control_fd
, ELE_READ
,
1784 dhcpcd_readdump0
, ctx
);
1788 dhcpcd_fork_cb(void *arg
, unsigned short events
)
1790 struct dhcpcd_ctx
*ctx
= arg
;
1794 if (!(events
& ELE_READ
))
1795 logerrx("%s: unexpected event 0x%04x", __func__
, events
);
1797 len
= read(ctx
->fork_fd
, &exit_code
, sizeof(exit_code
));
1800 exit_code
= EXIT_FAILURE
;
1801 } else if ((size_t)len
< sizeof(exit_code
)) {
1802 logerrx("%s: truncated read %zd (expected %zu)",
1803 __func__
, len
, sizeof(exit_code
));
1804 exit_code
= EXIT_FAILURE
;
1806 if (ctx
->options
& DHCPCD_FORKED
)
1807 eloop_exit(ctx
->eloop
, exit_code
);
1809 dhcpcd_signal_cb(exit_code
, ctx
);
1813 dhcpcd_stderr_cb(void *arg
, unsigned short events
)
1815 struct dhcpcd_ctx
*ctx
= arg
;
1819 if (events
& ELE_HANGUP
)
1820 eloop_exit(ctx
->eloop
, EXIT_SUCCESS
);
1822 if (!(events
& ELE_READ
))
1825 len
= read(ctx
->stderr_fd
, log
, sizeof(log
));
1827 if (errno
!= ECONNRESET
)
1833 fprintf(stderr
, "%s", log
);
1837 dhcpcd_pidfile_timeout(void *arg
)
1839 struct dhcpcd_ctx
*ctx
= arg
;
1842 pid
= pidfile_read(ctx
->pidfile
);
1845 eloop_exit(ctx
->eloop
, EXIT_SUCCESS
);
1846 else if (++ctx
->duid_len
>= 100) { /* overload duid_len */
1847 logerrx("pid %d failed to exit", pid
);
1848 eloop_exit(ctx
->eloop
, EXIT_FAILURE
);
1850 eloop_timeout_add_msec(ctx
->eloop
, 100,
1851 dhcpcd_pidfile_timeout
, ctx
);
1855 main(int argc
, char **argv
, char **envp
)
1857 struct dhcpcd_ctx ctx
;
1858 struct ifaddrs
*ifaddrs
= NULL
;
1859 struct if_options
*ifo
;
1860 struct interface
*ifp
;
1861 sa_family_t family
= AF_UNSPEC
;
1863 unsigned int logopts
, t
;
1865 #if defined(USE_SIGNALS) || !defined(THERE_IS_NO_FORK)
1867 int fork_fd
[2], stderr_fd
[2];
1871 const char *siga
= NULL
;
1875 #ifdef SETPROCTITLE_H
1876 setproctitle_init(argc
, argv
, envp
);
1881 /* Test for --help and --version */
1883 if (strcmp(argv
[1], "--help") == 0) {
1885 return EXIT_SUCCESS
;
1886 } else if (strcmp(argv
[1], "--version") == 0) {
1887 printf(""PACKAGE
" "VERSION
"\n%s\n", dhcpcd_copyright
);
1888 printf("Compiled in features:"
1914 return EXIT_SUCCESS
;
1918 memset(&ctx
, 0, sizeof(ctx
));
1921 ctx
.cffile
= CONFIG
;
1922 ctx
.script
= UNCONST(dhcpcd_default_script
);
1923 ctx
.control_fd
= ctx
.control_unpriv_fd
= ctx
.link_fd
= -1;
1924 ctx
.pf_inet_fd
= -1;
1926 ctx
.pf_link_fd
= -1;
1929 TAILQ_INIT(&ctx
.control_fds
);
1940 #if defined(INET6) && !defined(__sun)
1949 TAILQ_INIT(&ctx
.ps_processes
);
1952 /* Check our streams for validity */
1953 ctx
.stdin_valid
= fcntl(STDIN_FILENO
, F_GETFD
) != -1;
1954 ctx
.stdout_valid
= fcntl(STDOUT_FILENO
, F_GETFD
) != -1;
1955 ctx
.stderr_valid
= fcntl(STDERR_FILENO
, F_GETFD
) != -1;
1957 logopts
= LOGERR_LOG
| LOGERR_LOG_DATE
| LOGERR_LOG_PID
;
1958 if (ctx
.stderr_valid
)
1959 logopts
|= LOGERR_ERR
;
1963 while ((opt
= getopt_long(argc
, argv
,
1964 ctx
.options
& DHCPCD_PRINT_PIDFILE
? NOERR_IF_OPTS
: IF_OPTS
,
1965 cf_options
, &oi
)) != -1)
1975 ctx
.cffile
= optarg
;
1979 ctx
.logfile
= strdup(optarg
);
1991 /* -qq disables console output entirely.
1992 * This is important for systemd because it logs
1993 * both console AND syslog to the same log
1994 * resulting in untold confusion. */
1995 if (logopts
& LOGERR_QUIET
)
1996 logopts
&= ~LOGERR_ERR
;
1998 logopts
|= LOGERR_QUIET
;
2010 ctx
.options
|= DHCPCD_PRINT_PIDFILE
;
2011 logopts
&= ~(LOGERR_LOG
| LOGERR_ERR
);
2015 logopts
&= ~LOGERR_LOG
;
2024 if (ctx
.options
& DHCPCD_PRINT_PIDFILE
)
2031 if (optind
!= argc
- 1)
2032 ctx
.options
|= DHCPCD_MANAGER
;
2034 logsetopts(logopts
);
2035 logopen(ctx
.logfile
);
2039 ctx
.ifc
= argc
- optind
;
2040 ctx
.ifv
= argv
+ optind
;
2044 ifo
= read_config(&ctx
, NULL
, NULL
, NULL
);
2046 if (ctx
.options
& DHCPCD_PRINT_PIDFILE
)
2051 opt
= add_options(&ctx
, NULL
, ifo
, argc
, argv
);
2053 if (ctx
.options
& DHCPCD_PRINT_PIDFILE
)
2060 printf("Interface options:\n");
2061 if (optind
== argc
- 1) {
2062 free_options(&ctx
, ifo
);
2063 ifo
= read_config(&ctx
, argv
[optind
], NULL
, NULL
);
2066 add_options(&ctx
, NULL
, ifo
, argc
, argv
);
2070 if (family
== 0 || family
== AF_INET
) {
2071 printf("\nDHCPv4 options:\n");
2072 dhcp_printoptions(&ctx
,
2073 ifo
->dhcp_override
, ifo
->dhcp_override_len
);
2077 if (family
== 0 || family
== AF_INET6
) {
2078 printf("\nND options:\n");
2079 ipv6nd_printoptions(&ctx
,
2080 ifo
->nd_override
, ifo
->nd_override_len
);
2082 printf("\nDHCPv6 options:\n");
2083 dhcp6_printoptions(&ctx
,
2084 ifo
->dhcp6_override
, ifo
->dhcp6_override_len
);
2090 ctx
.options
|= ifo
->options
;
2092 if (i
== 1 || i
== 3) {
2094 ctx
.options
|= DHCPCD_TEST
;
2096 ctx
.options
|= DHCPCD_DUMPLEASE
;
2097 ctx
.options
|= DHCPCD_PERSISTENT
;
2098 ctx
.options
&= ~DHCPCD_DAEMONISE
;
2101 #ifdef THERE_IS_NO_FORK
2102 ctx
.options
&= ~DHCPCD_DAEMONISE
;
2105 if (ctx
.options
& DHCPCD_DEBUG
)
2106 logsetopts(logopts
| LOGERR_DEBUG
);
2108 if (!(ctx
.options
& (DHCPCD_TEST
| DHCPCD_DUMPLEASE
))) {
2110 /* If we have any other args, we should run as a single dhcpcd
2111 * instance for that interface. */
2112 if (optind
== argc
- 1 && !(ctx
.options
& DHCPCD_MANAGER
)) {
2117 if (ifname
== NULL
|| strlen(ifname
) > IF_NAMESIZE
) {
2118 errno
= ifname
== NULL
? EINVAL
: E2BIG
;
2119 logerr("%s: ", ifname
);
2122 /* Allow a dhcpcd interface per address family */
2133 snprintf(ctx
.pidfile
, sizeof(ctx
.pidfile
),
2134 PIDFILE
, ifname
, per
, ".");
2136 snprintf(ctx
.pidfile
, sizeof(ctx
.pidfile
),
2137 PIDFILE
, "", "", "");
2138 ctx
.options
|= DHCPCD_MANAGER
;
2141 * If we are given any interfaces, we
2142 * cannot send a signal as that would impact
2148 if (ctx
.options
& DHCPCD_PRINT_PIDFILE
) {
2149 printf("%s\n", ctx
.pidfile
);
2154 if (chdir("/") == -1)
2155 logerr("%s: chdir: /", __func__
);
2157 /* Freeing allocated addresses from dumping leases can trigger
2158 * eloop removals as well, so init here. */
2159 if ((ctx
.eloop
= eloop_new()) == NULL
) {
2160 logerr("%s: eloop_init", __func__
);
2165 for (si
= 0; si
< dhcpcd_signals_ignore_len
; si
++)
2166 signal(dhcpcd_signals_ignore
[si
], SIG_IGN
);
2168 /* Save signal mask, block and redirect signals to our handler */
2169 eloop_signal_set_cb(ctx
.eloop
,
2170 dhcpcd_signals
, dhcpcd_signals_len
,
2171 dhcpcd_signal_cb
, &ctx
);
2172 if (eloop_signal_mask(ctx
.eloop
, &ctx
.sigset
) == -1) {
2173 logerr("%s: eloop_signal_mask", __func__
);
2178 pid
= pidfile_read(ctx
.pidfile
);
2179 if (pid
!= 0 && pid
!= -1)
2180 loginfox("sending signal %s to pid %d", siga
, pid
);
2181 if (pid
== 0 || pid
== -1 || kill(pid
, sig
) != 0) {
2182 if (pid
!= 0 && pid
!= -1 && errno
!= ESRCH
) {
2186 unlink(ctx
.pidfile
);
2187 /* We can still continue and send the command
2188 * via the control socket. */
2190 if (sig
== SIGHUP
|| sig
== SIGUSR1
)
2192 /* Spin until it exits */
2193 loginfox("waiting for pid %d to exit", pid
);
2194 dhcpcd_pidfile_timeout(&ctx
);
2205 if (ctx
.options
& DHCPCD_DUMPLEASE
&&
2206 ioctl(fileno(stdin
), FIONREAD
, &i
, sizeof(i
)) == 0 &&
2209 ctx
.options
|= DHCPCD_FORKED
; /* pretend child process */
2211 if (IN_PRIVSEP(&ctx
) && ps_managersandbox(&ctx
, NULL
) == -1)
2214 ifp
= calloc(1, sizeof(*ifp
));
2224 if (dhcp_dump(ifp
) == -1)
2228 logerrx("No DHCP support");
2233 if (dhcp6_dump(ifp
) == -1)
2237 logerrx("No DHCP6 support");
2241 logerrx("Family not specified. Please use -4 or -6.");
2248 /* Try and contact the manager process to send the instruction. */
2249 if (!(ctx
.options
& DHCPCD_TEST
)) {
2250 ctx
.options
|= DHCPCD_FORKED
; /* avoid socket unlink */
2251 if (!(ctx
.options
& DHCPCD_MANAGER
))
2252 ctx
.control_fd
= control_open(argv
[optind
], family
,
2253 ctx
.options
& DHCPCD_DUMPLEASE
);
2254 if (!(ctx
.options
& DHCPCD_MANAGER
) && ctx
.control_fd
== -1)
2255 ctx
.control_fd
= control_open(argv
[optind
], AF_UNSPEC
,
2256 ctx
.options
& DHCPCD_DUMPLEASE
);
2257 if (ctx
.control_fd
== -1)
2258 ctx
.control_fd
= control_open(NULL
, AF_UNSPEC
,
2259 ctx
.options
& DHCPCD_DUMPLEASE
);
2260 if (ctx
.control_fd
!= -1) {
2262 if (IN_PRIVSEP(&ctx
) &&
2263 ps_managersandbox(&ctx
, NULL
) == -1)
2266 if (!(ctx
.options
& DHCPCD_DUMPLEASE
))
2267 loginfox("sending commands to dhcpcd process");
2268 len
= control_send(&ctx
, argc
, argv
);
2270 logdebugx("send OK");
2272 logerr("%s: control_send", __func__
);
2275 if (ctx
.options
& DHCPCD_DUMPLEASE
) {
2276 if (dhcpcd_readdump(&ctx
) == -1) {
2277 logerr("%s: dhcpcd_readdump", __func__
);
2284 if (errno
!= ENOENT
)
2285 logerr("%s: control_open", __func__
);
2286 /* If asking dhcpcd to exit and we failed to
2287 * send a signal or a message then we
2288 * don't proceed past here. */
2289 if (ctx
.options
& DHCPCD_DUMPLEASE
||
2290 sig
== SIGTERM
|| sig
== SIGALRM
)
2292 if (errno
== ENOENT
)
2293 logerrx(PACKAGE
" is not running");
2296 if (errno
== EPERM
|| errno
== EACCES
)
2299 ctx
.options
&= ~DHCPCD_FORKED
;
2302 if (!(ctx
.options
& DHCPCD_TEST
)) {
2303 /* Ensure we have the needed directories */
2304 if (mkdir(DBDIR
, 0750) == -1 && errno
!= EEXIST
)
2305 logerr("%s: mkdir: %s", __func__
, DBDIR
);
2306 if (mkdir(RUNDIR
, 0755) == -1 && errno
!= EEXIST
)
2307 logerr("%s: mkdir: %s", __func__
, RUNDIR
);
2308 if ((pid
= pidfile_lock(ctx
.pidfile
)) != 0) {
2310 logerr("%s: pidfile_lock: %s",
2311 __func__
, ctx
.pidfile
);
2314 " already running on pid %d (%s)",
2320 loginfox(PACKAGE
"-" VERSION
" starting");
2321 if (ctx
.stdin_valid
&& freopen(_PATH_DEVNULL
, "w", stdin
) == NULL
)
2322 logwarn("freopen stdin");
2324 #if defined(USE_SIGNALS) && !defined(THERE_IS_NO_FORK)
2325 if (!(ctx
.options
& DHCPCD_DAEMONISE
))
2328 if (xsocketpair(AF_UNIX
, SOCK_SEQPACKET
|SOCK_CXNB
, 0, fork_fd
) == -1 ||
2329 (ctx
.stderr_valid
&&
2330 xsocketpair(AF_UNIX
, SOCK_SEQPACKET
|SOCK_CXNB
, 0, stderr_fd
) == -1))
2332 logerr("socketpair");
2335 switch (pid
= fork()) {
2340 ctx
.fork_fd
= fork_fd
[1];
2342 #ifdef PRIVSEP_RIGHTS
2343 if (ps_rights_limit_fd(ctx
.fork_fd
) == -1) {
2344 logerr("ps_rights_limit_fdpair");
2348 if (eloop_event_add(ctx
.eloop
, ctx
.fork_fd
, ELE_READ
,
2349 dhcpcd_fork_cb
, &ctx
) == -1)
2350 logerr("%s: eloop_event_add", __func__
);
2353 * Redirect stderr to the stderr socketpair.
2354 * Redirect stdout as well.
2355 * dhcpcd doesn't output via stdout, but something in
2356 * a called script might.
2358 if (ctx
.stderr_valid
) {
2359 if (dup2(stderr_fd
[1], STDERR_FILENO
) == -1 ||
2360 (ctx
.stdout_valid
&&
2361 dup2(stderr_fd
[1], STDOUT_FILENO
) == -1))
2363 close(stderr_fd
[0]);
2364 close(stderr_fd
[1]);
2365 } else if (ctx
.stdout_valid
) {
2366 if (freopen(_PATH_DEVNULL
, "w", stdout
) == NULL
)
2367 logerr("freopen stdout");
2369 if (setsid() == -1) {
2370 logerr("%s: setsid", __func__
);
2373 /* Ensure we can never get a controlling terminal */
2374 switch (pid
= fork()) {
2379 eloop_forked(ctx
.eloop
);
2382 ctx
.options
|= DHCPCD_FORKED
; /* A lie */
2388 setproctitle("[launcher]");
2389 ctx
.options
|= DHCPCD_FORKED
| DHCPCD_LAUNCHER
;
2390 ctx
.fork_fd
= fork_fd
[0];
2392 #ifdef PRIVSEP_RIGHTS
2393 if (ps_rights_limit_fd(ctx
.fork_fd
) == -1) {
2394 logerr("ps_rights_limit_fd");
2398 if (eloop_event_add(ctx
.eloop
, ctx
.fork_fd
, ELE_READ
,
2399 dhcpcd_fork_cb
, &ctx
) == -1)
2400 logerr("%s: eloop_event_add", __func__
);
2402 if (ctx
.stderr_valid
) {
2403 ctx
.stderr_fd
= stderr_fd
[0];
2404 close(stderr_fd
[1]);
2405 #ifdef PRIVSEP_RIGHTS
2406 if (ps_rights_limit_fd(ctx
.stderr_fd
) == 1) {
2407 logerr("ps_rights_limit_fd");
2411 if (eloop_event_add(ctx
.eloop
, ctx
.stderr_fd
, ELE_READ
,
2412 dhcpcd_stderr_cb
, &ctx
) == -1)
2413 logerr("%s: eloop_event_add", __func__
);
2416 if (IN_PRIVSEP(&ctx
) && ps_managersandbox(&ctx
, NULL
) == -1)
2422 /* We have now forked, setsid, forked once more.
2423 * From this point on, we are the controlling daemon. */
2424 logdebugx("spawned manager process on PID %d", getpid());
2426 ctx
.options
|= DHCPCD_STARTED
;
2427 if ((pid
= pidfile_lock(ctx
.pidfile
)) != 0) {
2428 logerr("%s: pidfile_lock %d", __func__
, pid
);
2430 /* privsep has not started ... */
2431 ctx
.options
&= ~DHCPCD_PRIVSEP
;
2439 #if defined(BSD) && defined(INET6)
2440 /* Disable the kernel RTADV sysctl as early as possible. */
2441 if (ctx
.options
& DHCPCD_IPV6
&& ctx
.options
& DHCPCD_IPV6RS
)
2446 if (IN_PRIVSEP(&ctx
) && ps_start(&ctx
) == -1) {
2450 if (ctx
.options
& DHCPCD_FORKED
)
2454 if (!(ctx
.options
& DHCPCD_TEST
)) {
2455 if (control_start(&ctx
,
2456 ctx
.options
& DHCPCD_MANAGER
?
2457 NULL
: argv
[optind
], family
) == -1)
2459 logerr("%s: control_start", __func__
);
2465 /* Start any dev listening plugin which may want to
2466 * change the interface name provided by the kernel */
2467 if (!IN_PRIVSEP(&ctx
) &&
2468 (ctx
.options
& (DHCPCD_MANAGER
| DHCPCD_DEV
)) ==
2469 (DHCPCD_MANAGER
| DHCPCD_DEV
))
2470 dev_start(&ctx
, dhcpcd_handleinterface
);
2473 setproctitle("%s%s%s",
2474 ctx
.options
& DHCPCD_MANAGER
? "[manager]" : argv
[optind
],
2475 ctx
.options
& DHCPCD_IPV4
? " [ip4]" : "",
2476 ctx
.options
& DHCPCD_IPV6
? " [ip6]" : "");
2478 if (if_opensockets(&ctx
) == -1) {
2479 logerr("%s: if_opensockets", __func__
);
2483 dhcpcd_setlinkrcvbuf(&ctx
);
2486 /* Try and create DUID from the machine UUID. */
2487 dhcpcd_initduid(&ctx
, NULL
);
2489 /* Cache the default vendor option. */
2490 if (dhcp_vendor(ctx
.vendor
, sizeof(ctx
.vendor
)) == -1)
2491 logerr("dhcp_vendor");
2493 /* Start handling kernel messages for interfaces, addresses and
2495 if (eloop_event_add(ctx
.eloop
, ctx
.link_fd
, ELE_READ
,
2496 dhcpcd_handlelink
, &ctx
) == -1)
2497 logerr("%s: eloop_event_add", __func__
);
2500 if (IN_PRIVSEP(&ctx
) && ps_managersandbox(&ctx
, "stdio route") == -1)
2504 /* When running dhcpcd against a single interface, we need to retain
2505 * the old behaviour of waiting for an IP address */
2506 if (ctx
.ifc
== 1 && !(ctx
.options
& DHCPCD_BACKGROUND
))
2507 ctx
.options
|= DHCPCD_WAITIP
;
2509 ctx
.ifaces
= if_discover(&ctx
, &ifaddrs
, ctx
.ifc
, ctx
.ifv
);
2510 if (ctx
.ifaces
== NULL
) {
2511 logerr("%s: if_discover", __func__
);
2514 for (i
= 0; i
< ctx
.ifc
; i
++) {
2515 if ((ifp
= if_find(ctx
.ifaces
, ctx
.ifv
[i
])) == NULL
)
2516 logerrx("%s: interface not found",
2518 else if (!ifp
->active
)
2519 logerrx("%s: interface has an invalid configuration",
2522 TAILQ_FOREACH(ifp
, ctx
.ifaces
, next
) {
2523 if (ifp
->active
== IF_ACTIVE_USER
)
2530 loglevel
= ctx
.options
& DHCPCD_INACTIVE
?
2531 LOG_DEBUG
: LOG_ERR
;
2532 logmessage(loglevel
, "no valid interfaces found");
2533 dhcpcd_daemonise(&ctx
);
2536 if (!(ctx
.options
& DHCPCD_LINK
)) {
2537 logerrx("aborting as link detection is disabled");
2542 TAILQ_FOREACH(ifp
, ctx
.ifaces
, next
) {
2544 dhcpcd_initstate1(ifp
, argc
, argv
, 0);
2546 if_learnaddrs(&ctx
, ctx
.ifaces
, &ifaddrs
);
2547 if_freeifaddrs(&ctx
, &ifaddrs
);
2550 if (ctx
.options
& DHCPCD_BACKGROUND
)
2551 dhcpcd_daemonise(&ctx
);
2554 TAILQ_FOREACH(ifp
, ctx
.ifaces
, next
) {
2557 if (if_is_link_up(ifp
))
2562 if (!(ctx
.options
& DHCPCD_BACKGROUND
)) {
2563 if (ctx
.options
& DHCPCD_MANAGER
)
2567 TAILQ_FOREACH(ifp
, ctx
.ifaces
, next
) {
2569 t
= ifp
->options
->timeout
;
2575 ctx
.options
& DHCPCD_LINK
&&
2576 !(ctx
.options
& DHCPCD_WAITIP
))
2580 loglevel
= ctx
.options
& DHCPCD_INACTIVE
?
2581 LOG_DEBUG
: LOG_WARNING
;
2582 logmessage(loglevel
, "no interfaces have a carrier");
2583 dhcpcd_daemonise(&ctx
);
2585 /* Test mode removes the daemonise bit, so check for both */
2586 ctx
.options
& (DHCPCD_DAEMONISE
| DHCPCD_TEST
))
2588 eloop_timeout_add_sec(ctx
.eloop
, t
,
2589 handle_exit_timeout
, &ctx
);
2592 free_options(&ctx
, ifo
);
2595 TAILQ_FOREACH(ifp
, ctx
.ifaces
, next
) {
2597 eloop_timeout_add_sec(ctx
.eloop
, 0,
2598 dhcpcd_prestartinterface
, ifp
);
2602 i
= eloop_start(ctx
.eloop
, &ctx
.sigset
);
2604 logerr("%s: eloop_start", __func__
);
2617 if (!(ctx
.options
& DHCPCD_TEST
) && control_stop(&ctx
) == -1)
2618 logerr("%s: control_stop", __func__
);
2619 if_freeifaddrs(&ctx
, &ifaddrs
);
2623 /* Free memory and close fd's */
2625 while ((ifp
= TAILQ_FIRST(ctx
.ifaces
))) {
2626 TAILQ_REMOVE(ctx
.ifaces
, ifp
, next
);
2632 free_options(&ctx
, ifo
);
2633 #ifdef HAVE_OPEN_MEMSTREAM
2635 fclose(ctx
.script_fp
);
2637 free(ctx
.script_buf
);
2638 free(ctx
.script_env
);
2641 if (ctx
.link_fd
!= -1) {
2642 eloop_event_delete(ctx
.eloop
, ctx
.link_fd
);
2645 if_closesockets(&ctx
);
2653 if (ctx
.script
!= dhcpcd_default_script
)
2656 if (ps_stopwait(&ctx
) != EXIT_SUCCESS
)
2659 if (ctx
.options
& DHCPCD_STARTED
&& !(ctx
.options
& DHCPCD_FORKED
))
2660 loginfox(PACKAGE
" exited");
2662 if (ps_root_stop(&ctx
) == -1)
2664 eloop_free(ctx
.ps_eloop
);
2666 eloop_free(ctx
.eloop
);
2670 #ifdef SETPROCTITLE_H
2671 setproctitle_fini();
2674 if (ctx
.options
& DHCPCD_STARTED
) {
2675 /* Try to detach from the launch process. */
2676 if (ctx
.fork_fd
!= -1 &&
2677 write(ctx
.fork_fd
, &i
, sizeof(i
)) == -1)
2678 logerr("%s: write", __func__
);
2680 if (ctx
.options
& (DHCPCD_FORKED
| DHCPCD_PRIVSEP
))
2681 _exit(i
); /* so atexit won't remove our pidfile */