Hostapd_cli failed to cummunicuate with hostapd for socket error

Hi ,all,

Does hostapd_cli create socket before daemon called right?

While ctrl->s = socket(PF_UNIX, SOCK_DGRAM, 0); called in function hostapd_cli_reconnect, and os_daemonize calls daemon(0,0).

In some case, it will cause socket receive error.

It happened as follows:

I encountered a strange thing that hostapd_cli can not trigger script specified by -a

/usr/sbin/hostapd_cli -i ath01 -p /var/run/hostapd-wifi0 -P /var/run/hostapd_cli-ath01.pid -a /lib/wifi/wps-hostapd-update-uci -B.

When I apply wifi changes, /sbin/reload_config is been called and hostapd_cli command executes in wifi reload process, then when WPS related event happens, /lib/wifi/wps-hostapd-update-uci should be invoked. However, it fails.

When I execute /usr/sbin/hostapd_cli -i ath01 -p /var/run/hostapd-wifi0 -P /var/run/hostapd_cli-ath01.pid -a /lib/wifi/wps-hostapd-update-uci -B in shell, the script works well.

Finally, I found that, /sbin/reload_config calls ubus call service event '{ "type": "config.change", "data": { "package": "wireless" }}', after that, procd fork a child process and close its stdin/stdout/stderr, in this stdin closed process, /etc/init.d/network reload is called. and finally hostapd_cli command invoked like above.

While socket function will get 0 as its return value in function hostapd_cli_reconnect.
image

And then, os_daemonize will call daemon(0,0), while it will open /dev/null and dup to stdin/stdout/stderr, now 0 should point to stdin and not the original socket.

So, hostapd_cli will never got messages from hostapd again. Following code will get recvfrom(0, 0x7fd6530f18, 4095, 0, 0, 0) = -1 ENOTSOCK (Not a socket) error.

image

My question is:

  1. Is it right to create a socket before daemon called, or use a father created socket in child process? Especially when 0/1/2 may be the socket fd.
  2. /sbin/reload_config should be common usage in OpenWrt? Anyone encountered this before?