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