aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2007-06-17 23:40:26 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2007-06-17 23:40:26 +0000
commitbd7bb299c0e9ee5ff52c9d12b46fb14a907b34da (patch)
tree117a635e9457e1a932bc64941db5869e33179fc9
parent459be35234cc24b69309eb0ee22600024c73713e (diff)
downloadbusybox-w32-bd7bb299c0e9ee5ff52c9d12b46fb14a907b34da.tar.gz
busybox-w32-bd7bb299c0e9ee5ff52c9d12b46fb14a907b34da.tar.bz2
busybox-w32-bd7bb299c0e9ee5ff52c9d12b46fb14a907b34da.zip
wget: use monotonic_sec instead of gettimeofday
zcip: use monotonic_us instead of gettimeofday udhcpcd: simpler, shorter random_xid() function old new delta monotonic_sec - 41 +41 find_pair 164 180 +16 run_list_real 2018 2028 +10 cmp_main 547 555 +8 collect_ctx 112 119 +7 singlemount 4544 4549 +5 time_main 1124 1128 +4 static.start_sec - 4 +4 static.lastupdate_sec - 4 +4 sock - 4 +4 read_package_field 253 257 +4 pick 38 40 +2 get_next_line 145 147 +2 count_lines 59 61 +2 process_stdin 435 433 -2 xstrtoul_range_sfx 229 226 -3 static.initialized 4 1 -3 dhcprelay_main 1125 1122 -3 catcher 380 377 -3 arping_main 1969 1966 -3 s 8 4 -4 cfg 4 - -4 static.lastupdate 8 - -8 start 8 - -8 random_xid 95 33 -62 .rodata 129114 129050 -64 zcip_main 1731 1576 -155 progressmeter 1035 867 -168 ------------------------------------------------------------------------------ (add/remove: 4/3 grow/shrink: 10/11 up/down: 113/-490) Total: -377 bytes
-rw-r--r--include/libbb.h1
-rw-r--r--libbb/time.c15
-rw-r--r--miscutils/time.c7
-rw-r--r--networking/arping.c83
-rw-r--r--networking/udhcp/clientpacket.c16
-rw-r--r--networking/udhcp/dhcpc.h2
-rw-r--r--networking/wget.c56
-rw-r--r--networking/zcip.c115
8 files changed, 151 insertions, 144 deletions
diff --git a/include/libbb.h b/include/libbb.h
index 2a50bce66..3c9f9fe81 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -201,6 +201,7 @@ struct sysinfo {
201int sysinfo(struct sysinfo* info); 201int sysinfo(struct sysinfo* info);
202 202
203unsigned long long monotonic_us(void); 203unsigned long long monotonic_us(void);
204unsigned monotonic_sec(void);
204 205
205extern void chomp(char *s); 206extern void chomp(char *s);
206extern void trim(char *s); 207extern void trim(char *s);
diff --git a/libbb/time.c b/libbb/time.c
index f2a741521..3e35542f3 100644
--- a/libbb/time.c
+++ b/libbb/time.c
@@ -21,12 +21,23 @@ unsigned long long monotonic_us(void)
21 bb_error_msg_and_die("clock_gettime(MONOTONIC) failed"); 21 bb_error_msg_and_die("clock_gettime(MONOTONIC) failed");
22 return ts.tv_sec * 1000000ULL + ts.tv_nsec/1000; 22 return ts.tv_sec * 1000000ULL + ts.tv_nsec/1000;
23} 23}
24unsigned monotonic_sec(void)
25{
26 struct timespec ts;
27 if (syscall(__NR_clock_gettime, CLOCK_MONOTONIC, &ts))
28 bb_error_msg_and_die("clock_gettime(MONOTONIC) failed");
29 return ts.tv_sec;
30}
24#else 31#else
25unsigned long long monotonic_us(void) 32unsigned long long monotonic_us(void)
26{ 33{
27 struct timeval tv; 34 struct timeval tv;
28 if (gettimeofday(&tv, NULL)) 35 gettimeofday(&tv, NULL);
29 bb_error_msg_and_die("gettimeofday failed");
30 return tv.tv_sec * 1000000ULL + tv_usec; 36 return tv.tv_sec * 1000000ULL + tv_usec;
31} 37}
38
39unsigned monotonic_sec(void)
40{
41 return time(NULL);
42}
32#endif 43#endif
diff --git a/miscutils/time.c b/miscutils/time.c
index 578cb35ff..9b7db662a 100644
--- a/miscutils/time.c
+++ b/miscutils/time.c
@@ -180,7 +180,12 @@ static void summarize(const char *fmt, char **command, resource_t * resp)
180 vv_ms = (resp->ru.ru_utime.tv_sec + resp->ru.ru_stime.tv_sec) * 1000 180 vv_ms = (resp->ru.ru_utime.tv_sec + resp->ru.ru_stime.tv_sec) * 1000
181 + (resp->ru.ru_utime.tv_usec + resp->ru.ru_stime.tv_usec) / 1000; 181 + (resp->ru.ru_utime.tv_usec + resp->ru.ru_stime.tv_usec) / 1000;
182 182
183 cpu_ticks = vv_ms * TICKS_PER_SEC / 1000; 183#if (1000 / TICKS_PER_SEC) * TICKS_PER_SEC == 1000
184 /* 1000 is exactly divisible by TICKS_PER_SEC */
185 cpu_ticks = vv_ms / (1000 / TICKS_PER_SEC);
186#else
187 cpu_ticks = vv_ms * (unsigned long long)TICKS_PER_SEC / 1000;
188#endif
184 if (!cpu_ticks) cpu_ticks = 1; /* we divide by it, must be nonzero */ 189 if (!cpu_ticks) cpu_ticks = 1; /* we divide by it, must be nonzero */
185 190
186 /* putchar() != putc(stdout) in glibc! */ 191 /* putchar() != putc(stdout) in glibc! */
diff --git a/networking/arping.c b/networking/arping.c
index a022e70ea..7a9fbc2f4 100644
--- a/networking/arping.c
+++ b/networking/arping.c
@@ -33,18 +33,17 @@ enum {
33 BCAST_ONLY = 32, 33 BCAST_ONLY = 32,
34 UNICASTING = 64 34 UNICASTING = 64
35}; 35};
36static int cfg;
37 36
38static int s; 37static int sock;
39static unsigned count = UINT_MAX; 38static unsigned count = UINT_MAX;
40static unsigned timeout_us; 39static unsigned timeout_us;
41static int sent; 40static unsigned sent;
42static int brd_sent; 41static unsigned brd_sent;
43static int received; 42static unsigned received;
44static int brd_recv; 43static unsigned brd_recv;
45static int req_recv; 44static unsigned req_recv;
46 45
47static int send_pack(int sock, struct in_addr *src_addr, 46static int send_pack(struct in_addr *src_addr,
48 struct in_addr *dst_addr, struct sockaddr_ll *ME, 47 struct in_addr *dst_addr, struct sockaddr_ll *ME,
49 struct sockaddr_ll *HE) 48 struct sockaddr_ll *HE)
50{ 49{
@@ -59,7 +58,7 @@ static int send_pack(int sock, struct in_addr *src_addr,
59 ah->ar_pro = htons(ETH_P_IP); 58 ah->ar_pro = htons(ETH_P_IP);
60 ah->ar_hln = ME->sll_halen; 59 ah->ar_hln = ME->sll_halen;
61 ah->ar_pln = 4; 60 ah->ar_pln = 4;
62 ah->ar_op = cfg & ADVERT ? htons(ARPOP_REPLY) : htons(ARPOP_REQUEST); 61 ah->ar_op = option_mask32 & ADVERT ? htons(ARPOP_REPLY) : htons(ARPOP_REQUEST);
63 62
64 memcpy(p, &ME->sll_addr, ah->ar_hln); 63 memcpy(p, &ME->sll_addr, ah->ar_hln);
65 p += ME->sll_halen; 64 p += ME->sll_halen;
@@ -67,7 +66,7 @@ static int send_pack(int sock, struct in_addr *src_addr,
67 memcpy(p, src_addr, 4); 66 memcpy(p, src_addr, 4);
68 p += 4; 67 p += 4;
69 68
70 if (cfg & ADVERT) 69 if (option_mask32 & ADVERT)
71 memcpy(p, &ME->sll_addr, ah->ar_hln); 70 memcpy(p, &ME->sll_addr, ah->ar_hln);
72 else 71 else
73 memcpy(p, &HE->sll_addr, ah->ar_hln); 72 memcpy(p, &HE->sll_addr, ah->ar_hln);
@@ -81,7 +80,7 @@ static int send_pack(int sock, struct in_addr *src_addr,
81 if (err == p - buf) { 80 if (err == p - buf) {
82 last = now; 81 last = now;
83 sent++; 82 sent++;
84 if (!(cfg & UNICASTING)) 83 if (!(option_mask32 & UNICASTING))
85 brd_sent++; 84 brd_sent++;
86 } 85 }
87 return err; 86 return err;
@@ -90,17 +89,17 @@ static int send_pack(int sock, struct in_addr *src_addr,
90static void finish(void) ATTRIBUTE_NORETURN; 89static void finish(void) ATTRIBUTE_NORETURN;
91static void finish(void) 90static void finish(void)
92{ 91{
93 if (!(cfg & QUIET)) { 92 if (!(option_mask32 & QUIET)) {
94 printf("Sent %d probe(s) (%d broadcast(s))\n" 93 printf("Sent %u probe(s) (%u broadcast(s))\n"
95 "Received %d repl%s" 94 "Received %u repl%s"
96 " (%d request(s), %d broadcast(s))\n", 95 " (%u request(s), %u broadcast(s))\n",
97 sent, brd_sent, 96 sent, brd_sent,
98 received, (received == 1) ? "ies" : "y", 97 received, (received == 1) ? "ies" : "y",
99 req_recv, brd_recv); 98 req_recv, brd_recv);
100 } 99 }
101 if (cfg & DAD) 100 if (option_mask32 & DAD)
102 exit(!!received); 101 exit(!!received);
103 if (cfg & UNSOLICITED) 102 if (option_mask32 & UNSOLICITED)
104 exit(0); 103 exit(0);
105 exit(!received); 104 exit(!received);
106} 105}
@@ -121,8 +120,8 @@ static void catcher(void)
121 count--; 120 count--;
122 121
123 if (last == 0 || (now - last) > 500000) { 122 if (last == 0 || (now - last) > 500000) {
124 send_pack(s, &src, &dst, &me, &he); 123 send_pack(&src, &dst, &me, &he);
125 if (count == 0 && (cfg & UNSOLICITED)) 124 if (count == 0 && (option_mask32 & UNSOLICITED))
126 finish(); 125 finish();
127 } 126 }
128 alarm(1); 127 alarm(1);
@@ -160,7 +159,7 @@ static int recv_pack(unsigned char *buf, int len, struct sockaddr_ll *FROM)
160 return 0; 159 return 0;
161 memcpy(&src_ip, p + ah->ar_hln, 4); 160 memcpy(&src_ip, p + ah->ar_hln, 4);
162 memcpy(&dst_ip, p + ah->ar_hln + 4 + ah->ar_hln, 4); 161 memcpy(&dst_ip, p + ah->ar_hln + 4 + ah->ar_hln, 4);
163 if (!(cfg & DAD)) { 162 if (!(option_mask32 & DAD)) {
164 if (src_ip.s_addr != dst.s_addr) 163 if (src_ip.s_addr != dst.s_addr)
165 return 0; 164 return 0;
166 if (src.s_addr != dst_ip.s_addr) 165 if (src.s_addr != dst_ip.s_addr)
@@ -188,7 +187,7 @@ static int recv_pack(unsigned char *buf, int len, struct sockaddr_ll *FROM)
188 if (src.s_addr && src.s_addr != dst_ip.s_addr) 187 if (src.s_addr && src.s_addr != dst_ip.s_addr)
189 return 0; 188 return 0;
190 } 189 }
191 if (!(cfg & QUIET)) { 190 if (!(option_mask32 & QUIET)) {
192 int s_printed = 0; 191 int s_printed = 0;
193 192
194 printf("%scast re%s from %s [%s]", 193 printf("%scast re%s from %s [%s]",
@@ -219,11 +218,11 @@ static int recv_pack(unsigned char *buf, int len, struct sockaddr_ll *FROM)
219 brd_recv++; 218 brd_recv++;
220 if (ah->ar_op == htons(ARPOP_REQUEST)) 219 if (ah->ar_op == htons(ARPOP_REQUEST))
221 req_recv++; 220 req_recv++;
222 if (cfg & QUIT_ON_REPLY) 221 if (option_mask32 & QUIT_ON_REPLY)
223 finish(); 222 finish();
224 if (!(cfg & BCAST_ONLY)) { 223 if (!(option_mask32 & BCAST_ONLY)) {
225 memcpy(he.sll_addr, p, me.sll_halen); 224 memcpy(he.sll_addr, p, me.sll_halen);
226 cfg |= UNICASTING; 225 option_mask32 |= UNICASTING;
227 } 226 }
228 return 1; 227 return 1;
229} 228}
@@ -237,32 +236,32 @@ int arping_main(int argc, char **argv)
237 char *target; 236 char *target;
238 unsigned char *packet; 237 unsigned char *packet;
239 238
240 s = xsocket(PF_PACKET, SOCK_DGRAM, 0); 239 sock = xsocket(PF_PACKET, SOCK_DGRAM, 0);
241 240
242 // Drop suid root privileges 241 // Drop suid root privileges
243 xsetuid(getuid()); 242 xsetuid(getuid());
244 243
245 { 244 {
246 unsigned opt; 245 unsigned opt;
247 char *_count, *_timeout; 246 char *str_count, *str_timeout;
248 247
249 /* Dad also sets quit_on_reply. 248 /* Dad also sets quit_on_reply.
250 * Advert also sets unsolicited. 249 * Advert also sets unsolicited.
251 */ 250 */
252 opt_complementary = "=1:Df:AU"; 251 opt_complementary = "=1:Df:AU";
253 opt = getopt32(argc, argv, "DUAqfbc:w:I:s:", 252 opt = getopt32(argc, argv, "DUAqfbc:w:I:s:",
254 &_count, &_timeout, &device, &source); 253 &str_count, &str_timeout, &device, &source);
255 cfg |= opt & 0x3f; /* set respective flags */
256 if (opt & 0x40) /* -c: count */ 254 if (opt & 0x40) /* -c: count */
257 count = xatou(_count); 255 count = xatou(str_count);
258 if (opt & 0x80) /* -w: timeout */ 256 if (opt & 0x80) /* -w: timeout */
259 timeout_us = xatou_range(_timeout, 0, INT_MAX/2000000) * 1000000; 257 timeout_us = xatou_range(str_timeout, 0, INT_MAX/2000000) * 1000000;
260 //if (opt & 0x100) /* -I: interface */ 258 //if (opt & 0x100) /* -I: interface */
261 if (strlen(device) >= IF_NAMESIZE) { 259 if (strlen(device) >= IF_NAMESIZE) {
262 bb_error_msg_and_die("interface name '%s' is too long", 260 bb_error_msg_and_die("interface name '%s' is too long",
263 device); 261 device);
264 } 262 }
265 //if (opt & 0x200) /* -s: source */ 263 //if (opt & 0x200) /* -s: source */
264 option_mask32 &= 0x3f; /* set respective flags */
266 } 265 }
267 266
268 target = argv[optind]; 267 target = argv[optind];
@@ -274,12 +273,12 @@ int arping_main(int argc, char **argv)
274 273
275 memset(&ifr, 0, sizeof(ifr)); 274 memset(&ifr, 0, sizeof(ifr));
276 strncpy(ifr.ifr_name, device, IFNAMSIZ - 1); 275 strncpy(ifr.ifr_name, device, IFNAMSIZ - 1);
277 if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) { 276 if (ioctl(sock, SIOCGIFINDEX, &ifr) < 0) {
278 bb_error_msg_and_die("interface %s not found", device); 277 bb_error_msg_and_die("interface %s not found", device);
279 } 278 }
280 ifindex = ifr.ifr_ifindex; 279 ifindex = ifr.ifr_ifindex;
281 280
282 if (ioctl(s, SIOCGIFFLAGS, (char *) &ifr)) { 281 if (ioctl(sock, SIOCGIFFLAGS, (char *) &ifr)) {
283 bb_error_msg_and_die("SIOCGIFFLAGS"); 282 bb_error_msg_and_die("SIOCGIFFLAGS");
284 } 283 }
285 if (!(ifr.ifr_flags & IFF_UP)) { 284 if (!(ifr.ifr_flags & IFF_UP)) {
@@ -287,7 +286,7 @@ int arping_main(int argc, char **argv)
287 } 286 }
288 if (ifr.ifr_flags & (IFF_NOARP | IFF_LOOPBACK)) { 287 if (ifr.ifr_flags & (IFF_NOARP | IFF_LOOPBACK)) {
289 bb_error_msg("interface %s is not ARPable", device); 288 bb_error_msg("interface %s is not ARPable", device);
290 return (cfg & DAD ? 0 : 2); 289 return (option_mask32 & DAD ? 0 : 2);
291 } 290 }
292 } 291 }
293 292
@@ -303,10 +302,10 @@ int arping_main(int argc, char **argv)
303 bb_error_msg_and_die("invalid source address %s", source); 302 bb_error_msg_and_die("invalid source address %s", source);
304 } 303 }
305 304
306 if (!(cfg & DAD) && (cfg & UNSOLICITED) && src.s_addr == 0) 305 if (!(option_mask32 & DAD) && (option_mask32 & UNSOLICITED) && src.s_addr == 0)
307 src = dst; 306 src = dst;
308 307
309 if (!(cfg & DAD) || src.s_addr) { 308 if (!(option_mask32 & DAD) || src.s_addr) {
310 struct sockaddr_in saddr; 309 struct sockaddr_in saddr;
311 int probe_fd = xsocket(AF_INET, SOCK_DGRAM, 0); 310 int probe_fd = xsocket(AF_INET, SOCK_DGRAM, 0);
312 311
@@ -319,7 +318,7 @@ int arping_main(int argc, char **argv)
319 if (src.s_addr) { 318 if (src.s_addr) {
320 saddr.sin_addr = src; 319 saddr.sin_addr = src;
321 xbind(probe_fd, (struct sockaddr *) &saddr, sizeof(saddr)); 320 xbind(probe_fd, (struct sockaddr *) &saddr, sizeof(saddr));
322 } else if (!(cfg & DAD)) { 321 } else if (!(option_mask32 & DAD)) {
323 socklen_t alen = sizeof(saddr); 322 socklen_t alen = sizeof(saddr);
324 323
325 saddr.sin_port = htons(1025); 324 saddr.sin_port = htons(1025);
@@ -339,29 +338,29 @@ int arping_main(int argc, char **argv)
339 me.sll_family = AF_PACKET; 338 me.sll_family = AF_PACKET;
340 me.sll_ifindex = ifindex; 339 me.sll_ifindex = ifindex;
341 me.sll_protocol = htons(ETH_P_ARP); 340 me.sll_protocol = htons(ETH_P_ARP);
342 xbind(s, (struct sockaddr *) &me, sizeof(me)); 341 xbind(sock, (struct sockaddr *) &me, sizeof(me));
343 342
344 { 343 {
345 socklen_t alen = sizeof(me); 344 socklen_t alen = sizeof(me);
346 345
347 if (getsockname(s, (struct sockaddr *) &me, &alen) == -1) { 346 if (getsockname(sock, (struct sockaddr *) &me, &alen) == -1) {
348 bb_error_msg_and_die("getsockname"); 347 bb_error_msg_and_die("getsockname");
349 } 348 }
350 } 349 }
351 if (me.sll_halen == 0) { 350 if (me.sll_halen == 0) {
352 bb_error_msg("interface \"%s\" is not ARPable (no ll address)", device); 351 bb_error_msg("interface \"%s\" is not ARPable (no ll address)", device);
353 return (cfg & DAD ? 0 : 2); 352 return (option_mask32 & DAD ? 0 : 2);
354 } 353 }
355 he = me; 354 he = me;
356 memset(he.sll_addr, -1, he.sll_halen); 355 memset(he.sll_addr, -1, he.sll_halen);
357 356
358 if (!(cfg & QUIET)) { 357 if (!(option_mask32 & QUIET)) {
359 printf("ARPING to %s from %s via %s\n", 358 printf("ARPING to %s from %s via %s\n",
360 inet_ntoa(dst), inet_ntoa(src), 359 inet_ntoa(dst), inet_ntoa(src),
361 device ? device : "unknown"); 360 device ? device : "unknown");
362 } 361 }
363 362
364 if (!src.s_addr && !(cfg & DAD)) { 363 if (!src.s_addr && !(option_mask32 & DAD)) {
365 bb_error_msg_and_die("no src address in the non-DAD mode"); 364 bb_error_msg_and_die("no src address in the non-DAD mode");
366 } 365 }
367 366
@@ -387,7 +386,7 @@ int arping_main(int argc, char **argv)
387 socklen_t alen = sizeof(from); 386 socklen_t alen = sizeof(from);
388 int cc; 387 int cc;
389 388
390 cc = recvfrom(s, packet, 4096, 0, (struct sockaddr *) &from, &alen); 389 cc = recvfrom(sock, packet, 4096, 0, (struct sockaddr *) &from, &alen);
391 if (cc < 0) { 390 if (cc < 0) {
392 bb_perror_msg("recvfrom"); 391 bb_perror_msg("recvfrom");
393 continue; 392 continue;
diff --git a/networking/udhcp/clientpacket.c b/networking/udhcp/clientpacket.c
index 69d7f2d38..af97962a0 100644
--- a/networking/udhcp/clientpacket.c
+++ b/networking/udhcp/clientpacket.c
@@ -25,19 +25,13 @@
25 25
26 26
27/* Create a random xid */ 27/* Create a random xid */
28unsigned long random_xid(void) 28unsigned random_xid(void)
29{ 29{
30 static int initialized; 30 static smallint initialized;
31
31 if (!initialized) { 32 if (!initialized) {
32 unsigned long seed; 33 srand(monotonic_us());
33 34 initialized = 1;
34 if (open_read_close("/dev/urandom", &seed, sizeof(seed)) < 0) {
35 bb_info_msg("Cannot load seed "
36 "from /dev/urandom: %s", strerror(errno));
37 seed = time(0);
38 }
39 srand(seed);
40 initialized++;
41 } 35 }
42 return rand(); 36 return rand();
43} 37}
diff --git a/networking/udhcp/dhcpc.h b/networking/udhcp/dhcpc.h
index 4ddd12120..fa091bf72 100644
--- a/networking/udhcp/dhcpc.h
+++ b/networking/udhcp/dhcpc.h
@@ -38,7 +38,7 @@ extern struct client_config_t client_config;
38 38
39/*** clientpacket.h ***/ 39/*** clientpacket.h ***/
40 40
41unsigned long random_xid(void); 41unsigned random_xid(void);
42int send_discover(unsigned long xid, unsigned long requested); 42int send_discover(unsigned long xid, unsigned long requested);
43int send_selecting(unsigned long xid, unsigned long server, unsigned long requested); 43int send_selecting(unsigned long xid, unsigned long server, unsigned long requested);
44int send_renew(unsigned long xid, unsigned long server, unsigned long ciaddr); 44int send_renew(unsigned long xid, unsigned long server, unsigned long ciaddr);
diff --git a/networking/wget.c b/networking/wget.c
index fc7f7c32c..8be37a744 100644
--- a/networking/wget.c
+++ b/networking/wget.c
@@ -38,7 +38,6 @@ static bool chunked; /* chunked transfer encoding */
38#if ENABLE_FEATURE_WGET_STATUSBAR 38#if ENABLE_FEATURE_WGET_STATUSBAR
39static void progressmeter(int flag); 39static void progressmeter(int flag);
40static const char *curfile; /* Name of current file being transferred */ 40static const char *curfile; /* Name of current file being transferred */
41static struct timeval start; /* Time a transfer started */
42enum { 41enum {
43 STALLTIME = 5 /* Seconds when xfer considered "stalled" */ 42 STALLTIME = 5 /* Seconds when xfer considered "stalled" */
44}; 43};
@@ -683,30 +682,30 @@ static void alarmtimer(int iwait)
683 setitimer(ITIMER_REAL, &itv, NULL); 682 setitimer(ITIMER_REAL, &itv, NULL);
684} 683}
685 684
686
687static void 685static void
688progressmeter(int flag) 686progressmeter(int flag)
689{ 687{
690 static struct timeval lastupdate; 688 static unsigned lastupdate_sec;
689 static unsigned start_sec;
691 static off_t lastsize, totalsize; 690 static off_t lastsize, totalsize;
692 691
693 struct timeval now, td, tvwait;
694 off_t abbrevsize; 692 off_t abbrevsize;
695 int elapsed, ratio, barlength, i; 693 unsigned since_last_update, elapsed;
694 unsigned ratio;
695 int barlength, i;
696 696
697 if (flag == -1) { /* first call to progressmeter */ 697 if (flag == -1) { /* first call to progressmeter */
698 gettimeofday(&start, NULL); 698 start_sec = monotonic_sec();
699 lastupdate = start; 699 lastupdate_sec = start_sec;
700 lastsize = 0; 700 lastsize = 0;
701 totalsize = content_len + beg_range; /* as content_len changes.. */ 701 totalsize = content_len + beg_range; /* as content_len changes.. */
702 } 702 }
703 703
704 gettimeofday(&now, NULL);
705 ratio = 100; 704 ratio = 100;
706 if (totalsize != 0 && !chunked) { 705 if (totalsize != 0 && !chunked) {
707 /* long long helps to have working ETA even if !LFS */ 706 /* long long helps to have it working even if !LFS */
708 ratio = (int) (100ULL * (transferred+beg_range) / totalsize); 707 ratio = (unsigned) (100ULL * (transferred+beg_range) / totalsize);
709 ratio = MIN(ratio, 100); 708 if (ratio > 100) ratio = 100;
710 } 709 }
711 710
712 fprintf(stderr, "\r%-20.20s%4d%% ", curfile, ratio); 711 fprintf(stderr, "\r%-20.20s%4d%% ", curfile, ratio);
@@ -714,12 +713,13 @@ progressmeter(int flag)
714 barlength = getttywidth() - 51; 713 barlength = getttywidth() - 51;
715 if (barlength > 0) { 714 if (barlength > 0) {
716 /* god bless gcc for variable arrays :) */ 715 /* god bless gcc for variable arrays :) */
717 char buf[barlength+1];
718 i = barlength * ratio / 100; 716 i = barlength * ratio / 100;
719 memset(buf, '*', i); 717 {
720 memset(buf + i, ' ', barlength - i); 718 char buf[i+1];
721 buf[barlength] = '\0'; 719 memset(buf, '*', i);
722 fprintf(stderr, "|%s|", buf); 720 buf[i] = '\0';
721 fprintf(stderr, "|%s%*s|", buf, barlength - i, "");
722 }
723 } 723 }
724 i = 0; 724 i = 0;
725 abbrevsize = transferred + beg_range; 725 abbrevsize = transferred + beg_range;
@@ -730,22 +730,28 @@ progressmeter(int flag)
730 /* see http://en.wikipedia.org/wiki/Tera */ 730 /* see http://en.wikipedia.org/wiki/Tera */
731 fprintf(stderr, "%6d %c%c ", (int)abbrevsize, " KMGTPEZY"[i], i?'B':' '); 731 fprintf(stderr, "%6d %c%c ", (int)abbrevsize, " KMGTPEZY"[i], i?'B':' ');
732 732
733 timersub(&now, &lastupdate, &tvwait); 733// Nuts! Ain't it easier to update progress meter ONLY when we transferred++?
734// FIXME: get rid of alarmtimer + updateprogressmeter mess
735
736 elapsed = monotonic_sec();
737 since_last_update = elapsed - lastupdate_sec;
734 if (transferred > lastsize) { 738 if (transferred > lastsize) {
735 lastupdate = now; 739 lastupdate_sec = elapsed;
736 lastsize = transferred; 740 lastsize = transferred;
737 if (tvwait.tv_sec >= STALLTIME) 741 if (since_last_update >= STALLTIME) {
738 timeradd(&start, &tvwait, &start); 742 /* We "cut off" these seconds from elapsed time
739 tvwait.tv_sec = 0; 743 * by adjusting start time */
744 start_sec += since_last_update;
745 }
746 since_last_update = 0; /* we are un-stalled now */
740 } 747 }
741 timersub(&now, &start, &td); 748 elapsed -= start_sec; /* now it's "elapsed since start" */
742 elapsed = td.tv_sec;
743 749
744 if (tvwait.tv_sec >= STALLTIME) { 750 if (since_last_update >= STALLTIME) {
745 fprintf(stderr, " - stalled -"); 751 fprintf(stderr, " - stalled -");
746 } else { 752 } else {
747 off_t to_download = totalsize - beg_range; 753 off_t to_download = totalsize - beg_range;
748 if (transferred <= 0 || elapsed <= 0 || transferred > to_download || chunked) { 754 if (transferred <= 0 || (int)elapsed <= 0 || transferred > to_download || chunked) {
749 fprintf(stderr, "--:--:-- ETA"); 755 fprintf(stderr, "--:--:-- ETA");
750 } else { 756 } else {
751 /* to_download / (transferred/elapsed) - elapsed: */ 757 /* to_download / (transferred/elapsed) - elapsed: */
diff --git a/networking/zcip.c b/networking/zcip.c
index eb0a7ba41..de4ee0b1a 100644
--- a/networking/zcip.c
+++ b/networking/zcip.c
@@ -23,7 +23,6 @@
23// - avoid silent script failures, especially under load... 23// - avoid silent script failures, especially under load...
24// - link status monitoring (restart on link-up; stop on link-down) 24// - link status monitoring (restart on link-up; stop on link-down)
25 25
26#include "libbb.h"
27#include <syslog.h> 26#include <syslog.h>
28#include <poll.h> 27#include <poll.h>
29#include <sys/wait.h> 28#include <sys/wait.h>
@@ -31,10 +30,13 @@
31#include <net/ethernet.h> 30#include <net/ethernet.h>
32#include <net/if.h> 31#include <net/if.h>
33#include <net/if_arp.h> 32#include <net/if_arp.h>
34
35#include <linux/if_packet.h> 33#include <linux/if_packet.h>
36#include <linux/sockios.h> 34#include <linux/sockios.h>
37 35
36#include "libbb.h"
37
38/* We don't need more than 32 bits of the counter */
39#define MONOTONIC_US() ((unsigned)monotonic_us())
38 40
39struct arp_packet { 41struct arp_packet {
40 struct ether_header hdr; 42 struct ether_header hdr;
@@ -78,15 +80,12 @@ static void pick(struct in_addr *ip)
78{ 80{
79 unsigned tmp; 81 unsigned tmp;
80 82
81 /* use cheaper math than lrand48() mod N */
82 do { 83 do {
83 tmp = (lrand48() >> 16) & IN_CLASSB_HOST; 84 tmp = rand() & IN_CLASSB_HOST;
84 } while (tmp > (IN_CLASSB_HOST - 0x0200)); 85 } while (tmp > (IN_CLASSB_HOST - 0x0200));
85 ip->s_addr = htonl((LINKLOCAL_ADDR + 0x0100) + tmp); 86 ip->s_addr = htonl((LINKLOCAL_ADDR + 0x0100) + tmp);
86} 87}
87 88
88/* TODO: we need a flag to direct bb_[p]error_msg output to stderr. */
89
90/** 89/**
91 * Broadcast an ARP packet. 90 * Broadcast an ARP packet.
92 */ 91 */
@@ -151,7 +150,7 @@ static int run(char *argv[3], const char *intf, struct in_addr *ip)
151 */ 150 */
152static unsigned ALWAYS_INLINE ms_rdelay(unsigned secs) 151static unsigned ALWAYS_INLINE ms_rdelay(unsigned secs)
153{ 152{
154 return lrand48() % (secs * 1000); 153 return rand() % (secs * 1000);
155} 154}
156 155
157/** 156/**
@@ -176,29 +175,31 @@ int zcip_main(int argc, char **argv)
176 struct ifreq ifr; 175 struct ifreq ifr;
177 char *intf; 176 char *intf;
178 char *script_av[3]; 177 char *script_av[3];
179 suseconds_t timeout; // milliseconds 178 int timeout_ms; /* must be signed */
180 unsigned conflicts; 179 unsigned conflicts;
181 unsigned nprobes; 180 unsigned nprobes;
182 unsigned nclaims; 181 unsigned nclaims;
183 int ready; 182 int ready;
184 int verbose; 183 int verbose;
185 } L; 184 } L;
186#define null_ip (L.null_ip ) 185#define null_ip (L.null_ip )
187#define null_addr (L.null_addr) 186#define null_addr (L.null_addr )
188#define saddr (L.saddr ) 187#define saddr (L.saddr )
189#define ip (L.ip ) 188#define ip (L.ip )
190#define ifr (L.ifr ) 189#define ifr (L.ifr )
191#define intf (L.intf ) 190#define intf (L.intf )
192#define script_av (L.script_av) 191#define script_av (L.script_av )
193#define timeout (L.timeout ) 192#define timeout_ms (L.timeout_ms)
194#define conflicts (L.conflicts) 193#define conflicts (L.conflicts )
195#define nprobes (L.nprobes ) 194#define nprobes (L.nprobes )
196#define nclaims (L.nclaims ) 195#define nclaims (L.nclaims )
197#define ready (L.ready ) 196#define ready (L.ready )
198#define verbose (L.verbose ) 197#define verbose (L.verbose )
199 198
200 memset(&L, 0, sizeof(L)); 199 memset(&L, 0, sizeof(L));
201 200
201 srand(MONOTONIC_US());
202
202#define FOREGROUND (opts & 1) 203#define FOREGROUND (opts & 1)
203#define QUIT (opts & 2) 204#define QUIT (opts & 2)
204 // parse commandline: prog [options] ifname script 205 // parse commandline: prog [options] ifname script
@@ -282,7 +283,7 @@ int zcip_main(int argc, char **argv)
282 // - defend it, within limits 283 // - defend it, within limits
283 while (1) { 284 while (1) {
284 struct pollfd fds[1]; 285 struct pollfd fds[1];
285 struct timeval tv1; 286 unsigned deadline_us;
286 struct arp_packet p; 287 struct arp_packet p;
287 288
288 int source_ip_conflict = 0; 289 int source_ip_conflict = 0;
@@ -293,24 +294,18 @@ int zcip_main(int argc, char **argv)
293 fds[0].revents = 0; 294 fds[0].revents = 0;
294 295
295 // poll, being ready to adjust current timeout 296 // poll, being ready to adjust current timeout
296 if (!timeout) { 297 if (!timeout_ms) {
297 timeout = ms_rdelay(PROBE_WAIT); 298 timeout_ms = ms_rdelay(PROBE_WAIT);
298 // FIXME setsockopt(fd, SO_ATTACH_FILTER, ...) to 299 // FIXME setsockopt(fd, SO_ATTACH_FILTER, ...) to
299 // make the kernel filter out all packets except 300 // make the kernel filter out all packets except
300 // ones we'd care about. 301 // ones we'd care about.
301 } 302 }
302 // set tv1 to the point in time when we timeout 303 // set deadline_us to the point in time when we timeout
303 gettimeofday(&tv1, NULL); 304 deadline_us = MONOTONIC_US() + timeout_ms * 1000;
304 tv1.tv_usec += (timeout % 1000) * 1000;
305 while (tv1.tv_usec > 1000000) {
306 tv1.tv_usec -= 1000000;
307 tv1.tv_sec++;
308 }
309 tv1.tv_sec += timeout / 1000;
310 305
311 VDBG("...wait %ld %s nprobes=%d, nclaims=%d\n", 306 VDBG("...wait %d %s nprobes=%u, nclaims=%u\n",
312 timeout, intf, nprobes, nclaims); 307 timeout_ms, intf, nprobes, nclaims);
313 switch (poll(fds, 1, timeout)) { 308 switch (poll(fds, 1, timeout_ms)) {
314 309
315 // timeout 310 // timeout
316 case 0: 311 case 0:
@@ -321,25 +316,24 @@ int zcip_main(int argc, char **argv)
321 // have been received, so we can progress through the states 316 // have been received, so we can progress through the states
322 if (nprobes < PROBE_NUM) { 317 if (nprobes < PROBE_NUM) {
323 nprobes++; 318 nprobes++;
324 VDBG("probe/%d %s@%s\n", 319 VDBG("probe/%u %s@%s\n",
325 nprobes, intf, inet_ntoa(ip)); 320 nprobes, intf, inet_ntoa(ip));
326 arp(fd, &saddr, ARPOP_REQUEST, 321 arp(fd, &saddr, ARPOP_REQUEST,
327 &eth_addr, null_ip, 322 &eth_addr, null_ip,
328 &null_addr, ip); 323 &null_addr, ip);
329 timeout = PROBE_MIN * 1000; 324 timeout_ms = PROBE_MIN * 1000;
330 timeout += ms_rdelay(PROBE_MAX 325 timeout_ms += ms_rdelay(PROBE_MAX - PROBE_MIN);
331 - PROBE_MIN);
332 } 326 }
333 else { 327 else {
334 // Switch to announce state. 328 // Switch to announce state.
335 state = ANNOUNCE; 329 state = ANNOUNCE;
336 nclaims = 0; 330 nclaims = 0;
337 VDBG("announce/%d %s@%s\n", 331 VDBG("announce/%u %s@%s\n",
338 nclaims, intf, inet_ntoa(ip)); 332 nclaims, intf, inet_ntoa(ip));
339 arp(fd, &saddr, ARPOP_REQUEST, 333 arp(fd, &saddr, ARPOP_REQUEST,
340 &eth_addr, ip, 334 &eth_addr, ip,
341 &eth_addr, ip); 335 &eth_addr, ip);
342 timeout = ANNOUNCE_INTERVAL * 1000; 336 timeout_ms = ANNOUNCE_INTERVAL * 1000;
343 } 337 }
344 break; 338 break;
345 case RATE_LIMIT_PROBE: 339 case RATE_LIMIT_PROBE:
@@ -347,24 +341,24 @@ int zcip_main(int argc, char **argv)
347 // have been received, so we can move immediately to the announce state 341 // have been received, so we can move immediately to the announce state
348 state = ANNOUNCE; 342 state = ANNOUNCE;
349 nclaims = 0; 343 nclaims = 0;
350 VDBG("announce/%d %s@%s\n", 344 VDBG("announce/%u %s@%s\n",
351 nclaims, intf, inet_ntoa(ip)); 345 nclaims, intf, inet_ntoa(ip));
352 arp(fd, &saddr, ARPOP_REQUEST, 346 arp(fd, &saddr, ARPOP_REQUEST,
353 &eth_addr, ip, 347 &eth_addr, ip,
354 &eth_addr, ip); 348 &eth_addr, ip);
355 timeout = ANNOUNCE_INTERVAL * 1000; 349 timeout_ms = ANNOUNCE_INTERVAL * 1000;
356 break; 350 break;
357 case ANNOUNCE: 351 case ANNOUNCE:
358 // timeouts in the ANNOUNCE state mean no conflicting ARP packets 352 // timeouts in the ANNOUNCE state mean no conflicting ARP packets
359 // have been received, so we can progress through the states 353 // have been received, so we can progress through the states
360 if (nclaims < ANNOUNCE_NUM) { 354 if (nclaims < ANNOUNCE_NUM) {
361 nclaims++; 355 nclaims++;
362 VDBG("announce/%d %s@%s\n", 356 VDBG("announce/%u %s@%s\n",
363 nclaims, intf, inet_ntoa(ip)); 357 nclaims, intf, inet_ntoa(ip));
364 arp(fd, &saddr, ARPOP_REQUEST, 358 arp(fd, &saddr, ARPOP_REQUEST,
365 &eth_addr, ip, 359 &eth_addr, ip,
366 &eth_addr, ip); 360 &eth_addr, ip);
367 timeout = ANNOUNCE_INTERVAL * 1000; 361 timeout_ms = ANNOUNCE_INTERVAL * 1000;
368 } 362 }
369 else { 363 else {
370 // Switch to monitor state. 364 // Switch to monitor state.
@@ -375,7 +369,7 @@ int zcip_main(int argc, char **argv)
375 run(script_av, intf, &ip); 369 run(script_av, intf, &ip);
376 ready = 1; 370 ready = 1;
377 conflicts = 0; 371 conflicts = 0;
378 timeout = -1; // Never timeout in the monitor state. 372 timeout_ms = -1; // Never timeout in the monitor state.
379 373
380 // NOTE: all other exit paths 374 // NOTE: all other exit paths
381 // should deconfig ... 375 // should deconfig ...
@@ -386,14 +380,14 @@ int zcip_main(int argc, char **argv)
386 case DEFEND: 380 case DEFEND:
387 // We won! No ARP replies, so just go back to monitor. 381 // We won! No ARP replies, so just go back to monitor.
388 state = MONITOR; 382 state = MONITOR;
389 timeout = -1; 383 timeout_ms = -1;
390 conflicts = 0; 384 conflicts = 0;
391 break; 385 break;
392 default: 386 default:
393 // Invalid, should never happen. Restart the whole protocol. 387 // Invalid, should never happen. Restart the whole protocol.
394 state = PROBE; 388 state = PROBE;
395 pick(&ip); 389 pick(&ip);
396 timeout = 0; 390 timeout_ms = 0;
397 nprobes = 0; 391 nprobes = 0;
398 nclaims = 0; 392 nclaims = 0;
399 break; 393 break;
@@ -403,20 +397,17 @@ int zcip_main(int argc, char **argv)
403 case 1: 397 case 1:
404 // We need to adjust the timeout in case we didn't receive 398 // We need to adjust the timeout in case we didn't receive
405 // a conflicting packet. 399 // a conflicting packet.
406 if (timeout > 0) { 400 if (timeout_ms > 0) {
407 struct timeval tv2; 401 unsigned diff = deadline_us - MONOTONIC_US();
408 402 if ((int)(diff) < 0) {
409 gettimeofday(&tv2, NULL);
410 if (timercmp(&tv1, &tv2, <)) {
411 // Current time is greater than the expected timeout time. 403 // Current time is greater than the expected timeout time.
412 // Should never happen. 404 // Should never happen.
413 VDBG("missed an expected timeout\n"); 405 VDBG("missed an expected timeout\n");
414 timeout = 0; 406 timeout_ms = 0;
415 } else { 407 } else {
416 VDBG("adjusting timeout\n"); 408 VDBG("adjusting timeout\n");
417 timersub(&tv1, &tv2, &tv1); 409 timeout_ms = diff / 1000;
418 timeout = 1000 * tv1.tv_sec 410 if (!timeout_ms) timeout_ms = 1;
419 + tv1.tv_usec / 1000;
420 } 411 }
421 } 412 }
422 413
@@ -484,13 +475,13 @@ int zcip_main(int argc, char **argv)
484 conflicts++; 475 conflicts++;
485 if (conflicts >= MAX_CONFLICTS) { 476 if (conflicts >= MAX_CONFLICTS) {
486 VDBG("%s ratelimit\n", intf); 477 VDBG("%s ratelimit\n", intf);
487 timeout = RATE_LIMIT_INTERVAL * 1000; 478 timeout_ms = RATE_LIMIT_INTERVAL * 1000;
488 state = RATE_LIMIT_PROBE; 479 state = RATE_LIMIT_PROBE;
489 } 480 }
490 481
491 // restart the whole protocol 482 // restart the whole protocol
492 pick(&ip); 483 pick(&ip);
493 timeout = 0; 484 timeout_ms = 0;
494 nprobes = 0; 485 nprobes = 0;
495 nclaims = 0; 486 nclaims = 0;
496 } 487 }
@@ -500,7 +491,7 @@ int zcip_main(int argc, char **argv)
500 if (source_ip_conflict) { 491 if (source_ip_conflict) {
501 VDBG("monitor conflict -- defending\n"); 492 VDBG("monitor conflict -- defending\n");
502 state = DEFEND; 493 state = DEFEND;
503 timeout = DEFEND_INTERVAL * 1000; 494 timeout_ms = DEFEND_INTERVAL * 1000;
504 arp(fd, &saddr, 495 arp(fd, &saddr,
505 ARPOP_REQUEST, 496 ARPOP_REQUEST,
506 &eth_addr, ip, 497 &eth_addr, ip,
@@ -518,7 +509,7 @@ int zcip_main(int argc, char **argv)
518 509
519 // restart the whole protocol 510 // restart the whole protocol
520 pick(&ip); 511 pick(&ip);
521 timeout = 0; 512 timeout_ms = 0;
522 nprobes = 0; 513 nprobes = 0;
523 nclaims = 0; 514 nclaims = 0;
524 } 515 }
@@ -528,7 +519,7 @@ int zcip_main(int argc, char **argv)
528 VDBG("invalid state -- starting over\n"); 519 VDBG("invalid state -- starting over\n");
529 state = PROBE; 520 state = PROBE;
530 pick(&ip); 521 pick(&ip);
531 timeout = 0; 522 timeout_ms = 0;
532 nprobes = 0; 523 nprobes = 0;
533 nclaims = 0; 524 nclaims = 0;
534 break; 525 break;