2 # dhcpcd client configuration script
4 # Handy variables and functions for our hooks to use
5 ifname="$interface${protocol+.}$protocol"
7 signature_base="# Generated by dhcpcd"
8 signature="$signature_base $from $ifname"
9 signature_base_end="# End of dhcpcd"
10 signature_end="$signature_base_end $from $ifname"
11 state_dir=@RUNDIR@/hook-state
16 : ${syslog_debug:=false}
18 # Ensure that all arguments are unique
25 *) result="$result${result:+ }$i";;
31 # List interface config files in a directory.
32 # If dhcpcd is running as a single instance then it will have a list of
33 # interfaces in the preferred order.
34 # Otherwise we just use what we have.
38 for i in $interface_order; do
39 for x in "$1"/$i.*; do
40 [ -f "$x" ] && ifaces="$ifaces${ifaces:+ }${x##*/}"
44 [ -f "$x" ] && ifaces="$ifaces${ifaces:+ }${x##*/}"
53 var=${var#"${var%%[![:space:]]*}"}
54 var=${var%"${var##*[![:space:]]}"}
55 if [ -z "$var" ]; then
56 # So it seems our shell doesn't support wctype(3) patterns
58 var=$(echo "$*" | sed -e 's/^[[:space:]]*//;s/[[:space:]]*$//')
63 # We normally use sed to extract values using a key from a list of files
64 # but sed may not always be available at the time.
70 if type sed >/dev/null 2>&1; then
71 sed -n "s/^$key//p" $@
76 "$key"*) echo "${line##$key}";;
83 # We normally use sed to remove markers from a configuration file
84 # but sed may not always be available at the time.
92 if type sed >/dev/null 2>&1; then
93 sed "/^$m1/,/^$m2/d" $@
100 *) [ $in_marker = 0 ] && echo "$line";;
110 [ -e "$1" ] && [ -e "$2" ] || return 1
112 if type cmp >/dev/null 2>&1; then
114 elif type diff >/dev/null 2>&1; then
115 diff -q "$1" "$2" >/dev/null
117 # Hopefully we're only working on small text files ...
118 [ "$(cat "$1")" = "$(cat "$2")" ]
123 # If different, replace first with second otherwise remove second.
127 if comp_file "$1" "$2"; then
138 # If different, copy or link depending on target type
142 [ "$(readlink "$2")" = "$1" ] && return 1
145 comp_file "$1" "$2" && return 1
154 rm -f "$1-pre.$interface"
155 cat "$1" > "$1-pre.$interface"
159 # Restore a config file
162 [ -f "$1-pre.$interface" ] || return 1
163 cat "$1-pre.$interface" > "$1"
164 rm -f "$1-pre.$interface"
167 # Write a syslog entry
172 if [ "$lvl" = debug ]; then
173 ${syslog_debug} || return 0
175 [ -n "$lvl" ] && shift
176 [ -n "$*" ] || return 0
178 err|error) echo "$interface: $*" >&2;;
179 *) echo "$interface: $*";;
181 if type logger >/dev/null 2>&1; then
182 logger -i -p daemon."$lvl" -t dhcpcd-run-hooks "$interface: $*"
186 # Check for a valid name as per RFC952 and RFC1123 section 2.1
190 [ -z "$name" ] || [ ${#name} -gt 255 ] && return 1
192 while [ -n "$name" ]; do
194 [ -z "$label" ] || [ ${#label} -gt 63 ] && return 1
196 -*|_*|*-|*_) return 1;;
197 *[![:alnum:]_-]*) return 1;;
205 valid_domainname_list()
208 valid_domainname "$name" || return $?
213 # With the advent of alternative init systems, it's possible to have
214 # more than one installed. So we need to try to guess what one we're
215 # using unless overridden by configure.
218 _service_exists="@SERVICEEXISTS@"
219 _service_cmd="@SERVICECMD@"
220 _service_status="@SERVICESTATUS@"
222 [ -n "$_service_cmd" ] && return 0
224 if $_detected_init; then
225 [ -n "$_service_cmd" ]
229 # Detect the running init system.
230 # As systemd and OpenRC can be installed on top of legacy init
231 # systems we try to detect them first.
234 if [ -x /bin/systemctl ] && [ -S /run/systemd/private ]; then
235 _service_exists="/bin/systemctl --quiet is-enabled \$1.service"
236 _service_status="/bin/systemctl --quiet is-active \$1.service"
237 _service_cmd="/bin/systemctl \$2 \$1.service"
238 elif [ -x /usr/bin/systemctl ] && [ -S /run/systemd/private ]; then
239 _service_exists="/usr/bin/systemctl --quiet is-enabled \$1.service"
240 _service_status="/usr/bin/systemctl --quiet is-active \$1.service"
241 _service_cmd="/usr/bin/systemctl \$2 \$1.service"
242 elif [ -x /sbin/rc-service ] &&
243 { [ -s /libexec/rc/init.d/softlevel ] ||
244 [ -s /run/openrc/softlevel ]; }
246 _service_exists="/sbin/rc-service -e \$1"
247 _service_cmd="/sbin/rc-service \$1 -- -D \$2"
248 elif [ -x /usr/sbin/invoke-rc.d ]; then
249 _service_exists="/usr/sbin/invoke-rc.d --query --quiet \$1 start >/dev/null 2>&1 || [ \$? = 104 ]"
250 _service_cmd="/usr/sbin/invoke-rc.d \$1 \$2"
251 elif [ -x /sbin/service ]; then
252 _service_exists="/sbin/service \$1 >/dev/null 2>&1"
253 _service_cmd="/sbin/service \$1 \$2"
254 elif [ -x /usr/sbin/service ]; then
255 _service_exists="/usr/sbin/service \$1 $status >/dev/null 2>&1"
256 _service_cmd="/usr/sbin/service \$1 \$2"
257 elif [ -x /bin/sv ]; then
258 _service_exists="/bin/sv status \$1 >/dev/null 2>&1"
259 _service_cmd="/bin/sv \$2 \$1"
260 elif [ -x /usr/bin/sv ]; then
261 _service_exists="/usr/bin/sv status \$1 >/dev/null 2>&1"
262 _service_cmd="/usr/bin/sv \$2 \$1"
263 elif [ -e /etc/slackware-version ] && [ -d /etc/rc.d ]; then
264 _service_exists="[ -x /etc/rc.d/rc.\$1 ]"
265 _service_cmd="/etc/rc.d/rc.\$1 \$2"
266 _service_status="/etc/rc.d/rc.\$1 status >/dev/null 2>&1"
268 for x in /etc/init.d/rc.d /etc/rc.d /etc/init.d; do
270 _service_exists="[ -x $x/\$1 ]"
271 _service_cmd="$x/\$1 \$2"
272 _service_status="$x/\$1 $status >/dev/null 2>&1"
276 if [ -e /etc/arch-release ]; then
277 _service_status="[ -e /var/run/daemons/\$1 ]"
278 elif [ "$x" = "/etc/rc.d" ] && [ -e /etc/rc.d/rc.subr ]; then
279 _service_status="$x/\$1 check >/dev/null 2>&1"
284 if [ -z "$_service_cmd" ]; then
285 syslog err "could not detect a useable init system"
291 # Check a system service exists
294 if [ -z "$_service_exists" ]; then
295 detect_init || return 1
297 eval $_service_exists
300 # Send a command to a system service
303 if [ -z "$_service_cmd" ]; then
304 detect_init || return 1
309 # Send a command to a system service if it is running
312 if [ -z "$_service_cmd" ]; then
313 detect_init || return 1
315 if [ -n "$_service_status" ]; then
316 eval $_service_status
318 service_command $1 status >/dev/null 2>&1
322 # Handy macros for our hooks
325 service_exists $1 && service_cmd $1 $2
327 service_condcommand()
329 service_exists $1 && service_status $1 && service_cmd $1 $2
332 # We source each script into this one so that scripts run earlier can
333 # remove variables from the environment so later scripts don't see them.
334 # Thus, the user can create their dhcpcd.enter/exit-hook script to configure
335 # /etc/resolv.conf how they want and stop the system scripts ever updating it.
337 @SYSCONFDIR@/dhcpcd.enter-hook \
339 @SYSCONFDIR@/dhcpcd.exit-hook
341 for skip in $skip_hooks; do
344 */"$skip") continue 2;;
345 */[0-9][0-9]"-$skip") continue 2;;
346 */[0-9][0-9]"-$skip.sh") continue 2;;
349 if [ -f "$hook" ]; then