uv_stdio_fix.cc 1.1 KB

12345678910111213141516171819202122232425262728
  1. // Copyright (c) 2022 Slack Technologies, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #include "shell/app/uv_stdio_fix.h"
  5. #include <errno.h>
  6. #include <sys/stat.h>
  7. #include <unistd.h>
  8. #include <cstdio>
  9. #include <tuple>
  10. void FixStdioStreams() {
  11. // libuv may mark stdin/stdout/stderr as close-on-exec, which interferes
  12. // with chromium's subprocess spawning. As a workaround, we detect if these
  13. // streams are closed on startup, and reopen them as /dev/null if necessary.
  14. // Otherwise, an unrelated file descriptor will be assigned as stdout/stderr
  15. // which may cause various errors when attempting to write to them.
  16. //
  17. // For details see https://github.com/libuv/libuv/issues/2062
  18. struct stat st;
  19. if (fstat(STDIN_FILENO, &st) < 0 && errno == EBADF)
  20. std::ignore = freopen("/dev/null", "r", stdin);
  21. if (fstat(STDOUT_FILENO, &st) < 0 && errno == EBADF)
  22. std::ignore = freopen("/dev/null", "w", stdout);
  23. if (fstat(STDERR_FILENO, &st) < 0 && errno == EBADF)
  24. std::ignore = freopen("/dev/null", "w", stderr);
  25. }