Subprocess with pipe capturing
This commit is contained in:
parent
7a3f1955b9
commit
a0da4ce1a3
6 changed files with 225 additions and 0 deletions
1
src/Subprocess/CMakeLists.txt
Normal file
1
src/Subprocess/CMakeLists.txt
Normal file
|
@ -0,0 +1 @@
|
|||
add_library(Subprocess STATIC Subprocess.c)
|
109
src/Subprocess/Subprocess.c
Normal file
109
src/Subprocess/Subprocess.c
Normal file
|
@ -0,0 +1,109 @@
|
|||
#include "Subprocess.h"
|
||||
#include <stdlib.h>
|
||||
#include <errno.h>
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
|
||||
typedef struct PipePair_s {
|
||||
int read;
|
||||
int write;
|
||||
} PipePair;
|
||||
|
||||
static int _PipePair_Create(PipePair* pair)
|
||||
{
|
||||
int pipefds[2];
|
||||
int pipe_call = pipe(pipefds);
|
||||
|
||||
if (pipe_call == 0) {
|
||||
pair->read = pipefds[0];
|
||||
pair->write = pipefds[1];
|
||||
}
|
||||
|
||||
return pipe_call;
|
||||
}
|
||||
|
||||
static void _PipePair_Destroy(PipePair* pair)
|
||||
{
|
||||
close(pair->read);
|
||||
close(pair->write);
|
||||
}
|
||||
|
||||
int Subprocess_Create(Subprocess* subprocess, char* file, char** argv)
|
||||
{
|
||||
if (subprocess == NULL) {
|
||||
return EDESTADDRREQ;
|
||||
}
|
||||
if (file == NULL) {
|
||||
return EINVAL;
|
||||
}
|
||||
if (argv == NULL) {
|
||||
return EINVAL;
|
||||
}
|
||||
if (subprocess->in.type != SUBPROCESSREDIRECTIONTYPE_PIPE) {
|
||||
return ENOTSUP;
|
||||
}
|
||||
if (subprocess->out.type != SUBPROCESSREDIRECTIONTYPE_PIPE) {
|
||||
return ENOTSUP;
|
||||
}
|
||||
if (subprocess->err.type != SUBPROCESSREDIRECTIONTYPE_PIPE) {
|
||||
return ENOTSUP;
|
||||
}
|
||||
|
||||
// TODO: Implement FILE redirection
|
||||
|
||||
// Create pipe pairs
|
||||
PipePair in_pair, out_pair, err_pair;
|
||||
if (_PipePair_Create(&in_pair)) {
|
||||
return errno;
|
||||
}
|
||||
if (_PipePair_Create(&out_pair)) {
|
||||
_PipePair_Destroy(&in_pair);
|
||||
return errno;
|
||||
}
|
||||
if (_PipePair_Create(&err_pair)) {
|
||||
_PipePair_Destroy(&in_pair);
|
||||
_PipePair_Destroy(&out_pair);
|
||||
return errno;
|
||||
}
|
||||
|
||||
pid_t fork_result = fork();
|
||||
if (fork_result == 0) {
|
||||
// child
|
||||
// TODO: Check errors
|
||||
|
||||
close(in_pair.write);
|
||||
close(out_pair.read);
|
||||
close(err_pair.read);
|
||||
dup2(in_pair.read, STDIN_FILENO);
|
||||
dup2(out_pair.write, STDOUT_FILENO);
|
||||
dup2(err_pair.write, STDERR_FILENO);
|
||||
execvp(file, argv);
|
||||
|
||||
} else if (fork_result != -1) {
|
||||
// parent
|
||||
|
||||
close(in_pair.read);
|
||||
close(out_pair.write);
|
||||
close(err_pair.write);
|
||||
subprocess->in.get.pipefd = in_pair.write;
|
||||
subprocess->out.get.pipefd = out_pair.read;
|
||||
subprocess->err.get.pipefd = err_pair.read;
|
||||
subprocess->child_pid = fork_result;
|
||||
|
||||
} else {
|
||||
// error
|
||||
return errno;
|
||||
}
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
void Subprocess_Destroy(Subprocess* subprocess)
|
||||
{
|
||||
waitpid(subprocess->child_pid, NULL, 0);
|
||||
|
||||
if (subprocess->in .type == SUBPROCESSREDIRECTIONTYPE_PIPE) close(subprocess->in.get.pipefd);
|
||||
if (subprocess->out.type == SUBPROCESSREDIRECTIONTYPE_PIPE) close(subprocess->out.get.pipefd);
|
||||
if (subprocess->err.type == SUBPROCESSREDIRECTIONTYPE_PIPE) close(subprocess->err.get.pipefd);
|
||||
}
|
43
src/Subprocess/Subprocess.h
Normal file
43
src/Subprocess/Subprocess.h
Normal file
|
@ -0,0 +1,43 @@
|
|||
#ifndef UTILITIEC_SUBPROCESS_H
|
||||
#define UTILITIEC_SUBPROCESS_H
|
||||
|
||||
#include "../StringView/StringView.h"
|
||||
#include <sys/types.h>
|
||||
|
||||
// TODO: Implement FILE redirection
|
||||
enum SubprocessRedirectionType {
|
||||
SUBPROCESSREDIRECTIONTYPE_PIPE,
|
||||
SUBPROCESSREDIRECTIONTYPE_FILE,
|
||||
};
|
||||
|
||||
union SubprocessRedirectionContent {
|
||||
int pipefd;
|
||||
StringView file_name;
|
||||
};
|
||||
|
||||
typedef struct SubprocessRedirection_s {
|
||||
enum SubprocessRedirectionType type;
|
||||
union SubprocessRedirectionContent get;
|
||||
} SubprocessRedirection;
|
||||
|
||||
typedef struct Subprocess_t {
|
||||
pid_t child_pid;
|
||||
SubprocessRedirection in;
|
||||
SubprocessRedirection out;
|
||||
SubprocessRedirection err;
|
||||
} Subprocess;
|
||||
|
||||
|
||||
#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
|
||||
|
||||
|
||||
int Subprocess_Create(Subprocess* subprocess, char* file, char** argv);
|
||||
void Subprocess_Destroy(Subprocess* subprocess);
|
||||
|
||||
|
||||
#else
|
||||
// TODO: Implement for Windows with a windows testing machine
|
||||
#error "This module is not implemented for your platform yet"
|
||||
#endif // platform
|
||||
|
||||
#endif // header
|
Loading…
Add table
Add a link
Reference in a new issue