aboutsummaryrefslogtreecommitdiff
path: root/libbb/xfuncs_printf.c
diff options
context:
space:
mode:
authorJames Byrne <james.byrne@origamienergy.com>2019-07-02 11:35:03 +0200
committerDenys Vlasenko <vda.linux@googlemail.com>2019-07-02 11:35:03 +0200
commit6937487be73cd4563b876413277a295a5fe2f32c (patch)
treef16cc9999a7c827891e6ec8d99c699fc791008ee /libbb/xfuncs_printf.c
parentcaecfdc20d450686cd1f7e9b5f650322f894b3c2 (diff)
downloadbusybox-w32-6937487be73cd4563b876413277a295a5fe2f32c.tar.gz
busybox-w32-6937487be73cd4563b876413277a295a5fe2f32c.tar.bz2
busybox-w32-6937487be73cd4563b876413277a295a5fe2f32c.zip
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 <james.byrne@origamienergy.com> Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'libbb/xfuncs_printf.c')
-rw-r--r--libbb/xfuncs_printf.c44
1 files changed, 21 insertions, 23 deletions
diff --git a/libbb/xfuncs_printf.c b/libbb/xfuncs_printf.c
index 6cc60f6c0..93f325c62 100644
--- a/libbb/xfuncs_printf.c
+++ b/libbb/xfuncs_printf.c
@@ -27,7 +27,7 @@
27 27
28void FAST_FUNC bb_die_memory_exhausted(void) 28void FAST_FUNC bb_die_memory_exhausted(void)
29{ 29{
30 bb_error_msg_and_die(bb_msg_memory_exhausted); 30 bb_simple_error_msg_and_die(bb_msg_memory_exhausted);
31} 31}
32 32
33#ifndef DMALLOC 33#ifndef DMALLOC
@@ -40,7 +40,7 @@ void* FAST_FUNC malloc_or_warn(size_t size)
40{ 40{
41 void *ptr = malloc(size); 41 void *ptr = malloc(size);
42 if (ptr == NULL && size != 0) 42 if (ptr == NULL && size != 0)
43 bb_error_msg(bb_msg_memory_exhausted); 43 bb_simple_error_msg(bb_msg_memory_exhausted);
44 return ptr; 44 return ptr;
45} 45}
46 46
@@ -97,7 +97,7 @@ char* FAST_FUNC xstrndup(const char *s, int n)
97 char *t; 97 char *t;
98 98
99 if (ENABLE_DEBUG && s == NULL) 99 if (ENABLE_DEBUG && s == NULL)
100 bb_error_msg_and_die("xstrndup bug"); 100 bb_simple_error_msg_and_die("xstrndup bug");
101 101
102 /* We can just xmalloc(n+1) and strncpy into it, */ 102 /* We can just xmalloc(n+1) and strncpy into it, */
103 /* but think about xstrndup("abc", 10000) wastage! */ 103 /* but think about xstrndup("abc", 10000) wastage! */
@@ -215,13 +215,13 @@ int FAST_FUNC rename_or_warn(const char *oldpath, const char *newpath)
215void FAST_FUNC xpipe(int filedes[2]) 215void FAST_FUNC xpipe(int filedes[2])
216{ 216{
217 if (pipe(filedes)) 217 if (pipe(filedes))
218 bb_perror_msg_and_die("can't create pipe"); 218 bb_simple_perror_msg_and_die("can't create pipe");
219} 219}
220 220
221void FAST_FUNC xdup2(int from, int to) 221void FAST_FUNC xdup2(int from, int to)
222{ 222{
223 if (dup2(from, to) != to) 223 if (dup2(from, to) != to)
224 bb_perror_msg_and_die("can't duplicate file descriptor"); 224 bb_simple_perror_msg_and_die("can't duplicate file descriptor");
225 // " %d to %d", from, to); 225 // " %d to %d", from, to);
226} 226}
227 227
@@ -245,7 +245,7 @@ void FAST_FUNC xwrite(int fd, const void *buf, size_t count)
245 * or some writes succeeded, then we hit an error. 245 * or some writes succeeded, then we hit an error.
246 * In either case, errno is set. 246 * In either case, errno is set.
247 */ 247 */
248 bb_perror_msg_and_die( 248 bb_simple_perror_msg_and_die(
249 size >= 0 ? "short write" : "write error" 249 size >= 0 ? "short write" : "write error"
250 ); 250 );
251 } 251 }
@@ -259,7 +259,7 @@ void FAST_FUNC xwrite_str(int fd, const char *str)
259void FAST_FUNC xclose(int fd) 259void FAST_FUNC xclose(int fd)
260{ 260{
261 if (close(fd)) 261 if (close(fd))
262 bb_perror_msg_and_die("close failed"); 262 bb_simple_perror_msg_and_die("close failed");
263} 263}
264 264
265// Die with an error message if we can't lseek to the right spot. 265// Die with an error message if we can't lseek to the right spot.
@@ -267,9 +267,7 @@ off_t FAST_FUNC xlseek(int fd, off_t offset, int whence)
267{ 267{
268 off_t off = lseek(fd, offset, whence); 268 off_t off = lseek(fd, offset, whence);
269 if (off == (off_t)-1) { 269 if (off == (off_t)-1) {
270 if (whence == SEEK_SET) 270 bb_perror_msg_and_die("lseek(%"OFF_FMT"u, %d)", offset, whence);
271 bb_perror_msg_and_die("lseek(%"OFF_FMT"u)", offset);
272 bb_perror_msg_and_die("lseek");
273 } 271 }
274 return off; 272 return off;
275} 273}
@@ -384,23 +382,23 @@ void FAST_FUNC bb_unsetenv_and_free(char *var)
384// setgid() will fail and we'll _still_be_root_, which is bad.) 382// setgid() will fail and we'll _still_be_root_, which is bad.)
385void FAST_FUNC xsetgid(gid_t gid) 383void FAST_FUNC xsetgid(gid_t gid)
386{ 384{
387 if (setgid(gid)) bb_perror_msg_and_die("setgid"); 385 if (setgid(gid)) bb_simple_perror_msg_and_die("setgid");
388} 386}
389 387
390// Die with an error message if we can't set uid. (See xsetgid() for why.) 388// Die with an error message if we can't set uid. (See xsetgid() for why.)
391void FAST_FUNC xsetuid(uid_t uid) 389void FAST_FUNC xsetuid(uid_t uid)
392{ 390{
393 if (setuid(uid)) bb_perror_msg_and_die("setuid"); 391 if (setuid(uid)) bb_simple_perror_msg_and_die("setuid");
394} 392}
395 393
396void FAST_FUNC xsetegid(gid_t egid) 394void FAST_FUNC xsetegid(gid_t egid)
397{ 395{
398 if (setegid(egid)) bb_perror_msg_and_die("setegid"); 396 if (setegid(egid)) bb_simple_perror_msg_and_die("setegid");
399} 397}
400 398
401void FAST_FUNC xseteuid(uid_t euid) 399void FAST_FUNC xseteuid(uid_t euid)
402{ 400{
403 if (seteuid(euid)) bb_perror_msg_and_die("seteuid"); 401 if (seteuid(euid)) bb_simple_perror_msg_and_die("seteuid");
404} 402}
405 403
406// Die if we can't chdir to a new path. 404// Die if we can't chdir to a new path.
@@ -413,7 +411,7 @@ void FAST_FUNC xchdir(const char *path)
413void FAST_FUNC xfchdir(int fd) 411void FAST_FUNC xfchdir(int fd)
414{ 412{
415 if (fchdir(fd)) 413 if (fchdir(fd))
416 bb_perror_msg_and_die("fchdir"); 414 bb_simple_perror_msg_and_die("fchdir");
417} 415}
418 416
419void FAST_FUNC xchroot(const char *path) 417void FAST_FUNC xchroot(const char *path)
@@ -463,7 +461,7 @@ int FAST_FUNC xsocket(int domain, int type, int protocol)
463IF_FEATURE_IPV6(if (domain == AF_INET6) s = "INET6";) 461IF_FEATURE_IPV6(if (domain == AF_INET6) s = "INET6";)
464 bb_perror_msg_and_die("socket(AF_%s,%d,%d)", s, type, protocol); 462 bb_perror_msg_and_die("socket(AF_%s,%d,%d)", s, type, protocol);
465#else 463#else
466 bb_perror_msg_and_die("socket"); 464 bb_simple_perror_msg_and_die("socket");
467#endif 465#endif
468 } 466 }
469 467
@@ -473,13 +471,13 @@ IF_FEATURE_IPV6(if (domain == AF_INET6) s = "INET6";)
473// Die with an error message if we can't bind a socket to an address. 471// Die with an error message if we can't bind a socket to an address.
474void FAST_FUNC xbind(int sockfd, struct sockaddr *my_addr, socklen_t addrlen) 472void FAST_FUNC xbind(int sockfd, struct sockaddr *my_addr, socklen_t addrlen)
475{ 473{
476 if (bind(sockfd, my_addr, addrlen)) bb_perror_msg_and_die("bind"); 474 if (bind(sockfd, my_addr, addrlen)) bb_simple_perror_msg_and_die("bind");
477} 475}
478 476
479// Die with an error message if we can't listen for connections on a socket. 477// Die with an error message if we can't listen for connections on a socket.
480void FAST_FUNC xlisten(int s, int backlog) 478void FAST_FUNC xlisten(int s, int backlog)
481{ 479{
482 if (listen(s, backlog)) bb_perror_msg_and_die("listen"); 480 if (listen(s, backlog)) bb_simple_perror_msg_and_die("listen");
483} 481}
484 482
485/* Die with an error message if sendto failed. 483/* Die with an error message if sendto failed.
@@ -491,7 +489,7 @@ ssize_t FAST_FUNC xsendto(int s, const void *buf, size_t len, const struct socka
491 if (ret < 0) { 489 if (ret < 0) {
492 if (ENABLE_FEATURE_CLEAN_UP) 490 if (ENABLE_FEATURE_CLEAN_UP)
493 close(s); 491 close(s);
494 bb_perror_msg_and_die("sendto"); 492 bb_simple_perror_msg_and_die("sendto");
495 } 493 }
496 return ret; 494 return ret;
497} 495}
@@ -519,12 +517,12 @@ void FAST_FUNC selinux_or_die(void)
519#if ENABLE_SELINUX 517#if ENABLE_SELINUX
520 int rc = is_selinux_enabled(); 518 int rc = is_selinux_enabled();
521 if (rc == 0) { 519 if (rc == 0) {
522 bb_error_msg_and_die("SELinux is disabled"); 520 bb_simple_error_msg_and_die("SELinux is disabled");
523 } else if (rc < 0) { 521 } else if (rc < 0) {
524 bb_error_msg_and_die("is_selinux_enabled() failed"); 522 bb_simple_error_msg_and_die("is_selinux_enabled() failed");
525 } 523 }
526#else 524#else
527 bb_error_msg_and_die("SELinux support is disabled"); 525 bb_simple_error_msg_and_die("SELinux support is disabled");
528#endif 526#endif
529} 527}
530 528
@@ -675,7 +673,7 @@ pid_t FAST_FUNC xfork(void)
675 pid_t pid; 673 pid_t pid;
676 pid = fork(); 674 pid = fork();
677 if (pid < 0) /* wtf? */ 675 if (pid < 0) /* wtf? */
678 bb_perror_msg_and_die("vfork"+1); 676 bb_simple_perror_msg_and_die("vfork"+1);
679 return pid; 677 return pid;
680} 678}
681#endif 679#endif