From eb0c2bbbaf0722103124a589e3dfe952c2664cbb Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Mon, 25 Jan 2021 13:57:08 +0000 Subject: tls: avoid unnecessary changes to POSIX build, part 2 On reflection, the previous commit may have been ill-advised. There are many calls to open_read_close() and most shouldn't be able to access special devices. (Though in practice only a few are enabled in busybox-w32.) Nonetheless, I've implemented a new mechanism which uses the macro MINGW_SPECIAL() to mark calls to functions that are allowed to access special devices. An unrelated change is to avoid compiling fputs_stdout() in coreutils/printf.c for the POSIX build. --- coreutils/dd.c | 6 ++---- coreutils/printf.c | 2 ++ coreutils/shred.c | 8 ++------ include/libbb.h | 3 +++ include/mingw.h | 4 ++++ libbb/read.c | 7 ------- networking/tls.c | 2 +- win32/mingw.c | 9 +++++++++ 8 files changed, 23 insertions(+), 18 deletions(-) diff --git a/coreutils/dd.c b/coreutils/dd.c index c150ef5bc..bd799aa2b 100644 --- a/coreutils/dd.c +++ b/coreutils/dd.c @@ -505,10 +505,8 @@ int dd_main(int argc UNUSED_PARAM, char **argv) if (G.flags & FLAG_IDIRECT) iflag |= O_DIRECT; #endif -#if !ENABLE_PLATFORM_MINGW32 - xmove_fd(xopen(infile, iflag), ifd); -#else - xmove_fd(mingw_xopen(infile, iflag), ifd); + xmove_fd(MINGW_SPECIAL(xopen)(infile, iflag), ifd); +#if ENABLE_PLATFORM_MINGW32 update_dev_fd(get_dev_type(infile), ifd); #endif } else { diff --git a/coreutils/printf.c b/coreutils/printf.c index aabc51e0d..d1d22f39c 100644 --- a/coreutils/printf.c +++ b/coreutils/printf.c @@ -151,10 +151,12 @@ static double my_xstrtod(const char *arg) return result; } +#if ENABLE_PLATFORM_MINGW32 static int fputs_stdout(const char *s) { return fputs(s, stdout); } +#endif /* Handles %b; return 1 if output is to be short-circuited by \c */ static int print_esc_string(const char *str) diff --git a/coreutils/shred.c b/coreutils/shred.c index 86d4b66b4..0b11b9491 100644 --- a/coreutils/shred.c +++ b/coreutils/shred.c @@ -38,10 +38,6 @@ #include "libbb.h" -#if ENABLE_PLATFORM_MINGW32 -#define xopen mingw_xopen -#endif - int shred_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; int shred_main(int argc UNUSED_PARAM, char **argv) { @@ -61,9 +57,9 @@ int shred_main(int argc UNUSED_PARAM, char **argv) opt = getopt32(argv, "fuzn:+vx", &num_iter); argv += optind; - zero_fd = xopen("/dev/zero", O_RDONLY); + zero_fd = MINGW_SPECIAL(xopen)("/dev/zero", O_RDONLY); if (num_iter != 0) - rand_fd = xopen("/dev/urandom", O_RDONLY); + rand_fd = MINGW_SPECIAL(xopen)("/dev/urandom", O_RDONLY); if (!*argv) bb_show_usage(); diff --git a/include/libbb.h b/include/libbb.h index 3d6a6a0cf..a7f32e21e 100644 --- a/include/libbb.h +++ b/include/libbb.h @@ -202,6 +202,9 @@ int klogctl(int type, char *b, int len); #if ENABLE_PLATFORM_MINGW32 # include "mingw.h" +# define MINGW_SPECIAL(a) mingw_ ## a +#else +# define MINGW_SPECIAL(a) a #endif /* Busybox does not use threads, we can speed up stdio. */ diff --git a/include/mingw.h b/include/mingw.h index d1e638231..a1ba5f5af 100644 --- a/include/mingw.h +++ b/include/mingw.h @@ -429,7 +429,11 @@ enum {DEV_NULL, DEV_ZERO, DEV_URANDOM, NOT_DEVICE = -1}; int get_dev_type(const char *filename); void update_dev_fd(int dev, int fd); int mingw_open (const char *filename, int oflags, ...); + +/* functions which add O_SPECIAL to open(2) to allow access to devices */ int mingw_xopen(const char *filename, int oflags); +ssize_t mingw_open_read_close(const char *fn, void *buf, size_t size) FAST_FUNC; + ssize_t mingw_read(int fd, void *buf, size_t count); int mingw_close(int fd); int pipe(int filedes[2]); diff --git a/libbb/read.c b/libbb/read.c index 2e4317cd5..a342506a8 100644 --- a/libbb/read.c +++ b/libbb/read.c @@ -73,14 +73,7 @@ ssize_t FAST_FUNC read_close(int fd, void *buf, size_t size) ssize_t FAST_FUNC open_read_close(const char *filename, void *buf, size_t size) { -#if !ENABLE_PLATFORM_MINGW32 int fd = open(filename, O_RDONLY); -#else - int fd, flag; - - flag = O_RDONLY | (get_dev_type(filename) == DEV_URANDOM ? O_SPECIAL : 0); - fd = mingw_open(filename, flag); -#endif if (fd < 0) return fd; return read_close(fd, buf, size); diff --git a/networking/tls.c b/networking/tls.c index e34acd69f..b05afe1c9 100644 --- a/networking/tls.c +++ b/networking/tls.c @@ -349,7 +349,7 @@ static void dump_tls_record(const void *vp, int len) void FAST_FUNC tls_get_random(void *buf, unsigned len) { - if (len != open_read_close("/dev/urandom", buf, len)) + if (len != MINGW_SPECIAL(open_read_close)("/dev/urandom", buf, len)) xfunc_die(); } diff --git a/win32/mingw.c b/win32/mingw.c index ed0989be2..474d9cdc6 100644 --- a/win32/mingw.c +++ b/win32/mingw.c @@ -229,6 +229,15 @@ int mingw_xopen(const char *pathname, int flags) return ret; } +ssize_t FAST_FUNC mingw_open_read_close(const char *fn, void *buf, size_t size) +{ + /* allow use of special devices */ + int fd = mingw_open(fn, O_RDONLY|O_SPECIAL); + if (fd < 0) + return fd; + return read_close(fd, buf, size); +} + #undef fopen FILE *mingw_fopen (const char *filename, const char *otype) { -- cgit v1.2.3-55-g6feb