当前位置: 首页 > 工具软件 > dhcpcd > 使用案例 >

android dhcpcd 命令,dhcpcd守护进程

蒋高扬
2023-12-01

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=非空

=============================================================================================================

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"

...

 类似资料: