Commit changes on the files for gpiolib
[openwrt.git] / package / hotplug2 / patches / 120-throttling.patch
1 Index: hotplug2-0.9/hotplug2.c
2 ===================================================================
3 --- hotplug2-0.9.orig/hotplug2.c        2008-08-04 10:02:27.000000000 +0200
4 +++ hotplug2-0.9/hotplug2.c     2008-08-04 10:03:04.000000000 +0200
5 @@ -21,6 +21,7 @@
6  #include <sys/mman.h>
7  #include <linux/types.h>
8  #include <linux/netlink.h>
9 +#include <poll.h>
10  
11  #include "mem_utils.h"
12  #include "filemap_utils.h"
13 @@ -492,6 +493,11 @@
14         char *coldplug_command = NULL;
15         char *rules_file = HOTPLUG2_RULE_PATH;
16         sigset_t block_mask;
17 +       struct pollfd msg_poll;
18 +
19 +       struct hotplug2_event_t *backlog = NULL;
20 +       struct hotplug2_event_t *backlog_tail = NULL;
21 +       int n_backlog = 0;
22         
23         struct rules_t *rules = NULL;
24         struct filemap_t filemap;
25 @@ -602,6 +608,8 @@
26          * Open netlink socket to read the uevents
27          */
28         netlink_socket = init_netlink_socket(NETLINK_BIND);
29 +       msg_poll.fd = netlink_socket;
30 +       msg_poll.events = POLLIN;
31         
32         if (netlink_socket == -1) {
33                 ERROR("netlink init","Unable to open netlink socket.");
34 @@ -642,20 +650,44 @@
35          * Main loop reading uevents
36          */
37         while (!terminate) {
38 -               /*
39 -                * Read the uevent packet
40 -                */
41 -               size = recv(netlink_socket, &buffer, sizeof(buffer), 0);
42 -               recv_errno = errno;
43 +               if ((n_backlog > 0) && (child_c < max_child_c)) {
44 +                       /* dequeue backlog message */
45 +                       tmpevent = backlog;
46 +                       backlog = backlog->next;
47 +                       n_backlog--;
48 +                       if (backlog_tail == tmpevent)
49 +                               backlog_tail = NULL;
50 +               } else {
51 +                       /*
52 +                        * Read the uevent packet
53 +                        */
54 +                       if (n_backlog >= HOTPLUG2_MSG_BACKLOG) {
55 +                               usleep(HOTPLUG2_THROTTLE_INTERVAL * 1000);
56 +                               continue;
57 +                       }
58 +
59 +                       if ((n_backlog > 0) && (child_c >= max_child_c)) {
60 +                               int fds;
61 +                               msg_poll.revents = 0;
62 +                               fds = poll(&msg_poll, 1, HOTPLUG2_THROTTLE_INTERVAL);
63 +                               if (fds < 0) {
64 +                                       continue;
65 +                               }
66 +                               if (fds == 0)
67 +                                       continue;
68 +                       }
69 +                       size = recv(netlink_socket, &buffer, sizeof(buffer), 0);
70 +                       recv_errno = errno;
71         
72 -               /*
73 -                * Parse the event into an event structure
74 -                */
75 -               tmpevent = get_hotplug2_event(buffer, size);
76 +                       /*
77 +                        * Parse the event into an event structure
78 +                        */
79 +                       tmpevent = get_hotplug2_event(buffer, size);
80                 
81 -               if (tmpevent == NULL) {
82 -                       ERROR("reading events", "Malformed event read (missing action prefix).");
83 -                       continue;
84 +                       if (tmpevent == NULL) {
85 +                               ERROR("reading events", "Malformed event read (missing action prefix).");
86 +                               continue;
87 +                       }
88                 }
89                 
90                 /*
91 @@ -706,13 +738,16 @@
92                          * Unless, of course, we've specified otherwise and no rules that match
93                          * need throttling.
94                          */
95 -                       if (!flags & FLAG_NOTHROTTLE) {
96 -                               /*
97 -                                * Okay, throttle away!
98 -                                */
99 -                               while (child_c >= max_child_c) {
100 -                                       usleep(HOTPLUG2_THROTTLE_INTERVAL);
101 -                               }
102 +                       if (!(flags & FLAG_NOTHROTTLE) && (child_c >= max_child_c)) {
103 +                               /* log the packet and process it later */
104 +                               if (backlog_tail)
105 +                                       backlog_tail->next = tmpevent;
106 +                               else
107 +                                       backlog = tmpevent;
108 +                               tmpevent->next = NULL;
109 +                               backlog_tail = tmpevent;
110 +                               n_backlog++;
111 +                               continue;
112                         }
113                         
114                         sigemptyset(&block_mask);
115 Index: hotplug2-0.9/hotplug2.h
116 ===================================================================
117 --- hotplug2-0.9.orig/hotplug2.h        2008-08-04 10:02:27.000000000 +0200
118 +++ hotplug2-0.9/hotplug2.h     2008-08-04 10:02:27.000000000 +0200
119 @@ -45,9 +45,9 @@
120  #define DBG(action, fmt, arg...)
121  #endif
122  
123 +#define HOTPLUG2_MSG_BACKLOG   64
124  #define UEVENT_BUFFER_SIZE             2048
125 -#define HOTPLUG2_POLL_INTERVAL         20000
126 -#define HOTPLUG2_THROTTLE_INTERVAL     10000
127 +#define HOTPLUG2_THROTTLE_INTERVAL     50
128  #define HOTPLUG2_RULE_PATH             "/etc/hotplug2.rules"
129  
130  #define ACTION_ADD                     0
131 @@ -76,6 +76,7 @@
132         int env_vars_c;
133         char *plain;
134         int plain_s;
135 +       struct hotplug2_event_t *next;
136  };
137  
138  struct options_t {