- service netd /system/bin/netd
- class main
- socket netd stream 0660 root system
- socket dnsproxyd stream 0660 root inet
启动/system/bin/netd应用程序为服务,入口函数为main,创建2个名字分别为netd和dnsproxyd的本地socket连接。分别对应:
- root@android:/ # ll /dev/socket/
- srwxrwxrwx root inet 1970-01-01 00:00 dnsproxyd
- srwxrwxrwx root system 1970-01-01 00:00 netd
备注:socket关键字用法,
- 代码阅读(注:由于说第一次分析netd的代码,我完全跟踪此代码的流程来看,先说init.rc,然后是main,之后依次展开):
- //main.cpp:main
- int main() {
- //........
- if (!(nm = NetlinkManager::Instance())) { //NetlinkManager需要分析,一眼看出它为单根模式。看看他到底做来什么?
- LOGE("Unable to create NetlinkManager");
- exit(1);
- };
- cl = new CommandListener();//CommandListener需要分析
- nm->setBroadcaster((SocketListener *) cl);
- if (nm->start()) { //直接看start函数。
- LOGE("Unable to start NetlinkManager (%s)", strerror(errno));
- exit(1);
- }
- /*
- * Now that we're up, we can respond to commands
- */
- if (cl->startListener()) {
- LOGE("Unable to start CommandListener (%s)", strerror(errno));
- exit(1);
- }
- // Eventually we'll become the monitoring thread
- while(1) {
- sleep(1000);
- }
- LOGI("Netd exiting");
- exit(0);
- }
直接跟进看NetlinkManager::start()
- int NetlinkManager::start() {
- struct sockaddr_nl nladdr;
- int sz = 64 * 1024;
- memset(&nladdr, 0, sizeof(nladdr));
- nladdr.nl_family = AF_NETLINK;
- nladdr.nl_pid = getpid();
- nladdr.nl_groups = 0xffffffff;
- if ((mSock = socket(PF_NETLINK,
- SOCK_DGRAM,NETLINK_KOBJECT_UEVENT)) < 0) {
- LOGE("Unable to create uevent socket: %s", strerror(errno));
- return -1;
- }
- if (setsockopt(mSock, SOL_SOCKET, SO_RCVBUFFORCE, &sz, sizeof(sz)) < 0) {
- LOGE("Unable to set uevent socket options: %s", strerror(errno));
- return -1;
- }
- if (bind(mSock, (struct sockaddr *) &nladdr, sizeof(nladdr)) < 0) {
- LOGE("Unable to bind uevent socket: %s", strerror(errno));
- return -1;
- }
- mHandler = new NetlinkHandler(this, mSock);
- if (mHandler->start()) {
- LOGE("Unable to start NetlinkHandler: %s", strerror(errno));
- return -1;
- }
- return 0;
- }
看NetlinkHandler类:
- #ifndef _NETLINKHANDLER_H
- #define _NETLINKHANDLER_H
- #include <sysutils/NetlinkListener.h>
- #include "NetlinkManager.h"
- class NetlinkHandler: public NetlinkListener { // NetlinkListener类之前有分析,这个是网络监听的框架实现类。
- NetlinkManager *mNm;
- public:
- NetlinkHandler(NetlinkManager *nm, int listenerSocket);
- virtual ~NetlinkHandler();
- int start(void);
- int stop(void);
- protected:
- virtual void onEvent(NetlinkEvent *evt);
- void notifyInterfaceAdded(const char *name);
- void notifyInterfaceRemoved(const char *name);
- void notifyInterfaceChanged(const char *name, bool isUp);
- };
- #endif
晕死,怎么排版乱完了,而且还丢失了部分数据。