本文转载自;http://blog.csdn.net/lishanmin11/article/details/37930073
最近在调android ethernet功能,android本身不带 ethernet 功能,需要打patch。这个patch可以在setting里出来 ethernet configuration 选项。即添加了用户配置IP的功能。
我打上patch之后,点击选上DHCP功能,结果路由器一直不能自动分配IP。
经检测,命令行里运行 netcfg eth0 up dhcp 时,ethernet能被正常启动,DHCP能分配到IP。
但是Setting里选上时,dhcp却不能正常分配IP。 这很费解,我先后查看了,
/system/core/libnetutils/*
/externel/dhcpcd/*
发现都没什么问题。
在Setting中点击turn on ethernet选项时,从log看到能调到
- E/EthernetStateTracker( 185): DhcpHandler: DHCP request failed: Timed out waiting for dhcpcd to start
- D/EthernetStateTracker( 185): DhcpHandler: DHCP request started
说明patch是好的能正常工作,能正常掉用dhcp,只是DHCP运行不成功。
然后我查了 getprop: 显示 init.svc.dhcpcd_eth0 = stop
正常应该是running的,这样DHCP 才能运行成功。
最后调试了两天,才搞明白,原来是 init.rc 中 dhcpcd_eth0 守护进程的问题:
改成:
- on property:init.svc.dhcpcd_eth0=stopped
- start dhcpcd_eth0
- service dhcpcd_eth0 /system/bin/dhcpcd -ABKL -f /system/etc/dhcpcd/dhcpcd.conf -d eth0
- class main
- disabled
- oneshot
这样就可以了。
这时init.svc.dhcpcd_eth0 就会是 running 了。这时再点击Setting -> ethernet configuration, DHCP就能正常分配IP了。
libnetutils 和dhcpcd 调用过程如下:
1. libnetutils 调用过程:
- jni
- =>runDhcp
- =>android_net_utils_runDhcp
- libs/netutils/dhcp_utils.c
- =>dhcp_do_request
- =>
- static const char DAEMON_NAME[] = "dhcpcd";
- static const char DAEMON_PROP_NAME[] = "init.svc.dhcpcd";
- static const char DHCP_PROP_NAME_PREFIX[] = "dhcp";
- const char *ctrl_prop = "ctl.start";
- const char *desired_status = "running";
- snprintf(result_prop_name, sizeof(result_prop_name), "%s.%s.result",
- DHCP_PROP_NAME_PREFIX,
- interface);
- property_set(result_prop_name, "");//设置dhcp.eth0.result="";等到成功完成dhcp之后,
- property_set(ctrl_prop, DAEMON_NAME);//向名字为dhcpcd的service,发送"ctrl.start"启动命令字,该service在init.rc中
- //init.rc中dhcpcd服务进程命令字
- //service dhcpcd /system/bin/dhcpcd eth0
- // disabled
- // oneshot
- wait_for_property(DAEMON_PROP_NAME, desired_status, 10);
- //init.c=>init进程
- //=>handle_property_set_fd因为是"ctrl.start"命令字,所以调用handle_control_message处理控制信息
- //=>handle_control_message
- //=>msg_start
- //=>
- // struct service *svc = service_find_by_name(name);
- // service_start(svc);//启动svc,即执行:/system/bin/dhcpcd eth0
- //=>service_start
- //=>pid = fork();
- // if(pid == 0)execve(svc->args[0], (char**) svc->args, (char**) ENV);子进程执行execve运行/system/bin/dhcpcd,参数为eth0
- //=>否则父进程,即init进程将
- //=>notify_service_state(svc->name, "running");设置该svc的状态prop
- // snprintf(pname, sizeof(pname), "init.svc.%s", name);
- // property_set(pname, state);//所以这样上面wait_for_property(DAEMON_PROP_NAME, desired_status, 10);也才能够正常pass[luther.gliethttp].
- wait_for_property(result_prop_name, NULL, 15);//等待dhcp.eth0.result=非空
2. dhcpcd 调用过程:
-
- system/extra/dhcpcd-4.0.0-beta9/dhcpcd.c
- dhcpcd
- =>main
- # define SYSCONFDIR "/system/etc/dhcpcd"
- #define PACKAGE "dhcpcd"
- # define CONFIG SYSCONFDIR "/" PACKAGE ".conf"
- # define LIBEXECDIR "/system/etc/dhcpcd"
- # define SCRIPT LIBEXECDIR "/" PACKAGE "-run-hooks"
- =>strlcpy(options->script, SCRIPT, sizeof(options->script));//默认的options->script="/system/etc/dhcpcd /dhcpcd-run-hooks"
- =>f = fopen(cf ? cf : CONFIG, "r");//如果没有指定.conf文件,那么使用默认.conf文件
- =>parse_config_line//解析"/system/etc/dhcpcd/dhcpcd.conf"默认配置文件
- =>parse_option
- =>如果在"/system/etc/dhcpcd/dhcpcd.conf"有"script"这个节
- =>那么执行strlcpy(options->script, oarg, sizeof(options->script));直接拷贝
- /*
- {"script", required_argument, NULL, 'c'},
- {"option", required_argument, NULL, 'o'},
- "/system/etc/dhcpcd/dhcpcd.conf"中的部分内容如下:
- ...
- option domain_name_servers, domain_name, domain_search, host_name
- ...
- */
- =>dhcp_run
- =>handle_dhcp_packet
- =>handle_dhcp
- =>bind_dhcp
- reason = "TIMEOUT";reason = "BOUND";reason = "REBIND";reason = "RENEW";
- system/extra/dhcpcd-4.0.0-beta9/configure.c
- => configure(iface, reason, state->new, state->old, &state->lease, options, 1);
- //如果dhcp超时或者dhcp成功,都会调用exec_script来执行脚本,
- //执行setprop dhcp.${interface}.result "failed"或者
- //执行setprop dhcp.${interface}.result "ok"
- =>exec_script(options, iface->name, reason, NULL, old);
- =>然后configure_env通过环境变量将reason传递到脚本中
- int exec_script(const struct options *options, const char *iface, const char *reason,
- const struct dhcp_message *dhcpn, const struct dhcp_message *dhcpo)
- =>pid = fork();
- =>if(pid == 0)execve(options->script, argv, env);//子进程执行脚本,默认"/system/etc/dhcpcd/dhcpcd-run-hooks"
- //dhcpcd-run-hooks脚本会根据level值,决定是否执行system/etc/dhcpcd/dhcpcd-hook/*目录下的相应文件
- //我们的系统在该system/etc/dhcpcd/dhcpcd-hook/*目录下有如下3个文件
- //95-configured
- //20-dns.conf
- //01-test
- =>父进程返回while (waitpid(pid, &status, 0) == -1)等待子进程脚本执行完成
- system/extra/dhcpcd-4.0.0-beta9/dhcpcd-hooks/20-dns.conf
- system/extra/dhcpcd-4.0.0-beta9/dhcpcd-hooks/95-configured
- ...
- setprop dhcp.${interface}.ipaddress "${new_ip_address}"
- setprop dhcp.${interface}.result "ok"//设置属性为ok
- setprop dhcp.${interface}.result "failed"
- ...