[BusyBox] [RFC] enforce using functions from libbb

Bernhard Fischer rep.nop at aon.at
Mon Aug 15 13:49:20 UTC 2005


Hi,

libbb provides a number of wrappers for commonly used functions.
The attached proposed patch tries to make developers aware to use the
functions provided by libbb.
It's config option is located in the "Debugging" section of menuconfig.

The intention of this patch is to aid folks in porting new applets and
to encourage people to reuse existing infrastructure.
Furthermore, docs/contributing.txt in section "Janitorial Work" reads:
Replace any "naked" calls [...] with the x* equivalents found in libbb
[...].


Theory of operation:

A file include/libbb_overrides.h is generated for the overrides enlisted
in libbb/Makefile.in.
For each function in the overrides-list, a non-existing function named
"please_use_xyz" is defined, -Werror-implicit-function-declaration is
set in the toplevel Makefile.


This results in verbose messages whenever a function is called directly
instead of the wrapper provided by libbb.

busybox/applets/applets.c: In function 'bb_show_usage':
busybox/applets/applets.c:109: error: implicit declaration of function 'please_use_bb_fprintf'
make: *** [busybox/applets/applets.o] Error 1


Comments?
-------------- next part --------------
diff -X excl -rduNp busybox.oorig/archival/dpkg.c busybox/archival/dpkg.c
--- busybox.oorig/archival/dpkg.c	2005-07-28 18:51:57.000000000 +0200
+++ busybox/archival/dpkg.c	2005-08-15 15:16:10.000000000 +0200
@@ -1299,7 +1299,7 @@ static char **all_control_list(const cha
 	char **remove_files;
 
 	/* Create a list of all /var/lib/dpkg/info/<package> files */
-	remove_files = malloc(sizeof(all_control_files));
+	remove_files = xmalloc(sizeof(all_control_files));
 	while (all_control_files[i]) {
 		remove_files[i] = xmalloc(strlen(package_name) + strlen(all_control_files[i]) + 21);
 		sprintf(remove_files[i], "/var/lib/dpkg/info/%s.%s", package_name, all_control_files[i]);
diff -X excl -rduNp busybox.oorig/archival/libunarchive/decompress_unzip.c busybox/archival/libunarchive/decompress_unzip.c
--- busybox.oorig/archival/libunarchive/decompress_unzip.c	2005-07-28 18:51:57.000000000 +0200
+++ busybox/archival/libunarchive/decompress_unzip.c	2005-08-15 15:16:10.000000000 +0200
@@ -176,7 +176,7 @@ static void make_gunzip_crc_table(void)
 
 	/* initial shift register value */
 	gunzip_crc = 0xffffffffL;
-	gunzip_crc_table = (unsigned int *) malloc(256 * sizeof(unsigned int));
+	gunzip_crc_table = (unsigned int *) xmalloc(256 * sizeof(unsigned int));
 
 	/* Compute and print table of CRC's, five per line */
 	for (i = 0; i < 256; i++) {
diff -X excl -rduNp busybox.oorig/archival/rpm.c busybox/archival/rpm.c
--- busybox.oorig/archival/rpm.c	2005-04-24 21:31:02.000000000 +0200
+++ busybox/archival/rpm.c	2005-08-15 15:16:10.000000000 +0200
@@ -243,7 +243,7 @@ rpm_index **rpm_gettags(int fd, int *num
 		storepos = lseek(fd,0,SEEK_CUR) + header.entries * 16;
 
 		while (header.entries--) {
-			tmpindex = tags[tagindex++] = malloc(sizeof(rpm_index));
+			tmpindex = tags[tagindex++] = xmalloc(sizeof(rpm_index));
 			read(fd, tmpindex, sizeof(rpm_index));
 			tmpindex->tag = ntohl(tmpindex->tag); tmpindex->type = ntohl(tmpindex->type); tmpindex->count = ntohl(tmpindex->count);
 			tmpindex->offset = storepos + ntohl(tmpindex->offset);
diff -X excl -rduNp busybox.oorig/include/busybox.h busybox/include/busybox.h
--- busybox.oorig/include/busybox.h	2005-07-28 18:51:59.000000000 +0200
+++ busybox/include/busybox.h	2005-08-15 15:16:10.000000000 +0200
@@ -87,23 +87,6 @@ extern const struct BB_applet applets[];
 #include "applets.h"
 #undef PROTOTYPES
 
-#ifdef CONFIG_FEATURE_BUFFERS_GO_ON_STACK
-#define RESERVE_CONFIG_BUFFER(buffer,len)           char buffer[len]
-#define RESERVE_CONFIG_UBUFFER(buffer,len) unsigned char buffer[len]
-#define RELEASE_CONFIG_BUFFER(buffer)      ((void)0)
-#else
-#ifdef CONFIG_FEATURE_BUFFERS_GO_IN_BSS
-#define RESERVE_CONFIG_BUFFER(buffer,len)  static          char buffer[len]
-#define RESERVE_CONFIG_UBUFFER(buffer,len) static unsigned char buffer[len]
-#define RELEASE_CONFIG_BUFFER(buffer)      ((void)0)
-#else
-#define RESERVE_CONFIG_BUFFER(buffer,len)           char *buffer=xmalloc(len)
-#define RESERVE_CONFIG_UBUFFER(buffer,len) unsigned char *buffer=xmalloc(len)
-#define RELEASE_CONFIG_BUFFER(buffer)      free (buffer)
-#endif
-#endif
-
-
 #ifndef RB_POWER_OFF
 /* Stop system and switch power off if possible.  */
 #define RB_POWER_OFF   0x4321fedc
@@ -118,4 +101,10 @@ extern const struct BB_applet applets[];
 #define  PATH_MAX         256
 #endif
 
+#ifdef CONFIG_PORT_NEW_APPLET
+/* undefine the functions from libbb so the xfuncs get used.
+ * see docs/contributing.txt */
+#include BUSYBOX_H_POST_INCLUDE
+#endif
+
 #endif							/* _BB_INTERNAL_H_ */
diff -X excl -rduNp busybox.oorig/include/libbb.h busybox/include/libbb.h
--- busybox.oorig/include/libbb.h	2005-08-12 00:09:45.000000000 +0200
+++ busybox/include/libbb.h	2005-08-15 15:16:10.000000000 +0200
@@ -46,6 +46,22 @@
 #include <selinux/selinux.h>  
 #endif
 
+#ifdef CONFIG_FEATURE_BUFFERS_GO_ON_STACK
+#define RESERVE_CONFIG_BUFFER(buffer,len)           char buffer[len]
+#define RESERVE_CONFIG_UBUFFER(buffer,len) unsigned char buffer[len]
+#define RELEASE_CONFIG_BUFFER(buffer)      ((void)0)
+#else
+#ifdef CONFIG_FEATURE_BUFFERS_GO_IN_BSS
+#define RESERVE_CONFIG_BUFFER(buffer,len)  static          char buffer[len]
+#define RESERVE_CONFIG_UBUFFER(buffer,len) static unsigned char buffer[len]
+#define RELEASE_CONFIG_BUFFER(buffer)      ((void)0)
+#else
+#define RESERVE_CONFIG_BUFFER(buffer,len)           char *buffer=xmalloc(len)
+#define RESERVE_CONFIG_UBUFFER(buffer,len) unsigned char *buffer=xmalloc(len)
+#define RELEASE_CONFIG_BUFFER(buffer)      free (buffer)
+#endif
+#endif
+
 #include "pwd_.h"
 #include "grp_.h"
 #ifdef CONFIG_FEATURE_SHADOWPASSWDS
diff -X excl -rduNp busybox.oorig/libbb/copyfd.c busybox/libbb/copyfd.c
--- busybox.oorig/libbb/copyfd.c	2005-04-27 19:08:01.000000000 +0200
+++ busybox/libbb/copyfd.c	2005-08-15 15:16:10.000000000 +0200
@@ -24,10 +24,8 @@
 #include <string.h>
 #include <unistd.h>
 
-#include "busybox.h"
 #include "libbb.h"
 
-
 #if BUFSIZ < 4096
 #undef BUFSIZ
 #define BUFSIZ 4096
diff -X excl -rduNp busybox.oorig/libbb/copy_file.c busybox/libbb/copy_file.c
--- busybox.oorig/libbb/copy_file.c	2005-07-28 18:51:59.000000000 +0200
+++ busybox/libbb/copy_file.c	2005-08-15 15:16:10.000000000 +0200
@@ -30,7 +30,7 @@
 #include <stdlib.h>
 #include <string.h>
 
-#include "busybox.h"
+#include "libbb.h"
 
 int copy_file(const char *source, const char *dest, int flags)
 {
diff -X excl -rduNp busybox.oorig/libbb/get_terminal_width_height.c busybox/libbb/get_terminal_width_height.c
--- busybox.oorig/libbb/get_terminal_width_height.c	2005-04-01 22:05:47.000000000 +0200
+++ busybox/libbb/get_terminal_width_height.c	2005-08-15 15:16:10.000000000 +0200
@@ -26,7 +26,7 @@
 #include <unistd.h>
 #include <termios.h>
 #include <sys/ioctl.h>
-#include "busybox.h"
+#include "libbb.h"
 
 /* It is perfectly ok to pass in a NULL for either width or for
  * height, in which case that value will not be set.  It is also
diff -X excl -rduNp busybox.oorig/libbb/hash_fd.c busybox/libbb/hash_fd.c
--- busybox.oorig/libbb/hash_fd.c	2005-04-16 22:13:24.000000000 +0200
+++ busybox/libbb/hash_fd.c	2005-08-15 15:16:10.000000000 +0200
@@ -30,7 +30,7 @@
 #include <string.h>
 #include <unistd.h>
 
-#include "busybox.h"
+#include "libbb.h"
 
 
 #ifdef CONFIG_SHA1SUM
diff -X excl -rduNp busybox.oorig/libbb/Makefile.in busybox/libbb/Makefile.in
--- busybox.oorig/libbb/Makefile.in	2005-08-09 21:42:39.000000000 +0200
+++ busybox/libbb/Makefile.in	2005-08-15 15:16:10.000000000 +0200
@@ -89,7 +89,7 @@ $(LIBBB_DIR)$(LIBBB_AR): $(LIBBB_OBJS) $
 		$(LIBBB_MOBJS2) $(LIBBB_MOBJS3) $(LIBBB_MOBJS4)
 
 $(LIBBB_DIR)%.o: $(srcdir)/%.c
-	$(CC) $(CFLAGS) $(EXTRA_CFLAGS) -c -o $@ $<
+	$(CC) $(CFLAGS) $(EXTRA_CFLAGS) -DL_$(*F) -c -o $@ $<
 
 $(LIBBB_MOBJS0): $(LIBBB_MSRC0)
 	$(CC) $(CFLAGS) $(EXTRA_CFLAGS) -DL_$(notdir $*) -c $< -o $@
@@ -106,3 +106,38 @@ $(LIBBB_MOBJS3): $(LIBBB_MSRC3)
 $(LIBBB_MOBJS4): $(LIBBB_MSRC4)
 	$(CC) $(CFLAGS) $(EXTRA_CFLAGS) -DL_$(notdir $*) -c $< -o $@
 
+ifeq ($(strip $(CONFIG_PORT_NEW_APPLET)),y)
+# OBJ1
+LIBBB_X_FUNCTIONS:= malloc realloc calloc 
+LIBBB_FUNCTIONS:= strlen
+LIBBB_BB_X_FUNCTIONS:= strdup strndup fopen open read ferror 
+# OBJ2
+LIBBB_BB_FUNCTIONS:= vfprintf fprintf printf
+# OBJ4
+LIBBB_SAFE_FUNCTIONS := strtoi strtod strtol
+# OBJ
+LIBBB_MY_FUNCTIONS:= getgrgid getgrnam getpwnam getpwuid
+LIBBB__FUNCTIONS:= asprintf
+
+$(BUSYBOX_H_POST_INCLUDE):
+	@[ -f $(top_builddir)/include/$(BUSYBOX_H_POST_INCLUDE) ] || \
+	(for f in $(LIBBB_X_FUNCTIONS); do \
+	  echo -e "#ifndef L_x$$f\n#undef $$f\n#define $$f please_use_x$$f\n#endif" ; \
+	done ; \
+	for f in $(LIBBB_FUNCTIONS); do \
+	  echo -e "#ifndef L_$$f\n#undef $$f\n#define $$f please_use_bb_$$f\n#endif" ; \
+	done ; \
+	for f in $(LIBBB_BB_X_FUNCTIONS); do \
+	  echo -e "#ifndef L_x$$f\n#undef $$f\n#define $$f please_use_bb_x$$f\n#endif" ; \
+	done ; \
+	for f in $(LIBBB_BB_FUNCTIONS); do \
+	  echo -e "#ifndef L_bb_$$f\n#undef $$f\n#define $$f please_use_bb_$$f\n#endif" ; \
+	done ; \
+	for f in $(LIBBB_MY_FUNCTIONS); do \
+	  echo -e "#ifndef L_$$f\n#undef $$f\n#define $$f please_use_my_$$f\n#endif" ; \
+	done ; \
+	for f in $(LIBBB__FUNCTIONS); do \
+	  echo -e "#ifndef L_$$f\n#undef $$f\n#define $$f please_use_$$f\n#endif" ; \
+	done ;) > $(top_builddir)/include/$(BUSYBOX_H_POST_INCLUDE)
+endif
+
diff -X excl -rduNp busybox.oorig/libbb/messages.c busybox/libbb/messages.c
--- busybox.oorig/libbb/messages.c	2005-04-16 21:35:20.000000000 +0200
+++ busybox/libbb/messages.c	2005-08-15 15:16:10.000000000 +0200
@@ -18,8 +18,8 @@
  *
  */
 
-#include "busybox.h"
 #include "libbb.h"
+#include "busybox.h"
 
 #ifdef L_full_version
 	const char * const bb_msg_full_version = BB_BANNER " multi-call binary";
diff -X excl -rduNp busybox.oorig/libbb/xgetlarg.c busybox/libbb/xgetlarg.c
--- busybox.oorig/libbb/xgetlarg.c	2005-04-01 22:05:47.000000000 +0200
+++ busybox/libbb/xgetlarg.c	2005-08-15 15:16:10.000000000 +0200
@@ -11,7 +11,7 @@
 #include <assert.h>
 #include <ctype.h>
 
-#include "busybox.h"
+#include "libbb.h"
 
 extern long bb_xgetlarg(const char *arg, int base, long lower, long upper)
 {
@@ -22,7 +22,7 @@ extern long bb_xgetlarg(const char *arg,
 	assert(arg!=NULL);
 
 	/* Don't allow leading whitespace. */
-	if ((isspace)(*arg)) {	/* Use an actual funciton call for minimal size. */
+	if ((isspace)(*arg)) {	/* Use an actual function call for minimal size. */
 		bb_show_usage();
 	}
 
diff -X excl -rduNp busybox.oorig/Makefile busybox/Makefile
--- busybox.oorig/Makefile	2005-08-12 00:09:45.000000000 +0200
+++ busybox/Makefile	2005-08-15 15:16:59.000000000 +0200
@@ -39,10 +39,10 @@ vpath %/Config.in $(srctree)
 
 include $(top_builddir)/Rules.mak
 
-DIRS:=applets archival archival/libunarchive coreutils console-tools \
+DIRS:=libbb applets archival archival/libunarchive coreutils console-tools \
 	debianutils editors findutils init miscutils modutils networking \
 	networking/libiproute networking/udhcp procps loginutils shell \
-	sysklogd util-linux e2fsprogs libpwdgrp coreutils/libcoreutils libbb
+	sysklogd util-linux e2fsprogs libpwdgrp coreutils/libcoreutils
 
 SRC_DIRS:=$(patsubst %,$(top_srcdir)/%,$(DIRS))
 
@@ -122,7 +122,7 @@ $(ALL_MAKEFILES): %/Makefile: $(top_srcd
 include $(patsubst %,%/Makefile.in, $(SRC_DIRS))
 -include $(top_builddir)/.depend
 
-busybox: $(ALL_MAKEFILES) .depend include/bb_config.h $(libraries-y)
+busybox: $(ALL_MAKEFILES) .depend include/bb_config.h $(BUSYBOX_H_POST_INCLUDE) $(libraries-y)
 	$(CC) $(LDFLAGS) -o $@ -Wl,--start-group $(libraries-y) $(LIBRARIES) -Wl,--end-group
 	$(STRIPCMD) $@
 
diff -X excl -rduNp busybox.oorig/Rules.mak busybox/Rules.mak
--- busybox.oorig/Rules.mak	2005-08-08 19:38:07.000000000 +0200
+++ busybox/Rules.mak	2005-08-15 15:16:10.000000000 +0200
@@ -180,6 +180,13 @@ ifeq ($(strip $(PREFIX)),)
     PREFIX:=`pwd`/_install
 endif
 
+ifeq ($(strip $(CONFIG_PORT_NEW_APPLET)),y)
+    BUSYBOX_H_POST_INCLUDE:=libbb_overrides.h
+    CFLAGS += -DBUSYBOX_H_POST_INCLUDE='"$(BUSYBOX_H_POST_INCLUDE)"'
+    CFLAGS += -Werror-implicit-function-declaration
+endif
+
+
 # Additional complications due to support for pristine source dir.
 # Include files in the build directory should take precedence over
 # the copy in BB_SRC_DIR, both during the compilation phase and the
diff -X excl -rduNp busybox.oorig/sysdeps/linux/Config.in busybox/sysdeps/linux/Config.in
--- busybox.oorig/sysdeps/linux/Config.in	2005-07-03 12:58:36.000000000 +0200
+++ busybox/sysdeps/linux/Config.in	2005-08-15 15:16:10.000000000 +0200
@@ -297,5 +297,13 @@ config CONFIG_EFENCE
 
 endchoice
 
+config CONFIG_PORT_NEW_APPLET
+	bool "Enforce use of libbb functions"
+	default n
+	help
+	  Say Y here if you port new applets to busybox. The functions
+	  provided by libbb should be used in all applets, this config
+	  options produces errors whenever one of the replaced symbols
+	  is used directly.
 
 endmenu


More information about the busybox mailing list