Wpa_supplicant source code analysis (1): Implementation of event Loop

Source: Internet
Author: User

Turn: http://blog.chinaunix.net/uid-20273473-id-3128151.html

The life of the wpa_supplicant program is to run an event loop, wait for the arrival of various events, and then perform corresponding processing. Therefore, the analysis of the implementation of event loop can play a role in outline.


1. Data Structure: see

The struct eloop_data struct is a global data structure with only one instance, namely line 75:

Click (here) to fold or open

  1. Static struct eloop_data eloop;

There are three types of events to process: Socket events, timeout events, and signal events. Socket events are classified into three types:

  1. * @ Event_type_read: socket has data available for reading
  2. * @ Event_type_write: socket has room for new data to be written
  3. * @ Event_type_exception: an exception has been reported

Socket event: There are three eloop_sock_table struct types: readers, writers, and exceptions, each of which has a dynamic array. Each of the arrays is a specific eloop_sock. you can add or delete eloop_sock to tables. event distribution is to traverse sock_table and run each handler in sequence.

Timeout event: Each struct eloop_timeout is placed in a two-way linked list. The linked list header is the "timeout" item in eloop_data. The struct eloop_timeout values are sorted by timeout.

Signal event: Each struct eloop_signal is placed in a dynamic array

Q: Why does the socket and signal events dynamically expand and narrow the array instead of using the linked list?

2. Select-Based Multi-Event Processing

Void eloop_run (void) is a total function. We can see how it uses the Select system call to implement multiplexing to listen to multiple events.

  1. While (! Eloop. Terminate &&
  2. (! Dl_list_empty (& eloop. Timeout) | eloop. Readers. Count> 0 |
  3. Eloop. Writers. Count> 0 | eloop. Exceptions. Count> 0 )){

If eloop. Terminate changes to a non-zero value, the loop is exited. This is to provide a method to end the loop from the external.

If eloop. Terminate is zero, the loop continues as long as the timeout linked list or any socket is not empty.

The prototype of the select system call is:

  1. Int select (INT NFDs, fd_set * readfds, fd_set * writefds, fd_set * limit TFDs, struct timeval * timeout );

In the loop body:

(1) Find the first item of the timeout linked list (because it is sorted by time-out, so the first item must be the first time-out), and calculate how long the time-out time is from now, set the select timeout parameter accordingly.

(2) set three fd_sets: readfd, writefds, and limit TFDs. The method is to traverse each eloop_sock_table and add each sock descriptor to the corresponding fd_set.

(3) Call select. It may be blocked here.

(4) eloop_process_pending_signals (); process signal events. It will be analyzed later.

(5) determine whether a timeout occurs. If yes, call the handler and remove it from the timeout linked list. Then continue the next loop.

(6) if it is not a time-out event, it should be a readfds, writefds, or exceptfds event. The fd_set event will be changed to store the event descriptor. Therefore, we traverse three sock_tables separately. If its descriptor is in fd_set, we call its handler.

(7) Continue the next cycle.

It is worth mentioning that the processing of signal is a bit special here. The handler of signal registered in the eloop_register_signal () function is not automatically executed when signal occurs. When signal occurs, only 1 is added to the signaled variable of the struct eloop_signal to indicate that the signal has been received and is in the pendning state. Eloop_process_pending_signals () is called when select () times out or when a socket event occurs.
Eloop_signal calls its handler.

Q: What are the benefits of doing so? If select is blocked for a long time, isn't it necessary for signal to pending for a long time to be processed?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.