*: intrduce and use safe_gethostname. By Tito <farmatito AT tiscali.it>

safe_gethostname                                       -      48     +48
glob3                                                 35      37      +2
timestamp_and_log                                    314     315      +1
udhcp_send_kernel_packet                             234     231      -3
scan_tree                                            275     271      -4
passwd_main                                         1074    1070      -4
print_login_prompt                                    68      58     -10
obscure                                              392     377     -15
syslogd_main                                         882     866     -16
print_login_issue                                    516     478     -38
hostname_main                                        278     223     -55
parse_and_put_prompt                                 825     756     -69
------------------------------------------------------------------------------
(add/remove: 1/0 grow/shrink: 2/9 up/down: 51/-214)          Total: -163 bytes
   text    data     bss     dec     hex filename
 798791     728    7484  807003   c505b busybox_old
 798631     728    7484  806843   c4fbb busybox_unstripped
This commit is contained in:
Denis Vlasenko 2008-02-25 23:23:58 +00:00
parent 394eebed66
commit 6f1713f216
8 changed files with 32 additions and 45 deletions

View File

@ -550,6 +550,8 @@ extern FILE *fopen_or_warn_stdin(const char *filename);
* If this is a problem, use bare poll and open-code EINTR/ENOMEM handling */ * If this is a problem, use bare poll and open-code EINTR/ENOMEM handling */
int safe_poll(struct pollfd *ufds, nfds_t nfds, int timeout_ms); int safe_poll(struct pollfd *ufds, nfds_t nfds, int timeout_ms);
char *safe_gethostname(void);
/* Convert each alpha char in str to lower-case */ /* Convert each alpha char in str to lower-case */
char* str_tolower(char *str); char* str_tolower(char *str);

View File

@ -73,6 +73,7 @@ lib-y += recursive_action.o
lib-y += remove_file.o lib-y += remove_file.o
lib-y += restricted_shell.o lib-y += restricted_shell.o
lib-y += run_shell.o lib-y += run_shell.o
lib-y += safe_gethostname.o
lib-y += safe_poll.o lib-y += safe_poll.o
lib-y += safe_strncpy.o lib-y += safe_strncpy.o
lib-y += safe_write.o lib-y += safe_write.o

View File

@ -1203,11 +1203,7 @@ static void parse_and_put_prompt(const char *prmt_ptr)
break; break;
#endif #endif
case 'h': case 'h':
pbuf = free_me = xzalloc(256); pbuf = free_me = safe_gethostname();
if (gethostname(pbuf, 255) < 0) {
pbuf[0] = '?';
pbuf[1] = '\0';
}
*strchrnul(pbuf, '.') = '\0'; *strchrnul(pbuf, '.') = '\0';
break; break;
case '$': case '$':

View File

@ -50,6 +50,7 @@ void print_login_issue(const char *issue_file, const char *tty)
outbuf = uts.sysname; outbuf = uts.sysname;
break; break;
case 'n': case 'n':
case 'h':
outbuf = uts.nodename; outbuf = uts.nodename;
break; break;
case 'r': case 'r':
@ -72,10 +73,6 @@ void print_login_issue(const char *issue_file, const char *tty)
case 't': case 't':
strftime(buf, sizeof(buf), fmtstr_t, localtime(&t)); strftime(buf, sizeof(buf), fmtstr_t, localtime(&t));
break; break;
case 'h':
gethostname(buf, sizeof(buf) - 1);
buf[sizeof(buf) - 1] = '\0';
break;
case 'l': case 'l':
outbuf = tty; outbuf = tty;
break; break;
@ -91,13 +88,12 @@ void print_login_issue(const char *issue_file, const char *tty)
void print_login_prompt(void) void print_login_prompt(void)
{ {
char buf[MAXHOSTNAMELEN+1]; char *hostname = safe_gethostname();
if (gethostname(buf, MAXHOSTNAMELEN) == 0) fputs(hostname, stdout);
fputs(buf, stdout);
fputs(LOGIN, stdout); fputs(LOGIN, stdout);
fflush(stdout); fflush(stdout);
free(hostname);
} }
/* Clear dangerous stuff, set PATH */ /* Clear dangerous stuff, set PATH */

View File

