A working DHCPCD Hook system

本文提供了一种方法,在Android设备上通过在init.<device>.rc文件中使用一些小的属性技巧来实现DHCP和Wi-Fi状态的缺失功能。通过这些方法,可以实现DHCP停止状态的调用、Wi-Fi开关状态的触发,并且可以在不运行Android类的情况下在后台运行SSHd和SMBd。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原文链接:http://forum.xda-developers.com/showthread.php?t=1392343

if you like to play around with the shell on Android, it might come in handy to have some proper hooks for the wifi and dhcp. By default (in custom roms anyway), we have hooks for dhcp. However this does not invoke anything on the dhcp state "STOP". Also there are no hooks for when wifi is turned on, only when dhcp configurations are made to it. Which means that you cannot invoke hooks when turning on/off wifi tethering. 

Personally I have an SSHd and SMBd running in the background. These exists as android apps, but I preferer not having android classes running in the background for a such simple task. Specially when I always have these running on wifi. And both SSHd and SMBd works locally on wifi tethering, which is why this hook is a good tool to have.

Also on regular wifi with dhcp, it would be great to have these turn off when wifi connection is off. 
The below code will provide these missing features by using some small property hacks in the init.<device>.rc file.

init.<device>.rc 

Code:
# This provides the missing 'STOP' state invoke in dhcpcd
on property:init.svc.dhcpcd_eth0=stopped
   restart dhcpcdstate

# This provides the wifi on/off hook invoke
on property:wlan.driver.status=*
   restart wifistate

service dhcpcdstate /system/etc/dhcpcd/dhcpcd-hook STOP eth0
    disabled
    oneshot

service wifistate /system/etc/wifi/wifi-hook eth0
    disabled
    oneshot

# ------------------------------------
# The below should be changed according to your device.
# The important thing here is the path to --config and --script

service dhcpcd_eth0 /system/bin/dhcpcd -ABKL --config /system/etc/dhcpcd/dhcpcd.conf --script /system/etc/dhcpcd/dhcpcd-hook
    class main
    disabled
    oneshot

service iprenew_eth0 /system/bin/dhcpcd -n --config /system/etc/dhcpcd/dhcpcd.conf --script /system/etc/dhcpcd/dhcpcd-hook
    class main
    disabled
    oneshot
/system/etc/dhcpcd/dhcpcd-hook 
Code:
#!/system/bin/sh

if [ -z "${reason}" ] || -z "${interface}"; then
	reason="$1"
	interface="$2"
fi

for i in /system/etc/dhcpcd/dhcpcd.d/*; do
	if [ -f $i ] || [ -L $i ]; then
		. "$i"
	fi
done
/system/etc/dhcpcd/dhcpcd.d/01-configure 
Code:
case "${reason}" in
	BOUND|INFORM|REBIND|REBOOT|RENEW|TIMEOUT)
		setprop dhcp.${interface}.ipaddress "${new_ip_address}"
		setprop dhcp.${interface}.gateway   "${new_routers%% *}"
		setprop dhcp.${interface}.mask      "${new_subnet_mask}"
		setprop dhcp.${interface}.leasetime "${new_dhcp_lease_time}"
		setprop dhcp.${interface}.server    "${new_dhcp_server_identifier}"
		setprop dhcp.${interface}.ndns      "${new_domain_name_servers}"
		setprop dhcp.${interface}.reason    "${reason}"
		setprop dhcp.${interface}.result    "ok"

		if [ ! -z "${new_domain_name_servers}" ]; then
			for i in 1 2 3 4; do
				setprop dhcp.${interface}.dns$i ""
			done

			count=1
			for dnsaddr in "${new_domain_name_servers}"; do
				setprop dhcp.${interface}.dns$count $dnsaddr
				count=$(($count + 1))
			done
		fi
	;;

	EXPIRE|FAIL|IPV4LL|RELEASE|STOP)
		setprop dhcp.${interface}.result "failed"

		for i in 1 2 3 4; do
			setprop dhcp.${interface}.dns$i ""
		done
	;;

	RELEASE)
		setprop dhcp.${interface}.result "released"
	;;
esac
/system/etc/wifi/wifi-hook 
Code:
#!/system/bin/sh

status=`getprop wlan.driver.status`
interface="$1"

for i in /system/etc/wifi/wifi.d/*; do
	if [ -f $i ] || [ -L $i ]; then
		. "$i"
	fi
done

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值