0003-cmdline-use-freopen-to-reopen-standard-streams.patch 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. From 7c74ad9c349e381decc84c218112ea8e7bcc0b9c Mon Sep 17 00:00:00 2001
  2. From: Simon Rowe <simon.rowe@nutanix.com>
  3. Date: Thu, 23 Mar 2023 09:57:59 +0000
  4. Subject: [PATCH] cmdline: use freopen() to reopen standard streams
  5. In glibc stdin, stdout & stderr are variables that can be assigned to
  6. (https://www.gnu.org/software/libc/manual/html_node/Standard-Streams.html)
  7. however this not necessarily true of other C libraries.
  8. The gentoo musl porting notes
  9. (https://wiki.gentoo.org/wiki/Musl_porting_notes)
  10. recommend the substitution of
  11. stdX = fopen(...)
  12. with
  13. freopen(..., stdX)
  14. Taken from: https://github.com/gentoo/gentoo/blob/master/sys-fs/lvm2/files/lvm2-2.03.14-r1-fopen-to-freopen.patch
  15. Signed-off-by: Simon Rowe <simon.rowe@nutanix.com>
  16. ---
  17. tools/lvmcmdline.c | 12 ++++++++++++
  18. 1 file changed, 12 insertions(+)
  19. diff --git a/tools/lvmcmdline.c b/tools/lvmcmdline.c
  20. index 1e12bedca..534368575 100644
  21. --- a/tools/lvmcmdline.c
  22. +++ b/tools/lvmcmdline.c
  23. @@ -3384,7 +3384,11 @@ static int _check_standard_fds(void)
  24. int err = is_valid_fd(STDERR_FILENO);
  25. if (!is_valid_fd(STDIN_FILENO) &&
  26. +#ifdef __GLIBC__
  27. !(stdin = fopen(_PATH_DEVNULL, "r"))) {
  28. +#else
  29. + !freopen(_PATH_DEVNULL, "r", stdin)) {
  30. +#endif
  31. if (err)
  32. perror("stdin stream open");
  33. else
  34. @@ -3394,7 +3398,11 @@ static int _check_standard_fds(void)
  35. }
  36. if (!is_valid_fd(STDOUT_FILENO) &&
  37. +#ifdef __GLIBC__
  38. !(stdout = fopen(_PATH_DEVNULL, "w"))) {
  39. +#else
  40. + !freopen(_PATH_DEVNULL, "w", stdout)) {
  41. +#endif
  42. if (err)
  43. perror("stdout stream open");
  44. /* else no stdout */
  45. @@ -3402,7 +3410,11 @@ static int _check_standard_fds(void)
  46. }
  47. if (!is_valid_fd(STDERR_FILENO) &&
  48. +#ifdef __GLIBC__
  49. !(stderr = fopen(_PATH_DEVNULL, "w"))) {
  50. +#else
  51. + !freopen(_PATH_DEVNULL, "w", stderr)) {
  52. +#endif
  53. printf("stderr stream open: %s\n",
  54. strerror(errno));
  55. return 0;
  56. --
  57. 2.22.3