49 lines
815 B
C
49 lines
815 B
C
|
#ifndef SUBPROCESS_H
|
||
|
#define SUBPROCESS_H
|
||
|
|
||
|
#include <unistd.h>
|
||
|
#include <stdlib.h>
|
||
|
#include <stdio.h>
|
||
|
#include <errno.h>
|
||
|
#include <sys/wait.h>
|
||
|
#include <sys/socket.h>
|
||
|
|
||
|
pid_t pid = NULL;
|
||
|
|
||
|
int subprocess(char *command)
|
||
|
{
|
||
|
int sp[2] = {0};
|
||
|
|
||
|
if (socketpair(AF_UNIX, SOCK_STREAM, 0, sp) == -1)
|
||
|
{
|
||
|
perror("socketpair");
|
||
|
exit(EXIT_FAILURE);
|
||
|
}
|
||
|
|
||
|
pid = fork();
|
||
|
|
||
|
if (pid == -1)
|
||
|
{
|
||
|
perror("fork");
|
||
|
exit(EXIT_FAILURE);
|
||
|
}
|
||
|
|
||
|
if (!pid)
|
||
|
{
|
||
|
close(sp[0]);
|
||
|
dup2(sp[1], STDIN_FILENO);
|
||
|
dup2(sp[1], STDOUT_FILENO);
|
||
|
dup2(sp[1], STDERR_FILENO);
|
||
|
close(sp[1]);
|
||
|
execlp(command, command, NULL);
|
||
|
perror("execlp");
|
||
|
exit(EXIT_FAILURE);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
close(sp[1]);
|
||
|
return sp[0];
|
||
|
}
|
||
|
}
|
||
|
#endif
|