Path: blob/21.2-virgl/src/freedreno/decode/pager.c
4565 views
/*1* Copyright (c) 2018 Rob Clark <[email protected]>2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,19* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE20* SOFTWARE.21*/2223#include <errno.h>24#include <signal.h>25#include <stdbool.h>26#include <stdio.h>27#include <stdlib.h>28#include <string.h>29#include <unistd.h>30#include <sys/types.h>31#include <sys/wait.h>3233#include "pager.h"3435static pid_t pager_pid;3637static void38pager_death(int n)39{40exit(0);41}4243void44pager_open(void)45{46int fd[2];4748if (pipe(fd) < 0) {49fprintf(stderr, "Failed to create pager pipe: %m\n");50exit(-1);51}5253pager_pid = fork();54if (pager_pid < 0) {55fprintf(stderr, "Failed to fork pager: %m\n");56exit(-1);57}5859if (pager_pid == 0) {60const char *less_opts;6162dup2(fd[0], STDIN_FILENO);63close(fd[0]);64close(fd[1]);6566less_opts = "FRSMKX";67setenv("LESS", less_opts, 1);6869execlp("less", "less", NULL);7071} else {72/* we want to kill the parent process when pager exits: */73signal(SIGCHLD, pager_death);74dup2(fd[1], STDOUT_FILENO);75close(fd[0]);76close(fd[1]);77}78}7980int81pager_close(void)82{83siginfo_t status;8485close(STDOUT_FILENO);8687while (true) {88memset(&status, 0, sizeof(status));89if (waitid(P_PID, pager_pid, &status, WEXITED) < 0) {90if (errno == EINTR)91continue;92return -errno;93}9495return 0;96}97}9899100