display.c (3322B)
1 /* 2 * 3 * Wireless daemon for Linux 4 * 5 * Copyright (C) 2013-2019 Intel Corporation. All rights reserved. 6 * 7 * This library is free software; you can redistribute it and/or 8 * modify it under the terms of the GNU Lesser General Public 9 * License as published by the Free Software Foundation; either 10 * version 2.1 of the License, or (at your option) any later version. 11 * 12 * This library is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 * Lesser General Public License for more details. 16 * 17 * You should have received a copy of the GNU Lesser General Public 18 * License along with this library; if not, write to the Free Software 19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 20 * 21 */ 22 23 #ifdef HAVE_CONFIG_H 24 #include <config.h> 25 #endif 26 27 #define _GNU_SOURCE 28 #include <stdio.h> 29 #include <errno.h> 30 #include <unistd.h> 31 #include <stdlib.h> 32 #include <string.h> 33 #include <signal.h> 34 #include <sys/wait.h> 35 #include <sys/prctl.h> 36 #include <sys/ioctl.h> 37 #include <termios.h> 38 39 #include "monitor/display.h" 40 41 #define FALLBACK_TERMINAL_WIDTH 80 42 43 static pid_t pager_pid = 0; 44 45 bool use_color(void) 46 { 47 static int cached_use_color = -1; 48 49 if (__builtin_expect(!!(cached_use_color < 0), 0)) 50 cached_use_color = isatty(STDOUT_FILENO) > 0 || pager_pid > 0; 51 52 return !!cached_use_color; 53 } 54 55 unsigned int num_columns(void) 56 { 57 static int cached_num_columns = -1; 58 59 if (__builtin_expect(!!(cached_num_columns < 0), 0)) { 60 struct winsize ws; 61 62 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) < 0 || 63 ws.ws_col == 0) 64 cached_num_columns = FALLBACK_TERMINAL_WIDTH; 65 else 66 cached_num_columns = ws.ws_col; 67 } 68 69 return cached_num_columns; 70 } 71 72 static void close_pipe(int p[]) 73 { 74 if (p[0] >= 0) 75 close(p[0]); 76 if (p[1] >= 0) 77 close(p[1]); 78 } 79 80 static void wait_for_terminate(pid_t pid) 81 { 82 siginfo_t dummy; 83 84 for (;;) { 85 memset(&dummy, 0, sizeof(dummy)); 86 87 if (waitid(P_PID, pid, &dummy, WEXITED) < 0) { 88 if (errno == EINTR) 89 continue; 90 return; 91 } 92 93 return; 94 } 95 } 96 97 void open_pager(void) 98 { 99 const char *pager; 100 pid_t parent_pid; 101 int fd[2]; 102 103 if (pager_pid > 0) 104 return; 105 106 pager = getenv("PAGER"); 107 if (pager) { 108 if (!*pager || strcmp(pager, "cat") == 0) 109 return; 110 } 111 112 if (!(isatty(STDOUT_FILENO) > 0)) 113 return; 114 115 num_columns(); 116 117 if (pipe(fd) < 0) { 118 perror("Failed to create pager pipe"); 119 return; 120 } 121 122 parent_pid = getpid(); 123 124 pager_pid = fork(); 125 if (pager_pid < 0) { 126 perror("Failed to fork pager"); 127 close_pipe(fd); 128 return; 129 } 130 131 if (pager_pid == 0) { 132 dup2(fd[0], STDIN_FILENO); 133 close_pipe(fd); 134 135 setenv("LESS", "FRSX", 0); 136 137 if (prctl(PR_SET_PDEATHSIG, SIGTERM) < 0) 138 _exit(EXIT_FAILURE); 139 140 if (getppid() != parent_pid) 141 _exit(EXIT_SUCCESS); 142 143 if (pager) { 144 execlp(pager, pager, NULL); 145 execl("/bin/sh", "sh", "-c", pager, NULL); 146 } 147 148 execlp("pager", "pager", NULL); 149 execlp("less", "less", NULL); 150 execlp("more", "more", NULL); 151 152 _exit(EXIT_FAILURE); 153 } 154 155 if (dup2(fd[1], STDOUT_FILENO) < 0) { 156 perror("Failed to duplicate pager pipe"); 157 return; 158 } 159 160 close_pipe(fd); 161 } 162 163 void close_pager(void) 164 { 165 if (pager_pid <= 0) 166 return; 167 168 fclose(stdout); 169 kill(pager_pid, SIGCONT); 170 wait_for_terminate(pager_pid); 171 pager_pid = 0; 172 }