• Netd学习笔记


    1. service netd /system/bin/netd
    2.     class main
    3.     socket netd stream 0660 root system
    4.     socket dnsproxyd stream 0660 root inet

    启动/system/bin/netd应用程序为服务,入口函数为main,创建2个名字分别为netd和dnsproxyd的本地socket连接。分别对应:

    1. root@android:/ # ll /dev/socket/
    2. srwxrwxrwx root inet 1970-01-01 00:00 dnsproxyd
    3. srwxrwxrwx root system 1970-01-01 00:00 netd

    备注:socket关键字用法,

    1. 代码阅读(注:由于说第一次分析netd的代码,我完全跟踪此代码的流程来看,先说init.rc,然后是main,之后依次展开):
    2. //main.cpp:main
    3. int main() {
    4. //........
    5.     if (!(nm = NetlinkManager::Instance())) { //NetlinkManager需要分析,一眼看出它为单根模式。看看他到底做来什么?
    6.         LOGE("Unable to create NetlinkManager");
    7.         exit(1);
    8.     };
    9.    
    10.    
    11.     cl = new CommandListener();//CommandListener需要分析
    12.     nm->setBroadcaster((SocketListener *) cl);
    13.    
    14.     if (nm->start()) { //直接看start函数。
    15.         LOGE("Unable to start NetlinkManager (%s)", strerror(errno));
    16.         exit(1);
    17.     }
    18.    
    19.     /*
    20.      * Now that we're up, we can respond to commands
    21.      */
    22.     if (cl->startListener()) {
    23.         LOGE("Unable to start CommandListener (%s)", strerror(errno));
    24.         exit(1);
    25.     }
    26.    
    27.     // Eventually we'll become the monitoring thread
    28.     while(1) {
    29.         sleep(1000);
    30.     }
    31.    
    32.     LOGI("Netd exiting");
    33.     exit(0);
    34. }

      直接跟进看NetlinkManager::start()

    35. int NetlinkManager::start() {
    36.     struct sockaddr_nl nladdr;
    37.     int sz = 64 * 1024;
    38.    
    39.     memset(&nladdr, 0, sizeof(nladdr));
    40.     nladdr.nl_family = AF_NETLINK;
    41.     nladdr.nl_pid = getpid();
    42.     nladdr.nl_groups = 0xffffffff;
    43.    
    44.     if ((mSock = socket(PF_NETLINK,
    45.                         SOCK_DGRAM,NETLINK_KOBJECT_UEVENT)) < 0) {
    46.         LOGE("Unable to create uevent socket: %s", strerror(errno));
    47.         return -1;
    48.     }
    49.    
    50.     if (setsockopt(mSock, SOL_SOCKET, SO_RCVBUFFORCE, &sz, sizeof(sz)) < 0) {
    51.         LOGE("Unable to set uevent socket options: %s", strerror(errno));
    52.         return -1;
    53.     }
    54.     if (bind(mSock, (struct sockaddr *) &nladdr, sizeof(nladdr)) < 0) {
    55.         LOGE("Unable to bind uevent socket: %s", strerror(errno));
    56.         return -1;
    57.     }
    58.    
    59.     mHandler = new NetlinkHandler(this, mSock);
    60.     if (mHandler->start()) {
    61.         LOGE("Unable to start NetlinkHandler: %s", strerror(errno));
    62.         return -1;
    63.     }
    64.     return 0;
    65. }

      看NetlinkHandler类:

    66. #ifndef _NETLINKHANDLER_H
    67. #define _NETLINKHANDLER_H
    68.    
    69. #include <sysutils/NetlinkListener.h>
    70. #include "NetlinkManager.h"
    71.    
    72. class NetlinkHandler: public NetlinkListener { // NetlinkListener类之前有分析,这个是网络监听的框架实现类。
    73.     NetlinkManager *mNm;
    74.    
    75. public:
    76.     NetlinkHandler(NetlinkManager *nm, int listenerSocket);
    77.     virtual ~NetlinkHandler();
    78.    
    79.     int start(void);
    80.     int stop(void);
    81.    
    82. protected:
    83.     virtual void onEvent(NetlinkEvent *evt);
    84.    
    85.     void notifyInterfaceAdded(const char *name);
    86.     void notifyInterfaceRemoved(const char *name);
    87.     void notifyInterfaceChanged(const char *name, bool isUp);
    88. };
    89. #endif

      晕死,怎么排版乱完了,而且还丢失了部分数据。

      

  • 相关阅读:
    浅析[53300] FATAL: remaining connection slots are reserved for non-replication superuser connections
    浅析Web安全漏洞里的X-Frame-Options、X-XSS-Protection、X-Content-Type-Options响应头配置以及如何通过nginx配置避免
    浅析SpringSecurity的方法级安全管控
    浅析mybatis的全局配置文件mybatis-config.xml各配置的意义
    字节序--大端字节序和小端
    【转】go语言中int和byte转换方式
    [转]float32数据在内存中的存储方法
    go语言复制一个struct 对象
    一个table 中 可以有多个tbody. table 可以嵌套
    【转】rpc框架设计
  • 原文地址:https://www.cnblogs.com/jevan/p/3154630.html
Copyright © 2020-2023  润新知