AOSP dhcpcd-6.8.2 how to run hooks

Since Android 7, AOSP dhcpcd (standard dhcp client) was replaced by dhcpcd-6.8.2 that is, reading in Android.mk, a “D-Bus enabled and compatible with Brillo daemons” dhcp client based on dhcpcd: let’s see how it should be customized for running hooks after completing some actions.

AOSP dhcpcd-6.8.2

For my tests I’m using the Hikey960, even though there should not be almost anything board related in the next steps besides package availability: in fact dhcpcd does not belong to the default packages of the Hikey build, so it should be added.

Open the device makefile (device/linaro/hikey/hikey960/device-hikey960.mk) and add the following line:

PRODUCT_PACKAGES += dhcpcd-6.8.2

After a successful build the package is ready to be used (generally through the definition of a service), but, by default, it won’t run any hook.

Taking a look at the Android makefile (external/dhcpcd-6.8.2/Android.mk) we can see the interesting symbol DHCPCD_USE_SCRIPT: this should be set to ‘yes’ to enable hook execution, so add the line

DHCPCD_USE_SCRIPT := yes

after the first instance of LOCAL_PATH

Now you need two other elements:

  • The bash script used for running the hooks
  • The hooks to be run

The first one could be easily generated using the standard makefile: open a shell in the dhcpcd directory and type

$ make dhcpcd-run-hooks

to create a script called dhcpcd-run-hooks used for running the hooks.

However, with my setup it was not properly working as it was generated, so I had to customize the path of sh: instead of

#!/bin/sh

for the Hikey960 it was

#!/system/bin/sh

I had also to modify the hooks directory in the following block

for hook in \
    /dhcpcd.enter-hook \
    /dhcpcd-hooks/* \
    /dhcpcd.exit-hook

e.g. in my system I placed the hooks in /system/etc/dhcpcd/dhcpcd-hooks, so the block becomes

for hook in \
    /system/etc/dhcpcd/dhcpcd.enter-hook \
    /system/etc/dhcpcd/dhcpcd-hooks/* \
    /system/etc/dhcpcd/dhcpcd.exit-hook

Some hooks can be found in

external/dhcpcd-6.8.2/dhcpcd-hooks

Choose the needed ones (or create a new one): I find especially useful 50-dhcpcd-compat.

Once identified the hooks you need, the Android makefile should be changed for copying the right files in the filesystem. Android.mk has the following commented part:

# Each build target using dhcpcd-6.8.2 should define its own source
# and destination for its dhcpcd.conf file.
# include $(CLEAR_VARS)
# LOCAL_MODULE := dhcpcd-6.8.2.conf
# LOCAL_MODULE_CLASS := ETC
# LOCAL_MODULE_PATH := $(TARGET_OUT)/etc/dhcpcd-6.8.2
# LOCAL_SRC_FILES := android.conf
# include $(BUILD_PREBUILT)

We can use similar lines for copying the hooks running script and the needed hooks, like the following

include $(CLEAR_VARS)
 LOCAL_MODULE := dhcpcd-run-hooks
 LOCAL_MODULE_CLASS := EXECUTABLES
 LOCAL_MODULE_PATH := $(TARGET_OUT_ETC)/dhcpcd
 LOCAL_SRC_FILES := $(LOCAL_MODULE)
 include $(BUILD_PREBUILT)

include $(CLEAR_VARS)
 LOCAL_MODULE := 50-dhcpcd-compat
 LOCAL_MODULE_CLASS := ETC
 LOCAL_MODULE_PATH := $(TARGET_OUT_ETC)/dhcpcd/dhcpcd-hooks
 LOCAL_SRC_FILES := dhcpcd/$(LOCAL_MODULE)
 include $(BUILD_PREBUILT)

Do not forget to set the executability bit to the various scripts, add the new created packages, rebuild and you should have your hooks properly working.