From: Denis Vlasenko Date: Tue, 23 Dec 2008 23:36:47 +0000 (-0000) Subject: libbb: introduce and use xmalloc_ttyname (-32 in bss). X-Git-Tag: 1_14_0~384 X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=4e12b1a2a9e68685dff61acaee1e1f6c377d978c;p=oweals%2Fbusybox.git libbb: introduce and use xmalloc_ttyname (-32 in bss). ash: small code shrink text data bss dec hex filename 793669 504 7524 801697 c3ba1 busybox_old 793659 504 7492 801655 c3b77 busybox_unstripped --- diff --git a/TODO b/TODO index f57921122..6d6b7e971 100644 --- a/TODO +++ b/TODO @@ -23,7 +23,7 @@ Rob Landley suggested these: lash is phased out. hush can be configured down to be nearly as small, but less buggy :) init - General cleanup (should use ENABLE_FEATURE_INIT_SYSLOG and ENABLE_FEATURE_INIT_DEBUG). + General cleanup (should use ENABLE_FEATURE_INIT_SYSLOG). Do a SUSv3 audit Look at the full Single Unix Specification version 3 (available online at "http://www.opengroup.org/onlinepubs/009695399/nfindex.html") and diff --git a/coreutils/tty.c b/coreutils/tty.c index e83289440..d49fb50b6 100644 --- a/coreutils/tty.c +++ b/coreutils/tty.c @@ -30,7 +30,7 @@ int tty_main(int argc, char **argv SKIP_INCLUDE_SUSv2(UNUSED_PARAM)) retval = 0; - s = ttyname(0); + s = xmalloc_ttyname(0); if (s == NULL) { /* According to SUSv3, ttyname can fail with EBADF or ENOTTY. * We know the file descriptor is good, so failure means not a tty. */ diff --git a/include/libbb.h b/include/libbb.h index e6767e39e..e0541a731 100644 --- a/include/libbb.h +++ b/include/libbb.h @@ -1144,6 +1144,7 @@ const char *nth_string(const char *strings, int n) FAST_FUNC; extern void print_login_issue(const char *issue_file, const char *tty) FAST_FUNC; extern void print_login_prompt(void) FAST_FUNC; +char *xmalloc_ttyname(int fd) FAST_FUNC; /* NB: typically you want to pass fd 0, not 1. Think 'applet | grep something' */ int get_terminal_width_height(int fd, unsigned *width, unsigned *height) FAST_FUNC; diff --git a/init/mesg.c b/init/mesg.c index cfb517f60..ca230f363 100644 --- a/init/mesg.c +++ b/init/mesg.c @@ -25,7 +25,7 @@ int mesg_main(int argc, char **argv) if (--argc == 0 || (argc == 1 && ((c = **++argv) == 'y' || c == 'n')) ) { - tty = ttyname(STDERR_FILENO); + tty = xmalloc_ttyname(STDERR_FILENO); if (tty == NULL) { tty = "ttyname"; } else if (stat(tty, &sb) == 0) { diff --git a/libbb/xfuncs.c b/libbb/xfuncs.c index f3c3c538c..a86efc298 100644 --- a/libbb/xfuncs.c +++ b/libbb/xfuncs.c @@ -268,6 +268,17 @@ off_t FAST_FUNC fdlength(int fd) } #endif +char* FAST_FUNC xmalloc_ttyname(int fd) +{ + char *buf = xzalloc(128); + int r = ttyname_r(fd, buf, 127); + if (r) { + free(buf); + buf = NULL; + } + return buf; +} + /* It is perfectly ok to pass in a NULL for either width or for * height, in which case that value will not be set. */ int FAST_FUNC get_terminal_width_height(int fd, unsigned *width, unsigned *height) diff --git a/loginutils/login.c b/loginutils/login.c index a18b4d5d7..af871436a 100644 --- a/loginutils/login.c +++ b/loginutils/login.c @@ -275,7 +275,7 @@ int login_main(int argc UNUSED_PARAM, char **argv) if (!isatty(0) || !isatty(1) || !isatty(2)) return EXIT_FAILURE; /* Must be a terminal */ safe_strncpy(full_tty, "UNKNOWN", sizeof(full_tty)); - tmp = ttyname(0); + tmp = xmalloc_ttyname(STDIN_FILENO); if (tmp) { safe_strncpy(full_tty, tmp, sizeof(full_tty)); if (strncmp(full_tty, "/dev/", 5) == 0) @@ -285,12 +285,12 @@ int login_main(int argc UNUSED_PARAM, char **argv) read_or_build_utent(&utent, run_by_root); if (opt & LOGIN_OPT_h) { - USE_FEATURE_UTMP( + if (ENABLE_FEATURE_UTMP) safe_strncpy(utent.ut_host, opt_host, sizeof(utent.ut_host)); - ) fromhost = xasprintf(" on '%s' from '%s'", short_tty, opt_host); - } else + } else { fromhost = xasprintf(" on '%s'", short_tty); + } /* Was breaking "login " from shell command line: */ /*bb_setpgrp();*/ diff --git a/loginutils/su.c b/loginutils/su.c index e7e0001c7..de8c18d25 100644 --- a/loginutils/su.c +++ b/loginutils/su.c @@ -44,7 +44,7 @@ int su_main(int argc UNUSED_PARAM, char **argv) But getlogin can fail -- usually due to lack of utmp entry. in this case resort to getpwuid. */ old_user = xstrdup(USE_FEATURE_UTMP(getlogin() ? : ) (pw = getpwuid(cur_uid)) ? pw->pw_name : ""); - tty = ttyname(2) ? : "none"; + tty = xmalloc_ttyname(2) ? : "none"; openlog(applet_name, 0, LOG_AUTH); } diff --git a/selinux/sestatus.c b/selinux/sestatus.c index 216629615..1a02a6b0c 100644 --- a/selinux/sestatus.c +++ b/selinux/sestatus.c @@ -114,7 +114,8 @@ static void display_verbose(void) /* files contexts */ puts("\nFile contexts:"); - cterm = ttyname(0); + cterm = xmalloc_ttyname(0); +//FIXME: if cterm == NULL, we segfault!?? puts(cterm); if (cterm && lgetfilecon(cterm, &con) >= 0) { printf(COL_FMT "%s\n", "Controlling term:", con); @@ -122,7 +123,7 @@ static void display_verbose(void) freecon(con); } - for (i=0; fc[i] != NULL; i++) { + for (i = 0; fc[i] != NULL; i++) { struct stat stbuf; if (lgetfilecon(fc[i], &con) < 0) diff --git a/shell/ash.c b/shell/ash.c index 2fc903b37..492ccd79f 100644 --- a/shell/ash.c +++ b/shell/ash.c @@ -13691,7 +13691,7 @@ int ash_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; int ash_main(int argc UNUSED_PARAM, char **argv) { char *shinit; - volatile int state; + volatile smallint state; struct jmploc jmploc; struct stackmark smark; @@ -13714,7 +13714,7 @@ int ash_main(int argc UNUSED_PARAM, char **argv) state = 0; if (setjmp(jmploc.loc)) { int e; - int s; + smallint s; reset(); @@ -13769,7 +13769,7 @@ int ash_main(int argc UNUSED_PARAM, char **argv) } } #endif - if (argv[0] && argv[0][0] == '-') + if (/* argv[0] && */ argv[0][0] == '-') isloginsh = 1; if (isloginsh) { state = 1;