2016-08-26 21:00:30 +00:00
|
|
|
#define _GNU_SOURCE
|
2016-09-12 23:38:30 +00:00
|
|
|
#include <ctype.h>
|
2016-09-02 21:33:54 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdbool.h>
|
2016-08-26 21:00:30 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2016-09-02 21:33:54 +00:00
|
|
|
#include <sys/epoll.h>
|
2016-08-26 21:00:30 +00:00
|
|
|
#include <sys/prctl.h>
|
|
|
|
#include <sys/wait.h>
|
2016-09-02 21:33:54 +00:00
|
|
|
#include <termios.h>
|
|
|
|
#include <unistd.h>
|
2016-08-26 21:00:30 +00:00
|
|
|
|
|
|
|
#include <glib.h>
|
|
|
|
|
2016-09-02 21:33:54 +00:00
|
|
|
#define pexit(fmt, ...) \
|
|
|
|
do { \
|
|
|
|
fprintf(stderr, "conmon: " fmt " %m\n", ##__VA_ARGS__); \
|
|
|
|
exit(EXIT_FAILURE); \
|
|
|
|
} while (0)
|
2016-08-26 21:00:30 +00:00
|
|
|
|
2016-09-02 21:33:54 +00:00
|
|
|
#define nexit(fmt, ...) \
|
|
|
|
do { \
|
|
|
|
fprintf(stderr, "conmon: " fmt "\n", ##__VA_ARGS__); \
|
|
|
|
exit(EXIT_FAILURE); \
|
|
|
|
} while (0)
|
2016-08-26 21:00:30 +00:00
|
|
|
|
2016-09-02 21:33:54 +00:00
|
|
|
#define nwarn(fmt, ...) \
|
|
|
|
do { \
|
|
|
|
fprintf(stderr, "conmon: " fmt "\n", ##__VA_ARGS__); \
|
|
|
|
} while (0)
|
2016-08-26 21:00:30 +00:00
|
|
|
|
|
|
|
#define _cleanup_(x) __attribute__((cleanup(x)))
|
|
|
|
|
2016-09-17 09:37:45 +00:00
|
|
|
static inline void freep(void *p)
|
|
|
|
{
|
|
|
|
free(*(void **)p);
|
|
|
|
}
|
2016-09-02 21:33:54 +00:00
|
|
|
|
2016-09-17 09:37:45 +00:00
|
|
|
static inline void closep(int *fd)
|
|
|
|
{
|
2016-09-02 21:33:54 +00:00
|
|
|
if (*fd >= 0)
|
|
|
|
close(*fd);
|
|
|
|
*fd = -1;
|
2016-08-26 21:00:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#define _cleanup_free_ _cleanup_(freep)
|
2016-09-02 21:33:54 +00:00
|
|
|
#define _cleanup_close_ _cleanup_(closep)
|
|
|
|
|
|
|
|
struct termios tty_orig;
|
|
|
|
|
|
|
|
static void tty_restore(void)
|
|
|
|
{
|
|
|
|
if (tcsetattr(STDIN_FILENO, TCSANOW, &tty_orig) == -1)
|
|
|
|
pexit("tcsetattr");
|
|
|
|
}
|
|
|
|
|
|
|
|
#define BUF_SIZE 256
|
|
|
|
#define CMD_SIZE 1024
|
|
|
|
#define MAX_EVENTS 10
|
2016-08-26 21:00:30 +00:00
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
int ret;
|
2016-09-12 23:38:30 +00:00
|
|
|
int opt;
|
|
|
|
bool terminal = FALSE;
|
|
|
|
const char *cid = NULL;
|
2016-09-17 09:37:45 +00:00
|
|
|
const char *runtime_path = NULL;
|
2016-09-02 21:33:54 +00:00
|
|
|
char cmd[CMD_SIZE];
|
2016-08-26 21:00:30 +00:00
|
|
|
GError *err = NULL;
|
2016-09-02 21:33:54 +00:00
|
|
|
_cleanup_free_ char *contents;
|
2016-08-26 21:00:30 +00:00
|
|
|
int cpid = -1;
|
|
|
|
int status;
|
|
|
|
pid_t pid;
|
2016-09-02 21:33:54 +00:00
|
|
|
_cleanup_close_ int mfd = -1;
|
|
|
|
_cleanup_close_ int epfd = -1;
|
|
|
|
char slname[BUF_SIZE];
|
|
|
|
char buf[BUF_SIZE];
|
|
|
|
int num_read;
|
|
|
|
struct termios t;
|
|
|
|
struct epoll_event ev;
|
|
|
|
struct epoll_event evlist[MAX_EVENTS];
|
2016-09-15 23:40:44 +00:00
|
|
|
int child_pipe = -1;
|
|
|
|
char *sync_pipe, *endptr;
|
|
|
|
int len;
|
2016-08-26 21:00:30 +00:00
|
|
|
|
2016-09-17 09:37:45 +00:00
|
|
|
while ((opt = getopt(argc, argv, "tc:r:")) != -1) {
|
|
|
|
switch (opt) {
|
2016-09-12 23:38:30 +00:00
|
|
|
case 't':
|
|
|
|
terminal = TRUE;
|
|
|
|
break;
|
|
|
|
case 'c':
|
|
|
|
cid = optarg;
|
|
|
|
break;
|
2016-09-17 09:37:45 +00:00
|
|
|
case 'r':
|
|
|
|
runtime_path = optarg;
|
|
|
|
break;
|
2016-09-12 23:38:30 +00:00
|
|
|
case '?':
|
2016-09-17 09:37:45 +00:00
|
|
|
if (optopt == 'c' || optopt == 'r')
|
|
|
|
nexit("Option -%c requires an argument.",
|
|
|
|
optopt);
|
|
|
|
else if (isprint(optopt))
|
2016-09-12 23:38:30 +00:00
|
|
|
nexit("Unknown option `-%c'.", optopt);
|
|
|
|
else
|
2016-09-17 09:37:45 +00:00
|
|
|
nexit("Unknown option character `\\x%x'.\n",
|
|
|
|
optopt);
|
2016-09-12 23:38:30 +00:00
|
|
|
default:
|
2016-09-17 09:37:45 +00:00
|
|
|
nexit
|
|
|
|
("Usage: %s -r runtime_path [-c container_id] [-t]",
|
|
|
|
argv[0]);
|
2016-09-12 23:38:30 +00:00
|
|
|
}
|
2016-08-26 21:00:30 +00:00
|
|
|
}
|
|
|
|
|
2016-09-17 09:37:45 +00:00
|
|
|
if (cid == NULL)
|
|
|
|
nexit("Container ID not provided");
|
|
|
|
|
|
|
|
if (runtime_path == NULL)
|
|
|
|
nexit("Runtime path not provided");
|
2016-08-26 21:00:30 +00:00
|
|
|
|
2016-09-15 23:40:44 +00:00
|
|
|
sync_pipe = getenv("_OCI_SYNCPIPE");
|
|
|
|
if (sync_pipe) {
|
|
|
|
errno = 0;
|
|
|
|
child_pipe = strtol(sync_pipe, &endptr, 10);
|
|
|
|
if (errno != 0 || *endptr != '\0')
|
|
|
|
pexit("unable to parse _OCI_SYNCPIPE");
|
|
|
|
}
|
|
|
|
|
2016-09-02 21:33:54 +00:00
|
|
|
/*
|
|
|
|
* Set self as subreaper so we can wait for container process
|
|
|
|
* and return its exit code.
|
|
|
|
*/
|
2016-08-26 21:00:30 +00:00
|
|
|
ret = prctl(PR_SET_CHILD_SUBREAPER, 1, 0, 0, 0);
|
|
|
|
if (ret != 0) {
|
|
|
|
pexit("Failed to set as subreaper");
|
|
|
|
}
|
|
|
|
|
2016-09-12 23:38:30 +00:00
|
|
|
if (terminal) {
|
|
|
|
/* Open the master pty */
|
|
|
|
mfd = open("/dev/ptmx", O_RDWR | O_NOCTTY);
|
|
|
|
if (mfd < 0)
|
|
|
|
pexit("Failed to open console master pty");
|
2016-09-02 21:33:54 +00:00
|
|
|
|
2016-09-12 23:38:30 +00:00
|
|
|
/* Grant access to the slave pty */
|
|
|
|
if (grantpt(mfd) == -1)
|
|
|
|
pexit("Failed to grant access to slave pty");
|
2016-09-02 21:33:54 +00:00
|
|
|
|
2016-09-12 23:38:30 +00:00
|
|
|
/* Unlock the slave pty */
|
2016-09-17 09:37:45 +00:00
|
|
|
if (unlockpt(mfd) == -1) { /* Unlock slave pty */
|
2016-09-12 23:38:30 +00:00
|
|
|
pexit("Failed to unlock the slave pty");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the slave pty name */
|
|
|
|
ret = ptsname_r(mfd, slname, BUF_SIZE);
|
|
|
|
if (ret != 0) {
|
|
|
|
pexit("Failed to get the slave pty name");
|
|
|
|
}
|
2016-09-02 21:33:54 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create the container */
|
2016-09-12 23:38:30 +00:00
|
|
|
if (terminal) {
|
2016-09-17 09:37:45 +00:00
|
|
|
snprintf(cmd, CMD_SIZE,
|
|
|
|
"%s create %s --pid-file pidfile --console %s",
|
|
|
|
runtime_path, cid, slname);
|
2016-09-12 23:38:30 +00:00
|
|
|
} else {
|
2016-09-17 09:37:45 +00:00
|
|
|
snprintf(cmd, CMD_SIZE, "%s create %s --pid-file pidfile",
|
|
|
|
runtime_path, cid);
|
2016-09-12 23:38:30 +00:00
|
|
|
}
|
2016-08-26 21:00:30 +00:00
|
|
|
ret = system(cmd);
|
|
|
|
if (ret != 0) {
|
|
|
|
nexit("Failed to create container");
|
|
|
|
}
|
|
|
|
|
2016-09-02 21:33:54 +00:00
|
|
|
/* Read the pid so we can wait for the process to exit */
|
2016-08-26 21:00:30 +00:00
|
|
|
g_file_get_contents("pidfile", &contents, NULL, &err);
|
|
|
|
if (err) {
|
|
|
|
fprintf(stderr, "Failed to read pidfile: %s\n", err->message);
|
|
|
|
g_error_free(err);
|
|
|
|
exit(1);
|
2016-09-02 21:33:54 +00:00
|
|
|
}
|
2016-08-26 21:00:30 +00:00
|
|
|
|
|
|
|
cpid = atoi(contents);
|
|
|
|
printf("container PID: %d\n", cpid);
|
|
|
|
|
2016-09-15 23:40:44 +00:00
|
|
|
/* Send the container pid back to parent */
|
|
|
|
if (child_pipe > 0) {
|
|
|
|
len = snprintf(buf, BUF_SIZE, "{\"pid\": %d}\n", cpid);
|
|
|
|
if (len < 0 || write(child_pipe, buf, len) != len) {
|
|
|
|
pexit("unable to send container pid to parent");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-12 23:38:30 +00:00
|
|
|
if (terminal) {
|
|
|
|
/* Save exiting termios settings */
|
|
|
|
if (tcgetattr(STDIN_FILENO, &tty_orig) == -1)
|
|
|
|
pexit("tcegetattr");
|
|
|
|
|
|
|
|
/* Settings for raw mode */
|
2016-09-17 09:37:45 +00:00
|
|
|
t.c_lflag &=
|
|
|
|
~(ISIG | ICANON | ECHO | ECHOE | ECHOK | ECHONL | IEXTEN);
|
|
|
|
t.c_iflag &=
|
|
|
|
~(BRKINT | ICRNL | IGNBRK | IGNCR | INLCR | INPCK | ISTRIP |
|
|
|
|
IXON | IXOFF | IGNPAR | PARMRK);
|
2016-09-12 23:38:30 +00:00
|
|
|
t.c_oflag &= ~OPOST;
|
|
|
|
t.c_cc[VMIN] = 1;
|
|
|
|
t.c_cc[VTIME] = 0;
|
|
|
|
|
|
|
|
/* Set terminal to raw mode */
|
|
|
|
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &t) == -1)
|
|
|
|
pexit("tcsetattr");
|
|
|
|
|
|
|
|
/* Setup terminal restore on exit */
|
|
|
|
if (atexit(tty_restore) != 0)
|
|
|
|
pexit("atexit");
|
|
|
|
|
|
|
|
epfd = epoll_create(5);
|
|
|
|
if (epfd < 0)
|
|
|
|
pexit("epoll_create");
|
|
|
|
ev.events = EPOLLIN;
|
|
|
|
ev.data.fd = STDIN_FILENO;
|
|
|
|
if (epoll_ctl(epfd, EPOLL_CTL_ADD, STDIN_FILENO, &ev) < 0) {
|
|
|
|
pexit("Failed to add stdin to epoll");
|
|
|
|
}
|
|
|
|
ev.data.fd = mfd;
|
|
|
|
if (epoll_ctl(epfd, EPOLL_CTL_ADD, mfd, &ev) < 0) {
|
|
|
|
pexit("Failed to add console master fd to epoll");
|
|
|
|
}
|
2016-09-02 21:33:54 +00:00
|
|
|
|
2016-09-12 23:38:30 +00:00
|
|
|
/* Copy data back and forth between STDIN and master fd */
|
|
|
|
while (true) {
|
|
|
|
int ready = epoll_wait(epfd, evlist, MAX_EVENTS, -1);
|
|
|
|
for (int i = 0; i < ready; i++) {
|
|
|
|
if (evlist[i].events & EPOLLIN) {
|
|
|
|
if (evlist[i].data.fd == STDIN_FILENO) {
|
2016-09-17 09:37:45 +00:00
|
|
|
num_read =
|
|
|
|
read(STDIN_FILENO, buf,
|
|
|
|
BUF_SIZE);
|
2016-09-12 23:38:30 +00:00
|
|
|
if (num_read <= 0)
|
|
|
|
goto out;
|
|
|
|
|
2016-09-17 09:37:45 +00:00
|
|
|
if (write(mfd, buf, num_read) !=
|
|
|
|
num_read) {
|
|
|
|
nwarn
|
|
|
|
("partial/failed write (masterFd)");
|
2016-09-12 23:38:30 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
} else if (evlist[i].data.fd == mfd) {
|
2016-09-17 09:37:45 +00:00
|
|
|
num_read =
|
|
|
|
read(mfd, buf, BUF_SIZE);
|
2016-09-12 23:38:30 +00:00
|
|
|
if (num_read <= 0)
|
|
|
|
goto out;
|
|
|
|
|
2016-09-17 09:37:45 +00:00
|
|
|
if (write
|
|
|
|
(STDOUT_FILENO, buf,
|
|
|
|
num_read) != num_read) {
|
|
|
|
nwarn
|
|
|
|
("partial/failed write (STDOUT_FILENO)");
|
2016-09-12 23:38:30 +00:00
|
|
|
goto out;
|
|
|
|
}
|
2016-09-02 21:33:54 +00:00
|
|
|
}
|
2016-09-17 09:37:45 +00:00
|
|
|
} else if (evlist[i].events &
|
|
|
|
(EPOLLHUP | EPOLLERR)) {
|
|
|
|
printf("closing fd %d\n",
|
|
|
|
evlist[i].data.fd);
|
2016-09-12 23:38:30 +00:00
|
|
|
if (close(evlist[i].data.fd) < 0)
|
|
|
|
pexit("close");
|
|
|
|
goto out;
|
2016-09-02 21:33:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-09-17 09:37:45 +00:00
|
|
|
out:
|
2016-09-12 23:38:30 +00:00
|
|
|
tty_restore();
|
|
|
|
}
|
2016-09-02 21:33:54 +00:00
|
|
|
|
|
|
|
/* Wait for the container process and record its exit code */
|
2016-08-26 21:00:30 +00:00
|
|
|
while ((pid = waitpid(-1, &status, 0)) > 0) {
|
|
|
|
printf("PID %d exited\n", pid);
|
|
|
|
if (pid == cpid) {
|
|
|
|
_cleanup_free_ char *status_str = NULL;
|
|
|
|
ret = asprintf(&status_str, "%d", status);
|
|
|
|
if (ret < 0) {
|
|
|
|
pexit("Failed to allocate memory for status");
|
|
|
|
}
|
2016-09-17 09:37:45 +00:00
|
|
|
g_file_set_contents("exit", status_str,
|
|
|
|
strlen(status_str), &err);
|
2016-08-26 21:00:30 +00:00
|
|
|
if (err) {
|
2016-09-17 09:37:45 +00:00
|
|
|
fprintf(stderr,
|
|
|
|
"Failed to write %s to exit file: %s\n",
|
|
|
|
status_str, err->message);
|
2016-08-26 21:00:30 +00:00
|
|
|
g_error_free(err);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|