diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2007-09-07 13:43:28 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2007-09-07 13:43:28 +0000 |
commit | 87f3b26b3a17369c12f4f9a70d6845796f8648d6 (patch) | |
tree | 2f7fe9fc910d5e97f695c902f848db497fec8bfd | |
parent | 40f0bcf9d3f8f8a8d14a9b2cff51761019c75cf4 (diff) | |
download | busybox-w32-87f3b26b3a17369c12f4f9a70d6845796f8648d6.tar.gz busybox-w32-87f3b26b3a17369c12f4f9a70d6845796f8648d6.tar.bz2 busybox-w32-87f3b26b3a17369c12f4f9a70d6845796f8648d6.zip |
*: replace select-for-one descriptor with poll, it's smaller.
$ ./.cmk bloatcheck
function old new delta
readit 406 364 -42
syslogd_main 1249 1206 -43
traceroute_main 4115 4060 -55
mysleep 112 45 -67
arpping 579 441 -138
tftp 1575 1182 -393
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 0/6 up/down: 0/-738) Total: -738 bytes
text data bss dec hex filename
770580 1051 10764 782395 bf03b busybox_old
769820 1051 10764 781635 bed43 busybox_unstripped
-rw-r--r-- | editors/vi.c | 36 | ||||
-rw-r--r-- | networking/tftp.c | 33 | ||||
-rw-r--r-- | networking/traceroute.c | 24 | ||||
-rw-r--r-- | networking/udhcp/arpping.c | 29 | ||||
-rw-r--r-- | shell/ash.c | 1 | ||||
-rw-r--r-- | sysklogd/syslogd.c | 20 |
6 files changed, 61 insertions, 82 deletions
diff --git a/editors/vi.c b/editors/vi.c index afbddc251..f7f84807d 100644 --- a/editors/vi.c +++ b/editors/vi.c | |||
@@ -237,7 +237,8 @@ static char *yank_delete(char *, char *, int, int); // yank text[] into register | |||
237 | static void show_help(void); // display some help info | 237 | static void show_help(void); // display some help info |
238 | static void rawmode(void); // set "raw" mode on tty | 238 | static void rawmode(void); // set "raw" mode on tty |
239 | static void cookmode(void); // return to "cooked" mode on tty | 239 | static void cookmode(void); // return to "cooked" mode on tty |
240 | static int mysleep(int); // sleep for 'h' 1/100 seconds | 240 | // sleep for 'h' 1/100 seconds, return 1/0 if stdin is (ready for read)/(not ready) |
241 | static int mysleep(int); | ||
241 | static char readit(void); // read (maybe cursor) key from stdin | 242 | static char readit(void); // read (maybe cursor) key from stdin |
242 | static char get_one_char(void); // read 1 char from stdin | 243 | static char get_one_char(void); // read 1 char from stdin |
243 | static int file_size(const char *); // what is the byte size of "fn" | 244 | static int file_size(const char *); // what is the byte size of "fn" |
@@ -2134,17 +2135,11 @@ static void catch_sig(int sig) | |||
2134 | 2135 | ||
2135 | static int mysleep(int hund) // sleep for 'h' 1/100 seconds | 2136 | static int mysleep(int hund) // sleep for 'h' 1/100 seconds |
2136 | { | 2137 | { |
2137 | fd_set rfds; | 2138 | struct pollfd pfd[1]; |
2138 | struct timeval tv; | ||
2139 | 2139 | ||
2140 | // Don't hang- Wait 5/100 seconds- 1 Sec= 1000000 | 2140 | pfd[0].fd = 0; |
2141 | fflush(stdout); | 2141 | pfd[0].events = POLLIN; |
2142 | FD_ZERO(&rfds); | 2142 | return poll(pfd, 1, hund*10) > 0; |
2143 | FD_SET(0, &rfds); | ||
2144 | tv.tv_sec = 0; | ||
2145 | tv.tv_usec = hund * 10000; | ||
2146 | select(1, &rfds, NULL, NULL, &tv); | ||
2147 | return FD_ISSET(0, &rfds); | ||
2148 | } | 2143 | } |
2149 | 2144 | ||
2150 | #define readbuffer bb_common_bufsiz1 | 2145 | #define readbuffer bb_common_bufsiz1 |
@@ -2217,25 +2212,20 @@ static char readit(void) // read (maybe cursor) key from stdin | |||
2217 | if (n <= 0) | 2212 | if (n <= 0) |
2218 | return 0; // error | 2213 | return 0; // error |
2219 | if (readbuffer[0] == 27) { | 2214 | if (readbuffer[0] == 27) { |
2220 | fd_set rfds; | ||
2221 | struct timeval tv; | ||
2222 | |||
2223 | // This is an ESC char. Is this Esc sequence? | 2215 | // This is an ESC char. Is this Esc sequence? |
2224 | // Could be bare Esc key. See if there are any | 2216 | // Could be bare Esc key. See if there are any |
2225 | // more chars to read after the ESC. This would | 2217 | // more chars to read after the ESC. This would |
2226 | // be a Function or Cursor Key sequence. | 2218 | // be a Function or Cursor Key sequence. |
2227 | FD_ZERO(&rfds); | 2219 | struct pollfd pfd[1]; |
2228 | FD_SET(0, &rfds); | 2220 | pfd[0].fd = 0; |
2229 | tv.tv_sec = 0; | 2221 | pfd[0].events = POLLIN; |
2230 | tv.tv_usec = 50000; // Wait 5/100 seconds- 1 Sec=1000000 | 2222 | // Wait 50 ms |
2231 | |||
2232 | // keep reading while there are input chars and room in buffer | 2223 | // keep reading while there are input chars and room in buffer |
2233 | while (select(1, &rfds, NULL, NULL, &tv) > 0 && n <= (MAX_LINELEN - 5)) { | 2224 | while (poll(pfd, 1, 50) > 0 && n <= (MAX_LINELEN - 5)) { |
2234 | // read the rest of the ESC string | 2225 | // read the rest of the ESC string |
2235 | int r = read(0, (void *) (readbuffer + n), MAX_LINELEN - n); | 2226 | int r = read(0, readbuffer + n, MAX_LINELEN - n); |
2236 | if (r > 0) { | 2227 | if (r > 0) |
2237 | n += r; | 2228 | n += r; |
2238 | } | ||
2239 | } | 2229 | } |
2240 | } | 2230 | } |
2241 | readed_for_parse = n; | 2231 | readed_for_parse = n; |
diff --git a/networking/tftp.c b/networking/tftp.c index 0b25f752d..ac3a86afb 100644 --- a/networking/tftp.c +++ b/networking/tftp.c | |||
@@ -23,10 +23,10 @@ | |||
23 | 23 | ||
24 | #if ENABLE_FEATURE_TFTP_GET || ENABLE_FEATURE_TFTP_PUT | 24 | #if ENABLE_FEATURE_TFTP_GET || ENABLE_FEATURE_TFTP_PUT |
25 | 25 | ||
26 | #define TFTP_BLOCKSIZE_DEFAULT 512 /* according to RFC 1350, don't change */ | 26 | #define TFTP_BLOCKSIZE_DEFAULT 512 /* according to RFC 1350, don't change */ |
27 | #define TFTP_TIMEOUT 50000 /* 50ms, in microseconds */ | 27 | #define TFTP_TIMEOUT_MS 50 |
28 | #define TFTP_MAXTIMEOUT 999000 /* about 1 second, in microseconds */ | 28 | #define TFTP_MAXTIMEOUT_MS 2000 |
29 | #define TFTP_NUM_RETRIES 12 /* number of backed-off retries */ | 29 | #define TFTP_NUM_RETRIES 12 /* number of backed-off retries */ |
30 | 30 | ||
31 | /* opcodes we support */ | 31 | /* opcodes we support */ |
32 | #define TFTP_RRQ 1 | 32 | #define TFTP_RRQ 1 |
@@ -114,9 +114,8 @@ static int tftp( USE_GETPUT(const int cmd,) | |||
114 | const char *remotefile, const int localfd, | 114 | const char *remotefile, const int localfd, |
115 | unsigned port, int tftp_bufsize) | 115 | unsigned port, int tftp_bufsize) |
116 | { | 116 | { |
117 | struct timeval tv; | 117 | struct pollfd pfd[1]; |
118 | fd_set rfds; | 118 | #define socketfd (pfd[0].fd) |
119 | int socketfd; | ||
120 | int len; | 119 | int len; |
121 | int send_len; | 120 | int send_len; |
122 | USE_FEATURE_TFTP_BLOCKSIZE(smallint want_option_ack = 0;) | 121 | USE_FEATURE_TFTP_BLOCKSIZE(smallint want_option_ack = 0;) |
@@ -124,7 +123,7 @@ static int tftp( USE_GETPUT(const int cmd,) | |||
124 | uint16_t opcode; | 123 | uint16_t opcode; |
125 | uint16_t block_nr = 1; | 124 | uint16_t block_nr = 1; |
126 | uint16_t recv_blk; | 125 | uint16_t recv_blk; |
127 | int retries, waittime; | 126 | int retries, waittime_ms; |
128 | char *cp; | 127 | char *cp; |
129 | 128 | ||
130 | unsigned org_port; | 129 | unsigned org_port; |
@@ -208,7 +207,7 @@ static int tftp( USE_GETPUT(const int cmd,) | |||
208 | * for potential resend */ | 207 | * for potential resend */ |
209 | 208 | ||
210 | retries = TFTP_NUM_RETRIES; /* re-initialize */ | 209 | retries = TFTP_NUM_RETRIES; /* re-initialize */ |
211 | waittime = TFTP_TIMEOUT; | 210 | waittime_ms = TFTP_TIMEOUT_MS; |
212 | 211 | ||
213 | send_again: | 212 | send_again: |
214 | #if ENABLE_DEBUG_TFTP | 213 | #if ENABLE_DEBUG_TFTP |
@@ -224,11 +223,9 @@ static int tftp( USE_GETPUT(const int cmd,) | |||
224 | 223 | ||
225 | recv_again: | 224 | recv_again: |
226 | /* Receive packet */ | 225 | /* Receive packet */ |
227 | tv.tv_sec = 0; | 226 | /*pfd[0].fd = socketfd;*/ |
228 | tv.tv_usec = waittime; | 227 | pfd[0].events = POLLIN; |
229 | FD_ZERO(&rfds); | 228 | switch (poll(pfd, 1, waittime_ms)) { |
230 | FD_SET(socketfd, &rfds); | ||
231 | switch (select(socketfd + 1, &rfds, NULL, NULL, &tv)) { | ||
232 | unsigned from_port; | 229 | unsigned from_port; |
233 | case 1: | 230 | case 1: |
234 | from->len = peer_lsa->len; | 231 | from->len = peer_lsa->len; |
@@ -258,14 +255,14 @@ static int tftp( USE_GETPUT(const int cmd,) | |||
258 | } | 255 | } |
259 | 256 | ||
260 | /* exponential backoff with limit */ | 257 | /* exponential backoff with limit */ |
261 | waittime += waittime/2; | 258 | waittime_ms += waittime_ms/2; |
262 | if (waittime > TFTP_MAXTIMEOUT) { | 259 | if (waittime_ms > TFTP_MAXTIMEOUT_MS) { |
263 | waittime = TFTP_MAXTIMEOUT; | 260 | waittime_ms = TFTP_MAXTIMEOUT_MS; |
264 | } | 261 | } |
265 | 262 | ||
266 | goto send_again; /* resend last sent pkt */ | 263 | goto send_again; /* resend last sent pkt */ |
267 | default: | 264 | default: |
268 | bb_perror_msg("select"); | 265 | bb_perror_msg("poll"); |
269 | goto ret; | 266 | goto ret; |
270 | } | 267 | } |
271 | process_pkt: | 268 | process_pkt: |
diff --git a/networking/traceroute.c b/networking/traceroute.c index 236ddbdaf..21921e56d 100644 --- a/networking/traceroute.c +++ b/networking/traceroute.c | |||
@@ -346,10 +346,10 @@ static int optlen; /* length of ip options */ | |||
346 | 346 | ||
347 | 347 | ||
348 | struct globals { | 348 | struct globals { |
349 | /* last inbound (icmp) packet */ | ||
350 | unsigned char packet[512]; | ||
351 | struct sockaddr_storage whereto; /* Who to try to reach */ | 349 | struct sockaddr_storage whereto; /* Who to try to reach */ |
352 | struct sockaddr_storage wherefrom; /* Who we are */ | 350 | struct sockaddr_storage wherefrom; /* Who we are */ |
351 | /* last inbound (icmp) packet */ | ||
352 | unsigned char packet[512]; | ||
353 | #if ENABLE_FEATURE_TRACEROUTE_SOURCE_ROUTE | 353 | #if ENABLE_FEATURE_TRACEROUTE_SOURCE_ROUTE |
354 | /* Maximum number of gateways (include room for one noop) */ | 354 | /* Maximum number of gateways (include room for one noop) */ |
355 | #define NGATEWAYS ((int)((MAX_IPOPTLEN - IPOPT_MINOFF - 1) / sizeof(uint32_t))) | 355 | #define NGATEWAYS ((int)((MAX_IPOPTLEN - IPOPT_MINOFF - 1) / sizeof(uint32_t))) |
@@ -359,7 +359,7 @@ struct globals { | |||
359 | }; | 359 | }; |
360 | 360 | ||
361 | #define G (*ptr_to_globals) | 361 | #define G (*ptr_to_globals) |
362 | 362 | #define INIT_G() PTR_TO_GLOBALS = xzalloc(sizeof(G)) | |
363 | #define packet (G.packet ) | 363 | #define packet (G.packet ) |
364 | #define whereto (G.whereto ) | 364 | #define whereto (G.whereto ) |
365 | #define wherefrom (G.wherefrom) | 365 | #define wherefrom (G.wherefrom) |
@@ -537,21 +537,15 @@ findsaddr(const struct sockaddr_in *to, struct sockaddr_in *from) | |||
537 | static int | 537 | static int |
538 | wait_for_reply(int sock, struct sockaddr_in *fromp) | 538 | wait_for_reply(int sock, struct sockaddr_in *fromp) |
539 | { | 539 | { |
540 | fd_set fds; | 540 | struct pollfd pfd[1]; |
541 | struct timeval tvwait; | ||
542 | int cc = 0; | 541 | int cc = 0; |
543 | socklen_t fromlen = sizeof(*fromp); | 542 | socklen_t fromlen = sizeof(*fromp); |
544 | 543 | ||
545 | FD_ZERO(&fds); | 544 | pfd[0].fd = sock; |
546 | FD_SET(sock, &fds); | 545 | pfd[0].events = POLLIN; |
547 | 546 | if (poll(pfd, 1, waittime * 1000) > 0) | |
548 | tvwait.tv_sec = waittime; | 547 | cc = recvfrom(sock, packet, sizeof(packet), 0, |
549 | tvwait.tv_usec = 0; | ||
550 | |||
551 | if (select(sock + 1, &fds, NULL, NULL, &tvwait) > 0) | ||
552 | cc = recvfrom(sock, (char *)packet, sizeof(packet), 0, | ||
553 | (struct sockaddr *)fromp, &fromlen); | 548 | (struct sockaddr *)fromp, &fromlen); |
554 | |||
555 | return cc; | 549 | return cc; |
556 | } | 550 | } |
557 | 551 | ||
@@ -930,7 +924,7 @@ int traceroute_main(int argc, char **argv) | |||
930 | llist_t *source_route_list = NULL; | 924 | llist_t *source_route_list = NULL; |
931 | #endif | 925 | #endif |
932 | 926 | ||
933 | PTR_TO_GLOBALS = xzalloc(sizeof(G)); | 927 | INIT_G(); |
934 | from = (struct sockaddr_in *)&wherefrom; | 928 | from = (struct sockaddr_in *)&wherefrom; |
935 | to = (struct sockaddr_in *)&whereto; | 929 | to = (struct sockaddr_in *)&whereto; |
936 | 930 | ||
diff --git a/networking/udhcp/arpping.c b/networking/udhcp/arpping.c index 4ac52c640..33518077b 100644 --- a/networking/udhcp/arpping.c +++ b/networking/udhcp/arpping.c | |||
@@ -37,14 +37,12 @@ struct arpMsg { | |||
37 | 37 | ||
38 | int arpping(uint32_t test_ip, uint32_t from_ip, uint8_t *from_mac, const char *interface) | 38 | int arpping(uint32_t test_ip, uint32_t from_ip, uint8_t *from_mac, const char *interface) |
39 | { | 39 | { |
40 | int timeout = 2; | 40 | int timeout_ms = 2000; |
41 | int s; /* socket */ | 41 | struct pollfd pfd[1]; |
42 | #define s (pfd[0].fd) /* socket */ | ||
42 | int rv = 1; /* "no reply received" yet */ | 43 | int rv = 1; /* "no reply received" yet */ |
43 | struct sockaddr addr; /* for interface name */ | 44 | struct sockaddr addr; /* for interface name */ |
44 | struct arpMsg arp; | 45 | struct arpMsg arp; |
45 | fd_set fdset; | ||
46 | struct timeval tm; | ||
47 | unsigned prevTime; | ||
48 | 46 | ||
49 | s = socket(PF_PACKET, SOCK_PACKET, htons(ETH_P_ARP)); | 47 | s = socket(PF_PACKET, SOCK_PACKET, htons(ETH_P_ARP)); |
50 | if (s == -1) { | 48 | if (s == -1) { |
@@ -80,18 +78,17 @@ int arpping(uint32_t test_ip, uint32_t from_ip, uint8_t *from_mac, const char *i | |||
80 | /* wait for arp reply, and check it */ | 78 | /* wait for arp reply, and check it */ |
81 | do { | 79 | do { |
82 | int r; | 80 | int r; |
83 | prevTime = monotonic_sec(); | 81 | unsigned prevTime = monotonic_us(); |
84 | FD_ZERO(&fdset); | 82 | |
85 | FD_SET(s, &fdset); | 83 | pfd[0].events = POLLIN; |
86 | tm.tv_sec = timeout; | 84 | r = poll(pfd, 1, timeout_ms); |
87 | tm.tv_usec = 0; | ||
88 | r = select(s + 1, &fdset, NULL, NULL, &tm); | ||
89 | if (r < 0) { | 85 | if (r < 0) { |
90 | bb_perror_msg("error on ARPING request"); | 86 | if (errno != EINTR) { |
91 | if (errno != EINTR) | 87 | bb_perror_msg("poll"); |
92 | break; | 88 | break; |
89 | } | ||
93 | } else if (r) { | 90 | } else if (r) { |
94 | if (recv(s, &arp, sizeof(arp), 0) < 0) | 91 | if (read(s, &arp, sizeof(arp)) < 0) |
95 | break; | 92 | break; |
96 | if (arp.operation == htons(ARPOP_REPLY) | 93 | if (arp.operation == htons(ARPOP_REPLY) |
97 | && memcmp(arp.tHaddr, from_mac, 6) == 0 | 94 | && memcmp(arp.tHaddr, from_mac, 6) == 0 |
@@ -101,8 +98,8 @@ int arpping(uint32_t test_ip, uint32_t from_ip, uint8_t *from_mac, const char *i | |||
101 | break; | 98 | break; |
102 | } | 99 | } |
103 | } | 100 | } |
104 | timeout -= monotonic_sec() - prevTime; | 101 | timeout_ms -= (monotonic_us() - prevTime) / 1000; |
105 | } while (timeout > 0); | 102 | } while (timeout_ms > 0); |
106 | 103 | ||
107 | ret: | 104 | ret: |
108 | close(s); | 105 | close(s); |
diff --git a/shell/ash.c b/shell/ash.c index 46f00dd3d..ec3e17618 100644 --- a/shell/ash.c +++ b/shell/ash.c | |||
@@ -11589,6 +11589,7 @@ readcmd(int argc, char **argv) | |||
11589 | #endif | 11589 | #endif |
11590 | #if ENABLE_ASH_READ_TIMEOUT | 11590 | #if ENABLE_ASH_READ_TIMEOUT |
11591 | if (ts.tv_sec || ts.tv_usec) { | 11591 | if (ts.tv_sec || ts.tv_usec) { |
11592 | // TODO: replace with poll, it is smaller | ||
11592 | FD_ZERO(&set); | 11593 | FD_ZERO(&set); |
11593 | FD_SET(0, &set); | 11594 | FD_SET(0, &set); |
11594 | 11595 | ||
diff --git a/sysklogd/syslogd.c b/sysklogd/syslogd.c index 5b153f509..ae3f1a2eb 100644 --- a/sysklogd/syslogd.c +++ b/sysklogd/syslogd.c | |||
@@ -471,8 +471,8 @@ static void do_syslogd(void) ATTRIBUTE_NORETURN; | |||
471 | static void do_syslogd(void) | 471 | static void do_syslogd(void) |
472 | { | 472 | { |
473 | struct sockaddr_un sunx; | 473 | struct sockaddr_un sunx; |
474 | int sock_fd; | 474 | struct pollfd pfd[1]; |
475 | fd_set fds; | 475 | #define sock_fd (pfd[0].fd) |
476 | char *dev_log_name; | 476 | char *dev_log_name; |
477 | 477 | ||
478 | /* Set up signal handlers */ | 478 | /* Set up signal handlers */ |
@@ -526,20 +526,20 @@ static void do_syslogd(void) | |||
526 | (char*)"syslogd started: BusyBox v" BB_VER, 0); | 526 | (char*)"syslogd started: BusyBox v" BB_VER, 0); |
527 | 527 | ||
528 | for (;;) { | 528 | for (;;) { |
529 | FD_ZERO(&fds); | 529 | /*pfd[0].fd = sock_fd;*/ |
530 | FD_SET(sock_fd, &fds); | 530 | pfd[0].events = POLLIN; |
531 | 531 | pfd[0].revents = 0; | |
532 | if (select(sock_fd + 1, &fds, NULL, NULL, NULL) < 0) { | 532 | if (poll(pfd, 1, -1) < 0) { /* -1: no timeout */ |
533 | if (errno == EINTR) { | 533 | if (errno == EINTR) { |
534 | /* alarm may have happened */ | 534 | /* alarm may have happened */ |
535 | continue; | 535 | continue; |
536 | } | 536 | } |
537 | bb_perror_msg_and_die("select"); | 537 | bb_perror_msg_and_die("poll"); |
538 | } | 538 | } |
539 | 539 | ||
540 | if (FD_ISSET(sock_fd, &fds)) { | 540 | if (pfd[0].revents) { |
541 | int i; | 541 | int i; |
542 | i = recv(sock_fd, G.recvbuf, MAX_READ - 1, 0); | 542 | i = read(sock_fd, G.recvbuf, MAX_READ - 1); |
543 | if (i <= 0) | 543 | if (i <= 0) |
544 | bb_perror_msg_and_die("UNIX socket error"); | 544 | bb_perror_msg_and_die("UNIX socket error"); |
545 | /* TODO: maybe suppress duplicates? */ | 545 | /* TODO: maybe suppress duplicates? */ |
@@ -559,7 +559,7 @@ static void do_syslogd(void) | |||
559 | #endif | 559 | #endif |
560 | G.recvbuf[i] = '\0'; | 560 | G.recvbuf[i] = '\0'; |
561 | split_escape_and_log(G.recvbuf, i); | 561 | split_escape_and_log(G.recvbuf, i); |
562 | } /* FD_ISSET() */ | 562 | } |
563 | } /* for */ | 563 | } /* for */ |
564 | } | 564 | } |
565 | 565 | ||