From 0b9858f28ff0681d0fb08217aed6de710ae93902 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Tue, 10 Dec 2024 01:13:19 +0100 Subject: Fix capture options not available on nvidia x11 --- src/Process.cpp | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) (limited to 'src/Process.cpp') diff --git a/src/Process.cpp b/src/Process.cpp index e5886c0..a8e5fb5 100644 --- a/src/Process.cpp +++ b/src/Process.cpp @@ -92,6 +92,48 @@ namespace gsr { } } + int exec_program_get_stdout(const char **args, std::string &result) { + result.clear(); + int read_fd = -1; + pid_t process_id = exec_program(args, &read_fd); + if(process_id == -1) + return -1; + + int exit_status = 0; + char buffer[8192]; + for(;;) { + ssize_t bytes_read = read(read_fd, buffer, sizeof(buffer)); + if(bytes_read == 0) { + break; + } else if(bytes_read == -1) { + fprintf(stderr, "Failed to read from pipe to program %s, error: %s\n", args[0], strerror(errno)); + exit_status = -1; + break; + } + + buffer[bytes_read] = '\0'; + result.append(buffer, bytes_read); + } + + if(exit_status != 0) + kill(process_id, SIGKILL); + + int status = 0; + if(waitpid(process_id, &status, 0) == -1) { + perror("waitpid failed"); + exit_status = -1; + } + + if(!WIFEXITED(status)) + exit_status = -1; + + if(exit_status == 0) + exit_status = WEXITSTATUS(status); + + close(read_fd); + return exit_status; + } + bool read_cmdline_arg0(const char *filepath, char *output_buffer) { output_buffer[0] = '\0'; -- cgit v1.2.3