1 /* 2 * Run a program using the supplied arguments. 3 * 4 * Copyright (C) 2024 Paul Boddie <paul@boddie.org.uk> 5 * 6 * This program is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU General Public License as 8 * published by the Free Software Foundation; either version 2 of 9 * the License, or (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, 19 * Boston, MA 02110-1301, USA 20 */ 21 22 #include <stdio.h> 23 #include <sys/types.h> 24 #include <errno.h> 25 #include <port.h> 26 27 28 29 /* NOTE: To be provided via a header. */ 30 31 extern pid_t popenv(int, const char *[], FILE **input, FILE **output, FILE **error); 32 33 34 35 /* Transfer size for communication. */ 36 37 static const offset_t TO_TRANSFER = 1024; 38 39 /* Transfer data between streams. */ 40 41 static void transfer(FILE *input, FILE *output) 42 { 43 char buffer[TO_TRANSFER]; 44 size_t nread; 45 46 while ((nread = fread(buffer, sizeof(char), TO_TRANSFER, input))) 47 { 48 if (!fwrite(buffer, sizeof(char), nread, output)) 49 break; 50 } 51 } 52 53 54 55 /* List objects in the filesystem image. */ 56 57 int main(int argc, char *argv[]) 58 { 59 FILE *input, *output, *error; 60 pid_t pid; 61 port_t port; 62 event_source_t source; 63 64 if (argc < 2) 65 { 66 printf("Usage: test_popenv <program> <argument>...\n"); 67 return 1; 68 } 69 70 printf("Running: %s\n", argv[1]); 71 72 pid = popenv(argc - 1, (const char **) argv + 1, &input, &output, &error); 73 74 if (pid == -1) 75 { 76 printf("Error: %d\n", errno); 77 return 1; 78 } 79 80 printf("Process: %d\n", pid); 81 82 /* Monitor the streams and the process. */ 83 84 port = port_create(); 85 86 port_subscribe(port, port_process_source(pid), PORT_PROCESS_ALL); 87 88 if (stdin != NULL) 89 port_subscribe(port, port_file_source(fileno(stdin)), PORT_INPUT | PORT_CLOSE); 90 91 port_subscribe(port, port_file_source(fileno(output)), PORT_INPUT | PORT_CLOSE); 92 93 if (error != NULL) 94 port_subscribe(port, port_file_source(fileno(error)), PORT_INPUT | PORT_CLOSE); 95 96 /* Wait for events on the streams and the process. */ 97 98 while (1) 99 { 100 if (port_wait(port, &source)) 101 { 102 printf("Error waiting for notifications.\n"); 103 return 1; 104 } 105 106 /* Handle input to this process. */ 107 108 if ((stdin != NULL) && (source == port_file_source(fileno(stdin)))) 109 transfer(stdin, input); 110 111 /* Handle output from the process. */ 112 113 else if (source == port_file_source(fileno(output))) 114 transfer(output, stdout); 115 else if ((error != NULL) && (source == port_file_source(fileno(error)))) 116 transfer(error, stderr); 117 118 /* Handle process termination. */ 119 120 else if (source == port_process_source(pid)) 121 { 122 transfer(output, stdout); 123 124 if (error != NULL) 125 transfer(error, stderr); 126 127 break; 128 } 129 } 130 131 fclose(input); 132 fclose(output); 133 134 if (error != NULL) 135 fclose(error); 136 137 return 0; 138 } 139 140 /* vim: tabstop=4 expandtab shiftwidth=4 141 */