/* * * Copyright 2023 Oleg Borodin * */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include //#include //#include //#include //#include //#include //#include //#include #include 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; }