From d077565bb27966c47ea6f0e9de092133954b5807 Mon Sep 17 00:00:00 2001 From: Bartosz Golaszewski Date: Thu, 13 Aug 2015 15:57:21 +0200 Subject: unit-tests: remove unnecesary field from struct bbunit_listelem In the initial submission struct bbunit_listelem was manipulated by custom list functions implemented in bbunit.c. Since the tests are now added to the list by llist_add_to_end(), which allocates the llist_t objects behind the scenes, there's no need for the *next field. function old new delta unit_main 142 141 -1 bbunit_strrstr_elem 24 16 -8 bbunit_obscure_weak_pass_elem 24 16 -8 bbunit_obscure_strong_pass_elem 24 16 -8 ------------------------------------------------------------------------------ (add/remove: 0/0 grow/shrink: 0/4 up/down: 0/-25) Total: -25 bytes Signed-off-by: Bartosz Golaszewski Signed-off-by: Denys Vlasenko --- libbb/bbunit.c | 1 - 1 file changed, 1 deletion(-) (limited to 'libbb') diff --git a/libbb/bbunit.c b/libbb/bbunit.c index 4c692d59f..66a7df945 100644 --- a/libbb/bbunit.c +++ b/libbb/bbunit.c @@ -71,7 +71,6 @@ int unit_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) tests_failed++; } tests_run++; - el = el->next; } #if WANT_TIMING -- cgit v1.2.3-55-g6feb From 718e228adcd7b9592b504b55217d13f4216a0450 Mon Sep 17 00:00:00 2001 From: Bartosz Golaszewski Date: Thu, 13 Aug 2015 15:57:22 +0200 Subject: unit-tests: remove code depending on WANT_TIMING Since there is no interest in merging a config option for WANT_TIMING, remove the parts of code depending on it altogether. While we're at it: add some newlines to improve readability. Signed-off-by: Bartosz Golaszewski Signed-off-by: Denys Vlasenko --- libbb/bbunit.c | 30 +++--------------------------- 1 file changed, 3 insertions(+), 27 deletions(-) (limited to 'libbb') diff --git a/libbb/bbunit.c b/libbb/bbunit.c index 66a7df945..db67b1081 100644 --- a/libbb/bbunit.c +++ b/libbb/bbunit.c @@ -17,8 +17,6 @@ #include "libbb.h" -#define WANT_TIMING 0 - static llist_t *tests = NULL; static unsigned tests_registered = 0; static int test_retval; @@ -34,38 +32,22 @@ void bbunit_settestfailed(void) test_retval = -1; } -#if WANT_TIMING -static void timeval_diff(struct timeval* res, - const struct timeval* x, - const struct timeval* y) -{ - long udiff = x->tv_usec - y->tv_usec; - - res->tv_sec = x->tv_sec - y->tv_sec - (udiff < 0); - res->tv_usec = (udiff >= 0 ? udiff : udiff + 1000000); -} -#endif - int unit_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) MAIN_EXTERNALLY_VISIBLE; int unit_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) { unsigned tests_run = 0; unsigned tests_failed = 0; -#if WANT_TIMING - struct timeval begin; - struct timeval end; - struct timeval time_spent; - gettimeofday(&begin, NULL); -#endif bb_error_msg("Running %d test(s)...", tests_registered); for (;;) { struct bbunit_listelem* el = llist_pop(&tests); if (!el) break; + bb_error_msg("Case: [%s]", el->name); test_retval = 0; el->testfunc(); + if (test_retval < 0) { bb_error_msg("[ERROR] [%s]: TEST FAILED", el->name); tests_failed++; @@ -73,17 +55,11 @@ int unit_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) tests_run++; } -#if WANT_TIMING - gettimeofday(&end, NULL); - timeval_diff(&time_spent, &end, &begin); - bb_error_msg("Elapsed time %u.%06u seconds", - (int)time_spent.tv_sec, - (int)time_spent.tv_usec); -#endif if (tests_failed > 0) { bb_error_msg("[ERROR] %u test(s) FAILED", tests_failed); return EXIT_FAILURE; } + bb_error_msg("All tests passed"); return EXIT_SUCCESS; } -- cgit v1.2.3-55-g6feb From c52cbea2bba6582b44facb424a15dc544b54fb28 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Mon, 24 Aug 2015 19:48:03 +0200 Subject: libbb: add setsockopt_foo helpers function old new delta setsockopt_int - 23 +23 do_load 918 934 +16 setsockopt_SOL_SOCKET_int - 14 +14 setsockopt_keepalive - 10 +10 setsockopt_SOL_SOCKET_1 - 10 +10 buffer_fill_and_print 169 178 +9 setsockopt_1 - 8 +8 nfsmount 3560 3566 +6 redirect 1277 1282 +5 tcpudpsvd_main 1782 1786 +4 d6_send_kernel_packet 272 275 +3 i2cget_main 380 382 +2 ed_main 2544 2545 +1 scan_recursive 380 378 -2 nbdclient_main 492 490 -2 hash_find 235 233 -2 cmdputs 334 332 -2 parse_command 1443 1440 -3 static.two 4 - -4 ntpd_main 1039 1035 -4 const_int_1 4 - -4 const_IPTOS_LOWDELAY 4 - -4 RCVBUF 4 - -4 ntp_init 474 469 -5 change_listen_mode 316 310 -6 uevent_main 416 409 -7 arping_main 1697 1690 -7 telnet_main 1612 1603 -9 socket_want_pktinfo 42 33 -9 setsockopt_reuseaddr 21 10 -11 setsockopt_broadcast 21 10 -11 httpd_main 772 757 -15 get_remote_transfer_fd 109 94 -15 make_new_session 503 487 -16 ftpd_main 2177 2160 -17 read_bunzip 1896 1866 -30 common_traceroute_main 4099 4058 -41 common_ping_main 1836 1783 -53 ------------------------------------------------------------------------------ (add/remove: 5/4 grow/shrink: 8/21 up/down: 111/-283) Total: -172 bytes Signed-off-by: Denys Vlasenko --- include/libbb.h | 8 +++++++- libbb/messages.c | 2 +- libbb/udp_io.c | 4 ++-- libbb/xconnect.c | 25 +++++++++++++++++++++++-- networking/arping.c | 2 +- networking/ftpd.c | 8 ++++---- networking/httpd.c | 4 ++-- networking/nbd-client.c | 2 +- networking/nc_bloaty.c | 4 ++-- networking/ntpd.c | 6 ++---- networking/ping.c | 16 ++++++++-------- networking/telnet.c | 2 +- networking/telnetd.c | 2 +- networking/traceroute.c | 41 ++++++++++++++++------------------------- networking/udhcp/dhcpc.c | 4 +--- util-linux/uevent.c | 6 +++--- 16 files changed, 75 insertions(+), 61 deletions(-) (limited to 'libbb') diff --git a/include/libbb.h b/include/libbb.h index 136d4fd87..2e20706e7 100644 --- a/include/libbb.h +++ b/include/libbb.h @@ -561,6 +561,11 @@ void xlisten(int s, int backlog) FAST_FUNC; void xconnect(int s, const struct sockaddr *s_addr, socklen_t addrlen) FAST_FUNC; ssize_t xsendto(int s, const void *buf, size_t len, const struct sockaddr *to, socklen_t tolen) FAST_FUNC; + +int setsockopt_int(int fd, int level, int optname, int optval) FAST_FUNC; +int setsockopt_1(int fd, int level, int optname) FAST_FUNC; +int setsockopt_SOL_SOCKET_int(int fd, int optname, int optval) FAST_FUNC; +int setsockopt_SOL_SOCKET_1(int fd, int optname) FAST_FUNC; /* SO_REUSEADDR allows a server to rebind to an address that is already * "in use" by old connections to e.g. previous server instance which is * killed or crashed. Without it bind will fail until all such connections @@ -568,6 +573,7 @@ ssize_t xsendto(int s, const void *buf, size_t len, const struct sockaddr *to, * regardless of SO_REUSEADDR (unlike some other flavors of Unix). * Turn it on before you call bind(). */ void setsockopt_reuseaddr(int fd) FAST_FUNC; /* On Linux this never fails. */ +int setsockopt_keepalive(int fd) FAST_FUNC; int setsockopt_broadcast(int fd) FAST_FUNC; int setsockopt_bindtodevice(int fd, const char *iface) FAST_FUNC; /* NB: returns port in host byte order */ @@ -1807,7 +1813,7 @@ extern const char bb_PATH_root_path[] ALIGN1; /* "PATH=/sbin:/usr/sbin:/bin:/usr #define bb_default_path (bb_PATH_root_path + sizeof("PATH=/sbin:/usr/sbin")) extern const int const_int_0; -extern const int const_int_1; +//extern const int const_int_1; /* Providing hard guarantee on minimum size (think of BUFSIZ == 128) */ diff --git a/libbb/messages.c b/libbb/messages.c index fad82c9da..c1b7ba252 100644 --- a/libbb/messages.c +++ b/libbb/messages.c @@ -43,7 +43,7 @@ const char bb_PATH_root_path[] ALIGN1 = "PATH=/sbin:/usr/sbin:/bin:/usr/bin" BB_ADDITIONAL_PATH; -const int const_int_1 = 1; +//const int const_int_1 = 1; /* explicitly = 0, otherwise gcc may make it a common variable * and it will end up in bss */ const int const_int_0 = 0; diff --git a/libbb/udp_io.c b/libbb/udp_io.c index 7985a9723..a32af9bd2 100644 --- a/libbb/udp_io.c +++ b/libbb/udp_io.c @@ -16,10 +16,10 @@ void FAST_FUNC socket_want_pktinfo(int fd UNUSED_PARAM) { #ifdef IP_PKTINFO - setsockopt(fd, IPPROTO_IP, IP_PKTINFO, &const_int_1, sizeof(int)); + setsockopt_1(fd, IPPROTO_IP, IP_PKTINFO); #endif #if ENABLE_FEATURE_IPV6 && defined(IPV6_PKTINFO) - setsockopt(fd, IPPROTO_IPV6, IPV6_PKTINFO, &const_int_1, sizeof(int)); + setsockopt_1(fd, IPPROTO_IPV6, IPV6_PKTINFO); #endif } diff --git a/libbb/xconnect.c b/libbb/xconnect.c index 2a96e03dc..6e78e6363 100644 --- a/libbb/xconnect.c +++ b/libbb/xconnect.c @@ -14,13 +14,34 @@ #include #include "libbb.h" +int FAST_FUNC setsockopt_int(int fd, int level, int optname, int optval) +{ + return setsockopt(fd, level, optname, &optval, sizeof(int)); +} +int FAST_FUNC setsockopt_1(int fd, int level, int optname) +{ + return setsockopt_int(fd, level, optname, 1); +} +int FAST_FUNC setsockopt_SOL_SOCKET_int(int fd, int optname, int optval) +{ + return setsockopt_int(fd, SOL_SOCKET, optname, optval); +} +int FAST_FUNC setsockopt_SOL_SOCKET_1(int fd, int optname) +{ + return setsockopt_SOL_SOCKET_int(fd, optname, 1); +} + void FAST_FUNC setsockopt_reuseaddr(int fd) { - setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &const_int_1, sizeof(const_int_1)); + setsockopt_SOL_SOCKET_1(fd, SO_REUSEADDR); } int FAST_FUNC setsockopt_broadcast(int fd) { - return setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &const_int_1, sizeof(const_int_1)); + return setsockopt_SOL_SOCKET_1(fd, SO_BROADCAST); +} +int FAST_FUNC setsockopt_keepalive(int fd) +{ + return setsockopt_SOL_SOCKET_1(fd, SO_KEEPALIVE); } #ifdef SO_BINDTODEVICE diff --git a/networking/arping.c b/networking/arping.c index 9ac4a7cee..ce7fa299c 100644 --- a/networking/arping.c +++ b/networking/arping.c @@ -357,7 +357,7 @@ int arping_main(int argc UNUSED_PARAM, char **argv) saddr.sin_port = htons(1025); saddr.sin_addr = dst; - if (setsockopt(probe_fd, SOL_SOCKET, SO_DONTROUTE, &const_int_1, sizeof(const_int_1)) == -1) + if (setsockopt_SOL_SOCKET_1(probe_fd, SO_DONTROUTE) != 0) bb_perror_msg("setsockopt(%s)", "SO_DONTROUTE"); xconnect(probe_fd, (struct sockaddr *) &saddr, sizeof(saddr)); getsockname(probe_fd, (struct sockaddr *) &saddr, &alen); diff --git a/networking/ftpd.c b/networking/ftpd.c index 2351d6dd3..7735b7233 100644 --- a/networking/ftpd.c +++ b/networking/ftpd.c @@ -377,7 +377,7 @@ ftpdataio_get_pasv_fd(void) return remote_fd; } - setsockopt(remote_fd, SOL_SOCKET, SO_KEEPALIVE, &const_int_1, sizeof(const_int_1)); + setsockopt_keepalive(remote_fd); return remote_fd; } @@ -1186,11 +1186,11 @@ int ftpd_main(int argc UNUSED_PARAM, char **argv) , SIG_IGN); /* Set up options on the command socket (do we need these all? why?) */ - setsockopt(STDIN_FILENO, IPPROTO_TCP, TCP_NODELAY, &const_int_1, sizeof(const_int_1)); - setsockopt(STDIN_FILENO, SOL_SOCKET, SO_KEEPALIVE, &const_int_1, sizeof(const_int_1)); + setsockopt_1(STDIN_FILENO, IPPROTO_TCP, TCP_NODELAY); + setsockopt_keepalive(STDIN_FILENO); /* Telnet protocol over command link may send "urgent" data, * we prefer it to be received in the "normal" data stream: */ - setsockopt(STDIN_FILENO, SOL_SOCKET, SO_OOBINLINE, &const_int_1, sizeof(const_int_1)); + setsockopt_1(STDIN_FILENO, SOL_SOCKET, SO_OOBINLINE); WRITE_OK(FTP_GREET); signal(SIGALRM, timeout_handler); diff --git a/networking/httpd.c b/networking/httpd.c index 7a9065fcc..feaaa94d5 100644 --- a/networking/httpd.c +++ b/networking/httpd.c @@ -2352,7 +2352,7 @@ static void mini_httpd(int server_socket) continue; /* set the KEEPALIVE option to cull dead connections */ - setsockopt(n, SOL_SOCKET, SO_KEEPALIVE, &const_int_1, sizeof(const_int_1)); + setsockopt_keepalive(n); if (fork() == 0) { /* child */ @@ -2395,7 +2395,7 @@ static void mini_httpd_nommu(int server_socket, int argc, char **argv) continue; /* set the KEEPALIVE option to cull dead connections */ - setsockopt(n, SOL_SOCKET, SO_KEEPALIVE, &const_int_1, sizeof(const_int_1)); + setsockopt_keepalive(n); if (vfork() == 0) { /* child */ diff --git a/networking/nbd-client.c b/networking/nbd-client.c index cadda5261..a601430b6 100644 --- a/networking/nbd-client.c +++ b/networking/nbd-client.c @@ -83,7 +83,7 @@ int nbdclient_main(int argc, char **argv) // Find and connect to server sock = create_and_connect_stream_or_die(host, xatou16(port)); - setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &const_int_1, sizeof(const_int_1)); + setsockopt_1(sock, IPPROTO_TCP, TCP_NODELAY); // Log on to the server xread(sock, &nbd_header, 8+8+8+4 + 124); diff --git a/networking/nc_bloaty.c b/networking/nc_bloaty.c index b28d05f51..471ae1a12 100644 --- a/networking/nc_bloaty.c +++ b/networking/nc_bloaty.c @@ -863,8 +863,8 @@ int nc_main(int argc UNUSED_PARAM, char **argv) xbind(netfd, &ouraddr->u.sa, ouraddr->len); } #if 0 - setsockopt(netfd, SOL_SOCKET, SO_RCVBUF, &o_rcvbuf, sizeof o_rcvbuf); - setsockopt(netfd, SOL_SOCKET, SO_SNDBUF, &o_sndbuf, sizeof o_sndbuf); + setsockopt_SOL_SOCKET_int(netfd, SO_RCVBUF, o_rcvbuf); + setsockopt_SOL_SOCKET_int(netfd, SO_SNDBUF, o_sndbuf); #endif #ifdef BLOAT diff --git a/networking/ntpd.c b/networking/ntpd.c index b5120a70d..9732c9b1a 100644 --- a/networking/ntpd.c +++ b/networking/ntpd.c @@ -405,8 +405,6 @@ struct globals { }; #define G (*ptr_to_globals) -static const int const_IPTOS_LOWDELAY = IPTOS_LOWDELAY; - #define VERB1 if (MAX_VERBOSE && G.verbose) #define VERB2 if (MAX_VERBOSE >= 2 && G.verbose >= 2) @@ -837,7 +835,7 @@ send_query_to_peer(peer_t *p) #if ENABLE_FEATURE_IPV6 if (family == AF_INET) #endif - setsockopt(fd, IPPROTO_IP, IP_TOS, &const_IPTOS_LOWDELAY, sizeof(const_IPTOS_LOWDELAY)); + setsockopt_int(fd, IPPROTO_IP, IP_TOS, IPTOS_LOWDELAY); free(local_lsa); } @@ -2186,7 +2184,7 @@ static NOINLINE void ntp_init(char **argv) xfunc_die(); } socket_want_pktinfo(G_listen_fd); - setsockopt(G_listen_fd, IPPROTO_IP, IP_TOS, &const_IPTOS_LOWDELAY, sizeof(const_IPTOS_LOWDELAY)); + setsockopt_int(G_listen_fd, IPPROTO_IP, IP_TOS, IPTOS_LOWDELAY); } #endif if (!(opts & OPT_n)) { diff --git a/networking/ping.c b/networking/ping.c index 20489a070..e1f9ebc3a 100644 --- a/networking/ping.c +++ b/networking/ping.c @@ -247,7 +247,7 @@ static void ping6(len_and_sockaddr *lsa) pkt->icmp6_type = ICMP6_ECHO_REQUEST; sockopt = offsetof(struct icmp6_hdr, icmp6_cksum); - setsockopt(pingsock, SOL_RAW, IPV6_CHECKSUM, &sockopt, sizeof(sockopt)); + setsockopt_int(pingsock, SOL_RAW, IPV6_CHECKSUM, sockopt); xsendto(pingsock, G.packet, DEFDATALEN + sizeof(struct icmp6_hdr), &lsa->u.sa, lsa->len); @@ -700,12 +700,12 @@ static void ping4(len_and_sockaddr *lsa) /* set recv buf (needed if we can get lots of responses: flood ping, * broadcast ping etc) */ sockopt = (datalen * 2) + 7 * 1024; /* giving it a bit of extra room */ - setsockopt(pingsock, SOL_SOCKET, SO_RCVBUF, &sockopt, sizeof(sockopt)); + setsockopt_SOL_SOCKET_int(pingsock, SO_RCVBUF, sockopt); if (opt_ttl != 0) { - setsockopt(pingsock, IPPROTO_IP, IP_TTL, &opt_ttl, sizeof(opt_ttl)); + setsockopt_int(pingsock, IPPROTO_IP, IP_TTL, opt_ttl); /* above doesnt affect packets sent to bcast IP, so... */ - setsockopt(pingsock, IPPROTO_IP, IP_MULTICAST_TTL, &opt_ttl, sizeof(opt_ttl)); + setsockopt_int(pingsock, IPPROTO_IP, IP_MULTICAST_TTL, opt_ttl); } signal(SIGINT, print_stats_and_exit); @@ -766,15 +766,15 @@ static void ping6(len_and_sockaddr *lsa) /* set recv buf (needed if we can get lots of responses: flood ping, * broadcast ping etc) */ sockopt = (datalen * 2) + 7 * 1024; /* giving it a bit of extra room */ - setsockopt(pingsock, SOL_SOCKET, SO_RCVBUF, &sockopt, sizeof(sockopt)); + setsockopt_SOL_SOCKET_int(pingsock, SO_RCVBUF, sockopt); sockopt = offsetof(struct icmp6_hdr, icmp6_cksum); - if (offsetof(struct icmp6_hdr, icmp6_cksum) != 2) + if (sockopt != 2) BUG_bad_offsetof_icmp6_cksum(); - setsockopt(pingsock, SOL_RAW, IPV6_CHECKSUM, &sockopt, sizeof(sockopt)); + setsockopt_int(pingsock, SOL_RAW, IPV6_CHECKSUM, sockopt); /* request ttl info to be returned in ancillary data */ - setsockopt(pingsock, SOL_IPV6, IPV6_HOPLIMIT, &const_int_1, sizeof(const_int_1)); + setsockopt_1(pingsock, SOL_IPV6, IPV6_HOPLIMIT); if (if_index) pingaddr.sin6.sin6_scope_id = if_index; diff --git a/networking/telnet.c b/networking/telnet.c index a25579773..3bb6fb1ba 100644 --- a/networking/telnet.c +++ b/networking/telnet.c @@ -623,7 +623,7 @@ int telnet_main(int argc UNUSED_PARAM, char **argv) xmove_fd(create_and_connect_stream_or_die(host, port), netfd); - setsockopt(netfd, SOL_SOCKET, SO_KEEPALIVE, &const_int_1, sizeof(const_int_1)); + setsockopt_keepalive(netfd); signal(SIGINT, record_signo); diff --git a/networking/telnetd.c b/networking/telnetd.c index 6aee95871..25d05fe7a 100644 --- a/networking/telnetd.c +++ b/networking/telnetd.c @@ -265,7 +265,7 @@ make_new_session( close_on_exec_on(fd); /* SO_KEEPALIVE by popular demand */ - setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, &const_int_1, sizeof(const_int_1)); + setsockopt_keepalive(sock); #if ENABLE_FEATURE_TELNETD_STANDALONE ts->sockfd_read = sock; ndelay_on(sock); diff --git a/networking/traceroute.c b/networking/traceroute.c index e41d89e9f..642110c54 100644 --- a/networking/traceroute.c +++ b/networking/traceroute.c @@ -473,8 +473,8 @@ send_probe(int seq, int ttl) #if ENABLE_TRACEROUTE6 if (dest_lsa->u.sa.sa_family == AF_INET6) { - res = setsockopt(sndsock, SOL_IPV6, IPV6_UNICAST_HOPS, &ttl, sizeof(ttl)); - if (res < 0) + res = setsockopt_int(sndsock, SOL_IPV6, IPV6_UNICAST_HOPS, ttl); + if (res != 0) bb_perror_msg_and_die("setsockopt(%s) %d", "UNICAST_HOPS", ttl); out = outip; len = packlen; @@ -482,8 +482,8 @@ send_probe(int seq, int ttl) #endif { #if defined IP_TTL - res = setsockopt(sndsock, IPPROTO_IP, IP_TTL, &ttl, sizeof(ttl)); - if (res < 0) + res = setsockopt_int(sndsock, IPPROTO_IP, IP_TTL, ttl); + if (res != 0) bb_perror_msg_and_die("setsockopt(%s) %d", "TTL", ttl); #endif out = outicmp; @@ -902,13 +902,10 @@ common_traceroute_main(int op, char **argv) if (af == AF_INET6) { xmove_fd(xsocket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6), rcvsock); # ifdef IPV6_RECVPKTINFO - setsockopt(rcvsock, SOL_IPV6, IPV6_RECVPKTINFO, - &const_int_1, sizeof(const_int_1)); - setsockopt(rcvsock, SOL_IPV6, IPV6_2292PKTINFO, - &const_int_1, sizeof(const_int_1)); + setsockopt_1(rcvsock, SOL_IPV6, IPV6_RECVPKTINFO); + setsockopt_1(rcvsock, SOL_IPV6, IPV6_2292PKTINFO); # else - setsockopt(rcvsock, SOL_IPV6, IPV6_PKTINFO, - &const_int_1, sizeof(const_int_1)); + setsockopt_1(rcvsock, SOL_IPV6, IPV6_PKTINFO); # endif } else #endif @@ -918,17 +915,14 @@ common_traceroute_main(int op, char **argv) #if TRACEROUTE_SO_DEBUG if (op & OPT_DEBUG) - setsockopt(rcvsock, SOL_SOCKET, SO_DEBUG, - &const_int_1, sizeof(const_int_1)); + setsockopt_SOL_SOCKET_1(rcvsock, SO_DEBUG); #endif if (op & OPT_BYPASS_ROUTE) - setsockopt(rcvsock, SOL_SOCKET, SO_DONTROUTE, - &const_int_1, sizeof(const_int_1)); + setsockopt_SOL_SOCKET_1(rcvsock, SO_DONTROUTE); #if ENABLE_TRACEROUTE6 if (af == AF_INET6) { - static const int two = 2; - if (setsockopt(rcvsock, SOL_RAW, IPV6_CHECKSUM, &two, sizeof(two)) < 0) + if (setsockopt_int(rcvsock, SOL_RAW, IPV6_CHECKSUM, 2) != 0) bb_perror_msg_and_die("setsockopt(%s)", "IPV6_CHECKSUM"); xmove_fd(xsocket(af, SOCK_DGRAM, 0), sndsock); } else @@ -966,28 +960,25 @@ common_traceroute_main(int op, char **argv) } #ifdef SO_SNDBUF - if (setsockopt(sndsock, SOL_SOCKET, SO_SNDBUF, &packlen, sizeof(packlen)) < 0) { - bb_perror_msg_and_die("SO_SNDBUF"); + if (setsockopt_SOL_SOCKET_int(sndsock, SO_SNDBUF, packlen) != 0) { + bb_perror_msg_and_die("setsockopt(%s)", "SO_SNDBUF"); } #endif #ifdef IP_TOS - if ((op & OPT_TOS) && setsockopt(sndsock, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)) < 0) { + if ((op & OPT_TOS) && setsockopt_int(sndsock, IPPROTO_IP, IP_TOS, tos) != 0) { bb_perror_msg_and_die("setsockopt(%s) %d", "TOS", tos); } #endif #ifdef IP_DONTFRAG if (op & OPT_DONT_FRAGMNT) - setsockopt(sndsock, IPPROTO_IP, IP_DONTFRAG, - &const_int_1, sizeof(const_int_1)); + setsockopt_1(sndsock, IPPROTO_IP, IP_DONTFRAG); #endif #if TRACEROUTE_SO_DEBUG if (op & OPT_DEBUG) - setsockopt(sndsock, SOL_SOCKET, SO_DEBUG, - &const_int_1, sizeof(const_int_1)); + setsockopt_SOL_SOCKET_1(sndsock, SO_DEBUG); #endif if (op & OPT_BYPASS_ROUTE) - setsockopt(sndsock, SOL_SOCKET, SO_DONTROUTE, - &const_int_1, sizeof(const_int_1)); + setsockopt_SOL_SOCKET_1(sndsock, SO_DONTROUTE); outip = xzalloc(packlen); diff --git a/networking/udhcp/dhcpc.c b/networking/udhcp/dhcpc.c index 811a1a1ee..11f7b2d49 100644 --- a/networking/udhcp/dhcpc.c +++ b/networking/udhcp/dhcpc.c @@ -1043,9 +1043,7 @@ static int udhcp_raw_socket(int ifindex) } #endif - if (setsockopt(fd, SOL_PACKET, PACKET_AUXDATA, - &const_int_1, sizeof(int)) < 0 - ) { + if (setsockopt_1(fd, SOL_PACKET, PACKET_AUXDATA) != 0) { if (errno != ENOPROTOOPT) log1("Can't set PACKET_AUXDATA on raw socket"); } diff --git a/util-linux/uevent.c b/util-linux/uevent.c index fb98b4845..514a9e934 100644 --- a/util-linux/uevent.c +++ b/util-linux/uevent.c @@ -37,7 +37,7 @@ enum { #ifndef SO_RCVBUFFORCE #define SO_RCVBUFFORCE 33 #endif -static const int RCVBUF = 2 * 1024 * 1024; +enum { RCVBUF = 2 * 1024 * 1024 }; int uevent_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; int uevent_main(int argc UNUSED_PARAM, char **argv) @@ -63,8 +63,8 @@ int uevent_main(int argc UNUSED_PARAM, char **argv) // find /sys -name uevent -exec sh -c 'echo add >"{}"' ';' // // SO_RCVBUFFORCE (root only) can go above net.core.rmem_max sysctl - setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &RCVBUF, sizeof(RCVBUF)); - setsockopt(fd, SOL_SOCKET, SO_RCVBUFFORCE, &RCVBUF, sizeof(RCVBUF)); + setsockopt_SOL_SOCKET_int(fd, SO_RCVBUF, RCVBUF); + setsockopt_SOL_SOCKET_int(fd, SO_RCVBUFFORCE, RCVBUF); if (0) { int z; socklen_t zl = sizeof(z); -- cgit v1.2.3-55-g6feb From b432923e29dcd8c6f3a528bb9d61952de68e790c Mon Sep 17 00:00:00 2001 From: Bartosz Golaszewski Date: Tue, 25 Aug 2015 13:09:59 +0200 Subject: libbb: add unit tests for is_prefixed_with() Test corner cases too like looking for an empty prefix etc. Signed-off-by: Bartosz Golaszewski Signed-off-by: Denys Vlasenko --- libbb/compare_string_array.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'libbb') diff --git a/libbb/compare_string_array.c b/libbb/compare_string_array.c index e24815a03..450916c3a 100644 --- a/libbb/compare_string_array.c +++ b/libbb/compare_string_array.c @@ -110,3 +110,22 @@ smallint FAST_FUNC yesno(const char *str) return ret / 3; } #endif + +#if ENABLE_UNIT_TEST + +BBUNIT_DEFINE_TEST(is_prefixed_with) +{ + BBUNIT_ASSERT_STREQ(" bar", is_prefixed_with("foo bar", "foo")); + BBUNIT_ASSERT_STREQ("bar", is_prefixed_with("foo bar", "foo ")); + BBUNIT_ASSERT_STREQ("", is_prefixed_with("foo", "foo")); + BBUNIT_ASSERT_STREQ("foo", is_prefixed_with("foo", "")); + BBUNIT_ASSERT_STREQ("", is_prefixed_with("", "")); + + BBUNIT_ASSERT_NULL(is_prefixed_with("foo", "bar foo")); + BBUNIT_ASSERT_NULL(is_prefixed_with("foo foo", "bar")); + BBUNIT_ASSERT_NULL(is_prefixed_with("", "foo")); + + BBUNIT_ENDTEST; +} + +#endif /* ENABLE_UNIT_TEST */ -- cgit v1.2.3-55-g6feb From 0a4d0e8fbf119e61f7223ac00a42505abf7eb168 Mon Sep 17 00:00:00 2001 From: Bartosz Golaszewski Date: Tue, 25 Aug 2015 13:10:00 +0200 Subject: libbb: add a comment describing the way is_prefixed_with() works Signed-off-by: Bartosz Golaszewski Signed-off-by: Denys Vlasenko --- libbb/compare_string_array.c | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'libbb') diff --git a/libbb/compare_string_array.c b/libbb/compare_string_array.c index 450916c3a..cdcb2718d 100644 --- a/libbb/compare_string_array.c +++ b/libbb/compare_string_array.c @@ -5,6 +5,11 @@ #include "libbb.h" +/* + * Return NULL if string is not prefixed with key. Return pointer to the + * first character in string after the prefix key. If key is an empty string, + * return pointer to the beginning of string. + */ char* FAST_FUNC is_prefixed_with(const char *string, const char *key) { #if 0 /* Two passes over key - probably slower */ -- cgit v1.2.3-55-g6feb From 7448b513c84feb3fd06fc57b39f5ab450970c01e Mon Sep 17 00:00:00 2001 From: Bartosz Golaszewski Date: Tue, 25 Aug 2015 16:36:43 +0200 Subject: libbb: add is_suffixed_with() function Signed-off-by: Bartosz Golaszewski Signed-off-by: Denys Vlasenko --- include/libbb.h | 1 + libbb/compare_string_array.c | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) (limited to 'libbb') diff --git a/include/libbb.h b/include/libbb.h index 2e20706e7..543214ea4 100644 --- a/include/libbb.h +++ b/include/libbb.h @@ -413,6 +413,7 @@ const char *bb_basename(const char *name) FAST_FUNC; char *last_char_is(const char *s, int c) FAST_FUNC; const char* endofname(const char *name) FAST_FUNC; char *is_prefixed_with(const char *string, const char *key) FAST_FUNC; +char *is_suffixed_with(const char *string, const char *key) FAST_FUNC; int ndelay_on(int fd) FAST_FUNC; int ndelay_off(int fd) FAST_FUNC; diff --git a/libbb/compare_string_array.c b/libbb/compare_string_array.c index cdcb2718d..e0d8e421b 100644 --- a/libbb/compare_string_array.c +++ b/libbb/compare_string_array.c @@ -28,6 +28,25 @@ char* FAST_FUNC is_prefixed_with(const char *string, const char *key) #endif } +/* + * Return NULL if string is not suffixed with key. Return pointer to the + * beginning of prefix key in string. If key is an empty string return pointer + * to the end of string. + */ +char* FAST_FUNC is_suffixed_with(const char *string, const char *key) +{ + size_t key_len = strlen(key); + ssize_t len_diff = strlen(string) - key_len; + + if (len_diff >= 0) { + if (strcmp(string + len_diff, key) == 0) { + return (char*)key; + } + } + + return NULL; +} + /* returns the array index of the string */ /* (index of first match is returned, or -1) */ int FAST_FUNC index_in_str_array(const char *const string_array[], const char *key) @@ -133,4 +152,18 @@ BBUNIT_DEFINE_TEST(is_prefixed_with) BBUNIT_ENDTEST; } +BBUNIT_DEFINE_TEST(is_suffixed_with) +{ + BBUNIT_ASSERT_STREQ("bar", is_suffixed_with("foo bar", "bar")); + BBUNIT_ASSERT_STREQ("foo", is_suffixed_with("foo", "foo")); + BBUNIT_ASSERT_STREQ("", is_suffixed_with("foo", "")); + BBUNIT_ASSERT_STREQ("", is_suffixed_with("", "")); + + BBUNIT_ASSERT_NULL(is_suffixed_with("foo", "bar foo")); + BBUNIT_ASSERT_NULL(is_suffixed_with("foo foo", "bar")); + BBUNIT_ASSERT_NULL(is_suffixed_with("", "foo")); + + BBUNIT_ENDTEST; +} + #endif /* ENABLE_UNIT_TEST */ -- cgit v1.2.3-55-g6feb From 68acc0f835360d439c65d349812b817b1ce5dc61 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Tue, 25 Aug 2015 21:47:33 +0200 Subject: libbb: make is_suffixed_with() return pointer inside string, not key. Signed-off-by: Denys Vlasenko --- libbb/compare_string_array.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'libbb') diff --git a/libbb/compare_string_array.c b/libbb/compare_string_array.c index e0d8e421b..3dbd3eb1a 100644 --- a/libbb/compare_string_array.c +++ b/libbb/compare_string_array.c @@ -39,8 +39,9 @@ char* FAST_FUNC is_suffixed_with(const char *string, const char *key) ssize_t len_diff = strlen(string) - key_len; if (len_diff >= 0) { - if (strcmp(string + len_diff, key) == 0) { - return (char*)key; + string += len_diff; + if (strcmp(string, key) == 0) { + return (char*)string; } } -- cgit v1.2.3-55-g6feb From f085344d5c4de46d0ef3e15a97ef444fd7cc3194 Mon Sep 17 00:00:00 2001 From: Tito Ragusa Date: Tue, 15 Sep 2015 23:38:01 +0200 Subject: libbb: another unit test for is_suffixed_with Suggested by Bartosz Golaszewski. Signed-off-by: Tito Ragusa Signed-off-by: Denys Vlasenko --- libbb/compare_string_array.c | 1 + 1 file changed, 1 insertion(+) (limited to 'libbb') diff --git a/libbb/compare_string_array.c b/libbb/compare_string_array.c index 3dbd3eb1a..2f51237a3 100644 --- a/libbb/compare_string_array.c +++ b/libbb/compare_string_array.c @@ -159,6 +159,7 @@ BBUNIT_DEFINE_TEST(is_suffixed_with) BBUNIT_ASSERT_STREQ("foo", is_suffixed_with("foo", "foo")); BBUNIT_ASSERT_STREQ("", is_suffixed_with("foo", "")); BBUNIT_ASSERT_STREQ("", is_suffixed_with("", "")); + BBUNIT_ASSERT_STREQ("foo", is_suffixed_with("barfoofoo", "foo")); BBUNIT_ASSERT_NULL(is_suffixed_with("foo", "bar foo")); BBUNIT_ASSERT_NULL(is_suffixed_with("foo foo", "bar")); -- cgit v1.2.3-55-g6feb From 5711a2a4ad51ad203a2ed4ffc72593e83920b36a Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Wed, 7 Oct 2015 17:55:33 +0200 Subject: libbb: more compact API for bb_parse_mode() function old new delta make_device 2182 2188 +6 parse_command 1440 1443 +3 parse_params 1497 1499 +2 install_main 773 769 -4 mkdir_main 168 160 -8 getoptscmd 641 632 -9 builtin_umask 158 147 -11 bb_parse_mode 431 410 -21 umaskcmd 286 258 -28 ------------------------------------------------------------------------------ (add/remove: 0/0 grow/shrink: 3/6 up/down: 11/-81) Total: -70 bytes Signed-off-by: Denys Vlasenko --- coreutils/chmod.c | 4 ++-- coreutils/install.c | 2 +- coreutils/libcoreutils/getopt_mk_fifo_nod.c | 4 +++- coreutils/mkdir.c | 4 ++-- findutils/find.c | 3 ++- include/libbb.h | 3 ++- libbb/parse_mode.c | 16 +++++++--------- shell/ash.c | 3 ++- shell/hush.c | 7 ++++--- util-linux/mdev.c | 2 +- 10 files changed, 26 insertions(+), 22 deletions(-) (limited to 'libbb') diff --git a/coreutils/chmod.c b/coreutils/chmod.c index 5ee45b942..a21c6d501 100644 --- a/coreutils/chmod.c +++ b/coreutils/chmod.c @@ -69,9 +69,9 @@ static int FAST_FUNC fileAction(const char *fileName, struct stat *statbuf, void if (S_ISLNK(statbuf->st_mode)) return TRUE; } - newmode = statbuf->st_mode; - if (!bb_parse_mode((char *)param, &newmode)) + newmode = bb_parse_mode((char *)param, statbuf->st_mode); + if (newmode == (mode_t)-1) bb_error_msg_and_die("invalid mode '%s'", (char *)param); if (chmod(fileName, newmode) == 0) { diff --git a/coreutils/install.c b/coreutils/install.c index 73f9c70d5..8aa51cc34 100644 --- a/coreutils/install.c +++ b/coreutils/install.c @@ -159,7 +159,7 @@ int install_main(int argc, char **argv) } mode = 0755; /* GNU coreutils 6.10 compat */ if (opts & OPT_MODE) - bb_parse_mode(mode_str, &mode); + mode = bb_parse_mode(mode_str, mode); uid = (opts & OPT_OWNER) ? get_ug_id(uid_str, xuname2uid) : getuid(); gid = (opts & OPT_GROUP) ? get_ug_id(gid_str, xgroup2gid) : getgid(); diff --git a/coreutils/libcoreutils/getopt_mk_fifo_nod.c b/coreutils/libcoreutils/getopt_mk_fifo_nod.c index 222717149..47375ff91 100644 --- a/coreutils/libcoreutils/getopt_mk_fifo_nod.c +++ b/coreutils/libcoreutils/getopt_mk_fifo_nod.c @@ -33,7 +33,9 @@ mode_t FAST_FUNC getopt_mk_fifo_nod(char **argv) int opt; opt = getopt32(argv, "m:" IF_SELINUX("Z:"), &smode IF_SELINUX(,&scontext)); if (opt & 1) { - if (bb_parse_mode(smode, &mode)) + mode = bb_parse_mode(smode, mode); + if (mode != (mode_t)-1) /* if mode is valid */ + /* make future mknod/mkfifo set mode bits exactly */ umask(0); } diff --git a/coreutils/mkdir.c b/coreutils/mkdir.c index 864edfb0a..6f7b004dd 100644 --- a/coreutils/mkdir.c +++ b/coreutils/mkdir.c @@ -71,8 +71,8 @@ int mkdir_main(int argc UNUSED_PARAM, char **argv) #endif opt = getopt32(argv, "m:pv" IF_SELINUX("Z:"), &smode IF_SELINUX(,&scontext)); if (opt & 1) { - mode_t mmode = 0777; - if (!bb_parse_mode(smode, &mmode)) { + mode_t mmode = bb_parse_mode(smode, 0777); + if (mmode == (mode_t)-1) { bb_error_msg_and_die("invalid mode '%s'", smode); } mode = mmode; diff --git a/findutils/find.c b/findutils/find.c index ced8922e7..f72cad7d1 100644 --- a/findutils/find.c +++ b/findutils/find.c @@ -1261,7 +1261,8 @@ static action*** parse_params(char **argv) ap->perm_char = arg1[0]; arg1 = (arg1[0] == '/' ? arg1+1 : plus_minus_num(arg1)); /*ap->perm_mask = 0; - ALLOC_ACTION did it */ - if (!bb_parse_mode(arg1, &ap->perm_mask)) + ap->perm_mask = bb_parse_mode(arg1, ap->perm_mask); + if (ap->perm_mask == (mode_t)-1) bb_error_msg_and_die("invalid mode '%s'", arg1); } #endif diff --git a/include/libbb.h b/include/libbb.h index 543214ea4..d79843a2d 100644 --- a/include/libbb.h +++ b/include/libbb.h @@ -1251,7 +1251,8 @@ char *bb_ask_stdin(const char * prompt) FAST_FUNC; char *bb_ask(const int fd, int timeout, const char * prompt) FAST_FUNC; int bb_ask_confirmation(void) FAST_FUNC; -int bb_parse_mode(const char* s, mode_t* theMode) FAST_FUNC; +/* Returns -1 if input is invalid. current_mode is a base for e.g. "u+rw" */ +int bb_parse_mode(const char* s, unsigned cur_mode) FAST_FUNC; /* * Config file parser diff --git a/libbb/parse_mode.c b/libbb/parse_mode.c index 5a4e1c579..bddd39bca 100644 --- a/libbb/parse_mode.c +++ b/libbb/parse_mode.c @@ -15,7 +15,7 @@ #define FILEMODEBITS (S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO) -int FAST_FUNC bb_parse_mode(const char *s, mode_t *current_mode) +int FAST_FUNC bb_parse_mode(const char *s, unsigned current_mode) { static const mode_t who_mask[] = { S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO, /* a */ @@ -46,13 +46,12 @@ int FAST_FUNC bb_parse_mode(const char *s, mode_t *current_mode) tmp = strtoul(s, &e, 8); if (*e || (tmp > 07777U)) { /* Check range and trailing chars. */ - return 0; + return -1; } - *current_mode = tmp; - return 1; + return tmp; } - new_mode = *current_mode; + new_mode = current_mode; /* Note: we allow empty clauses, and hence empty modes. * We treat an empty mode as no change to perms. */ @@ -71,7 +70,7 @@ int FAST_FUNC bb_parse_mode(const char *s, mode_t *current_mode) if (*p == *s) { wholist |= who_mask[(int)(p-who_chars)]; if (!*++s) { - return 0; + return -1; } goto WHO_LIST; } @@ -80,7 +79,7 @@ int FAST_FUNC bb_parse_mode(const char *s, mode_t *current_mode) do { /* Process action list. */ if ((*s != '+') && (*s != '-')) { if (*s != '=') { - return 0; + return -1; } /* Since op is '=', clear all bits corresponding to the * wholist, or all file bits if wholist is empty. */ @@ -145,6 +144,5 @@ int FAST_FUNC bb_parse_mode(const char *s, mode_t *current_mode) } while (*s && (*s != ',')); } - *current_mode = new_mode; - return 1; + return new_mode; } diff --git a/shell/ash.c b/shell/ash.c index 80dfc1d6a..ab8ec006f 100644 --- a/shell/ash.c +++ b/shell/ash.c @@ -12862,7 +12862,8 @@ umaskcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) /* symbolic umasks are inverted: "umask a=rx" calls umask(222) */ if (!isdigit(modestr[0])) mask ^= 0777; - if (!bb_parse_mode(modestr, &mask) || (unsigned)mask > 0777) { + mask = bb_parse_mode(modestr, mask); + if ((unsigned)mask > 0777) { ash_msg_and_raise_error("illegal mode: %s", modestr); } if (!isdigit(modestr[0])) diff --git a/shell/hush.c b/shell/hush.c index 8b8d5fc8b..bccd9c1e9 100644 --- a/shell/hush.c +++ b/shell/hush.c @@ -8965,6 +8965,7 @@ static int FAST_FUNC builtin_umask(char **argv) int rc; mode_t mask; + rc = 1; mask = umask(0); argv = skip_dash_dash(argv); if (argv[0]) { @@ -8974,19 +8975,19 @@ static int FAST_FUNC builtin_umask(char **argv) /* symbolic umasks are inverted: "umask a=rx" calls umask(222) */ if (!isdigit(argv[0][0])) mask ^= 0777; - rc = bb_parse_mode(argv[0], &mask); + mask = bb_parse_mode(argv[0], mask); if (!isdigit(argv[0][0])) mask ^= 0777; - if (rc == 0 || (unsigned)mask > 0777) { + if ((unsigned)mask > 0777) { mask = old_mask; /* bash messages: * bash: umask: 'q': invalid symbolic mode operator * bash: umask: 999: octal number out of range */ bb_error_msg("%s: invalid mode '%s'", "umask", argv[0]); + rc = 0; } } else { - rc = 1; /* Mimic bash */ printf("%04o\n", (unsigned) mask); /* fall through and restore mask which we set to 0 */ diff --git a/util-linux/mdev.c b/util-linux/mdev.c index 662e8ab38..51781d597 100644 --- a/util-linux/mdev.c +++ b/util-linux/mdev.c @@ -406,7 +406,7 @@ static void parse_next_rule(void) } /* 3rd field: mode - device permissions */ - bb_parse_mode(tokens[2], &G.cur_rule.mode); + G.cur_rule.mode = bb_parse_mode(tokens[2], G.cur_rule.mode); /* 4th field (opt): ">|=alias" or "!" to not create the node */ val = tokens[3]; -- cgit v1.2.3-55-g6feb From 305958dbd456d463a92c1fa4c85ceb69a8c7db32 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Wed, 7 Oct 2015 19:17:01 +0200 Subject: md5sum: code shrink For CONFIG_MD5_SMALL=1: function old new delta md5_process_block64 925 881 -44 For CONFIG_MD5_SMALL=0: function old new delta md5_process_block64 1603 1586 -17 Signed-off-by: Denys Vlasenko --- libbb/hash_md5_sha.c | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) (limited to 'libbb') diff --git a/libbb/hash_md5_sha.c b/libbb/hash_md5_sha.c index 1f63ccdee..d08c6b2f7 100644 --- a/libbb/hash_md5_sha.c +++ b/libbb/hash_md5_sha.c @@ -137,7 +137,7 @@ static void FAST_FUNC md5_process_block64(md5_ctx_t *ctx) #if MD5_SMALL > 0 /* Before we start, one word to the strange constants. They are defined in RFC 1321 as - T[i] = (int)(4294967296.0 * fabs(sin(i))), i=1..64 + T[i] = (int)(2^32 * fabs(sin(i))), i=1..64 */ static const uint32_t C_array[] = { /* round 1 */ @@ -213,7 +213,7 @@ static void FAST_FUNC md5_process_block64(md5_ctx_t *ctx) case 2: temp += FH(B, C, D); break; - case 3: + default: /* case 3 */ temp += FI(B, C, D); } temp += words[(int) (*pp++)] + *pc++; @@ -277,10 +277,6 @@ static void FAST_FUNC md5_process_block64(md5_ctx_t *ctx) #else /* MD5_SMALL == 0 or 1 */ - uint32_t A_save = A; - uint32_t B_save = B; - uint32_t C_save = C; - uint32_t D_save = D; # if MD5_SMALL == 1 const uint32_t *pc; const char *pp; @@ -425,10 +421,10 @@ static void FAST_FUNC md5_process_block64(md5_ctx_t *ctx) # undef OP # endif /* Add checksum to the starting values */ - ctx->hash[0] = A_save + A; - ctx->hash[1] = B_save + B; - ctx->hash[2] = C_save + C; - ctx->hash[3] = D_save + D; + ctx->hash[0] += A; + ctx->hash[1] += B; + ctx->hash[2] += C; + ctx->hash[3] += D; #endif } #undef FF -- cgit v1.2.3-55-g6feb From 550bf5b4a418378cd8f9fbbf5252fe57acdacb5a Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Fri, 9 Oct 2015 16:42:57 +0200 Subject: remove global "jmp_buf die_jmp" from !FEATURE_PREFER_APPLETS builds function old new delta xfunc_has_died - 21 +21 sleep_much - 12 +12 sleep10 - 9 +9 die_func - 4 +4 fflush_stdout_and_exit 35 36 +1 builtin_type 121 119 -2 die_sleep 4 - -4 xfunc_die 60 24 -36 hush_main 1128 1011 -117 die_jmp 156 - -156 ------------------------------------------------------------------------------ (add/remove: 4/2 grow/shrink: 1/3 up/down: 47/-315) Total: -268 bytes text data bss dec hex filename 939992 992 17652 958636 ea0ac busybox_old 939880 992 17496 958368 e9fa0 busybox_unstripped Signed-off-by: Denys Vlasenko --- include/libbb.h | 3 +-- init/init.c | 9 +++++++-- libbb/fflush_stdout_and_exit.c | 13 ++++--------- libbb/vfork_daemon_rexec.c | 39 +++++++++++++++++++++++++-------------- libbb/xfunc_die.c | 26 ++++---------------------- loginutils/getty.c | 7 ++++++- shell/hush.c | 25 ++++++++++++++----------- 7 files changed, 61 insertions(+), 61 deletions(-) (limited to 'libbb') diff --git a/include/libbb.h b/include/libbb.h index d79843a2d..2f24ecbc3 100644 --- a/include/libbb.h +++ b/include/libbb.h @@ -1127,9 +1127,8 @@ enum { extern const char *msg_eol; extern smallint syslog_level; extern smallint logmode; -extern int die_sleep; extern uint8_t xfunc_error_retval; -extern jmp_buf die_jmp; +extern void (*die_func)(void); extern void xfunc_die(void) NORETURN FAST_FUNC; extern void bb_show_usage(void) NORETURN FAST_FUNC; extern void bb_error_msg(const char *s, ...) __attribute__ ((format (printf, 1, 2))) FAST_FUNC; diff --git a/init/init.c b/init/init.c index b2fe85635..80c5d0f74 100644 --- a/init/init.c +++ b/init/init.c @@ -1015,6 +1015,11 @@ void handle_sigsegv(int sig, siginfo_t *info, void *ucontext) } #endif +static void sleep_much(void) +{ + sleep(30 * 24*60*60); +} + int init_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; int init_main(int argc UNUSED_PARAM, char **argv) { @@ -1051,12 +1056,12 @@ int init_main(int argc UNUSED_PARAM, char **argv) /* If, say, xmalloc would ever die, we don't want to oops kernel * by exiting. - * NB: we set die_sleep *after* PID 1 check and bb_show_usage. + * NB: we set die_func *after* PID 1 check and bb_show_usage. * Otherwise, for example, "init u" ("please rexec yourself" * command for sysvinit) will show help text (which isn't too bad), * *and sleep forever* (which is bad!) */ - die_sleep = 30 * 24*60*60; + die_func = sleep_much; /* Figure out where the default console should be */ console_init(); diff --git a/libbb/fflush_stdout_and_exit.c b/libbb/fflush_stdout_and_exit.c index 9ad5dbf96..b4bed865f 100644 --- a/libbb/fflush_stdout_and_exit.c +++ b/libbb/fflush_stdout_and_exit.c @@ -15,15 +15,10 @@ void FAST_FUNC fflush_stdout_and_exit(int retval) { + xfunc_error_retval = retval; if (fflush(stdout)) bb_perror_msg_and_die(bb_msg_standard_output); - - if (ENABLE_FEATURE_PREFER_APPLETS && die_sleep < 0) { - /* We are in NOFORK applet. Do not exit() directly, - * but use xfunc_die() */ - xfunc_error_retval = retval; - xfunc_die(); - } - - exit(retval); + /* In case we are in NOFORK applet. Do not exit() directly, + * but use xfunc_die() */ + xfunc_die(); } diff --git a/libbb/vfork_daemon_rexec.c b/libbb/vfork_daemon_rexec.c index ed1f86f0c..d6ca7b263 100644 --- a/libbb/vfork_daemon_rexec.c +++ b/libbb/vfork_daemon_rexec.c @@ -69,28 +69,44 @@ pid_t FAST_FUNC xspawn(char **argv) } #if ENABLE_FEATURE_PREFER_APPLETS +static jmp_buf die_jmp; +static void jump(void) +{ + /* Special case. We arrive here if NOFORK applet + * calls xfunc, which then decides to die. + * We don't die, but jump instead back to caller. + * NOFORK applets still cannot carelessly call xfuncs: + * p = xmalloc(10); + * q = xmalloc(10); // BUG! if this dies, we leak p! + */ + /* | 0x100 allows to pass zero exitcode (longjmp can't pass 0). + * This works because exitcodes are bytes, + * run_nofork_applet() ensures that by "& 0xff" */ + longjmp(die_jmp, xfunc_error_retval | 0x100); +} + struct nofork_save_area { jmp_buf die_jmp; + void (*die_func)(void); const char *applet_name; uint32_t option_mask32; - int die_sleep; uint8_t xfunc_error_retval; }; static void save_nofork_data(struct nofork_save_area *save) { memcpy(&save->die_jmp, &die_jmp, sizeof(die_jmp)); + save->die_func = die_func; save->applet_name = applet_name; - save->xfunc_error_retval = xfunc_error_retval; save->option_mask32 = option_mask32; - save->die_sleep = die_sleep; + save->xfunc_error_retval = xfunc_error_retval; } static void restore_nofork_data(struct nofork_save_area *save) { memcpy(&die_jmp, &save->die_jmp, sizeof(die_jmp)); + die_func = save->die_func; applet_name = save->applet_name; - xfunc_error_retval = save->xfunc_error_retval; option_mask32 = save->option_mask32; - die_sleep = save->die_sleep; + xfunc_error_retval = save->xfunc_error_retval; } int FAST_FUNC run_nofork_applet(int applet_no, char **argv) @@ -133,11 +149,8 @@ int FAST_FUNC run_nofork_applet(int applet_no, char **argv) while (argv[argc]) argc++; - /* Special flag for xfunc_die(). If xfunc will "die" - * in NOFORK applet, xfunc_die() sees negative - * die_sleep and longjmp here instead. */ - die_sleep = -1; - + /* If xfunc "dies" in NOFORK applet, die_func longjmp's here instead */ + die_func = jump; rc = setjmp(die_jmp); if (!rc) { /* Some callers (xargs) @@ -146,10 +159,8 @@ int FAST_FUNC run_nofork_applet(int applet_no, char **argv) memcpy(tmp_argv, argv, (argc+1) * sizeof(tmp_argv[0])); /* Finally we can call NOFORK applet's main() */ rc = applet_main[applet_no](argc, tmp_argv); - } else { /* xfunc died in NOFORK applet */ - /* in case they meant to return 0... */ - if (rc == -2222) - rc = 0; + } else { + /* xfunc died in NOFORK applet */ } /* Restoring some globals */ diff --git a/libbb/xfunc_die.c b/libbb/xfunc_die.c index 204e5e49d..73f7998e5 100644 --- a/libbb/xfunc_die.c +++ b/libbb/xfunc_die.c @@ -7,34 +7,16 @@ * Licensed under GPLv2, see file LICENSE in this source tree. */ -/* Keeping it separate allows to NOT suck in stdio for VERY small applets. +/* Keeping it separate allows to NOT pull in stdio for VERY small applets. * Try building busybox with only "true" enabled... */ #include "libbb.h" -int die_sleep; -#if ENABLE_FEATURE_PREFER_APPLETS || ENABLE_HUSH -jmp_buf die_jmp; -#endif +void (*die_func)(void); void FAST_FUNC xfunc_die(void) { - if (die_sleep) { - if ((ENABLE_FEATURE_PREFER_APPLETS || ENABLE_HUSH) - && die_sleep < 0 - ) { - /* Special case. We arrive here if NOFORK applet - * calls xfunc, which then decides to die. - * We don't die, but jump instead back to caller. - * NOFORK applets still cannot carelessly call xfuncs: - * p = xmalloc(10); - * q = xmalloc(10); // BUG! if this dies, we leak p! - */ - /* -2222 means "zero" (longjmp can't pass 0) - * run_nofork_applet() catches -2222. */ - longjmp(die_jmp, xfunc_error_retval ? xfunc_error_retval : -2222); - } - sleep(die_sleep); - } + if (die_func) + die_func(); exit(xfunc_error_retval); } diff --git a/loginutils/getty.c b/loginutils/getty.c index 174542841..762d5c773 100644 --- a/loginutils/getty.c +++ b/loginutils/getty.c @@ -520,6 +520,11 @@ static void alarm_handler(int sig UNUSED_PARAM) _exit(EXIT_SUCCESS); } +static void sleep10(void) +{ + sleep(10); +} + int getty_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; int getty_main(int argc UNUSED_PARAM, char **argv) { @@ -599,7 +604,7 @@ int getty_main(int argc UNUSED_PARAM, char **argv) close(n--); /* Logging. We want special flavor of error_msg_and_die */ - die_sleep = 10; + die_func = sleep10; msg_eol = "\r\n"; /* most likely will internally use fd #3 in CLOEXEC mode: */ openlog(applet_name, LOG_PID, LOG_AUTH); diff --git a/shell/hush.c b/shell/hush.c index f085ed3eb..0d107715f 100644 --- a/shell/hush.c +++ b/shell/hush.c @@ -1479,10 +1479,11 @@ static sighandler_t install_sighandler(int sig, sighandler_t handler) #if ENABLE_HUSH_JOB +static void xfunc_has_died(void); /* After [v]fork, in child: do not restore tty pgrp on xfunc death */ -# define disable_restore_tty_pgrp_on_exit() (die_sleep = 0) +# define disable_restore_tty_pgrp_on_exit() (die_func = NULL) /* After [v]fork, in parent: restore tty pgrp on xfunc death */ -# define enable_restore_tty_pgrp_on_exit() (die_sleep = -1) +# define enable_restore_tty_pgrp_on_exit() (die_func = xfunc_has_died) /* Restores tty foreground process group, and exits. * May be called as signal handler for fatal signal @@ -1587,6 +1588,15 @@ static void hush_exit(int exitcode) #endif } +static void xfunc_has_died(void) NORETURN; +static void xfunc_has_died(void) +{ + /* xfunc has failed! die die die */ + /* no EXIT traps, this is an escape hatch! */ + G.exiting = 1; + hush_exit(xfunc_error_retval); +} + //TODO: return a mask of ALL handled sigs? static int check_and_run_traps(void) @@ -7866,12 +7876,7 @@ int hush_main(int argc, char **argv) /* Initialize some more globals to non-zero values */ cmdedit_update_prompt(); - if (setjmp(die_jmp)) { - /* xfunc has failed! die die die */ - /* no EXIT traps, this is an escape hatch! */ - G.exiting = 1; - hush_exit(xfunc_error_retval); - } + die_func = xfunc_has_died; /* Shell is non-interactive at first. We need to call * install_special_sighandlers() if we are going to execute "sh