Home | History | Annotate | Download | only in dhcpcd
      1 #!/bin/sh
      2 # dhcpcd client configuration script 
      3 
      4 # Handy variables and functions for our hooks to use
      5 from="from"
      6 signature_base="# Generated by dhcpcd"
      7 signature="${signature_base} ${from} ${interface}"
      8 signature_base_end="# End of dhcpcd"
      9 signature_end="${signature_base_end} ${from} ${interface}"
     10 state_dir="/var/run/dhcpcd"
     11 
     12 # Ensure that all arguments are unique
     13 uniqify()
     14 {
     15 	local result=
     16 
     17 	while [ -n "$1" ]; do
     18 		case " ${result} " in
     19 		*" $1 "*);;
     20 		*) result="${result}${result:+ }$1";;
     21 		esac
     22 		shift
     23 	done
     24 	echo "${result}"
     25 }
     26 
     27 # List interface config files in a dir
     28 # We may wish to control the order at some point rather than just lexical
     29 list_interfaces()
     30 {
     31 	local x= interfaces=
     32 	for x in "$1"/*; do
     33 		[ -e "${x}" ] || continue
     34 		interfaces="${interfaces}${interfaces:+ }${x##*/}"
     35 	done
     36 	echo "${interfaces}"
     37 }
     38 
     39 # We normally use sed to extract values using a key from a list of files
     40 # but sed may not always be available at the time.
     41 key_get_value()
     42 {
     43 	local key="$1" value= x= line=
     44 
     45 	shift
     46 	if type sed >/dev/null 2>&1; then
     47 		sed -n "s/^${key}//p" $@
     48 	else
     49 		for x; do
     50 			while read line; do
     51 				case "${line}" in
     52 				"${key}"*) echo "${line##${key}}";;
     53 				esac
     54 			done < "${x}"
     55 		done
     56 	fi
     57 }
     58 
     59 # We normally use sed to remove markers from a configuration file
     60 # but sed may not always be available at the time.
     61 remove_markers()
     62 {
     63 	local m1="$1" m2="$2" x= line= in_marker=0
     64 
     65 	shift; shift
     66 	if type sed >/dev/null 2>&1; then
     67 		sed "/^${m1}/,/^${m2}/d" $@
     68 	else
     69 		for x; do
     70 			while read line; do
     71 				case "${line}" in
     72 				"${m1}"*) in_marker=1;;
     73 				"${m2}"*) in_marker=0;;
     74 				*) [ ${in_marker} = 0 ] && echo "${line}";;
     75 				esac
     76 			done < "${x}"
     77 		done
     78 	fi
     79 }
     80 
     81 # Compare two files
     82 # If different, replace first with second otherwise remove second
     83 change_file()
     84 {
     85 	if type cmp >/dev/null 2>&1; then
     86 		cmp -s "$1" "$2"
     87 	elif type diff >/dev/null 2>&1; then
     88 		diff -q "$1" "$2" >/dev/null
     89 	else
     90 		# Hopefully we're only working on small text files ...
     91 		[ "$(cat "$1")" = "$(cat "$2")" ]
     92 	fi
     93 	if [ $? -eq 0 ]; then
     94 		rm -f "$2"
     95 		return 1
     96 	fi
     97 	mv -f "$2" "$1"
     98 	return 0
     99 }
    100 
    101 # Save a config file
    102 save_conf()
    103 {
    104 	if [ -f "$1" ]; then
    105 		rm -f "$1"-pre."${interface}"
    106 		mv -f "$1" "$1"-pre."${interface}"
    107 	fi
    108 }
    109 
    110 # Restore a config file
    111 restore_conf()
    112 {
    113 	[ -f "$1"-pre."${interface}" ] || return 1
    114 	rm -f "$1"
    115 	mv -f "$1"-pre."${interface}" "$1"
    116 }
    117 
    118 
    119 # We source each script into this one so that scripts run earlier can
    120 # remove variables from the environment so later scripts don't see them.
    121 # Thus, the user can create their dhcpcd.enter/exit-hook script to configure
    122 # /etc/resolv.conf how they want and stop the system scripts ever updating it.
    123 for hook in \
    124 	@SYSCONFDIR@/dhcpcd.enter-hook \
    125 	@HOOKDIR@/* \
    126 	@SYSCONFDIR@/dhcpcd.exit-hook
    127 do
    128 	for skip in ${skip_hooks}; do
    129 		case "${hook}" in
    130 			*/"${skip}")			continue 2;;
    131 			*/[0-9][0-9]"-${skip}")		continue 2;;
    132 			*/[0-9][0-9]"-${skip}.sh")	continue 2;;
    133 		esac
    134 	done
    135 	if [ -f "${hook}" ]; then
    136 		. "${hook}"
    137 	fi
    138 done
    139