display.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // SPDX-License-Identifier: LGPL-2.1-or-later
  2. /*
  3. *
  4. * BlueZ - Bluetooth protocol stack for Linux
  5. *
  6. * Copyright (C) 2011-2014 Intel Corporation
  7. * Copyright (C) 2002-2010 Marcel Holtmann <marcel@holtmann.org>
  8. *
  9. *
  10. */
  11. #ifdef HAVE_CONFIG_H
  12. #include <config.h>
  13. #endif
  14. #define _GNU_SOURCE
  15. #include <stdio.h>
  16. #include <errno.h>
  17. #include <unistd.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <signal.h>
  21. #include <sys/wait.h>
  22. #include <sys/prctl.h>
  23. #include <sys/ioctl.h>
  24. #include <termios.h>
  25. #include "display.h"
  26. static pid_t pager_pid = 0;
  27. int default_pager_num_columns = FALLBACK_TERMINAL_WIDTH;
  28. enum monitor_color setting_monitor_color = COLOR_AUTO;
  29. void set_monitor_color(enum monitor_color color)
  30. {
  31. setting_monitor_color = color;
  32. }
  33. bool use_color(void)
  34. {
  35. static int cached_use_color = -1;
  36. if (setting_monitor_color == COLOR_ALWAYS)
  37. cached_use_color = 1;
  38. else if (setting_monitor_color == COLOR_NEVER)
  39. cached_use_color = 0;
  40. else if (__builtin_expect(!!(cached_use_color < 0), 0))
  41. cached_use_color = isatty(STDOUT_FILENO) > 0 || pager_pid > 0;
  42. return cached_use_color;
  43. }
  44. void set_default_pager_num_columns(int num_columns)
  45. {
  46. default_pager_num_columns = num_columns;
  47. }
  48. int num_columns(void)
  49. {
  50. static int cached_num_columns = -1;
  51. if (__builtin_expect(!!(cached_num_columns < 0), 0)) {
  52. struct winsize ws;
  53. if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) < 0 ||
  54. ws.ws_col == 0)
  55. cached_num_columns = default_pager_num_columns;
  56. else
  57. cached_num_columns = ws.ws_col;
  58. }
  59. return cached_num_columns;
  60. }
  61. static void close_pipe(int p[])
  62. {
  63. if (p[0] >= 0)
  64. close(p[0]);
  65. if (p[1] >= 0)
  66. close(p[1]);
  67. }
  68. static void wait_for_terminate(pid_t pid)
  69. {
  70. siginfo_t dummy;
  71. for (;;) {
  72. memset(&dummy, 0, sizeof(dummy));
  73. if (waitid(P_PID, pid, &dummy, WEXITED) < 0) {
  74. if (errno == EINTR)
  75. continue;
  76. return;
  77. }
  78. return;
  79. }
  80. }
  81. void open_pager(void)
  82. {
  83. const char *pager;
  84. pid_t parent_pid;
  85. int fd[2];
  86. if (pager_pid > 0)
  87. return;
  88. pager = getenv("PAGER");
  89. if (pager) {
  90. if (!*pager || strcmp(pager, "cat") == 0)
  91. return;
  92. }
  93. if (!(isatty(STDOUT_FILENO) > 0))
  94. return;
  95. num_columns();
  96. if (pipe(fd) < 0) {
  97. perror("Failed to create pager pipe");
  98. return;
  99. }
  100. parent_pid = getpid();
  101. pager_pid = fork();
  102. if (pager_pid < 0) {
  103. perror("Failed to fork pager");
  104. close_pipe(fd);
  105. return;
  106. }
  107. if (pager_pid == 0) {
  108. dup2(fd[0], STDIN_FILENO);
  109. close_pipe(fd);
  110. setenv("LESS", "FRSX", 0);
  111. if (prctl(PR_SET_PDEATHSIG, SIGTERM) < 0)
  112. _exit(EXIT_FAILURE);
  113. if (getppid() != parent_pid)
  114. _exit(EXIT_SUCCESS);
  115. if (pager) {
  116. execlp(pager, pager, NULL);
  117. execl("/bin/sh", "sh", "-c", pager, NULL);
  118. }
  119. execlp("pager", "pager", NULL);
  120. execlp("less", "less", NULL);
  121. execlp("more", "more", NULL);
  122. _exit(EXIT_FAILURE);
  123. }
  124. if (dup2(fd[1], STDOUT_FILENO) < 0) {
  125. perror("Failed to duplicate pager pipe");
  126. return;
  127. }
  128. close_pipe(fd);
  129. }
  130. void close_pager(void)
  131. {
  132. if (pager_pid <= 0)
  133. return;
  134. fclose(stdout);
  135. kill(pager_pid, SIGCONT);
  136. wait_for_terminate(pager_pid);
  137. pager_pid = 0;
  138. }