@ -93,7 +93,7 @@ static const char *obscure_msg(const char *old_p, const char *new_p, const struc
/* Add 2 for each type of characters to the minlen of password */ /* Add 2 for each type of characters to the minlen of password */
int size = CONFIG_PASSWORD_MINLEN + 8; int size = CONFIG_PASSWORD_MINLEN + 8;
const char *p; const char *p;
char hostname[255]; char *hostname;
/* size */ /* size */
if (!new_p || (length = strlen(new_p)) < CONFIG_PASSWORD_MINLEN) if (!new_p || (length = strlen(new_p)) < CONFIG_PASSWORD_MINLEN)
@ -108,12 +108,11 @@ static const char *obscure_msg(const char *old_p, const char *new_p, const struc
return "similar to gecos"; return "similar to gecos";
} }
/* hostname as-is, as sub-string, reversed, capitalized, doubled */ /* hostname as-is, as sub-string, reversed, capitalized, doubled */
if (gethostname(hostname, 255) == 0) { hostname = safe_gethostname();
hostname[254] = '\0'; i = string_checker(new_p, hostname);
if (string_checker(new_p, hostname)) { free(hostname);
return "similar to hostname"; if (i)
} return "similar to hostname";
}
/* Should / Must contain a mix of: */ /* Should / Must contain a mix of: */
for (i = 0; i < length; i++) { for (i = 0; i < length; i++) {

View File

@ -1133,8 +1133,8 @@ static void signal_handler(int sig)
static const char *get_variable(const char *variable, void *info) static const char *get_variable(const char *variable, void *info)
{ {
static char sbuf[sizeof(int)*3 + 2]; /* sign and NUL */ static char sbuf[sizeof(int)*3 + 2]; /* sign and NUL */
static char *hostname;
char hostname[STRING_LENGTH];
struct get_variable_info *gv_info = info; struct get_variable_info *gv_info = info;
const char *field_names[] = { const char *field_names[] = {
"hostname", "mntpt", "devpath", "devname", "hostname", "mntpt", "devpath", "devname",
@ -1143,12 +1143,8 @@ static const char *get_variable(const char *variable, void *info)
}; };
int i; int i;
if (gethostname(hostname, STRING_LENGTH - 1) != 0) if (!hostname)
/* Here on error we should do exit(RV_SYS_ERROR), instead we do exit(EXIT_FAILURE) */ hostname = safe_gethostname();
error_logger_and_die(LOG_ERR, "gethostname");
hostname[STRING_LENGTH - 1] = '\0';
/* index_in_str_array returns i>=0 */ /* index_in_str_array returns i>=0 */
i = index_in_str_array(field_names, variable); i = index_in_str_array(field_names, variable);

View File

@ -24,8 +24,7 @@ static void do_sethostname(char *s, int isfile)
if (sethostname(s, strlen(s)) < 0) { if (sethostname(s, strlen(s)) < 0) {
if (errno == EPERM) if (errno == EPERM)
bb_error_msg_and_die(bb_msg_perm_denied_are_you_root); bb_error_msg_and_die(bb_msg_perm_denied_are_you_root);
else bb_perror_msg_and_die("sethostname");
bb_perror_msg_and_die("sethostname");
} }
} else { } else {
f = xfopen(s, "r"); f = xfopen(s, "r");
@ -54,27 +53,27 @@ int hostname_main(int argc, char **argv)
OPT_dfis = 0xf, OPT_dfis = 0xf,
}; };
char buf[256]; char *buf;
char *hostname_str; char *hostname_str;
if (argc < 1) if (argc < 1)
bb_show_usage(); bb_show_usage();
getopt32(argv, "dfisF:", &hostname_str); getopt32(argv, "dfisF:", &hostname_str);
argv += optind;
buf = safe_gethostname();
/* Output in desired format */ /* Output in desired format */
if (option_mask32 & OPT_dfis) { if (option_mask32 & OPT_dfis) {
struct hostent *hp; struct hostent *hp;
char *p; char *p;
gethostname(buf, sizeof(buf));
hp = xgethostbyname(buf); hp = xgethostbyname(buf);
p = strchr(hp->h_name, '.'); p = strchr(hp->h_name, '.');
if (option_mask32 & OPT_f) { if (option_mask32 & OPT_f) {
puts(hp->h_name); puts(hp->h_name);
} else if (option_mask32 & OPT_s) { } else if (option_mask32 & OPT_s) {
if (p != NULL) { if (p)
*p = '\0'; *p = '\0';
}
puts(hp->h_name); puts(hp->h_name);
} else if (option_mask32 & OPT_d) { } else if (option_mask32 & OPT_d) {
if (p) if (p)
@ -89,14 +88,15 @@ int hostname_main(int argc, char **argv)
/* Set the hostname */ /* Set the hostname */
else if (option_mask32 & OPT_F) { else if (option_mask32 & OPT_F) {
do_sethostname(hostname_str, 1); do_sethostname(hostname_str, 1);
} else if (optind < argc) { } else if (argv[0]) {
do_sethostname(argv[optind], 0); do_sethostname(argv[0], 0);
} }
/* Or if all else fails, /* Or if all else fails,
* just print the current hostname */ * just print the current hostname */
else { else {
gethostname(buf, sizeof(buf));
puts(buf); puts(buf);
} }
if (ENABLE_FEATURE_CLEAN_UP)
free(buf);
return 0; return 0;
} }

View File

@ -97,8 +97,8 @@ struct globals {
struct shbuf_ds *shbuf; struct shbuf_ds *shbuf;
#endif #endif
time_t last_log_time; time_t last_log_time;
/* localhost's name */ /* localhost's name. We print only first 64 chars */
char localHostName[64]; char *hostname;
/* We recv into recvbuf... */ /* We recv into recvbuf... */
char recvbuf[MAX_READ]; char recvbuf[MAX_READ];
@ -416,7 +416,7 @@ static void timestamp_and_log(int pri, char *msg, int len)
else { else {
char res[20]; char res[20];
parse_fac_prio_20(pri, res); parse_fac_prio_20(pri, res);
sprintf(G.printbuf, "%s %s %s %s\n", timestamp, G.localHostName, res, msg); sprintf(G.printbuf, "%s %.64s %s %s\n", timestamp, G.hostname, res, msg);
} }
/* Log message locally (to file or shared mem) */ /* Log message locally (to file or shared mem) */
@ -647,11 +647,8 @@ int syslogd_main(int argc, char **argv)
option_mask32 |= OPT_locallog; option_mask32 |= OPT_locallog;
/* Store away localhost's name before the fork */ /* Store away localhost's name before the fork */
/* "It is unspecified whether the truncated hostname G.hostname = safe_gethostname();
* will be null-terminated". We give it (size - 1), *strchrnul(G.hostname, '.') = '\0';
* thus last byte will be NUL no matter what. */
gethostname(G.localHostName, sizeof(G.localHostName) - 1);
*strchrnul(G.localHostName, '.') = '\0';
if (!(option_mask32 & OPT_nofork)) { if (!(option_mask32 & OPT_nofork)) {
bb_daemonize_or_rexec(DAEMON_CHDIR_ROOT, argv); bb_daemonize_or_rexec(DAEMON_CHDIR_ROOT, argv);