From 6937487be73cd4563b876413277a295a5fe2f32c Mon Sep 17 00:00:00 2001 From: James Byrne Date: Tue, 2 Jul 2019 11:35:03 +0200 Subject: libbb: reduce the overhead of single parameter bb_error_msg() calls Back in 2007, commit 0c97c9d43707 ("'simple' error message functions by Loic Grenie") introduced bb_simple_perror_msg() to allow for a lower overhead call to bb_perror_msg() when only a string was being printed with no parameters. This saves space for some CPU architectures because it avoids the overhead of a call to a variadic function. However there has never been a simple version of bb_error_msg(), and since 2007 many new calls to bb_perror_msg() have been added that only take a single parameter and so could have been using bb_simple_perror_message(). This changeset introduces 'simple' versions of bb_info_msg(), bb_error_msg(), bb_error_msg_and_die(), bb_herror_msg() and bb_herror_msg_and_die(), and replaces all calls that only take a single parameter, or use something like ("%s", arg), with calls to the corresponding 'simple' version. Since it is likely that single parameter calls to the variadic functions may be accidentally reintroduced in the future a new debugging config option WARN_SIMPLE_MSG has been introduced. This uses some macro magic which will cause any such calls to generate a warning, but this is turned off by default to avoid use of the unpleasant macros in normal circumstances. This is a large changeset due to the number of calls that have been replaced. The only files that contain changes other than simple substitution of function calls are libbb.h, libbb/herror_msg.c, libbb/verror_msg.c and libbb/xfuncs_printf.c. In miscutils/devfsd.c, networking/udhcp/common.h and util-linux/mdev.c additonal macros have been added for logging so that single parameter and multiple parameter logging variants exist. The amount of space saved varies considerably by architecture, and was found to be as follows (for 'defconfig' using GCC 7.4): Arm: -92 bytes MIPS: -52 bytes PPC: -1836 bytes x86_64: -938 bytes Note that for the MIPS architecture only an exception had to be made disabling the 'simple' calls for 'udhcp' (in networking/udhcp/common.h) because it made these files larger on MIPS. Signed-off-by: James Byrne Signed-off-by: Denys Vlasenko --- loginutils/addgroup.c | 2 +- loginutils/adduser.c | 4 ++-- loginutils/chpasswd.c | 4 ++-- loginutils/deluser.c | 2 +- loginutils/getty.c | 16 ++++++++-------- loginutils/login.c | 4 ++-- loginutils/su.c | 4 ++-- loginutils/sulogin.c | 8 ++++---- 8 files changed, 22 insertions(+), 22 deletions(-) (limited to 'loginutils') diff --git a/loginutils/addgroup.c b/loginutils/addgroup.c index baa961ff3..2a83c8a15 100644 --- a/loginutils/addgroup.c +++ b/loginutils/addgroup.c @@ -149,7 +149,7 @@ int addgroup_main(int argc UNUSED_PARAM, char **argv) /* need to be root */ if (geteuid()) { - bb_error_msg_and_die(bb_msg_perm_denied_are_you_root); + bb_simple_error_msg_and_die(bb_msg_perm_denied_are_you_root); } /* Syntax: * addgroup group diff --git a/loginutils/adduser.c b/loginutils/adduser.c index 850c810c4..d3c795afa 100644 --- a/loginutils/adduser.c +++ b/loginutils/adduser.c @@ -159,7 +159,7 @@ static void passwd_wrapper(const char *login_name) NORETURN; static void passwd_wrapper(const char *login_name) { BB_EXECLP("passwd", "passwd", "--", login_name, NULL); - bb_error_msg_and_die("can't execute passwd, you must set password manually"); + bb_simple_error_msg_and_die("can't execute passwd, you must set password manually"); } //FIXME: upstream adduser has no short options! NOT COMPATIBLE! @@ -193,7 +193,7 @@ int adduser_main(int argc UNUSED_PARAM, char **argv) /* got root? */ if (geteuid()) { - bb_error_msg_and_die(bb_msg_perm_denied_are_you_root); + bb_simple_error_msg_and_die(bb_msg_perm_denied_are_you_root); } pw.pw_gecos = (char *)"Linux User,,,"; diff --git a/loginutils/chpasswd.c b/loginutils/chpasswd.c index dd0532c66..4e70b2557 100644 --- a/loginutils/chpasswd.c +++ b/loginutils/chpasswd.c @@ -63,7 +63,7 @@ int chpasswd_main(int argc UNUSED_PARAM, char **argv) int opt; if (getuid() != 0) - bb_error_msg_and_die(bb_msg_perm_denied_are_you_root); + bb_simple_error_msg_and_die(bb_msg_perm_denied_are_you_root); opt = getopt32long(argv, "^" "emc:R:" "\0" "m--ec:e--mc:c--em", chpasswd_longopts, @@ -81,7 +81,7 @@ int chpasswd_main(int argc UNUSED_PARAM, char **argv) pass = strchr(name, ':'); if (!pass) - bb_error_msg_and_die("missing new password"); + bb_simple_error_msg_and_die("missing new password"); *pass++ = '\0'; xuname2uid(name); /* dies if there is no such user */ diff --git a/loginutils/deluser.c b/loginutils/deluser.c index d7e9327ba..56bc7eaa6 100644 --- a/loginutils/deluser.c +++ b/loginutils/deluser.c @@ -76,7 +76,7 @@ int deluser_main(int argc, char **argv) #endif if (geteuid() != 0) - bb_error_msg_and_die(bb_msg_perm_denied_are_you_root); + bb_simple_error_msg_and_die(bb_msg_perm_denied_are_you_root); name = argv[1]; member = NULL; diff --git a/loginutils/getty.c b/loginutils/getty.c index 23e92bc77..7393a3d1c 100644 --- a/loginutils/getty.c +++ b/loginutils/getty.c @@ -168,7 +168,7 @@ static void parse_speeds(char *arg) /* note: arg "0" turns into speed B0 */ G.numspeed++; if (G.numspeed > MAX_SPEED) - bb_error_msg_and_die("too many alternate speeds"); + bb_simple_error_msg_and_die("too many alternate speeds"); } debug("exiting parse_speeds\n"); } @@ -230,7 +230,7 @@ static void open_tty(void) * Make sure it is open for read/write. */ if ((fcntl(0, F_GETFL) & (O_RDWR|O_RDONLY|O_WRONLY)) != O_RDWR) - bb_error_msg_and_die("stdin is not open for read/write"); + bb_simple_error_msg_and_die("stdin is not open for read/write"); /* Try to get real tty name instead of "-" */ n = xmalloc_ttyname(0); @@ -243,7 +243,7 @@ static void open_tty(void) static void set_tty_attrs(void) { if (tcsetattr_stdin_TCSANOW(&G.tty_attrs) < 0) - bb_perror_msg_and_die("tcsetattr"); + bb_simple_perror_msg_and_die("tcsetattr"); } /* We manipulate tty_attrs this way: @@ -485,7 +485,7 @@ static char *get_logname(void) finalize_tty_attrs(); if (errno == EINTR || errno == EIO) exit(EXIT_SUCCESS); - bb_perror_msg_and_die(bb_msg_read_error); + bb_simple_perror_msg_and_die(bb_msg_read_error); } switch (c) { @@ -582,7 +582,7 @@ int getty_main(int argc UNUSED_PARAM, char **argv) // " sid %d pgid %d", // pid, getppid(), // getsid(0), getpgid(0)); - bb_perror_msg_and_die("setsid"); + bb_simple_perror_msg_and_die("setsid"); /* * When we can end up here? * Example: setsid() fails when run alone in interactive shell: @@ -651,13 +651,13 @@ int getty_main(int argc UNUSED_PARAM, char **argv) tsid = tcgetsid(STDIN_FILENO); if (tsid < 0 || pid != tsid) { if (ioctl(STDIN_FILENO, TIOCSCTTY, /*force:*/ (long)1) < 0) - bb_perror_msg_and_die("TIOCSCTTY"); + bb_simple_perror_msg_and_die("TIOCSCTTY"); } #ifdef __linux__ /* Make ourself a foreground process group within our session */ if (tcsetpgrp(STDIN_FILENO, pid) < 0) - bb_perror_msg_and_die("tcsetpgrp"); + bb_simple_perror_msg_and_die("tcsetpgrp"); #endif /* @@ -669,7 +669,7 @@ int getty_main(int argc UNUSED_PARAM, char **argv) * 5 seconds seems to be a good value. */ if (tcgetattr(STDIN_FILENO, &G.tty_attrs) < 0) - bb_perror_msg_and_die("tcgetattr"); + bb_simple_perror_msg_and_die("tcgetattr"); /* Update the utmp file. This tty is ours now! */ update_utmp(pid, LOGIN_PROCESS, G.tty_name, "LOGIN", G.fakehost); diff --git a/loginutils/login.c b/loginutils/login.c index a08642a34..4e65b3a19 100644 --- a/loginutils/login.c +++ b/loginutils/login.c @@ -358,7 +358,7 @@ int login_main(int argc UNUSED_PARAM, char **argv) opt = getopt32(argv, "f:h:p", &opt_user, &opt_host); if (opt & LOGIN_OPT_f) { if (!run_by_root) - bb_error_msg_and_die("-f is for root only"); + bb_simple_error_msg_and_die("-f is for root only"); safe_strncpy(username, opt_user, sizeof(username)); } argv += optind; @@ -529,7 +529,7 @@ int login_main(int argc UNUSED_PARAM, char **argv) child_pid = vfork(); if (child_pid != 0) { if (child_pid < 0) - bb_perror_msg("vfork"); + bb_simple_perror_msg("vfork"); else { wait_for_exitstatus(child_pid); update_utmp_DEAD_PROCESS(child_pid); diff --git a/loginutils/su.c b/loginutils/su.c index 41291ea8f..7c1fc69c5 100644 --- a/loginutils/su.c +++ b/loginutils/su.c @@ -147,7 +147,7 @@ int su_main(int argc UNUSED_PARAM, char **argv) syslog(LOG_NOTICE, "%c %s %s:%s", '-', tty, old_user, opt_username); bb_do_delay(LOGIN_FAIL_DELAY); - bb_error_msg_and_die("incorrect password"); + bb_simple_error_msg_and_die("incorrect password"); } if (ENABLE_FEATURE_CLEAN_UP && ENABLE_FEATURE_SU_SYSLOG) { @@ -165,7 +165,7 @@ int su_main(int argc UNUSED_PARAM, char **argv) * probably a uucp account or has restricted access. Don't * compromise the account by allowing access with a standard * shell. */ - bb_error_msg("using restricted shell"); + bb_simple_error_msg("using restricted shell"); opt_shell = NULL; /* ignore -s PROG */ } /* else: user can run whatever he wants via "su -s PROG USER". diff --git a/loginutils/sulogin.c b/loginutils/sulogin.c index 9bb4d3613..099085340 100644 --- a/loginutils/sulogin.c +++ b/loginutils/sulogin.c @@ -56,7 +56,7 @@ int sulogin_main(int argc UNUSED_PARAM, char **argv) pwd = getpwuid(0); if (!pwd) { - bb_error_msg_and_die("no password entry for root"); + bb_simple_error_msg_and_die("no password entry for root"); } while (1) { @@ -68,17 +68,17 @@ int sulogin_main(int argc UNUSED_PARAM, char **argv) ); if (r < 0) { /* ^D, ^C, timeout, or read error */ - bb_info_msg("normal startup"); + bb_simple_info_msg("normal startup"); return 0; } if (r > 0) { break; } bb_do_delay(LOGIN_FAIL_DELAY); - bb_info_msg("Login incorrect"); + bb_simple_info_msg("Login incorrect"); } - bb_info_msg("starting shell for system maintenance"); + bb_simple_info_msg("starting shell for system maintenance"); IF_SELINUX(renew_current_security_context()); -- cgit v1.2.3