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 #include <process.h> 27 28 29 30 /* Transfer size for communication. */ 31 32 static const offset_t TO_TRANSFER = 1024; 33 34 /* Transfer data between streams. */ 35 36 static void transfer(FILE *input, FILE *output) 37 { 38 char buffer[TO_TRANSFER]; 39 size_t nread; 40 41 while ((nread = fread(buffer, sizeof(char), TO_TRANSFER, input))) 42 { 43 if (!fwrite(buffer, sizeof(char), nread, output)) 44 break; 45 } 46 } 47 48 49 50 /* List objects in the filesystem image. */ 51 52 int main(int argc, char *argv[]) 53 { 54 FILE *input, *output, *error; 55 pid_t pid; 56 port_t port; 57 event_source_t source; 58 event_values_t result; 59 60 if (argc < 2) 61 { 62 printf("Usage: test_popenv <program> <argument>...\n"); 63 return 1; 64 } 65 66 printf("Running: %s\n", argv[1]); 67 68 pid = popenv(argc - 1, (const char **) argv + 1, &input, &output, &error); 69 70 if (pid == -1) 71 { 72 printf("Error: %d\n", errno); 73 return 1; 74 } 75 76 printf("Process: %d\n", pid); 77 78 /* Monitor the streams and the process. */ 79 80 port = port_create(); 81 82 port_subscribe(port, port_process_source(pid), PORT_PROCESS_ALL); 83 84 if (stdin != NULL) 85 port_subscribe(port, port_file_source(fileno(stdin)), PORT_INPUT | PORT_CLOSE); 86 87 port_subscribe(port, port_file_source(fileno(output)), PORT_INPUT | PORT_CLOSE); 88 89 if (error != NULL) 90 port_subscribe(port, port_file_source(fileno(error)), PORT_INPUT | PORT_CLOSE); 91 92 /* Wait for events on the streams and the process. */ 93 94 while (1) 95 { 96 if (port_wait(port, &source)) 97 { 98 printf("Error waiting for notifications.\n"); 99 return 1; 100 } 101 102 /* Handle input to this process. */ 103 104 if ((stdin != NULL) && (source == port_file_source(fileno(stdin)))) 105 transfer(stdin, input); 106 107 /* Handle output from the process. */ 108 109 else if (source == port_file_source(fileno(output))) 110 transfer(output, stdout); 111 else if ((error != NULL) && (source == port_file_source(fileno(error)))) 112 transfer(error, stderr); 113 114 /* Handle process termination. */ 115 116 else if (source == port_process_source(pid)) 117 { 118 transfer(output, stdout); 119 120 if (error != NULL) 121 transfer(error, stderr); 122 123 result = port_source_event_values(source); 124 break; 125 } 126 } 127 128 fclose(input); 129 fclose(output); 130 131 if (error != NULL) 132 fclose(error); 133 134 printf("Result: %ld\n", result.val); 135 return 0; 136 } 137 138 /* vim: tabstop=4 expandtab shiftwidth=4 139 */