Commit
Author: Kevin Schoon [me@kevinschoon.com]
Hash: 1e20c3815aed307150914c1436cd3d103c52c59d
Timestamp: Thu, 06 Apr 2023 22:47:40 +0000 (1 year ago)

+56 -2 +/-2 browse
progress
1diff --git a/Makefile b/Makefile
2index 4ee1724..b3fbc92 100644
3--- a/Makefile
4+++ b/Makefile
5 @@ -1,5 +1,8 @@
6- default: bin
7- gcc main.c -o bin/main
8+ default: bin/qemu-shim
9+
10+ .PHONY: bin/qemu-shim
11+ bin/qemu-shim:
12+ gcc main.c -o $@
13
14 bin:
15 mkdir -p bin
16 diff --git a/main.c b/main.c
17index 3c552b0..3cc3d2f 100644
18--- a/main.c
19+++ b/main.c
20 @@ -2,6 +2,10 @@
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24+ #include <unistd.h>
25+ #include <errno.h>
26+ #include <signal.h>
27+ #include <sys/wait.h>
28
29 #define _MONITOR_REGEXP "unix:\\([-/.[:alnum:]]*\\)"
30 #define _SPICE_REGEXP ".*addr=\\([-/.[:alnum:]]*\\)"
31 @@ -54,6 +58,42 @@ char *_get_arg_with_regexp(int argc, char *argv[], char *name,
32 return result;
33 }
34
35+ FILE *run_qemu(int *pid) {
36+ pid_t child_pid;
37+ int fd[2];
38+ pipe(fd);
39+ if ((child_pid = fork()) == -1) {
40+ fprintf(stderr, "failed to fork\n");
41+ exit(1);
42+ }
43+ if (child_pid == 0) {
44+ close(fd[0]);
45+ dup2(fd[1], 1);
46+
47+ setpgid(child_pid, child_pid);
48+ execl("/bin/sh", "/bin/sh", "-c", "for i in $(seq 1 10); do echo $i; sleep .5; done",
49+ NULL);
50+ exit(0);
51+ } else {
52+ close(fd[1]);
53+ }
54+
55+ *pid = child_pid;
56+ return fdopen(fd[0], "r");
57+ }
58+
59+ int shutdown(FILE *fp, pid_t pid) {
60+ int stat;
61+ fclose(fp);
62+ while (waitpid(pid, &stat, 0) == -1) {
63+ if (errno != EINTR) {
64+ stat = -1;
65+ break;
66+ }
67+ }
68+ return stat;
69+ }
70+
71 int main(int argc, char *argv[]) {
72 char *name;
73 name = _get_arg_by_name(argc, argv, "-name");
74 @@ -75,6 +115,17 @@ int main(int argc, char *argv[]) {
75 if (spice_path != NULL) {
76 printf("SPICE: %s\n", spice_path);
77 }
78+ int pid;
79+ FILE *fp;
80+ fp = run_qemu(&pid);
81+ char output[100] = {0};
82+
83+ while (read(fileno(fp), output, sizeof(output)-1) != 0) {
84+ printf("%s", output);
85+ }
86+
87+ shutdown(fp, pid);
88+ printf("..Done?\n");
89
90 /* CATCH SIGNAL TODO */
91 /* POPEN TODO */