diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2008-02-25 23:23:58 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2008-02-25 23:23:58 +0000 |
commit | 6f1713f216fef686a68db5ee02232bc67e525c7d (patch) | |
tree | 256adc75a88723a4b247c32e692d721d65243a82 | |
parent | 394eebed6656dfc2e56a79500b602023000ac415 (diff) | |
download | busybox-w32-6f1713f216fef686a68db5ee02232bc67e525c7d.tar.gz busybox-w32-6f1713f216fef686a68db5ee02232bc67e525c7d.tar.bz2 busybox-w32-6f1713f216fef686a68db5ee02232bc67e525c7d.zip |
*: 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
-rw-r--r-- | include/libbb.h | 2 | ||||
-rw-r--r-- | libbb/Kbuild | 1 | ||||
-rw-r--r-- | libbb/lineedit.c | 6 | ||||
-rw-r--r-- | libbb/login.c | 14 | ||||
-rw-r--r-- | libbb/obscure.c | 13 | ||||
-rw-r--r-- | miscutils/devfsd.c | 10 | ||||
-rw-r--r-- | networking/hostname.c | 18 | ||||
-rw-r--r-- | sysklogd/syslogd.c | 13 |
8 files changed, 32 insertions, 45 deletions
diff --git a/include/libbb.h b/include/libbb.h index 1ea2e35be..978cd2d87 100644 --- a/include/libbb.h +++ b/include/libbb.h | |||
@@ -550,6 +550,8 @@ extern FILE *fopen_or_warn_stdin(const char *filename); | |||
550 | * If this is a problem, use bare poll and open-code EINTR/ENOMEM handling */ | 550 | * If this is a problem, use bare poll and open-code EINTR/ENOMEM handling */ |
551 | int safe_poll(struct pollfd *ufds, nfds_t nfds, int timeout_ms); | 551 | int safe_poll(struct pollfd *ufds, nfds_t nfds, int timeout_ms); |
552 | 552 | ||
553 | char *safe_gethostname(void); | ||
554 | |||
553 | /* Convert each alpha char in str to lower-case */ | 555 | /* Convert each alpha char in str to lower-case */ |
554 | char* str_tolower(char *str); | 556 | char* str_tolower(char *str); |
555 | 557 | ||
diff --git a/libbb/Kbuild b/libbb/Kbuild index 515368de7..aab016e85 100644 --- a/libbb/Kbuild +++ b/libbb/Kbuild | |||
@@ -73,6 +73,7 @@ lib-y += recursive_action.o | |||
73 | lib-y += remove_file.o | 73 | lib-y += remove_file.o |
74 | lib-y += restricted_shell.o | 74 | lib-y += restricted_shell.o |
75 | lib-y += run_shell.o | 75 | lib-y += run_shell.o |
76 | lib-y += safe_gethostname.o | ||
76 | lib-y += safe_poll.o | 77 | lib-y += safe_poll.o |
77 | lib-y += safe_strncpy.o | 78 | lib-y += safe_strncpy.o |
78 | lib-y += safe_write.o | 79 | lib-y += safe_write.o |
diff --git a/libbb/lineedit.c b/libbb/lineedit.c index 9aab63702..c6aa45c93 100644 --- a/libbb/lineedit.c +++ b/libbb/lineedit.c | |||
@@ -1203,11 +1203,7 @@ static void parse_and_put_prompt(const char *prmt_ptr) | |||
1203 | break; | 1203 | break; |
1204 | #endif | 1204 | #endif |
1205 | case 'h': | 1205 | case 'h': |
1206 | pbuf = free_me = xzalloc(256); | 1206 | pbuf = free_me = safe_gethostname(); |
1207 | if (gethostname(pbuf, 255) < 0) { | ||
1208 | pbuf[0] = '?'; | ||
1209 | pbuf[1] = '\0'; | ||
1210 | } | ||
1211 | *strchrnul(pbuf, '.') = '\0'; | 1207 | *strchrnul(pbuf, '.') = '\0'; |
1212 | break; | 1208 | break; |
1213 | case '$': | 1209 | case '$': |
diff --git a/libbb/login.c b/libbb/login.c index d1f5d6498..a711a5437 100644 --- a/libbb/login.c +++ b/libbb/login.c | |||
@@ -50,6 +50,7 @@ void print_login_issue(const char *issue_file, const char *tty) | |||
50 | outbuf = uts.sysname; | 50 | outbuf = uts.sysname; |
51 | break; | 51 | break; |
52 | case 'n': | 52 | case 'n': |
53 | case 'h': | ||
53 | outbuf = uts.nodename; | 54 | outbuf = uts.nodename; |
54 | break; | 55 | break; |
55 | case 'r': | 56 | case 'r': |
@@ -72,10 +73,6 @@ void print_login_issue(const char *issue_file, const char *tty) | |||
72 | case 't': | 73 | case 't': |
73 | strftime(buf, sizeof(buf), fmtstr_t, localtime(&t)); | 74 | strftime(buf, sizeof(buf), fmtstr_t, localtime(&t)); |
74 | break; | 75 | break; |
75 | case 'h': | ||
76 | gethostname(buf, sizeof(buf) - 1); | ||
77 | buf[sizeof(buf) - 1] = '\0'; | ||
78 | break; | ||
79 | case 'l': | 76 | case 'l': |
80 | outbuf = tty; | 77 | outbuf = tty; |
81 | break; | 78 | break; |
@@ -91,13 +88,12 @@ void print_login_issue(const char *issue_file, const char *tty) | |||
91 | 88 | ||
92 | void print_login_prompt(void) | 89 | void print_login_prompt(void) |
93 | { | 90 | { |
94 | char buf[MAXHOSTNAMELEN+1]; | 91 | char *hostname = safe_gethostname(); |
95 | 92 | ||
96 | if (gethostname(buf, MAXHOSTNAMELEN) == 0) | 93 | fputs(hostname, stdout); |
97 | fputs(buf, stdout); | ||
98 | |||
99 | fputs(LOGIN, stdout); | 94 | fputs(LOGIN, stdout); |
100 | fflush(stdout); | 95 | fflush(stdout); |
96 | free(hostname); | ||
101 | } | 97 | } |
102 | 98 | ||
103 | /* Clear dangerous stuff, set PATH */ | 99 | /* Clear dangerous stuff, set PATH */ |
diff --git a/libbb/obscure.c b/libbb/obscure.c index 5cc906235..1841b27d6 100644 --- a/libbb/obscure.c +++ b/libbb/obscure.c | |||
@@ -93,7 +93,7 @@ static const char *obscure_msg(const char *old_p, const char *new_p, const struc | |||
93 | /* Add 2 for each type of characters to the minlen of password */ | 93 | /* Add 2 for each type of characters to the minlen of password */ |
94 | int size = CONFIG_PASSWORD_MINLEN + 8; | 94 | int size = CONFIG_PASSWORD_MINLEN + 8; |
95 | const char *p; | 95 | const char *p; |
96 | char hostname[255]; | 96 | char *hostname; |
97 | 97 | ||
98 | /* size */ | 98 | /* size */ |
99 | if (!new_p || (length = strlen(new_p)) < CONFIG_PASSWORD_MINLEN) | 99 | 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 | |||
108 | return "similar to gecos"; | 108 | return "similar to gecos"; |
109 | } | 109 | } |
110 | /* hostname as-is, as sub-string, reversed, capitalized, doubled */ | 110 | /* hostname as-is, as sub-string, reversed, capitalized, doubled */ |
111 | if (gethostname(hostname, 255) == 0) { | 111 | hostname = safe_gethostname(); |
112 | hostname[254] = '\0'; | 112 | i = string_checker(new_p, hostname); |
113 | if (string_checker(new_p, hostname)) { | 113 | free(hostname); |
114 | return "similar to hostname"; | 114 | if (i) |
115 | } | 115 | return "similar to hostname"; |
116 | } | ||
117 | 116 | ||
118 | /* Should / Must contain a mix of: */ | 117 | /* Should / Must contain a mix of: */ |
119 | for (i = 0; i < length; i++) { | 118 | for (i = 0; i < length; i++) { |
diff --git a/miscutils/devfsd.c b/miscutils/devfsd.c index 52a65bc98..286f00fd8 100644 --- a/miscutils/devfsd.c +++ b/miscutils/devfsd.c | |||
@@ -1133,8 +1133,8 @@ static void signal_handler(int sig) | |||
1133 | static const char *get_variable(const char *variable, void *info) | 1133 | static const char *get_variable(const char *variable, void *info) |
1134 | { | 1134 | { |
1135 | static char sbuf[sizeof(int)*3 + 2]; /* sign and NUL */ | 1135 | static char sbuf[sizeof(int)*3 + 2]; /* sign and NUL */ |
1136 | static char *hostname; | ||
1136 | 1137 | ||
1137 | char hostname[STRING_LENGTH]; | ||
1138 | struct get_variable_info *gv_info = info; | 1138 | struct get_variable_info *gv_info = info; |
1139 | const char *field_names[] = { | 1139 | const char *field_names[] = { |
1140 | "hostname", "mntpt", "devpath", "devname", | 1140 | "hostname", "mntpt", "devpath", "devname", |
@@ -1143,12 +1143,8 @@ static const char *get_variable(const char *variable, void *info) | |||
1143 | }; | 1143 | }; |
1144 | int i; | 1144 | int i; |
1145 | 1145 | ||
1146 | if (gethostname(hostname, STRING_LENGTH - 1) != 0) | 1146 | if (!hostname) |
1147 | /* Here on error we should do exit(RV_SYS_ERROR), instead we do exit(EXIT_FAILURE) */ | 1147 | hostname = safe_gethostname(); |
1148 | error_logger_and_die(LOG_ERR, "gethostname"); | ||
1149 | |||
1150 | hostname[STRING_LENGTH - 1] = '\0'; | ||
1151 | |||
1152 | /* index_in_str_array returns i>=0 */ | 1148 | /* index_in_str_array returns i>=0 */ |
1153 | i = index_in_str_array(field_names, variable); | 1149 | i = index_in_str_array(field_names, variable); |
1154 | 1150 | ||
diff --git a/networking/hostname.c b/networking/hostname.c index 2c224bef9..93cbc961f 100644 --- a/networking/hostname.c +++ b/networking/hostname.c | |||
@@ -24,8 +24,7 @@ static void do_sethostname(char *s, int isfile) | |||
24 | if (sethostname(s, strlen(s)) < 0) { | 24 | if (sethostname(s, strlen(s)) < 0) { |
25 | if (errno == EPERM) | 25 | if (errno == EPERM) |
26 | bb_error_msg_and_die(bb_msg_perm_denied_are_you_root); | 26 | bb_error_msg_and_die(bb_msg_perm_denied_are_you_root); |
27 | else | 27 | bb_perror_msg_and_die("sethostname"); |
28 | bb_perror_msg_and_die("sethostname"); | ||
29 | } | 28 | } |
30 | } else { | 29 | } else { |
31 | f = xfopen(s, "r"); | 30 | f = xfopen(s, "r"); |
@@ -54,27 +53,27 @@ int hostname_main(int argc, char **argv) | |||
54 | OPT_dfis = 0xf, | 53 | OPT_dfis = 0xf, |
55 | }; | 54 | }; |
56 | 55 | ||
57 | char buf[256]; | 56 | char *buf; |
58 | char *hostname_str; | 57 | char *hostname_str; |
59 | 58 | ||
60 | if (argc < 1) | 59 | if (argc < 1) |
61 | bb_show_usage(); | 60 | bb_show_usage(); |
62 | 61 | ||
63 | getopt32(argv, "dfisF:", &hostname_str); | 62 | getopt32(argv, "dfisF:", &hostname_str); |
63 | argv += optind; | ||
64 | buf = safe_gethostname(); | ||
64 | 65 | ||
65 | /* Output in desired format */ | 66 | /* Output in desired format */ |
66 | if (option_mask32 & OPT_dfis) { | 67 | if (option_mask32 & OPT_dfis) { |
67 | struct hostent *hp; | 68 | struct hostent *hp; |
68 | char *p; | 69 | char *p; |
69 | gethostname(buf, sizeof(buf)); | ||
70 | hp = xgethostbyname(buf); | 70 | hp = xgethostbyname(buf); |
71 | p = strchr(hp->h_name, '.'); | 71 | p = strchr(hp->h_name, '.'); |
72 | if (option_mask32 & OPT_f) { | 72 | if (option_mask32 & OPT_f) { |
73 | puts(hp->h_name); | 73 | puts(hp->h_name); |
74 | } else if (option_mask32 & OPT_s) { | 74 | } else if (option_mask32 & OPT_s) { |
75 | if (p != NULL) { | 75 | if (p) |
76 | *p = '\0'; | 76 | *p = '\0'; |
77 | } | ||
78 | puts(hp->h_name); | 77 | puts(hp->h_name); |
79 | } else if (option_mask32 & OPT_d) { | 78 | } else if (option_mask32 & OPT_d) { |
80 | if (p) | 79 | if (p) |
@@ -89,14 +88,15 @@ int hostname_main(int argc, char **argv) | |||
89 | /* Set the hostname */ | 88 | /* Set the hostname */ |
90 | else if (option_mask32 & OPT_F) { | 89 | else if (option_mask32 & OPT_F) { |
91 | do_sethostname(hostname_str, 1); | 90 | do_sethostname(hostname_str, 1); |
92 | } else if (optind < argc) { | 91 | } else if (argv[0]) { |
93 | do_sethostname(argv[optind], 0); | 92 | do_sethostname(argv[0], 0); |
94 | } | 93 | } |
95 | /* Or if all else fails, | 94 | /* Or if all else fails, |
96 | * just print the current hostname */ | 95 | * just print the current hostname */ |
97 | else { | 96 | else { |
98 | gethostname(buf, sizeof(buf)); | ||
99 | puts(buf); | 97 | puts(buf); |
100 | } | 98 | } |
99 | if (ENABLE_FEATURE_CLEAN_UP) | ||
100 | free(buf); | ||
101 | return 0; | 101 | return 0; |
102 | } | 102 | } |
diff --git a/sysklogd/syslogd.c b/sysklogd/syslogd.c index 0dc69d8cc..0d004bc27 100644 --- a/sysklogd/syslogd.c +++ b/sysklogd/syslogd.c | |||
@@ -97,8 +97,8 @@ struct globals { | |||
97 | struct shbuf_ds *shbuf; | 97 | struct shbuf_ds *shbuf; |
98 | #endif | 98 | #endif |
99 | time_t last_log_time; | 99 | time_t last_log_time; |
100 | /* localhost's name */ | 100 | /* localhost's name. We print only first 64 chars */ |
101 | char localHostName[64]; | 101 | char *hostname; |
102 | 102 | ||
103 | /* We recv into recvbuf... */ | 103 | /* We recv into recvbuf... */ |
104 | char recvbuf[MAX_READ]; | 104 | char recvbuf[MAX_READ]; |
@@ -416,7 +416,7 @@ static void timestamp_and_log(int pri, char *msg, int len) | |||
416 | else { | 416 | else { |
417 | char res[20]; | 417 | char res[20]; |
418 | parse_fac_prio_20(pri, res); | 418 | parse_fac_prio_20(pri, res); |
419 | sprintf(G.printbuf, "%s %s %s %s\n", timestamp, G.localHostName, res, msg); | 419 | sprintf(G.printbuf, "%s %.64s %s %s\n", timestamp, G.hostname, res, msg); |
420 | } | 420 | } |
421 | 421 | ||
422 | /* Log message locally (to file or shared mem) */ | 422 | /* Log message locally (to file or shared mem) */ |
@@ -647,11 +647,8 @@ int syslogd_main(int argc, char **argv) | |||
647 | option_mask32 |= OPT_locallog; | 647 | option_mask32 |= OPT_locallog; |
648 | 648 | ||
649 | /* Store away localhost's name before the fork */ | 649 | /* Store away localhost's name before the fork */ |
650 | /* "It is unspecified whether the truncated hostname | 650 | G.hostname = safe_gethostname(); |
651 | * will be null-terminated". We give it (size - 1), | 651 | *strchrnul(G.hostname, '.') = '\0'; |
652 | * thus last byte will be NUL no matter what. */ | ||
653 | gethostname(G.localHostName, sizeof(G.localHostName) - 1); | ||
654 | *strchrnul(G.localHostName, '.') = '\0'; | ||
655 | 652 | ||
656 | if (!(option_mask32 & OPT_nofork)) { | 653 | if (!(option_mask32 & OPT_nofork)) { |
657 | bb_daemonize_or_rexec(DAEMON_CHDIR_ROOT, argv); | 654 | bb_daemonize_or_rexec(DAEMON_CHDIR_ROOT, argv); |