-
Notifications
You must be signed in to change notification settings - Fork 39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update Omake for Linux running #1060
base: master
Are you sure you want to change the base?
Conversation
Ok, so I've done some testing, I've figured out how to get the readout from #define _GNU_SOURCE
#include <spawn.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/wait.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <poll.h>
extern char **environ;
int main()
{
posix_spawn_file_actions_t spawn_actions;
posix_spawnattr_t attrs;
posix_spawn_file_actions_init(&spawn_actions);
posix_spawnattr_init(&attrs);
pid_t spawned_pid;
int pipes[2];
int ret = pipe2(pipes, O_CLOEXEC);
if (ret != 0)
{
printf("Pipe failed\n");
}
// Copy stdout to the writable pipe, pipes[0] is the readable pipe. Mirrors stdin vs stdout
posix_spawn_file_actions_adddup2(&spawn_actions, pipes[1], 1);
// Ensure pipes close
posix_spawn_file_actions_addclose(&spawn_actions, pipes[1]);
posix_spawn_file_actions_addclose(&spawn_actions, pipes[0]);
ret = posix_spawn(&spawned_pid, "/bin/sh", &spawn_actions, &attrs, (char *const[]){"sh", "-c", "--", "ls /var/", NULL}, environ);
if (ret)
{
printf("Failed to spawn! errno: %d\n", ret);
}
posix_spawn_file_actions_destroy(&spawn_actions);
posix_spawnattr_destroy(&attrs);
int wait_status = 0;
do
{
wait_status = 0;
char val[80] = {0};
struct pollfd polls = {.events = POLLIN, .fd = pipes[0]};
// Poll for < 100ms, if we timeout on having any data whatsoever, we check if the process is done, if the process is done, exit the read loop.
int ret = poll(&polls, 1, 100);
if (ret > 0)
{
if (polls.revents & POLLIN)
{
int success = read(pipes[0], val, sizeof(val) - 1);
printf("%s", val);
if (success < 1)
{
break;
}
}
}
if (ret == 0)
{
waitpid(spawned_pid, &wait_status, WUNTRACED);
if (!(!WIFEXITED(wait_status) && !WIFSIGNALED(wait_status) && !WIFSTOPPED(wait_status)))
{
break;
}
}
} while (1);
close(pipes[0]);
close(pipes[1]);
} I have verified in WSL that this prints the exact output of |
😄 |
Minor update: |
Well... I got back to this.
On unix systems, this should default to forward slashes, I'll have to search for that logic somewhere.... |
This is a prototype with some basic changes for linux, this DOES NOT CURRENTLY WORK, and I know this because I haven't tested anything to do with actually capturing process output, this is just some fill-in from before to get some eyeballs on this.
The main thing stopping me from getting anywhere is my complete inability to understand how the dup2 stuff works, and all of the pipe issues, which gets me lost immediately upon trying to understand it, I'm at the point of trying to understand how glibc implements popen and it has also gotten me absolutely nowhere...
@GitMensch I assume you have the best eyes to know what in the universe POSIX is supposed to be doing and if I'm on the right track here, because I sure know I don't.