💾 Archived View for gemini.rmf-dev.com › repo › Vaati › Vgmi › files › fe59a37e8823beb633b2fa608416a… captured on 2023-12-28 at 15:44:39. Gemini links have been rewritten to link to archived content
-=-=-=-=-=-=-
0 /*
1 * ISC License
2 * Copyright (c) 2023 RMF <rawmonk@firemail.cc>
3 */
4 #ifdef __linux__
5 #define _DEFAULT_SOURCE
6 #include <syscall.h>
7 #endif
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <stdint.h>
11 #include <unistd.h>
12 #include "error.h"
13 #include "proc.h"
14
15 char **argv_ptr = NULL;
16
17 void proc_argv(char **argv) {
18 argv_ptr = argv;
19 }
20
21 int proc_fork(char *arg, int *fd_in, int *fd_out) {
22
23 int in[2], out[2];
24 uint8_t byte;
25 pid_t pid;
26
27 if (!argv_ptr) return -1;
28
29 if (pipe(in)) return ERROR_ERRNO;
30 if (pipe(out)) return ERROR_ERRNO;
31 pid = vfork();
32 if (pid < 0) return ERROR_ERRNO;
33 if (!pid) {
34 char **argv;
35 close(in[0]);
36 close(out[1]);
37
38 argv = malloc(sizeof(char*) * 3);
39 if (!argv) goto fail;
40 argv[0] = argv_ptr[0];
41 argv[1] = arg;
42 argv[2] = NULL;
43
44 close(STDOUT_FILENO);
45 dup(in[1]);
46 close(STDIN_FILENO);
47 dup(out[0]);
48
49 execvp(argv_ptr[0], argv);
50 printf("execvp failed\n");
51 fail:
52 close(in[0]);
53 close(out[1]);
54 exit(0);
55 }
56
57 close(in[1]);
58 close(out[0]);
59
60 byte = -1;
61 read(in[0], &byte, 1);
62 if (byte) return ERROR_SANDBOX_FAILURE;
63 *fd_out = out[1];
64 *fd_in = in[0];
65 return 0;
66 }
67
68 void proc_exit() {
69 #ifdef __linux__
70 syscall(SYS_exit, EXIT_SUCCESS);
71 #else
72 exit(0);
73 #endif
74 }
75