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 <errno.h> 23 #include <fcntl.h> 24 #include <port.h> 25 #include <process.h> 26 #include <stdio.h> 27 #include <sys/types.h> 28 29 30 31 /* Transfer size for communication. */ 32 33 static const offset_t TO_TRANSFER = 1024; 34 35 /* Transfer data between streams. */ 36 37 static void transfer(FILE *input, FILE *output, char *buffer, int *start, size_t *remaining) 38 { 39 size_t nwritten; 40 41 /* Transfer any previously read data before trying to read more. */ 42 43 while (*remaining || (*start = 0, *remaining = fread(buffer, sizeof(char), TO_TRANSFER, input))) 44 { 45 /* Write the data, stopping if no data can be written. */ 46 47 nwritten = fwrite(buffer + *start, sizeof(char), *remaining, output); 48 49 if (!nwritten) 50 return; 51 52 /* Record the remaining amount. */ 53 54 *remaining -= nwritten; 55 *start += nwritten; 56 } 57 } 58 59 60 61 /* List objects in the filesystem image. */ 62 63 int main(int argc, char *argv[]) 64 { 65 FILE *input, *output, *error; 66 pid_t pid; 67 port_t port; 68 event_source_t source; 69 event_values_t result; 70 71 /* Transfer buffers and status. */ 72 73 char input_buffer[TO_TRANSFER]; 74 char output_buffer[TO_TRANSFER]; 75 char error_buffer[TO_TRANSFER]; 76 int input_start = 0, output_start = 0, error_start = 0; 77 size_t input_remaining = 0, output_remaining = 0, error_remaining = 0; 78 79 if (argc < 2) 80 { 81 printf("Usage: test_popenv <program> <argument>...\n"); 82 return 1; 83 } 84 85 printf("Running: %s\n", argv[1]); 86 87 pid = popenv(argc - 1, (const char **) argv + 1, &input, &output, &error); 88 89 if (pid == -1) 90 { 91 printf("Error: %d\n", errno); 92 return 1; 93 } 94 95 printf("Process: %d\n", pid); 96 97 /* Make stdin and the pipes non-blocking to be able to read and write as 98 much data as is available and to monitor events on them manually. */ 99 100 if (stdin != NULL) 101 { 102 fcntl(fileno(stdin), F_SETFL, O_NONBLOCK | fcntl(fileno(stdin), F_GETFL)); 103 fcntl(fileno(input), F_SETFL, O_NONBLOCK | fcntl(fileno(input), F_GETFL)); 104 } 105 else 106 { 107 fclose(input); 108 input = NULL; 109 } 110 111 fcntl(fileno(output), F_SETFL, O_NONBLOCK | fcntl(fileno(output), F_GETFL)); 112 113 if (error != NULL) 114 fcntl(fileno(error), F_SETFL, O_NONBLOCK | fcntl(fileno(error), F_GETFL)); 115 116 /* Monitor the streams and the process. */ 117 118 port = port_create(); 119 120 port_subscribe(port, port_process_source(pid), PORT_PROCESS_ALL); 121 122 if (stdin != NULL) 123 { 124 port_subscribe(port, port_file_source(fileno(stdin)), PORT_INPUT | PORT_CLOSE); 125 port_subscribe(port, port_file_source(fileno(input)), PORT_OUTPUT | PORT_CLOSE); 126 } 127 128 port_subscribe(port, port_file_source(fileno(output)), PORT_INPUT | PORT_CLOSE); 129 130 if (error != NULL) 131 port_subscribe(port, port_file_source(fileno(error)), PORT_INPUT | PORT_CLOSE); 132 133 /* Wait for events on the streams and the process. */ 134 135 while (1) 136 { 137 if (port_wait(port, &source)) 138 { 139 printf("Error waiting for notifications.\n"); 140 return 1; 141 } 142 143 /* Handle input to this process and its transfer to the created 144 process. */ 145 146 if ((stdin != NULL) && (input != NULL)) 147 { 148 /* Attempt to transfer input. */ 149 150 if ((source == port_file_source(fileno(stdin))) || 151 input_remaining || (source == port_file_source(fileno(input)))) 152 153 transfer(stdin, input, input_buffer, &input_start, &input_remaining); 154 155 /* If no input remains and stdin has been closed, close the input 156 pipe. */ 157 158 if (!input_remaining && (feof(stdin) || 159 port_source_event_flags(port_file_source(fileno(stdin))) & PORT_CLOSE)) 160 { 161 fclose(input); 162 input = NULL; 163 } 164 } 165 166 /* Handle output from the process. */ 167 168 if (output_remaining || (source == port_file_source(fileno(output)))) 169 transfer(output, stdout, output_buffer, &output_start, &output_remaining); 170 171 if ((error != NULL) && (error_remaining || (source == port_file_source(fileno(error))))) 172 transfer(error, stderr, error_buffer, &error_start, &error_remaining); 173 174 /* Handle process termination. */ 175 176 if (source == port_process_source(pid)) 177 { 178 transfer(output, stdout, output_buffer, &output_start, &output_remaining); 179 180 if (error != NULL) 181 transfer(error, stderr, error_buffer, &error_start, &error_remaining); 182 183 result = port_source_event_values(source); 184 break; 185 } 186 } 187 188 /* Close any open pipes. */ 189 190 if (input != NULL) 191 fclose(input); 192 193 fclose(output); 194 195 if (error != NULL) 196 fclose(error); 197 198 printf("Result: %ld\n", result.val); 199 return 0; 200 } 201 202 /* vim: tabstop=4 expandtab shiftwidth=4 203 */