122 lines
2.6 KiB
C
122 lines
2.6 KiB
C
/*
|
|
*
|
|
* Copyright 2023 Oleg Borodin <borodin@unix7.org>
|
|
*
|
|
*/
|
|
|
|
|
|
#include <stdio.h>
|
|
#include <sys/param.h>
|
|
#include <sys/jail.h>
|
|
|
|
|
|
#include <sys/types.h>
|
|
#include <sys/stat.h>
|
|
#include <sys/socket.h>
|
|
#include <sys/sysctl.h>
|
|
|
|
#include <arpa/inet.h>
|
|
#include <netinet/in.h>
|
|
|
|
#include <err.h>
|
|
#include <errno.h>
|
|
#include <stdarg.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
//#include <sys/types.h>
|
|
//#include <sys/socket.h>
|
|
//#include <ifaddrs.h>
|
|
|
|
//#include <sys/types.h>
|
|
//#include <sys/sysctl.h>
|
|
//#include <vm/vm_param.h>
|
|
//#include <sys/user.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
|
|
|
|
int copy(int srcfd, int dstfd) {
|
|
char buffer[1024];
|
|
int n = 0;
|
|
while ((n = read(srcfd, buffer, sizeof(buffer) - 1)) != 0) {
|
|
buffer[n] = '\0';
|
|
write(dstfd, buffer, n);
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
int exec() {
|
|
|
|
char* cmd = "/foo1";
|
|
char* basepath = "./newroot";
|
|
char* v4addr = "192.168.52.7";
|
|
char* hostname = "localhost";
|
|
char* logpath = "output.log";
|
|
|
|
int logfd;
|
|
if ((logfd = open(logpath, O_RDWR|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR)) < 0) {
|
|
return -errno;
|
|
}
|
|
|
|
pid_t childpid = 0;
|
|
|
|
int stdoutfd[2];
|
|
|
|
if (pipe(stdoutfd) < 0) {
|
|
return -errno;
|
|
}
|
|
|
|
if((childpid = fork()) < 0) {
|
|
return -errno;
|
|
}
|
|
|
|
if (childpid == 0) {
|
|
/* child */
|
|
close(stdoutfd[0]);
|
|
dup2(stdoutfd[1], STDOUT_FILENO);
|
|
dup2(stdoutfd[1], STDERR_FILENO);
|
|
|
|
struct in_addr inaddr;
|
|
inet_pton(AF_INET, v4addr, &inaddr);
|
|
|
|
struct jail j = {
|
|
.version = JAIL_API_VERSION,
|
|
.path = basepath,
|
|
.hostname = hostname,
|
|
.jailname = "test",
|
|
.ip4s = 1,
|
|
.ip6s = 0,
|
|
.ip4 = &inaddr,
|
|
.ip6 = NULL
|
|
};
|
|
int id = 0;
|
|
if ((id = jail(&j)) < 0) {
|
|
return -1;
|
|
}
|
|
if (execl(cmd, cmd, NULL) < 0) {
|
|
return -1;
|
|
}
|
|
} else {
|
|
/* parent */
|
|
close(stdoutfd[1]);
|
|
|
|
if (copy(stdoutfd[0], logfd) < 0) {
|
|
return -1;
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
int main(int argc, char **argv) {
|
|
if (exec() < 0) {
|
|
return 1;
|
|
}
|
|
return 0;
|
|
}
|