From 610c4c385b38280c7bde7a48d95ec019cbfe1ab4 Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Wed, 30 Mar 2016 00:42:05 +0200 Subject: applet_tables: save space by removing applet name offsets The array applet_nameofs consumes two bytes per applet. It encodes nofork/noexec flags suid flags the offset of the applet name in the applet_name string Change the applet_table build tool to store the flags in two separate arrays (applet_flags and applet_suid). Replace applet_nameofs[] with a smaller version that only stores a limited number of offsets. This requires changes to the macros APPLET_IS_NOFORK, APPLET_IS_NOEXEC and APPLET_SUID. According to Valgrind the original find_applet_by_name required 353 cycles per call, averaged over all names. Adjusting the number of known offsets allows space to be traded off against execution time: KNOWN_OFFSETS cycles bytes (wrt KNOWN_OFFSETS = 0) 0 9057 - 2 4604 32 4 2407 75 8 1342 98 16 908 130 32 884 194 This patch uses KNOWN_OFFSETS = 8. v2: Remove some dead code from the applet_table tool; Treat the applet in the middle of the table as a special case. v3: Use the middle applet to adjust the start of the linear search as well as the last applet found. v4: Use an augmented linear search in find_applet_by_name. Drop the special treatment of the middle name from get_applet_name: most of the advantage now derives from the last stored value. v5: Don't store index in applet_nameofs, it can be calculated. v6: Tweaks by Denys function old new delta find_applet_by_name 25 125 +100 applet_suid - 92 +92 run_applet_no_and_exit 452 460 +8 run_applet_and_exit 695 697 +2 applet_name_compare 31 - -31 applet_nameofs 734 14 -720 ------------------------------------------------------------------------------ (add/remove: 1/1 grow/shrink: 3/1 up/down: 202/-751) Total: -549 bytes text data bss dec hex filename 925464 906 17160 943530 e65aa busybox_old 924915 906 17160 942981 e6385 busybox_unstripped Signed-off-by: Ron Yorston Signed-off-by: Denys Vlasenko --- libbb/appletlib.c | 82 ++++++++++++++++++++++++++++++---------------- libbb/vfork_daemon_rexec.c | 3 +- 2 files changed, 55 insertions(+), 30 deletions(-) (limited to 'libbb') diff --git a/libbb/appletlib.c b/libbb/appletlib.c index 95e589e74..aeaf238f1 100644 --- a/libbb/appletlib.c +++ b/libbb/appletlib.c @@ -139,36 +139,56 @@ void FAST_FUNC bb_show_usage(void) xfunc_die(); } -#if NUM_APPLETS > 8 -static int applet_name_compare(const void *name, const void *idx) -{ - int i = (int)(ptrdiff_t)idx - 1; - return strcmp(name, APPLET_NAME(i)); -} -#endif int FAST_FUNC find_applet_by_name(const char *name) { -#if NUM_APPLETS > 8 - /* Do a binary search to find the applet entry given the name. */ + unsigned i, max; + int j; const char *p; - p = bsearch(name, (void*)(ptrdiff_t)1, ARRAY_SIZE(applet_main), 1, applet_name_compare); - /* - * if (!p) return -1; - * ^^^^^^^^^^^^^^^^^^ the code below will do this if p == NULL :) - */ - return (int)(ptrdiff_t)p - 1; + + p = applet_names; + i = 0; +#if KNOWN_APPNAME_OFFSETS <= 0 + max = NUM_APPLETS; #else - /* A version which does not pull in bsearch */ - int i = 0; - const char *p = applet_names; - while (i < NUM_APPLETS) { - if (strcmp(name, p) == 0) - return i; - p += strlen(p) + 1; + max = NUM_APPLETS * KNOWN_APPNAME_OFFSETS; + for (j = ARRAY_SIZE(applet_nameofs)-1; j >= 0; j--) { + const char *pp = applet_names + applet_nameofs[j]; + if (strcmp(name, pp) >= 0) { + //bb_error_msg("name:'%s' >= pp:'%s'", name, pp); + p = pp; + i = max - NUM_APPLETS; + break; + } + max -= NUM_APPLETS; + } + max /= (unsigned)KNOWN_APPNAME_OFFSETS; + i /= (unsigned)KNOWN_APPNAME_OFFSETS; + //bb_error_msg("name:'%s' starting from:'%s' i:%u max:%u", name, p, i, max); +#endif + + /* Open-coding without strcmp/strlen calls for speed */ + while (i < max) { + char ch; + j = 0; + /* Do we see "name\0" in applet_names[p] position? */ + while ((ch = *p) == name[j]) { + if (ch == '\0') { + //bb_error_msg("found:'%s' i:%u", name, i); + return i; /* yes */ + } + p++; + j++; + } + /* No. + * p => 1st non-matching char in applet_names[], + * skip to and including NUL. + */ + while (ch != '\0') + ch = *++p; + p++; i++; } return -1; -#endif } @@ -583,6 +603,7 @@ static void install_links(const char *busybox, int use_symbolic_links, * busybox.h::bb_install_loc_t, or else... */ int (*lf)(const char *, const char *); char *fpc; + const char *appname = applet_names; unsigned i; int rc; @@ -593,7 +614,7 @@ static void install_links(const char *busybox, int use_symbolic_links, for (i = 0; i < ARRAY_SIZE(applet_main); i++) { fpc = concat_path_file( custom_install_dir ? custom_install_dir : install_dir[APPLET_INSTALL_LOC(i)], - APPLET_NAME(i)); + appname); // debug: bb_error_msg("%slinking %s to busybox", // use_symbolic_links ? "sym" : "", fpc); rc = lf(busybox, fpc); @@ -601,6 +622,8 @@ static void install_links(const char *busybox, int use_symbolic_links, bb_simple_perror_msg(fpc); } free(fpc); + while (*appname++ != '\0') + continue; } } # else @@ -754,7 +777,7 @@ void FAST_FUNC run_applet_no_and_exit(int applet_no, char **argv) /* Reinit some shared global data */ xfunc_error_retval = EXIT_FAILURE; - applet_name = APPLET_NAME(applet_no); + applet_name = bb_get_last_path_component_nostrip(argv[0]); /* Special case. POSIX says "test --help" * should be no different from e.g. "test --foo". @@ -785,11 +808,14 @@ void FAST_FUNC run_applet_no_and_exit(int applet_no, char **argv) void FAST_FUNC run_applet_and_exit(const char *name, char **argv) { - int applet = find_applet_by_name(name); - if (applet >= 0) - run_applet_no_and_exit(applet, argv); + int applet; + if (is_prefixed_with(name, "busybox")) exit(busybox_main(argv)); + /* find_applet_by_name() search is more expensive, so goes second */ + applet = find_applet_by_name(name); + if (applet >= 0) + run_applet_no_and_exit(applet, argv); } #endif /* !defined(SINGLE_APPLET_MAIN) */ diff --git a/libbb/vfork_daemon_rexec.c b/libbb/vfork_daemon_rexec.c index d6ca7b263..1adb5b3c4 100644 --- a/libbb/vfork_daemon_rexec.c +++ b/libbb/vfork_daemon_rexec.c @@ -116,8 +116,6 @@ int FAST_FUNC run_nofork_applet(int applet_no, char **argv) save_nofork_data(&old); - applet_name = APPLET_NAME(applet_no); - xfunc_error_retval = EXIT_FAILURE; /* In case getopt() or getopt32() was already called: @@ -157,6 +155,7 @@ int FAST_FUNC run_nofork_applet(int applet_no, char **argv) * need argv untouched because they free argv[i]! */ char *tmp_argv[argc+1]; memcpy(tmp_argv, argv, (argc+1) * sizeof(tmp_argv[0])); + applet_name = tmp_argv[0]; /* Finally we can call NOFORK applet's main() */ rc = applet_main[applet_no](argc, tmp_argv); } else { -- cgit v1.2.3-55-g6feb From 8f2e99c813dcac25faf58162ed18848a02888ff6 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Wed, 30 Mar 2016 18:41:23 +0200 Subject: udhcp: get rid of bb_info_msg() function old new delta udhcpd_main 1501 1531 +30 d6_recv_raw_packet 251 264 +13 perform_d6_release 188 198 +10 udhcpc6_main 2443 2449 +6 udhcp_recv_raw_packet 582 588 +6 udhcp_recv_kernel_packet 132 138 +6 send_d6_renew 140 146 +6 d6_recv_kernel_packet 118 124 +6 send_renew 77 82 +5 send_discover 85 90 +5 send_decline 84 89 +5 send_d6_select 97 102 +5 send_d6_discover 174 179 +5 perform_release 167 172 +5 count_lines 72 74 +2 udhcpc_main 2836 2837 +1 bb_info_msg 125 - -125 ------------------------------------------------------------------------------ (add/remove: 0/2 grow/shrink: 17/4 up/down: 117/-180) Total: -63 bytes text data bss dec hex filename 924935 906 17160 943001 e6399 busybox_old 924736 906 17160 942802 e62d2 busybox_unstripped Signed-off-by: Denys Vlasenko --- include/libbb.h | 1 - libbb/Kbuild.src | 1 - libbb/info_msg.c | 62 ----------------------------------- networking/udhcp/arpping.c | 2 +- networking/udhcp/common.c | 8 ++--- networking/udhcp/common.h | 6 ++-- networking/udhcp/d6_dhcpc.c | 60 +++++++++++++++++----------------- networking/udhcp/d6_packet.c | 10 +++--- networking/udhcp/d6_socket.c | 2 +- networking/udhcp/dhcpc.c | 70 ++++++++++++++++++++-------------------- networking/udhcp/dhcpd.c | 34 +++++++++---------- networking/udhcp/files.c | 2 +- networking/udhcp/leases.c | 2 +- networking/udhcp/packet.c | 12 +++---- networking/udhcp/socket.c | 4 +-- networking/udhcp/static_leases.c | 2 +- 16 files changed, 107 insertions(+), 171 deletions(-) delete mode 100644 libbb/info_msg.c (limited to 'libbb') diff --git a/include/libbb.h b/include/libbb.h index 8b226c00c..98d788402 100644 --- a/include/libbb.h +++ b/include/libbb.h @@ -1150,7 +1150,6 @@ extern void bb_herror_msg(const char *s, ...) __attribute__ ((format (printf, 1, extern void bb_herror_msg_and_die(const char *s, ...) __attribute__ ((noreturn, format (printf, 1, 2))) FAST_FUNC; extern void bb_perror_nomsg_and_die(void) NORETURN FAST_FUNC; extern void bb_perror_nomsg(void) FAST_FUNC; -extern void bb_info_msg(const char *s, ...) __attribute__ ((format (printf, 1, 2))) FAST_FUNC; extern void bb_verror_msg(const char *s, va_list p, const char *strerr) FAST_FUNC; extern void bb_logenv_override(void) FAST_FUNC; diff --git a/libbb/Kbuild.src b/libbb/Kbuild.src index 7fb687227..b08ce1158 100644 --- a/libbb/Kbuild.src +++ b/libbb/Kbuild.src @@ -46,7 +46,6 @@ lib-y += get_volsize.o lib-y += herror_msg.o lib-y += human_readable.o lib-y += inet_common.o -lib-y += info_msg.o lib-y += inode_hash.o lib-y += isdirectory.o lib-y += kernel_version.o diff --git a/libbb/info_msg.c b/libbb/info_msg.c deleted file mode 100644 index 56ca2efd4..000000000 --- a/libbb/info_msg.c +++ /dev/null @@ -1,62 +0,0 @@ -/* vi: set sw=4 ts=4: */ -/* - * Utility routines. - * - * Copyright (C) 1999-2004 by Erik Andersen - * - * Licensed under GPLv2 or later, see file LICENSE in this source tree. - */ - -#include "libbb.h" -#if ENABLE_FEATURE_SYSLOG -# include -#endif - -void FAST_FUNC bb_info_msg(const char *s, ...) -{ -#ifdef THIS_ONE_DOESNT_DO_SINGLE_WRITE - va_list p; - /* va_copy is used because it is not portable - * to use va_list p twice */ - va_list p2; - - va_start(p, s); - va_copy(p2, p); - if (logmode & LOGMODE_STDIO) { - vprintf(s, p); - fputs(msg_eol, stdout); - } -# if ENABLE_FEATURE_SYSLOG - if (logmode & LOGMODE_SYSLOG) - vsyslog(LOG_INFO, s, p2); -# endif - va_end(p2); - va_end(p); -#else - int used; - char *msg; - va_list p; - - if (logmode == 0) - return; - - va_start(p, s); - used = vasprintf(&msg, s, p); - va_end(p); - if (used < 0) - return; - -# if ENABLE_FEATURE_SYSLOG - if (logmode & LOGMODE_SYSLOG) - syslog(LOG_INFO, "%s", msg); -# endif - if (logmode & LOGMODE_STDIO) { - fflush_all(); - /* used = strlen(msg); - must be true already */ - msg[used++] = '\n'; - full_write(STDOUT_FILENO, msg, used); - } - - free(msg); -#endif -} diff --git a/networking/udhcp/arpping.c b/networking/udhcp/arpping.c index fad2283c3..c98027316 100644 --- a/networking/udhcp/arpping.c +++ b/networking/udhcp/arpping.c @@ -132,6 +132,6 @@ int FAST_FUNC arpping(uint32_t test_nip, ret: close(s); - log1("%srp reply received for this address", rv ? "No a" : "A"); + log1("%srp reply received for this address", rv ? "no a" : "A"); return rv; } diff --git a/networking/udhcp/common.c b/networking/udhcp/common.c index 680852ce4..1c1863451 100644 --- a/networking/udhcp/common.c +++ b/networking/udhcp/common.c @@ -183,7 +183,7 @@ static void log_option(const char *pfx, const uint8_t *opt) if (dhcp_verbose >= 2) { char buf[256 * 2 + 2]; *bin2hex(buf, (void*) (opt + OPT_DATA), opt[OPT_LEN]) = '\0'; - bb_info_msg("%s: 0x%02x %s", pfx, opt[OPT_CODE], buf); + bb_error_msg("%s: 0x%02x %s", pfx, opt[OPT_CODE], buf); } } #else @@ -269,7 +269,7 @@ uint8_t* FAST_FUNC udhcp_get_option(struct dhcp_packet *packet, int code) } /* log3 because udhcpc uses it a lot - very noisy */ - log3("Option 0x%02x not found", code); + log3("option 0x%02x not found", code); return NULL; } @@ -402,7 +402,7 @@ static NOINLINE void attach_option( struct option_set *new, **curr; /* make a new option */ - log2("Attaching option %02x to list", optflag->code); + log2("attaching option %02x to list", optflag->code); new = xmalloc(sizeof(*new)); new->data = xmalloc(length + OPT_DATA); new->data[OPT_CODE] = optflag->code; @@ -422,7 +422,7 @@ static NOINLINE void attach_option( unsigned old_len; /* add it to an existing option */ - log2("Attaching option %02x to existing member of list", optflag->code); + log2("attaching option %02x to existing member of list", optflag->code); old_len = existing->data[OPT_LEN]; if (old_len + length < 255) { /* actually 255 is ok too, but adding a space can overlow it */ diff --git a/networking/udhcp/common.h b/networking/udhcp/common.h index d20659e2f..496ab11a1 100644 --- a/networking/udhcp/common.h +++ b/networking/udhcp/common.h @@ -256,16 +256,16 @@ struct option_set *udhcp_find_option(struct option_set *opt_list, uint8_t code) #if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 1 # define IF_UDHCP_VERBOSE(...) __VA_ARGS__ extern unsigned dhcp_verbose; -# define log1(...) do { if (dhcp_verbose >= 1) bb_info_msg(__VA_ARGS__); } while (0) +# define log1(...) do { if (dhcp_verbose >= 1) bb_error_msg(__VA_ARGS__); } while (0) # if CONFIG_UDHCP_DEBUG >= 2 void udhcp_dump_packet(struct dhcp_packet *packet) FAST_FUNC; -# define log2(...) do { if (dhcp_verbose >= 2) bb_info_msg(__VA_ARGS__); } while (0) +# define log2(...) do { if (dhcp_verbose >= 2) bb_error_msg(__VA_ARGS__); } while (0) # else # define udhcp_dump_packet(...) ((void)0) # define log2(...) ((void)0) # endif # if CONFIG_UDHCP_DEBUG >= 3 -# define log3(...) do { if (dhcp_verbose >= 3) bb_info_msg(__VA_ARGS__); } while (0) +# define log3(...) do { if (dhcp_verbose >= 3) bb_error_msg(__VA_ARGS__); } while (0) # else # define log3(...) ((void)0) # endif diff --git a/networking/udhcp/d6_dhcpc.c b/networking/udhcp/d6_dhcpc.c index 4e9b705b9..422254d62 100644 --- a/networking/udhcp/d6_dhcpc.c +++ b/networking/udhcp/d6_dhcpc.c @@ -251,7 +251,7 @@ static void d6_run_script(struct d6_packet *packet, const char *name) envp = fill_envp(packet); /* call script */ - log1("Executing %s %s", client_config.script, name); + log1("executing %s %s", client_config.script, name); argv[0] = (char*) client_config.script; argv[1] = (char*) name; argv[2] = NULL; @@ -428,7 +428,7 @@ static NOINLINE int send_d6_discover(uint32_t xid, struct in6_addr *requested_ip */ opt_ptr = add_d6_client_options(opt_ptr); - bb_info_msg("Sending discover..."); + bb_error_msg("sending %s", "discover"); return d6_mcast_from_client_config_ifindex(&packet, opt_ptr); } @@ -481,7 +481,7 @@ static NOINLINE int send_d6_select(uint32_t xid) */ opt_ptr = add_d6_client_options(opt_ptr); - bb_info_msg("Sending select..."); + bb_error_msg("sending %s", "select"); return d6_mcast_from_client_config_ifindex(&packet, opt_ptr); } @@ -550,7 +550,7 @@ static NOINLINE int send_d6_renew(uint32_t xid, struct in6_addr *server_ipv6, st */ opt_ptr = add_d6_client_options(opt_ptr); - bb_info_msg("Sending renew..."); + bb_error_msg("sending %s", "renew"); if (server_ipv6) return d6_send_kernel_packet( &packet, (opt_ptr - (uint8_t*) &packet), @@ -573,7 +573,7 @@ static int send_d6_release(struct in6_addr *server_ipv6, struct in6_addr *our_cu /* IA NA (contains our current IP) */ opt_ptr = d6_store_blob(opt_ptr, client6_data.ia_na, client6_data.ia_na->len + 2+2); - bb_info_msg("Sending release..."); + bb_error_msg("sending %s", "release"); return d6_send_kernel_packet( &packet, (opt_ptr - (uint8_t*) &packet), our_cur_ipv6, CLIENT_PORT6, @@ -592,19 +592,19 @@ static NOINLINE int d6_recv_raw_packet(struct in6_addr *peer_ipv6 bytes = safe_read(fd, &packet, sizeof(packet)); if (bytes < 0) { - log1("Packet read error, ignoring"); + log1("packet read error, ignoring"); /* NB: possible down interface, etc. Caller should pause. */ return bytes; /* returns -1 */ } if (bytes < (int) (sizeof(packet.ip6) + sizeof(packet.udp))) { - log1("Packet is too short, ignoring"); + log1("packet is too short, ignoring"); return -2; } if (bytes < sizeof(packet.ip6) + ntohs(packet.ip6.ip6_plen)) { /* packet is bigger than sizeof(packet), we did partial read */ - log1("Oversized packet, ignoring"); + log1("oversized packet, ignoring"); return -2; } @@ -618,7 +618,7 @@ static NOINLINE int d6_recv_raw_packet(struct in6_addr *peer_ipv6 /* || bytes > (int) sizeof(packet) - can't happen */ || packet.udp.len != packet.ip6.ip6_plen ) { - log1("Unrelated/bogus packet, ignoring"); + log1("unrelated/bogus packet, ignoring"); return -2; } @@ -630,11 +630,11 @@ static NOINLINE int d6_recv_raw_packet(struct in6_addr *peer_ipv6 // check = packet.udp.check; // packet.udp.check = 0; // if (check && check != inet_cksum((uint16_t *)&packet, bytes)) { -// log1("Packet with bad UDP checksum received, ignoring"); +// log1("packet with bad UDP checksum received, ignoring"); // return -2; // } - log1("Received a packet"); + log1("received %s", "a packet"); d6_dump_packet(&packet.data); bytes -= sizeof(packet.ip6) + sizeof(packet.udp); @@ -722,10 +722,10 @@ static int d6_raw_socket(int ifindex) }; #endif - log1("Opening raw socket on ifindex %d", ifindex); //log2? + log1("opening raw socket on ifindex %d", ifindex); //log2? fd = xsocket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IPV6)); - log1("Got raw socket fd %d", fd); //log2? + log1("got raw socket fd %d", fd); //log2? sock.sll_family = AF_PACKET; sock.sll_protocol = htons(ETH_P_IPV6); @@ -738,18 +738,18 @@ static int d6_raw_socket(int ifindex) /* Ignoring error (kernel may lack support for this) */ if (setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, &filter_prog, sizeof(filter_prog)) >= 0) - log1("Attached filter to raw socket fd %d", fd); // log? + log1("attached filter to raw socket fd %d", fd); // log? } #endif - log1("Created raw socket"); + log1("created raw socket"); return fd; } static void change_listen_mode(int new_mode) { - log1("Entering listen mode: %s", + log1("entering listen mode: %s", new_mode != LISTEN_NONE ? (new_mode == LISTEN_KERNEL ? "kernel" : "raw") : "none" @@ -770,7 +770,7 @@ static void change_listen_mode(int new_mode) /* Called only on SIGUSR1 */ static void perform_renew(void) { - bb_info_msg("Performing a DHCP renew"); + bb_error_msg("performing DHCP renew"); switch (state) { case BOUND: change_listen_mode(LISTEN_KERNEL); @@ -794,11 +794,11 @@ static void perform_d6_release(struct in6_addr *server_ipv6, struct in6_addr *ou { /* send release packet */ if (state == BOUND || state == RENEWING || state == REBINDING) { - bb_info_msg("Unicasting a release"); + bb_error_msg("unicasting a release"); send_d6_release(server_ipv6, our_cur_ipv6); /* unicast */ d6_run_script(NULL, "deconfig"); } - bb_info_msg("Entering released state"); + bb_error_msg("entering released state"); change_listen_mode(LISTEN_NONE); state = RELEASED; @@ -1034,7 +1034,7 @@ int udhcpc6_main(int argc UNUSED_PARAM, char **argv) /* Create pidfile */ write_pidfile(client_config.pidfile); /* Goes to stdout (unless NOMMU) and possibly syslog */ - bb_info_msg("%s (v"BB_VER") started", applet_name); + bb_error_msg("started, v"BB_VER); /* Set up the signal pipe */ udhcp_sp_setup(); /* We want random_xid to be random... */ @@ -1074,7 +1074,7 @@ int udhcpc6_main(int argc UNUSED_PARAM, char **argv) retval = 0; /* If we already timed out, fall through with retval = 0, else... */ if ((int)tv.tv_sec > 0) { - log1("Waiting on select %u seconds", (int)tv.tv_sec); + log1("waiting on select %u seconds", (int)tv.tv_sec); timestamp_before_wait = (unsigned)monotonic_sec(); retval = select(max_fd + 1, &rfds, NULL, NULL, &tv); if (retval < 0) { @@ -1124,14 +1124,14 @@ int udhcpc6_main(int argc UNUSED_PARAM, char **argv) d6_run_script(NULL, "leasefail"); #if BB_MMU /* -b is not supported on NOMMU */ if (opt & OPT_b) { /* background if no lease */ - bb_info_msg("No lease, forking to background"); + bb_error_msg("no lease, forking to background"); client_background(); /* do not background again! */ opt = ((opt & ~OPT_b) | OPT_f); } else #endif if (opt & OPT_n) { /* abort if no lease */ - bb_info_msg("No lease, failing"); + bb_error_msg("no lease, failing"); retval = 1; goto ret; } @@ -1159,7 +1159,7 @@ int udhcpc6_main(int argc UNUSED_PARAM, char **argv) state = RENEWING; client_config.first_secs = 0; /* make secs field count from 0 */ change_listen_mode(LISTEN_KERNEL); - log1("Entering renew state"); + log1("entering renew state"); /* fall right through */ case RENEW_REQUESTED: /* manual (SIGUSR1) renew */ case_RENEW_REQUESTED: @@ -1179,7 +1179,7 @@ int udhcpc6_main(int argc UNUSED_PARAM, char **argv) continue; } /* Timed out, enter rebinding state */ - log1("Entering rebinding state"); + log1("entering rebinding state"); state = REBINDING; /* fall right through */ case REBINDING: @@ -1194,7 +1194,7 @@ int udhcpc6_main(int argc UNUSED_PARAM, char **argv) continue; } /* Timed out, enter init state */ - bb_info_msg("Lease lost, entering init state"); + bb_error_msg("lease lost, entering init state"); d6_run_script(NULL, "deconfig"); state = INIT_SELECTING; client_config.first_secs = 0; /* make secs field count from 0 */ @@ -1242,7 +1242,7 @@ int udhcpc6_main(int argc UNUSED_PARAM, char **argv) timeout = INT_MAX; continue; case SIGTERM: - bb_info_msg("Received SIGTERM"); + bb_error_msg("received %s", "SIGTERM"); goto ret0; } @@ -1260,7 +1260,7 @@ int udhcpc6_main(int argc UNUSED_PARAM, char **argv) len = d6_recv_raw_packet(&srv6_buf, &packet, sockfd); if (len == -1) { /* Error is severe, reopen socket */ - bb_info_msg("Read error: %s, reopening socket", strerror(errno)); + bb_error_msg("read error: %s, reopening socket", strerror(errno)); sleep(discover_timeout); /* 3 seconds by default */ change_listen_mode(listen_mode); /* just close and reopen */ } @@ -1298,7 +1298,7 @@ int udhcpc6_main(int argc UNUSED_PARAM, char **argv) option = d6_find_option(packet.d6_options, packet_end, D6_OPT_STATUS_CODE); if (option && option->data[4] != 0) { /* return to init state */ - bb_info_msg("Received DHCP NAK (%u)", option->data[4]); + bb_error_msg("received DHCP NAK (%u)", option->data[4]); d6_run_script(&packet, "nak"); if (state != REQUESTING) d6_run_script(NULL, "deconfig"); @@ -1453,7 +1453,7 @@ int udhcpc6_main(int argc UNUSED_PARAM, char **argv) lease_seconds = 0x0fffffff; /* enter bound state */ timeout = lease_seconds / 2; - bb_info_msg("Lease obtained, lease time %u", + bb_error_msg("lease obtained, lease time %u", /*inet_ntoa(temp_addr),*/ (unsigned)lease_seconds); d6_run_script(&packet, state == REQUESTING ? "bound" : "renew"); diff --git a/networking/udhcp/d6_packet.c b/networking/udhcp/d6_packet.c index b340b5db6..e166f520d 100644 --- a/networking/udhcp/d6_packet.c +++ b/networking/udhcp/d6_packet.c @@ -17,8 +17,8 @@ void FAST_FUNC d6_dump_packet(struct d6_packet *packet) if (dhcp_verbose < 2) return; - bb_info_msg( - " xid %x" + bb_error_msg( + "xid %x" , packet->d6_xid32 ); //*bin2hex(buf, (void *) packet->chaddr, sizeof(packet->chaddr)) = '\0'; @@ -35,15 +35,15 @@ int FAST_FUNC d6_recv_kernel_packet(struct in6_addr *peer_ipv6 memset(packet, 0, sizeof(*packet)); bytes = safe_read(fd, packet, sizeof(*packet)); if (bytes < 0) { - log1("Packet read error, ignoring"); + log1("packet read error, ignoring"); return bytes; /* returns -1 */ } if (bytes < offsetof(struct d6_packet, d6_options)) { - bb_info_msg("Packet with bad magic, ignoring"); + bb_error_msg("packet with bad magic, ignoring"); return -2; } - log1("Received a packet"); + log1("received %s", "a packet"); d6_dump_packet(packet); return bytes; diff --git a/networking/udhcp/d6_socket.c b/networking/udhcp/d6_socket.c index 56f69f6a1..910f296a3 100644 --- a/networking/udhcp/d6_socket.c +++ b/networking/udhcp/d6_socket.c @@ -13,7 +13,7 @@ int FAST_FUNC d6_listen_socket(int port, const char *inf) int fd; struct sockaddr_in6 addr; - log1("Opening listen socket on *:%d %s", port, inf); + log1("opening listen socket on *:%d %s", port, inf); fd = xsocket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP); setsockopt_reuseaddr(fd); diff --git a/networking/udhcp/dhcpc.c b/networking/udhcp/dhcpc.c index dfd5ca606..660b943ce 100644 --- a/networking/udhcp/dhcpc.c +++ b/networking/udhcp/dhcpc.c @@ -561,7 +561,7 @@ static void udhcp_run_script(struct dhcp_packet *packet, const char *name) envp = fill_envp(packet); /* call script */ - log1("Executing %s %s", client_config.script, name); + log1("executing %s %s", client_config.script, name); argv[0] = (char*) client_config.script; argv[1] = (char*) name; argv[2] = NULL; @@ -714,7 +714,7 @@ static NOINLINE int send_discover(uint32_t xid, uint32_t requested) */ add_client_options(&packet); - bb_info_msg("Sending discover..."); + bb_error_msg("sending %s", "discover"); return raw_bcast_from_client_config_ifindex(&packet, INADDR_ANY); } @@ -758,7 +758,7 @@ static NOINLINE int send_select(uint32_t xid, uint32_t server, uint32_t requeste add_client_options(&packet); addr.s_addr = requested; - bb_info_msg("Sending select for %s...", inet_ntoa(addr)); + bb_error_msg("sending select for %s", inet_ntoa(addr)); return raw_bcast_from_client_config_ifindex(&packet, INADDR_ANY); } @@ -797,7 +797,7 @@ static NOINLINE int send_renew(uint32_t xid, uint32_t server, uint32_t ciaddr) */ add_client_options(&packet); - bb_info_msg("Sending renew..."); + bb_error_msg("sending %s", "renew"); return bcast_or_ucast(&packet, ciaddr, server); } @@ -826,7 +826,7 @@ static NOINLINE int send_decline(/*uint32_t xid,*/ uint32_t server, uint32_t req udhcp_add_simple_option(&packet, DHCP_SERVER_ID, server); - bb_info_msg("Sending decline..."); + bb_error_msg("sending %s", "decline"); return raw_bcast_from_client_config_ifindex(&packet, INADDR_ANY); } #endif @@ -846,7 +846,7 @@ static int send_release(uint32_t server, uint32_t ciaddr) udhcp_add_simple_option(&packet, DHCP_SERVER_ID, server); - bb_info_msg("Sending release..."); + bb_error_msg("sending %s", "release"); /* Note: normally we unicast here since "server" is not zero. * However, there _are_ people who run "address-less" DHCP servers, * and reportedly ISC dhcp client and Windows allow that. @@ -881,7 +881,7 @@ static NOINLINE int udhcp_recv_raw_packet(struct dhcp_packet *dhcp_pkt, int fd) if (bytes < 0) { if (errno == EINTR) continue; - log1("Packet read error, ignoring"); + log1("packet read error, ignoring"); /* NB: possible down interface, etc. Caller should pause. */ return bytes; /* returns -1 */ } @@ -889,13 +889,13 @@ static NOINLINE int udhcp_recv_raw_packet(struct dhcp_packet *dhcp_pkt, int fd) } if (bytes < (int) (sizeof(packet.ip) + sizeof(packet.udp))) { - log1("Packet is too short, ignoring"); + log1("packet is too short, ignoring"); return -2; } if (bytes < ntohs(packet.ip.tot_len)) { /* packet is bigger than sizeof(packet), we did partial read */ - log1("Oversized packet, ignoring"); + log1("oversized packet, ignoring"); return -2; } @@ -910,7 +910,7 @@ static NOINLINE int udhcp_recv_raw_packet(struct dhcp_packet *dhcp_pkt, int fd) /* || bytes > (int) sizeof(packet) - can't happen */ || ntohs(packet.udp.len) != (uint16_t)(bytes - sizeof(packet.ip)) ) { - log1("Unrelated/bogus packet, ignoring"); + log1("unrelated/bogus packet, ignoring"); return -2; } @@ -918,7 +918,7 @@ static NOINLINE int udhcp_recv_raw_packet(struct dhcp_packet *dhcp_pkt, int fd) check = packet.ip.check; packet.ip.check = 0; if (check != inet_cksum((uint16_t *)&packet.ip, sizeof(packet.ip))) { - log1("Bad IP header checksum, ignoring"); + log1("bad IP header checksum, ignoring"); return -2; } @@ -943,17 +943,17 @@ static NOINLINE int udhcp_recv_raw_packet(struct dhcp_packet *dhcp_pkt, int fd) check = packet.udp.check; packet.udp.check = 0; if (check && check != inet_cksum((uint16_t *)&packet, bytes)) { - log1("Packet with bad UDP checksum received, ignoring"); + log1("packet with bad UDP checksum received, ignoring"); return -2; } skip_udp_sum_check: if (packet.data.cookie != htonl(DHCP_MAGIC)) { - bb_info_msg("Packet with bad magic, ignoring"); + bb_error_msg("packet with bad magic, ignoring"); return -2; } - log1("Received a packet"); + log1("received %s", "a packet"); udhcp_dump_packet(&packet.data); bytes -= sizeof(packet.ip) + sizeof(packet.udp); @@ -992,14 +992,14 @@ static int udhcp_raw_socket(int ifindex) int fd; struct sockaddr_ll sock; - log1("Opening raw socket on ifindex %d", ifindex); //log2? + log1("opening raw socket on ifindex %d", ifindex); //log2? fd = xsocket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP)); /* ^^^^^ * SOCK_DGRAM: remove link-layer headers on input (SOCK_RAW keeps them) * ETH_P_IP: want to receive only packets with IPv4 eth type */ - log1("Got raw socket fd"); //log2? + log1("got raw socket fd"); //log2? sock.sll_family = AF_PACKET; sock.sll_protocol = htons(ETH_P_IP); @@ -1055,23 +1055,23 @@ static int udhcp_raw_socket(int ifindex) /* Ignoring error (kernel may lack support for this) */ if (setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, &filter_prog, sizeof(filter_prog)) >= 0) - log1("Attached filter to raw socket fd"); // log? + log1("attached filter to raw socket fd"); // log? } #endif if (setsockopt_1(fd, SOL_PACKET, PACKET_AUXDATA) != 0) { if (errno != ENOPROTOOPT) - log1("Can't set PACKET_AUXDATA on raw socket"); + log1("can't set PACKET_AUXDATA on raw socket"); } - log1("Created raw socket"); + log1("created raw socket"); return fd; } static void change_listen_mode(int new_mode) { - log1("Entering listen mode: %s", + log1("entering listen mode: %s", new_mode != LISTEN_NONE ? (new_mode == LISTEN_KERNEL ? "kernel" : "raw") : "none" @@ -1092,7 +1092,7 @@ static void change_listen_mode(int new_mode) /* Called only on SIGUSR1 */ static void perform_renew(void) { - bb_info_msg("Performing a DHCP renew"); + bb_error_msg("performing DHCP renew"); switch (state) { case BOUND: change_listen_mode(LISTEN_KERNEL); @@ -1122,12 +1122,12 @@ static void perform_release(uint32_t server_addr, uint32_t requested_ip) temp_addr.s_addr = server_addr; strcpy(buffer, inet_ntoa(temp_addr)); temp_addr.s_addr = requested_ip; - bb_info_msg("Unicasting a release of %s to %s", + bb_error_msg("unicasting a release of %s to %s", inet_ntoa(temp_addr), buffer); send_release(server_addr, requested_ip); /* unicast */ udhcp_run_script(NULL, "deconfig"); } - bb_info_msg("Entering released state"); + bb_error_msg("entering released state"); change_listen_mode(LISTEN_NONE); state = RELEASED; @@ -1395,7 +1395,7 @@ int udhcpc_main(int argc UNUSED_PARAM, char **argv) /* Create pidfile */ write_pidfile(client_config.pidfile); /* Goes to stdout (unless NOMMU) and possibly syslog */ - bb_info_msg("%s (v"BB_VER") started", applet_name); + bb_error_msg("started, v"BB_VER); /* Set up the signal pipe */ udhcp_sp_setup(); /* We want random_xid to be random... */ @@ -1434,7 +1434,7 @@ int udhcpc_main(int argc UNUSED_PARAM, char **argv) retval = 0; /* If we already timed out, fall through with retval = 0, else... */ if ((int)tv.tv_sec > 0) { - log1("Waiting on select %u seconds", (int)tv.tv_sec); + log1("waiting on select %u seconds", (int)tv.tv_sec); timestamp_before_wait = (unsigned)monotonic_sec(); retval = select(max_fd + 1, &rfds, NULL, NULL, &tv); if (retval < 0) { @@ -1485,14 +1485,14 @@ int udhcpc_main(int argc UNUSED_PARAM, char **argv) udhcp_run_script(NULL, "leasefail"); #if BB_MMU /* -b is not supported on NOMMU */ if (opt & OPT_b) { /* background if no lease */ - bb_info_msg("No lease, forking to background"); + bb_error_msg("no lease, forking to background"); client_background(); /* do not background again! */ opt = ((opt & ~OPT_b) | OPT_f); } else #endif if (opt & OPT_n) { /* abort if no lease */ - bb_info_msg("No lease, failing"); + bb_error_msg("no lease, failing"); retval = 1; goto ret; } @@ -1520,7 +1520,7 @@ int udhcpc_main(int argc UNUSED_PARAM, char **argv) state = RENEWING; client_config.first_secs = 0; /* make secs field count from 0 */ change_listen_mode(LISTEN_KERNEL); - log1("Entering renew state"); + log1("entering renew state"); /* fall right through */ case RENEW_REQUESTED: /* manual (SIGUSR1) renew */ case_RENEW_REQUESTED: @@ -1540,7 +1540,7 @@ int udhcpc_main(int argc UNUSED_PARAM, char **argv) continue; } /* Timed out, enter rebinding state */ - log1("Entering rebinding state"); + log1("entering rebinding state"); state = REBINDING; /* fall right through */ case REBINDING: @@ -1555,7 +1555,7 @@ int udhcpc_main(int argc UNUSED_PARAM, char **argv) continue; } /* Timed out, enter init state */ - bb_info_msg("Lease lost, entering init state"); + bb_error_msg("lease lost, entering init state"); udhcp_run_script(NULL, "deconfig"); state = INIT_SELECTING; client_config.first_secs = 0; /* make secs field count from 0 */ @@ -1603,7 +1603,7 @@ int udhcpc_main(int argc UNUSED_PARAM, char **argv) timeout = INT_MAX; continue; case SIGTERM: - bb_info_msg("Received SIGTERM"); + bb_error_msg("received %s", "SIGTERM"); goto ret0; } @@ -1621,7 +1621,7 @@ int udhcpc_main(int argc UNUSED_PARAM, char **argv) len = udhcp_recv_raw_packet(&packet, sockfd); if (len == -1) { /* Error is severe, reopen socket */ - bb_info_msg("Read error: %s, reopening socket", strerror(errno)); + bb_error_msg("read error: %s, reopening socket", strerror(errno)); sleep(discover_timeout); /* 3 seconds by default */ change_listen_mode(listen_mode); /* just close and reopen */ } @@ -1744,7 +1744,7 @@ int udhcpc_main(int argc UNUSED_PARAM, char **argv) client_config.interface, arpping_ms) ) { - bb_info_msg("Offered address is in use " + bb_error_msg("offered address is in use " "(got ARP reply), declining"); send_decline(/*xid,*/ server_addr, packet.yiaddr); @@ -1763,7 +1763,7 @@ int udhcpc_main(int argc UNUSED_PARAM, char **argv) #endif /* enter bound state */ temp_addr.s_addr = packet.yiaddr; - bb_info_msg("Lease of %s obtained, lease time %u", + bb_error_msg("lease of %s obtained, lease time %u", inet_ntoa(temp_addr), (unsigned)lease_seconds); requested_ip = packet.yiaddr; @@ -1817,7 +1817,7 @@ int udhcpc_main(int argc UNUSED_PARAM, char **argv) goto non_matching_svid; } /* return to init state */ - bb_info_msg("Received DHCP NAK"); + bb_error_msg("received %s", "DHCP NAK"); udhcp_run_script(&packet, "nak"); if (state != REQUESTING) udhcp_run_script(NULL, "deconfig"); diff --git a/networking/udhcp/dhcpd.c b/networking/udhcp/dhcpd.c index 2de074f9b..79677ee93 100644 --- a/networking/udhcp/dhcpd.c +++ b/networking/udhcp/dhcpd.c @@ -61,11 +61,11 @@ static void send_packet_to_client(struct dhcp_packet *dhcp_pkt, int force_broadc || (dhcp_pkt->flags & htons(BROADCAST_FLAG)) || dhcp_pkt->ciaddr == 0 ) { - log1("Broadcasting packet to client"); + log1("broadcasting packet to client"); ciaddr = INADDR_BROADCAST; chaddr = MAC_BCAST_ADDR; } else { - log1("Unicasting packet to client ciaddr"); + log1("unicasting packet to client ciaddr"); ciaddr = dhcp_pkt->ciaddr; chaddr = dhcp_pkt->chaddr; } @@ -79,7 +79,7 @@ static void send_packet_to_client(struct dhcp_packet *dhcp_pkt, int force_broadc /* Send a packet to gateway_nip using the kernel ip stack */ static void send_packet_to_relay(struct dhcp_packet *dhcp_pkt) { - log1("Forwarding packet to relay"); + log1("forwarding packet to relay"); udhcp_send_kernel_packet(dhcp_pkt, server_config.server_nip, SERVER_PORT, @@ -214,7 +214,7 @@ static NOINLINE void send_offer(struct dhcp_packet *oldpacket, add_server_options(&packet); addr.s_addr = packet.yiaddr; - bb_info_msg("Sending OFFER of %s", inet_ntoa(addr)); + bb_error_msg("sending OFFER of %s", inet_ntoa(addr)); /* send_packet emits error message itself if it detects failure */ send_packet(&packet, /*force_bcast:*/ 0); } @@ -226,7 +226,7 @@ static NOINLINE void send_NAK(struct dhcp_packet *oldpacket) init_packet(&packet, oldpacket, DHCPNAK); - log1("Sending NAK"); + log1("sending NAK"); send_packet(&packet, /*force_bcast:*/ 1); } @@ -247,7 +247,7 @@ static NOINLINE void send_ACK(struct dhcp_packet *oldpacket, uint32_t yiaddr) add_server_options(&packet); addr.s_addr = yiaddr; - bb_info_msg("Sending ACK to %s", inet_ntoa(addr)); + bb_error_msg("sending ACK to %s", inet_ntoa(addr)); send_packet(&packet, /*force_bcast:*/ 0); p_host_name = (const char*) udhcp_get_option(oldpacket, DHCP_HOST_NAME); @@ -361,7 +361,7 @@ int udhcpd_main(int argc UNUSED_PARAM, char **argv) write_pidfile(server_config.pidfile); /* if (!..) bb_perror_msg("can't create pidfile %s", pidfile); */ - bb_info_msg("%s (v"BB_VER") started", applet_name); + bb_error_msg("started, v"BB_VER); option = udhcp_find_option(server_config.options, DHCP_LEASE_TIME); server_config.max_lease_sec = DEFAULT_LEASE_TIME; @@ -427,18 +427,18 @@ int udhcpd_main(int argc UNUSED_PARAM, char **argv) goto continue_with_autotime; } if (retval < 0 && errno != EINTR) { - log1("Error on select"); + log1("error on select"); continue; } switch (udhcp_sp_read(&rfds)) { case SIGUSR1: - bb_info_msg("Received SIGUSR1"); + bb_error_msg("received %s", "SIGUSR1"); write_leases(); /* why not just reset the timeout, eh */ goto continue_with_autotime; case SIGTERM: - bb_info_msg("Received SIGTERM"); + bb_error_msg("received %s", "SIGTERM"); write_leases(); goto ret0; case 0: /* no signal: read a packet */ @@ -451,7 +451,7 @@ int udhcpd_main(int argc UNUSED_PARAM, char **argv) if (bytes < 0) { /* bytes can also be -2 ("bad packet data") */ if (bytes == -1 && errno != EINTR) { - log1("Read error: %s, reopening socket", strerror(errno)); + log1("read error: %s, reopening socket", strerror(errno)); close(server_socket); server_socket = -1; } @@ -486,7 +486,7 @@ int udhcpd_main(int argc UNUSED_PARAM, char **argv) /* Look for a static/dynamic lease */ static_lease_nip = get_static_nip_by_mac(server_config.static_leases, &packet.chaddr); if (static_lease_nip) { - bb_info_msg("Found static lease: %x", static_lease_nip); + bb_error_msg("found static lease: %x", static_lease_nip); memcpy(&fake_lease.lease_mac, &packet.chaddr, 6); fake_lease.lease_nip = static_lease_nip; fake_lease.expires = 0; @@ -504,13 +504,13 @@ int udhcpd_main(int argc UNUSED_PARAM, char **argv) switch (state[0]) { case DHCPDISCOVER: - log1("Received DISCOVER"); + log1("received %s", "DISCOVER"); send_offer(&packet, static_lease_nip, lease, requested_ip_opt, arpping_ms); break; case DHCPREQUEST: - log1("Received REQUEST"); + log1("received %s", "REQUEST"); /* RFC 2131: o DHCPREQUEST generated during SELECTING state: @@ -635,7 +635,7 @@ o DHCPREQUEST generated during REBINDING state: * chaddr must be filled in, * ciaddr must be 0 (we do not check this) */ - log1("Received DECLINE"); + log1("received %s", "DECLINE"); if (server_id_opt && requested_ip_opt && lease /* chaddr matches this lease */ @@ -655,7 +655,7 @@ o DHCPREQUEST generated during REBINDING state: * chaddr must be filled in, * ciaddr must be filled in */ - log1("Received RELEASE"); + log1("received %s", "RELEASE"); if (server_id_opt && lease /* chaddr matches this lease */ && packet.ciaddr == lease->lease_nip @@ -665,7 +665,7 @@ o DHCPREQUEST generated during REBINDING state: break; case DHCPINFORM: - log1("Received INFORM"); + log1("received %s", "INFORM"); send_inform(&packet); break; } diff --git a/networking/udhcp/files.c b/networking/udhcp/files.c index 5b90e26d2..7b57c6258 100644 --- a/networking/udhcp/files.c +++ b/networking/udhcp/files.c @@ -226,7 +226,7 @@ void FAST_FUNC read_leases(const char *file) #endif } } - log1("Read %d leases", i); + log1("read %d leases", i); ret: close(fd); } diff --git a/networking/udhcp/leases.c b/networking/udhcp/leases.c index 411b74962..6642e396d 100644 --- a/networking/udhcp/leases.c +++ b/networking/udhcp/leases.c @@ -133,7 +133,7 @@ static int nobody_responds_to_arp(uint32_t nip, const uint8_t *safe_mac, unsigne return r; temp.s_addr = nip; - bb_info_msg("%s belongs to someone, reserving it for %u seconds", + bb_error_msg("%s belongs to someone, reserving it for %u seconds", inet_ntoa(temp), (unsigned)server_config.conflict_time); add_lease(NULL, nip, server_config.conflict_time, NULL, 0); return 0; diff --git a/networking/udhcp/packet.c b/networking/udhcp/packet.c index 148f52551..0a31f2643 100644 --- a/networking/udhcp/packet.c +++ b/networking/udhcp/packet.c @@ -38,8 +38,8 @@ void FAST_FUNC udhcp_dump_packet(struct dhcp_packet *packet) if (dhcp_verbose < 2) return; - bb_info_msg( - //" op %x" + bb_error_msg( + //"op %x" //" htype %x" " hlen %x" //" hops %x" @@ -73,7 +73,7 @@ void FAST_FUNC udhcp_dump_packet(struct dhcp_packet *packet) //, packet->options[] ); *bin2hex(buf, (void *) packet->chaddr, sizeof(packet->chaddr)) = '\0'; - bb_info_msg(" chaddr %s", buf); + bb_error_msg("chaddr %s", buf); } #endif @@ -85,17 +85,17 @@ int FAST_FUNC udhcp_recv_kernel_packet(struct dhcp_packet *packet, int fd) memset(packet, 0, sizeof(*packet)); bytes = safe_read(fd, packet, sizeof(*packet)); if (bytes < 0) { - log1("Packet read error, ignoring"); + log1("packet read error, ignoring"); return bytes; /* returns -1 */ } if (bytes < offsetof(struct dhcp_packet, options) || packet->cookie != htonl(DHCP_MAGIC) ) { - bb_info_msg("Packet with bad magic, ignoring"); + bb_error_msg("packet with bad magic, ignoring"); return -2; } - log1("Received a packet"); + log1("received %s", "a packet"); udhcp_dump_packet(packet); return bytes; diff --git a/networking/udhcp/socket.c b/networking/udhcp/socket.c index a42106960..4fd79f423 100644 --- a/networking/udhcp/socket.c +++ b/networking/udhcp/socket.c @@ -56,7 +56,7 @@ int FAST_FUNC udhcp_read_interface(const char *interface, int *ifindex, uint32_t close(fd); return -1; } - log1("Adapter index %d", ifr->ifr_ifindex); + log1("adapter index %d", ifr->ifr_ifindex); *ifindex = ifr->ifr_ifindex; } @@ -82,7 +82,7 @@ int FAST_FUNC udhcp_listen_socket(/*uint32_t ip,*/ int port, const char *inf) struct sockaddr_in addr; char *colon; - log1("Opening listen socket on *:%d %s", port, inf); + log1("opening listen socket on *:%d %s", port, inf); fd = xsocket(PF_INET, SOCK_DGRAM, IPPROTO_UDP); setsockopt_reuseaddr(fd); diff --git a/networking/udhcp/static_leases.c b/networking/udhcp/static_leases.c index f4a24ab62..b7f9e5c59 100644 --- a/networking/udhcp/static_leases.c +++ b/networking/udhcp/static_leases.c @@ -66,7 +66,7 @@ void FAST_FUNC log_static_leases(struct static_lease **st_lease_pp) cur = *st_lease_pp; while (cur) { - bb_info_msg("static lease: mac:%02x:%02x:%02x:%02x:%02x:%02x nip:%x", + bb_error_msg("static lease: mac:%02x:%02x:%02x:%02x:%02x:%02x nip:%x", cur->mac[0], cur->mac[1], cur->mac[2], cur->mac[3], cur->mac[4], cur->mac[5], cur->nip -- cgit v1.2.3-55-g6feb From c4199f22d0f7793b70db51c01783f0d45afce3d4 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Fri, 1 Apr 2016 22:12:44 +0200 Subject: libbb: two new functions: wait_for_exitstatus(pid), xfchdir(fd) Bartosz Golaszewski proposed xfchdir() Signed-off-by: Denys Vlasenko --- archival/libarchive/data_extract_to_command.c | 3 +-- include/libbb.h | 2 ++ libbb/xfuncs.c | 12 ++++++++++++ libbb/xfuncs_printf.c | 6 ++++++ runit/chpst.c | 3 +-- util-linux/unshare.c | 11 ----------- 6 files changed, 22 insertions(+), 15 deletions(-) (limited to 'libbb') diff --git a/archival/libarchive/data_extract_to_command.c b/archival/libarchive/data_extract_to_command.c index 6f5317a0e..5d8769382 100644 --- a/archival/libarchive/data_extract_to_command.c +++ b/archival/libarchive/data_extract_to_command.c @@ -112,8 +112,7 @@ void FAST_FUNC data_extract_to_command(archive_handle_t *archive_handle) bb_copyfd_exact_size(archive_handle->src_fd, p[1], -file_header->size); close(p[1]); - if (safe_waitpid(pid, &status, 0) == -1) - bb_perror_msg_and_die("waitpid"); + status = wait_for_exitstatus(pid); if (WIFEXITED(status) && WEXITSTATUS(status)) bb_error_msg_and_die("'%s' returned status %d", archive_handle->tar__to_command, WEXITSTATUS(status)); diff --git a/include/libbb.h b/include/libbb.h index 98d788402..5b4280e34 100644 --- a/include/libbb.h +++ b/include/libbb.h @@ -500,6 +500,7 @@ void xsetuid(uid_t uid) FAST_FUNC; void xsetegid(gid_t egid) FAST_FUNC; void xseteuid(uid_t euid) FAST_FUNC; void xchdir(const char *path) FAST_FUNC; +void xfchdir(int fd) FAST_FUNC; void xchroot(const char *path) FAST_FUNC; void xsetenv(const char *key, const char *value) FAST_FUNC; void bb_unsetenv(const char *key) FAST_FUNC; @@ -1021,6 +1022,7 @@ pid_t wait_any_nohang(int *wstat) FAST_FUNC; * if (rc > 0) bb_error_msg("exit code: %d", rc & 0xff); */ int wait4pid(pid_t pid) FAST_FUNC; +int wait_for_exitstatus(pid_t pid) FAST_FUNC; /* Same as wait4pid(spawn(argv)), but with NOFORK/NOEXEC if configured: */ int spawn_and_wait(char **argv) FAST_FUNC; /* Does NOT check that applet is NOFORK, just blindly runs it */ diff --git a/libbb/xfuncs.c b/libbb/xfuncs.c index 206edb4a0..3f9a84ad4 100644 --- a/libbb/xfuncs.c +++ b/libbb/xfuncs.c @@ -315,3 +315,15 @@ int FAST_FUNC wait4pid(pid_t pid) return WTERMSIG(status) + 0x180; return 0; } + +// Useful when we do know that pid is valid, and we just want to wait +// for it to exit. Not existing pid is fatal. waitpid() status is not returned. +int FAST_FUNC wait_for_exitstatus(pid_t pid) +{ + int exit_status, n; + + n = safe_waitpid(pid, &exit_status, 0); + if (n < 0) + bb_perror_msg_and_die("waitpid"); + return exit_status; +} diff --git a/libbb/xfuncs_printf.c b/libbb/xfuncs_printf.c index 73488908d..4aa1b5ce2 100644 --- a/libbb/xfuncs_printf.c +++ b/libbb/xfuncs_printf.c @@ -390,6 +390,12 @@ void FAST_FUNC xchdir(const char *path) bb_perror_msg_and_die("can't change directory to '%s'", path); } +void FAST_FUNC xfchdir(int fd) +{ + if (fchdir(fd)) + bb_perror_msg_and_die("fchdir"); +} + void FAST_FUNC xchroot(const char *path) { if (chroot(path)) diff --git a/runit/chpst.c b/runit/chpst.c index 301cdd08a..7fe5151db 100644 --- a/runit/chpst.c +++ b/runit/chpst.c @@ -255,8 +255,7 @@ static NOINLINE void edir(const char *directory_name) xsetenv(d->d_name, buf); } closedir(dir); - if (fchdir(wdir) == -1) - bb_perror_msg_and_die("fchdir"); + xfchdir(wdir); close(wdir); } diff --git a/util-linux/unshare.c b/util-linux/unshare.c index f1a9cdf19..b8cd4676a 100644 --- a/util-linux/unshare.c +++ b/util-linux/unshare.c @@ -57,17 +57,6 @@ static void mount_or_die(const char *source, const char *target, } } -// TODO: move to libbb -static int wait_for_exitstatus(pid_t pid) -{ - int exit_status, n; - - n = safe_waitpid(pid, &exit_status, 0); - if (n < 0) - bb_perror_msg_and_die("waitpid"); - return exit_status; -} - /* * Longest possible path to a procfs file used in unshare. Must be able to * contain the '/proc/' string, the '/ns/user' string which is the longest -- cgit v1.2.3-55-g6feb From 8220399173cf8d25e37059cadac96ac30f94e82a Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Sat, 2 Apr 2016 18:06:24 +0200 Subject: nsenter,unshare: share common code; fix a bug of not closing all fds function old new delta xvfork_parent_waits_and_exits - 64 +64 exec_prog_or_SHELL - 39 +39 unshare_main 873 810 -63 nsenter_main 663 596 -67 ------------------------------------------------------------------------------ (add/remove: 2/0 grow/shrink: 0/2 up/down: 106/-130) Total: -27 bytes Signed-off-by: Denys Vlasenko --- include/libbb.h | 6 ++++-- libbb/executable.c | 11 ++++++++++- libbb/xfuncs_printf.c | 16 ++++++++++++++++ util-linux/nsenter.c | 21 +++++---------------- util-linux/unshare.c | 19 +++---------------- 5 files changed, 38 insertions(+), 35 deletions(-) (limited to 'libbb') diff --git a/include/libbb.h b/include/libbb.h index 5b4280e34..64e61cd26 100644 --- a/include/libbb.h +++ b/include/libbb.h @@ -992,9 +992,10 @@ int BB_EXECVP(const char *file, char *const argv[]) FAST_FUNC; #define BB_EXECVP(prog,cmd) execvp(prog,cmd) #define BB_EXECLP(prog,cmd,...) execlp(prog,cmd,__VA_ARGS__) #endif -int BB_EXECVP_or_die(char **argv) NORETURN FAST_FUNC; +void BB_EXECVP_or_die(char **argv) NORETURN FAST_FUNC; +void exec_prog_or_SHELL(char **argv) NORETURN FAST_FUNC; -/* xvfork() can't be a _function_, return after vfork mangles stack +/* xvfork() can't be a _function_, return after vfork in child mangles stack * in the parent. It must be a macro. */ #define xvfork() \ ({ \ @@ -1006,6 +1007,7 @@ int BB_EXECVP_or_die(char **argv) NORETURN FAST_FUNC; #if BB_MMU pid_t xfork(void) FAST_FUNC; #endif +void xvfork_parent_waits_and_exits(void) FAST_FUNC; /* NOMMU friendy fork+exec: */ pid_t spawn(char **argv) FAST_FUNC; diff --git a/libbb/executable.c b/libbb/executable.c index 85ecc3e6c..05e70312f 100644 --- a/libbb/executable.c +++ b/libbb/executable.c @@ -83,10 +83,19 @@ int FAST_FUNC BB_EXECVP(const char *file, char *const argv[]) } #endif -int FAST_FUNC BB_EXECVP_or_die(char **argv) +void FAST_FUNC BB_EXECVP_or_die(char **argv) { BB_EXECVP(argv[0], argv); /* SUSv3-mandated exit codes */ xfunc_error_retval = (errno == ENOENT) ? 127 : 126; bb_perror_msg_and_die("can't execute '%s'", argv[0]); } + +/* Typical idiom for applets which exec *optional* PROG [ARGS] */ +void FAST_FUNC exec_prog_or_SHELL(char **argv) +{ + if (argv[0]) { + BB_EXECVP_or_die(argv); + } + run_shell(getenv("SHELL"), /*login:*/ 1, NULL, NULL); +} diff --git a/libbb/xfuncs_printf.c b/libbb/xfuncs_printf.c index 4aa1b5ce2..e9222f690 100644 --- a/libbb/xfuncs_printf.c +++ b/libbb/xfuncs_printf.c @@ -659,3 +659,19 @@ pid_t FAST_FUNC xfork(void) return pid; } #endif + +void FAST_FUNC xvfork_parent_waits_and_exits(void) +{ + pid_t pid; + + fflush_all(); + pid = xvfork(); + if (pid > 0) { + /* Parent */ + int exit_status = wait_for_exitstatus(pid); + if (WIFSIGNALED(exit_status)) + kill_myself_with_sig(WTERMSIG(exit_status)); + _exit(WEXITSTATUS(exit_status)); + } + /* Child continues */ +} diff --git a/util-linux/nsenter.c b/util-linux/nsenter.c index 9c1dabaa8..0dad595cd 100644 --- a/util-linux/nsenter.c +++ b/util-linux/nsenter.c @@ -230,7 +230,7 @@ int nsenter_main(int argc UNUSED_PARAM, char **argv) ns->ns_nsfile8 + 3 /* skip over "ns/" */ ); } - /*close(ns_ctx->fd);*/ + close(ns_ctx->fd); /* should close fds, to not confuse exec'ed PROG */ /*ns_ctx->fd = -1;*/ } @@ -244,13 +244,13 @@ int nsenter_main(int argc UNUSED_PARAM, char **argv) } xfchdir(root_fd); xchroot("."); - /*close(root_fd);*/ + close(root_fd); /*root_fd = -1;*/ } if (wd_fd >= 0) { xfchdir(wd_fd); - /*close(wd_fd);*/ + close(wd_fd); /*wd_fd = -1;*/ } @@ -259,14 +259,7 @@ int nsenter_main(int argc UNUSED_PARAM, char **argv) * explicitly requested by the user not to. */ if (!(opts & OPT_nofork) && (opts & OPT_pid)) { - pid_t pid = xvfork(); - if (pid > 0) { - /* Parent */ - int exit_status = wait_for_exitstatus(pid); - if (WIFSIGNALED(exit_status)) - kill_myself_with_sig(WTERMSIG(exit_status)); - return WEXITSTATUS(exit_status); - } + xvfork_parent_waits_and_exits(); /* Child continues */ } @@ -278,9 +271,5 @@ int nsenter_main(int argc UNUSED_PARAM, char **argv) if (opts & OPT_setuid) xsetuid(uid); - if (*argv) { - BB_EXECVP_or_die(argv); - } - - run_shell(getenv("SHELL"), /*login:*/ 1, NULL, NULL); + exec_prog_or_SHELL(argv); } diff --git a/util-linux/unshare.c b/util-linux/unshare.c index 2a5bea5a6..95a7cb647 100644 --- a/util-linux/unshare.c +++ b/util-linux/unshare.c @@ -281,7 +281,7 @@ int unshare_main(int argc UNUSED_PARAM, char **argv) if (fdp.wr >= 0) { close(fdp.wr); /* Release child */ - /*close(fdp.rd);*/ + close(fdp.rd); /* should close fd, to not confuse exec'ed PROG */ } if (need_mount) { @@ -307,14 +307,7 @@ int unshare_main(int argc UNUSED_PARAM, char **argv) * that'll become PID 1 in this new namespace. */ if (opts & OPT_fork) { - pid_t pid = xfork(); - if (pid > 0) { - /* Parent */ - int exit_status = wait_for_exitstatus(pid); - if (WIFSIGNALED(exit_status)) - kill_myself_with_sig(WTERMSIG(exit_status)); - return WEXITSTATUS(exit_status); - } + xvfork_parent_waits_and_exits(); /* Child continues */ } @@ -354,11 +347,5 @@ int unshare_main(int argc UNUSED_PARAM, char **argv) mount_or_die("proc", proc_mnt_target, "proc", MS_NOSUID | MS_NOEXEC | MS_NODEV); } - if (argv[0]) { - BB_EXECVP_or_die(argv); - } - /* unshare from util-linux 2.27.1, despite not documenting it, - * runs a login shell (argv0="-sh") if no PROG is given - */ - run_shell(getenv("SHELL"), /*login:*/ 1, NULL, NULL); + exec_prog_or_SHELL(argv); } -- cgit v1.2.3-55-g6feb From a93e4fd376d990ead254657228e75715b74ca0ac Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Sat, 2 Apr 2016 22:54:23 +0200 Subject: find_applet_by_name: add an example of faster linear search code Signed-off-by: Denys Vlasenko --- libbb/appletlib.c | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 77 insertions(+), 3 deletions(-) (limited to 'libbb') diff --git a/libbb/appletlib.c b/libbb/appletlib.c index aeaf238f1..18583f91a 100644 --- a/libbb/appletlib.c +++ b/libbb/appletlib.c @@ -141,10 +141,28 @@ void FAST_FUNC bb_show_usage(void) int FAST_FUNC find_applet_by_name(const char *name) { - unsigned i, max; - int j; + unsigned i, j, max; const char *p; +/* The commented-out word-at-a-time code is ~40% faster, but +160 bytes. + * "Faster" here saves ~0.5 microsecond of real time - not worth it. + */ +#if 0 /*BB_UNALIGNED_MEMACCESS_OK && BB_LITTLE_ENDIAN*/ + uint32_t n32; + + /* Handle all names < 2 chars long early */ + if (name[0] == '\0') + return -1; /* "" is not a valid applet name */ + if (name[1] == '\0') { + if (!ENABLE_TEST) + return -1; /* 1-char name is not valid */ + if (name[0] != ']') + return -1; /* 1-char name which isn't "[" is not valid */ + /* applet "[" is always applet #0: */ + return 0; + } +#endif + p = applet_names; i = 0; #if KNOWN_APPNAME_OFFSETS <= 0 @@ -166,7 +184,62 @@ int FAST_FUNC find_applet_by_name(const char *name) //bb_error_msg("name:'%s' starting from:'%s' i:%u max:%u", name, p, i, max); #endif - /* Open-coding without strcmp/strlen calls for speed */ + /* Open-coded linear seatch without strcmp/strlen calls for speed */ + +#if 0 /*BB_UNALIGNED_MEMACCESS_OK && BB_LITTLE_ENDIAN*/ + /* skip "[\0" name, it's surely not it */ + if (ENABLE_TEST && LONE_CHAR(p, '[')) + i++, p += 2; + /* All remaining applet names in p[] are at least 2 chars long */ + /* name[] is also at least 2 chars long */ + + n32 = (name[0] << 0) | (name[1] << 8) | (name[2] << 16); + while (i < max) { + uint32_t p32; + char ch; + + /* Quickly check match of the first 3 bytes */ + move_from_unaligned32(p32, p); + p += 3; + if ((p32 & 0x00ffffff) != n32) { + /* Most likely case: 3 first bytes do not match */ + i++; + if ((p32 & 0x00ff0000) == '\0') + continue; // p[2] was NUL + p++; + if ((p32 & 0xff000000) == '\0') + continue; // p[3] was NUL + /* p[0..3] aren't matching and none is NUL, check the rest */ + while (*p++ != '\0') + continue; + continue; + } + + /* Unlikely branch: first 3 bytes ([0..2]) match */ + if ((p32 & 0x00ff0000) == '\0') { + /* name is 2-byte long, it is full match */ + //bb_error_msg("found:'%s' i:%u", name, i); + return i; + } + /* Check remaining bytes [3..NUL] */ + ch = (p32 >> 24); + j = 3; + while (ch == name[j]) { + if (ch == '\0') { + //bb_error_msg("found:'%s' i:%u", name, i); + return i; + } + ch = *++p; + j++; + } + /* Not a match. Skip it, including NUL */ + while (ch != '\0') + ch = *++p; + p++; + i++; + } + return -1; +#else while (i < max) { char ch; j = 0; @@ -189,6 +262,7 @@ int FAST_FUNC find_applet_by_name(const char *name) i++; } return -1; +#endif } -- cgit v1.2.3-55-g6feb From 1cf68e303328671f74dfd9f7d24e6c9f91d18969 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Sat, 2 Apr 2016 22:57:17 +0200 Subject: typo fix Signed-off-by: Denys Vlasenko --- libbb/appletlib.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'libbb') diff --git a/libbb/appletlib.c b/libbb/appletlib.c index 18583f91a..4b5b09f45 100644 --- a/libbb/appletlib.c +++ b/libbb/appletlib.c @@ -184,7 +184,7 @@ int FAST_FUNC find_applet_by_name(const char *name) //bb_error_msg("name:'%s' starting from:'%s' i:%u max:%u", name, p, i, max); #endif - /* Open-coded linear seatch without strcmp/strlen calls for speed */ + /* Open-coded linear search without strcmp/strlen calls for speed */ #if 0 /*BB_UNALIGNED_MEMACCESS_OK && BB_LITTLE_ENDIAN*/ /* skip "[\0" name, it's surely not it */ -- cgit v1.2.3-55-g6feb From bc14f4d13d3cf1d43ae809d641e29174662cd1e4 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Sun, 3 Apr 2016 16:06:42 +0200 Subject: main(): add a TODO about finding a use for _end[] area Signed-off-by: Denys Vlasenko --- libbb/appletlib.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'libbb') diff --git a/libbb/appletlib.c b/libbb/appletlib.c index 4b5b09f45..d798a2eac 100644 --- a/libbb/appletlib.c +++ b/libbb/appletlib.c @@ -902,6 +902,19 @@ int lbb_main(char **argv) int main(int argc UNUSED_PARAM, char **argv) #endif { +#if 0 + /* TODO: find a use for a block of memory between end of .bss + * and end of page. For example, I'm getting "_end:0x812e698 2408 bytes" + * - more than 2k of wasted memory (in this particular build) + * *per each running process*! + * (If your linker does not generate "_end" name, weak attribute + * makes &_end == NULL, end_len == 0 here.) + */ + extern char _end[] __attribute__((weak)); + unsigned end_len = (-(int)_end) & 0xfff; + printf("_end:%p %u bytes\n", &_end, end_len); +#endif + /* Tweak malloc for reduced memory consumption */ #ifdef M_TRIM_THRESHOLD /* M_TRIM_THRESHOLD is the maximum amount of freed top-most memory -- cgit v1.2.3-55-g6feb From 46b494635e6ef54e282749d2c65dd1922b167931 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Sun, 3 Apr 2016 16:55:03 +0200 Subject: libbb: speed up error_msg functions function old new delta bb_verror_msg 386 466 +80 Signed-off-by: Denys Vlasenko --- libbb/verror_msg.c | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) (limited to 'libbb') diff --git a/libbb/verror_msg.c b/libbb/verror_msg.c index 0ef2a311f..22c30357b 100644 --- a/libbb/verror_msg.c +++ b/libbb/verror_msg.c @@ -20,6 +20,7 @@ const char *msg_eol = "\n"; void FAST_FUNC bb_verror_msg(const char *s, va_list p, const char* strerr) { char *msg, *msg1; + char stack_msg[80]; int applet_len, strerr_len, msgeol_len, used; if (!logmode) @@ -28,6 +29,27 @@ void FAST_FUNC bb_verror_msg(const char *s, va_list p, const char* strerr) if (!s) /* nomsg[_and_die] uses NULL fmt */ s = ""; /* some libc don't like printf(NULL) */ + applet_len = strlen(applet_name) + 2; /* "applet: " */ + strerr_len = strerr ? strlen(strerr) : 0; + msgeol_len = strlen(msg_eol); + + /* This costs ~90 bytes of code, but avoids costly + * malloc()[in vasprintf]+realloc()+memmove()+free() in 99% of cases. + * ~40% speedup. + */ + if ((int)sizeof(stack_msg) - applet_len > 0) { + va_list p2; + + /* It is not portable to use va_list twice, need to va_copy it */ + va_copy(p2, p); + used = vsnprintf(stack_msg + applet_len, (int)sizeof(stack_msg) - applet_len, s, p2); + va_end(p2); + msg = stack_msg; + used += applet_len; + if (used < (int)sizeof(stack_msg) - 3 - msgeol_len - strerr_len) + goto add_pfx_and_sfx; + } + used = vasprintf(&msg, s, p); if (used < 0) return; @@ -37,9 +59,6 @@ void FAST_FUNC bb_verror_msg(const char *s, va_list p, const char* strerr) * This is needed for e.g. httpd logging, when multiple * children can produce log messages simultaneously. */ - applet_len = strlen(applet_name) + 2; /* "applet: " */ - strerr_len = strerr ? strlen(strerr) : 0; - msgeol_len = strlen(msg_eol); /* can't use xrealloc: it calls error_msg on failure, * that may result in a recursion */ /* +3 is for ": " before strerr and for terminating NUL */ @@ -52,6 +71,7 @@ void FAST_FUNC bb_verror_msg(const char *s, va_list p, const char* strerr) /* TODO: maybe use writev instead of memmoving? Need full_writev? */ memmove(msg + applet_len, msg, used); used += applet_len; + add_pfx_and_sfx: strcpy(msg, applet_name); msg[applet_len - 2] = ':'; msg[applet_len - 1] = ' '; @@ -76,7 +96,8 @@ void FAST_FUNC bb_verror_msg(const char *s, va_list p, const char* strerr) syslog(syslog_level, "%s", msg + applet_len); } #endif - free(msg); + if (msg != stack_msg) + free(msg); } #ifdef VERSION_WITH_WRITEV -- cgit v1.2.3-55-g6feb From b068cf2a7e036da8d0b3533b41886c5605c8139d Mon Sep 17 00:00:00 2001 From: Sven Eisenberg Date: Sun, 3 Apr 2016 21:53:12 +0200 Subject: ubirmvol: Implement -N switch for ubirmvol function old new delta get_volid_by_name - 125 +125 ubi_devnum_from_devname - 43 +43 ubi_tools_main 1215 1220 +5 packed_usage 30674 30655 -19 ubirename_main 394 221 -173 ------------------------------------------------------------------------------ (add/remove: 3/0 grow/shrink: 1/2 up/down: 173/-192) Total: -19 bytes Signed-off-by: Denys Vlasenko --- include/libbb.h | 3 +++ libbb/ubi.c | 43 +++++++++++++++++++++++++++++++++++++++++++ miscutils/ubi_tools.c | 29 +++++++++++++++++++---------- miscutils/ubirename.c | 25 ++++--------------------- 4 files changed, 69 insertions(+), 31 deletions(-) create mode 100644 libbb/ubi.c (limited to 'libbb') diff --git a/include/libbb.h b/include/libbb.h index 64e61cd26..35c28df51 100644 --- a/include/libbb.h +++ b/include/libbb.h @@ -1770,6 +1770,9 @@ void bb_progress_update(bb_progress_t *p, uoff_t transferred, uoff_t totalsize) FAST_FUNC; +unsigned ubi_devnum_from_devname(const char *str) FAST_FUNC; +int get_volid_by_name(unsigned ubi_devnum, const char *vol_name) FAST_FUNC; + extern const char *applet_name; diff --git a/libbb/ubi.c b/libbb/ubi.c new file mode 100644 index 000000000..7d3b2952d --- /dev/null +++ b/libbb/ubi.c @@ -0,0 +1,43 @@ +/* vi: set sw=4 ts=4: */ +/* + * Utility routines. + * + * Copyright (C) 2016 Denys Vlasenko + * + * Licensed under GPLv2, see file LICENSE in this source tree. + */ +//kbuild:lib-y += ubi.o + +#include "libbb.h" + +// from ubi-media.h +#define UBI_MAX_VOLUME_NAME 127 +#define UBI_MAX_VOLUMES 128 + +unsigned FAST_FUNC ubi_devnum_from_devname(const char *str) +{ + unsigned ubi_devnum; + + if (sscanf(str, "/dev/ubi%u", &ubi_devnum) != 1) + bb_error_msg_and_die("not an UBI device: '%s'", str); + return ubi_devnum; +} + +int FAST_FUNC get_volid_by_name(unsigned ubi_devnum, const char *vol_name) +{ + unsigned i; + + for (i = 0; i < UBI_MAX_VOLUMES; i++) { + char buf[UBI_MAX_VOLUME_NAME + 1]; + char fname[sizeof("/sys/class/ubi/ubi%u_%u/name") + 2 * sizeof(int)*3]; + + sprintf(fname, "/sys/class/ubi/ubi%u_%u/name", ubi_devnum, i); + if (open_read_close(fname, buf, sizeof(buf)) <= 0) + continue; + + strchrnul(buf, '\n')[0] = '\0'; + if (strcmp(vol_name, buf) == 0) + return i; + } + bb_error_msg_and_die("volume '%s' not found", vol_name); +} diff --git a/miscutils/ubi_tools.c b/miscutils/ubi_tools.c index dd1bda300..ac0c56d6b 100644 --- a/miscutils/ubi_tools.c +++ b/miscutils/ubi_tools.c @@ -195,7 +195,7 @@ int ubi_tools_main(int argc UNUSED_PARAM, char **argv) } else //usage:#define ubimkvol_trivial_usage -//usage: "UBI_DEVICE -N NAME [-s SIZE | -m]" +//usage: "-N NAME [-s SIZE | -m] UBI_DEVICE" //usage:#define ubimkvol_full_usage "\n\n" //usage: "Create UBI volume\n" //usage: "\n -a ALIGNMENT Volume alignment (default 1)" @@ -212,9 +212,7 @@ int ubi_tools_main(int argc UNUSED_PARAM, char **argv) unsigned num; char *p; - if (sscanf(ubi_ctrl, "/dev/ubi%u", &num) != 1) - bb_error_msg_and_die("wrong format of UBI device name"); - + num = ubi_devnum_from_devname(ubi_ctrl); p = path_sys_class_ubi_ubi + sprintf(path_sys_class_ubi_ubi, "%u/", num); strcpy(p, "avail_eraseblocks"); @@ -248,20 +246,31 @@ int ubi_tools_main(int argc UNUSED_PARAM, char **argv) } else //usage:#define ubirmvol_trivial_usage -//usage: "UBI_DEVICE -n VOLID" +//usage: "-n VOLID / -N VOLNAME UBI_DEVICE" //usage:#define ubirmvol_full_usage "\n\n" //usage: "Remove UBI volume\n" //usage: "\n -n VOLID Volume ID" +//usage: "\n -N VOLNAME Volume name" if (do_rmvol) { - if (!(opts & OPTION_n)) + if (!(opts & (OPTION_n|OPTION_N))) bb_error_msg_and_die("volume id not specified"); - /* FIXME? kernel expects int32_t* here: */ - xioctl(fd, UBI_IOCRMVOL, &vol_id); + if (opts & OPTION_N) { + unsigned num = ubi_devnum_from_devname(ubi_ctrl); + vol_id = get_volid_by_name(num, vol_name); + } + + if (sizeof(vol_id) != 4) { + /* kernel expects int32_t* in this ioctl */ + int32_t t = vol_id; + xioctl(fd, UBI_IOCRMVOL, &t); + } else { + xioctl(fd, UBI_IOCRMVOL, &vol_id); + } } else //usage:#define ubirsvol_trivial_usage -//usage: "UBI_DEVICE -n VOLID -s SIZE" +//usage: "-n VOLID -s SIZE UBI_DEVICE" //usage:#define ubirsvol_full_usage "\n\n" //usage: "Resize UBI volume\n" //usage: "\n -n VOLID Volume ID" @@ -279,7 +288,7 @@ int ubi_tools_main(int argc UNUSED_PARAM, char **argv) } else //usage:#define ubiupdatevol_trivial_usage -//usage: "UBI_DEVICE [-t | [-s SIZE] IMG_FILE]" +//usage: "[-t | [-s SIZE] IMG_FILE] UBI_DEVICE" //usage:#define ubiupdatevol_full_usage "\n\n" //usage: "Update UBI volume\n" //usage: "\n -t Truncate to zero size" diff --git a/miscutils/ubirename.c b/miscutils/ubirename.c index 455a49485..d6ccfcb10 100644 --- a/miscutils/ubirename.c +++ b/miscutils/ubirename.c @@ -30,12 +30,10 @@ #endif // from ubi-media.h -#define UBI_VOL_NAME_MAX 127 +#define UBI_MAX_VOLUME_NAME 127 #define UBI_MAX_VOLUMES 128 // end ubi-media.h -#define UBI_MAX_VOLUME_NAME UBI_VOL_NAME_MAX - // from ubi-user.h /* ioctl commands of UBI character devices */ #define UBI_IOC_MAGIC 'o' @@ -64,7 +62,7 @@ int ubirename_main(int argc, char **argv) struct ubi_rnvol_req *rnvol; const char *ubi_devname; unsigned ubi_devnum; - unsigned i, n; + unsigned n; /* argc can be 4, 6, 8, ... */ if ((argc & 1) || (argc >>= 1) < 2) @@ -76,27 +74,12 @@ int ubirename_main(int argc, char **argv) bb_error_msg_and_die("too many renames requested"); ubi_devname = argv[1]; - if (sscanf(ubi_devname, "/dev/ubi%u", &ubi_devnum) != 1) - bb_error_msg_and_die("not a ubi device: '%s'", ubi_devname); + ubi_devnum = ubi_devnum_from_devname(ubi_devname); n = 0; argv += 2; while (argv[0]) { - for (i = 0; i < UBI_MAX_VOLUMES; i++) { - char buf[UBI_VOL_NAME_MAX + 1]; - char fname[sizeof("/sys/class/ubi/ubi%u_%u/name") + 2 * sizeof(int)*3]; - - sprintf(fname, "/sys/class/ubi/ubi%u_%u/name", ubi_devnum, i); - if (open_read_close(fname, buf, sizeof(buf)) <= 0) - continue; - - strchrnul(buf, '\n')[0] = '\0'; - if (strcmp(buf, argv[0]) == 0) - goto found; - } - bb_error_msg_and_die("no volume '%s' found", argv[0]); - found: - rnvol->ents[n].vol_id = i; + rnvol->ents[n].vol_id = get_volid_by_name(ubi_devnum, argv[0]); rnvol->ents[n].name_len = strlen(argv[1]); if (rnvol->ents[n].name_len >= sizeof(rnvol->ents[n].name)) bb_error_msg_and_die("new name '%s' is too long", argv[1]); -- cgit v1.2.3-55-g6feb From 6aab9928dec29855bcee21bce163e5fdf7144350 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Sun, 3 Apr 2016 22:24:51 +0200 Subject: whitespace and namespace cleanups Signed-off-by: Denys Vlasenko --- include/libbb.h | 2 +- libbb/ubi.c | 8 ++++---- miscutils/ubi_tools.c | 2 +- miscutils/ubirename.c | 4 ++-- 4 files changed, 8 insertions(+), 8 deletions(-) (limited to 'libbb') diff --git a/include/libbb.h b/include/libbb.h index 35c28df51..111dd66e0 100644 --- a/include/libbb.h +++ b/include/libbb.h @@ -1771,7 +1771,7 @@ void bb_progress_update(bb_progress_t *p, uoff_t totalsize) FAST_FUNC; unsigned ubi_devnum_from_devname(const char *str) FAST_FUNC; -int get_volid_by_name(unsigned ubi_devnum, const char *vol_name) FAST_FUNC; +int ubi_get_volid_by_name(unsigned ubi_devnum, const char *vol_name) FAST_FUNC; extern const char *applet_name; diff --git a/libbb/ubi.c b/libbb/ubi.c index 7d3b2952d..34595d797 100644 --- a/libbb/ubi.c +++ b/libbb/ubi.c @@ -16,14 +16,14 @@ unsigned FAST_FUNC ubi_devnum_from_devname(const char *str) { - unsigned ubi_devnum; + unsigned ubi_devnum; - if (sscanf(str, "/dev/ubi%u", &ubi_devnum) != 1) - bb_error_msg_and_die("not an UBI device: '%s'", str); + if (sscanf(str, "/dev/ubi%u", &ubi_devnum) != 1) + bb_error_msg_and_die("not an UBI device: '%s'", str); return ubi_devnum; } -int FAST_FUNC get_volid_by_name(unsigned ubi_devnum, const char *vol_name) +int FAST_FUNC ubi_get_volid_by_name(unsigned ubi_devnum, const char *vol_name) { unsigned i; diff --git a/miscutils/ubi_tools.c b/miscutils/ubi_tools.c index ac0c56d6b..4364bc807 100644 --- a/miscutils/ubi_tools.c +++ b/miscutils/ubi_tools.c @@ -257,7 +257,7 @@ int ubi_tools_main(int argc UNUSED_PARAM, char **argv) if (opts & OPTION_N) { unsigned num = ubi_devnum_from_devname(ubi_ctrl); - vol_id = get_volid_by_name(num, vol_name); + vol_id = ubi_get_volid_by_name(num, vol_name); } if (sizeof(vol_id) != 4) { diff --git a/miscutils/ubirename.c b/miscutils/ubirename.c index d6ccfcb10..8b1c3785a 100644 --- a/miscutils/ubirename.c +++ b/miscutils/ubirename.c @@ -31,7 +31,7 @@ // from ubi-media.h #define UBI_MAX_VOLUME_NAME 127 -#define UBI_MAX_VOLUMES 128 +#define UBI_MAX_VOLUMES 128 // end ubi-media.h // from ubi-user.h @@ -79,7 +79,7 @@ int ubirename_main(int argc, char **argv) n = 0; argv += 2; while (argv[0]) { - rnvol->ents[n].vol_id = get_volid_by_name(ubi_devnum, argv[0]); + rnvol->ents[n].vol_id = ubi_get_volid_by_name(ubi_devnum, argv[0]); rnvol->ents[n].name_len = strlen(argv[1]); if (rnvol->ents[n].name_len >= sizeof(rnvol->ents[n].name)) bb_error_msg_and_die("new name '%s' is too long", argv[1]); -- cgit v1.2.3-55-g6feb From b22061718db0111f9e7474f9b60aef02456ac070 Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Sun, 3 Apr 2016 22:29:35 +0200 Subject: find_applet_by_name: loop index should be signed The loop for (j = ARRAY_SIZE(applet_nameofs)-1; j >= 0; j--) { was intended to terminate when j goes negative, so j needs to be signed. Signed-off-by: Ron Yorston Signed-off-by: Denys Vlasenko --- libbb/appletlib.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'libbb') diff --git a/libbb/appletlib.c b/libbb/appletlib.c index d798a2eac..de654f64c 100644 --- a/libbb/appletlib.c +++ b/libbb/appletlib.c @@ -141,7 +141,8 @@ void FAST_FUNC bb_show_usage(void) int FAST_FUNC find_applet_by_name(const char *name) { - unsigned i, j, max; + unsigned i, max; + int j; const char *p; /* The commented-out word-at-a-time code is ~40% faster, but +160 bytes. -- cgit v1.2.3-55-g6feb