[Dissecting OpenWRT]#2: netifd – main()

netifd is an RPC-capable daemon written in C for better access to kernel APIs with the ability to listen on netlink events.

In this series serveral articles will be written introduce the internal of netifd which is a core componet of OpenWRT/LEDE system for network management and its support for 802.11. As a normal Linux program, standing at main() function as a start point is a good choice.

The main() function of netidf is rather short and simple. It hides all the details in functions

int main(int argc, char **argv)
{
    const char *socket = NULL;
    int ch;

    global_argv = argv;

    while ((ch = getopt(argc, argv, "d:s:p:c:h:r:l:S")) != -1) {
        switch(ch) {
        case 'd':
            debug_mask = strtoul(optarg, NULL, 0);
            break;
        case 's':
            socket = optarg;
            break;
        case 'p':
            main_path = optarg;
            break;
        case 'c':
            config_path = optarg;
            break;
        case 'h':
            hotplug_cmd_path = optarg;
            break;
        case 'r':
            resolv_conf = optarg;
            break;
        case 'l':
            log_level = atoi(optarg);
            if (log_level >= ARRAY_SIZE(log_class))
                log_level = ARRAY_SIZE(log_class) - 1;
            break;
#ifndef DUMMY_MODE
        case 'S':
            use_syslog = false;
            break;
#endif
        default:
            return usage(argv[0]);
        }
    }

    if (use_syslog)
        openlog("netifd", 0, LOG_DAEMON);

    netifd_setup_signals();
    if (netifd_ubus_init(socket) < 0) {
        fprintf(stderr, "Failed to connect to ubus\n");
        return 1;
    }

    proto_shell_init();
    wireless_init();

    if (system_init()) {
        fprintf(stderr, "Failed to initialize system control\n");
        return 1;
    }

    config_init_all();

    uloop_run();
    netifd_kill_processes();

    netifd_ubus_done();

    if (use_syslog)
        closelog();

    return 0;
}

A brief flow chart is shown below

netifd main function

where

Advertisements
This entry was posted in 未分类. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s