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>
|
2016-10-07 15:59:39 +00:00
|
|
|
#include <limits.h>
|
2016-09-02 21:33:54 +00:00
|
|
|
#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>
|
2016-10-07 15:59:39 +00:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/un.h>
|
2016-08-26 21:00:30 +00:00
|
|
|
#include <sys/wait.h>
|
2017-05-23 02:02:21 +00:00
|
|
|
#include <sys/eventfd.h>
|
2017-06-02 13:31:43 +00:00
|
|
|
#include <sys/stat.h>
|
2017-06-02 17:29:50 +00:00
|
|
|
#include <sys/uio.h>
|
2016-11-15 00:02:03 +00:00
|
|
|
#include <syslog.h>
|
2016-09-02 21:33:54 +00:00
|
|
|
#include <unistd.h>
|
2016-08-26 21:00:30 +00:00
|
|
|
|
|
|
|
#include <glib.h>
|
|
|
|
|
2016-10-07 15:59:39 +00:00
|
|
|
#include "cmsg.h"
|
|
|
|
|
2016-11-15 00:02:03 +00:00
|
|
|
#define pexit(fmt, ...) \
|
|
|
|
do { \
|
2016-10-07 15:59:39 +00:00
|
|
|
fprintf(stderr, "[conmon:e]: " fmt " %m\n", ##__VA_ARGS__); \
|
2016-11-15 00:02:03 +00:00
|
|
|
syslog(LOG_ERR, "conmon <error>: " fmt ": %m\n", ##__VA_ARGS__); \
|
|
|
|
exit(EXIT_FAILURE); \
|
2016-09-02 21:33:54 +00:00
|
|
|
} while (0)
|
2016-08-26 21:00:30 +00:00
|
|
|
|
2016-11-15 00:02:03 +00:00
|
|
|
#define nexit(fmt, ...) \
|
|
|
|
do { \
|
2016-10-07 15:59:39 +00:00
|
|
|
fprintf(stderr, "[conmon:e]: " fmt "\n", ##__VA_ARGS__); \
|
2016-11-15 00:02:03 +00:00
|
|
|
syslog(LOG_ERR, "conmon <error>: " fmt " \n", ##__VA_ARGS__); \
|
|
|
|
exit(EXIT_FAILURE); \
|
2016-09-02 21:33:54 +00:00
|
|
|
} while (0)
|
2016-08-26 21:00:30 +00:00
|
|
|
|
2016-09-02 21:33:54 +00:00
|
|
|
#define nwarn(fmt, ...) \
|
|
|
|
do { \
|
2016-10-07 15:59:39 +00:00
|
|
|
fprintf(stderr, "[conmon:w]: " fmt "\n", ##__VA_ARGS__); \
|
2017-05-30 21:30:45 +00:00
|
|
|
syslog(LOG_INFO, "conmon <nwarn>: " fmt " \n", ##__VA_ARGS__); \
|
2016-10-07 15:59:39 +00:00
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define ninfo(fmt, ...) \
|
|
|
|
do { \
|
|
|
|
fprintf(stderr, "[conmon:i]: " fmt "\n", ##__VA_ARGS__); \
|
2017-05-30 21:30:45 +00:00
|
|
|
syslog(LOG_INFO, "conmon <ninfo>: " fmt " \n", ##__VA_ARGS__); \
|
2016-09-02 21:33:54 +00:00
|
|
|
} 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
|
|
|
}
|
|
|
|
|
2017-05-23 02:00:02 +00:00
|
|
|
static inline void fclosep(FILE **fp) {
|
|
|
|
if (*fp)
|
|
|
|
fclose(*fp);
|
|
|
|
*fp = NULL;
|
|
|
|
}
|
|
|
|
|
2016-12-19 23:06:27 +00:00
|
|
|
static inline void gstring_free_cleanup(GString **string)
|
|
|
|
{
|
|
|
|
if (*string)
|
|
|
|
g_string_free(*string, TRUE);
|
|
|
|
}
|
|
|
|
|
2017-06-02 12:02:38 +00:00
|
|
|
static inline void strv_cleanup(char ***strv)
|
|
|
|
{
|
|
|
|
if (strv)
|
|
|
|
g_strfreev (*strv);
|
|
|
|
}
|
|
|
|
|
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)
|
2017-05-23 02:00:02 +00:00
|
|
|
#define _cleanup_fclose_ _cleanup_(fclosep)
|
2016-12-19 23:06:27 +00:00
|
|
|
#define _cleanup_gstring_ _cleanup_(gstring_free_cleanup)
|
2017-06-02 12:02:38 +00:00
|
|
|
#define _cleanup_strv_ _cleanup_(strv_cleanup)
|
2016-09-02 21:33:54 +00:00
|
|
|
|
2017-06-02 12:15:50 +00:00
|
|
|
#define BUF_SIZE 8192
|
2016-09-02 21:33:54 +00:00
|
|
|
#define CMD_SIZE 1024
|
|
|
|
#define MAX_EVENTS 10
|
2016-08-26 21:00:30 +00:00
|
|
|
|
2016-10-01 03:26:29 +00:00
|
|
|
static bool terminal = false;
|
|
|
|
static char *cid = NULL;
|
2017-05-30 21:35:12 +00:00
|
|
|
static char *cuuid = NULL;
|
2016-10-01 03:26:29 +00:00
|
|
|
static char *runtime_path = NULL;
|
2016-11-14 16:46:58 +00:00
|
|
|
static char *bundle_path = NULL;
|
|
|
|
static char *pid_file = NULL;
|
2016-12-19 23:06:27 +00:00
|
|
|
static bool systemd_cgroup = false;
|
2017-05-25 09:11:14 +00:00
|
|
|
static char *exec_process_spec = NULL;
|
2017-01-10 22:08:54 +00:00
|
|
|
static bool exec = false;
|
2016-10-07 15:59:39 +00:00
|
|
|
static char *log_path = NULL;
|
2016-10-01 03:26:29 +00:00
|
|
|
static GOptionEntry entries[] =
|
|
|
|
{
|
|
|
|
{ "terminal", 't', 0, G_OPTION_ARG_NONE, &terminal, "Terminal", NULL },
|
|
|
|
{ "cid", 'c', 0, G_OPTION_ARG_STRING, &cid, "Container ID", NULL },
|
2017-05-30 21:35:12 +00:00
|
|
|
{ "cuuid", 'u', 0, G_OPTION_ARG_STRING, &cuuid, "Container UUID", NULL },
|
2016-10-01 03:26:29 +00:00
|
|
|
{ "runtime", 'r', 0, G_OPTION_ARG_STRING, &runtime_path, "Runtime path", NULL },
|
2016-11-14 16:46:58 +00:00
|
|
|
{ "bundle", 'b', 0, G_OPTION_ARG_STRING, &bundle_path, "Bundle path", NULL },
|
|
|
|
{ "pidfile", 'p', 0, G_OPTION_ARG_STRING, &pid_file, "PID file", NULL },
|
2016-12-19 23:06:27 +00:00
|
|
|
{ "systemd-cgroup", 's', 0, G_OPTION_ARG_NONE, &systemd_cgroup, "Enable systemd cgroup manager", NULL },
|
2017-01-10 22:08:54 +00:00
|
|
|
{ "exec", 'e', 0, G_OPTION_ARG_NONE, &exec, "Exec a command in a running container", NULL },
|
2017-05-25 09:11:14 +00:00
|
|
|
{ "exec-process-spec", 0, 0, G_OPTION_ARG_STRING, &exec_process_spec, "Path to the process spec for exec", NULL },
|
2016-10-07 15:59:39 +00:00
|
|
|
{ "log-path", 'l', 0, G_OPTION_ARG_STRING, &log_path, "Log file path", NULL },
|
2016-10-01 03:26:29 +00:00
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
2017-06-02 11:37:53 +00:00
|
|
|
/* strlen("1997-03-25T13:20:42.999999999+01:00 stdout ") + 1 */
|
|
|
|
#define TSBUFLEN 44
|
2017-04-06 18:40:12 +00:00
|
|
|
|
2017-05-23 02:01:09 +00:00
|
|
|
#define CGROUP_ROOT "/sys/fs/cgroup"
|
|
|
|
|
2017-06-02 09:52:33 +00:00
|
|
|
static ssize_t write_all(int fd, const void *buf, size_t count)
|
|
|
|
{
|
|
|
|
size_t remaining = count;
|
|
|
|
const char *p = buf;
|
|
|
|
ssize_t res;
|
|
|
|
|
|
|
|
while (remaining > 0) {
|
|
|
|
do {
|
|
|
|
res = write(fd, p, remaining);
|
|
|
|
} while (res == -1 && errno == EINTR);
|
|
|
|
|
|
|
|
if (res <= 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
remaining -= res;
|
|
|
|
p += res;
|
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2017-06-02 11:37:53 +00:00
|
|
|
#define WRITEV_BUFFER_N_IOV 128
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
int iovcnt;
|
|
|
|
struct iovec iov[WRITEV_BUFFER_N_IOV];
|
|
|
|
} writev_buffer_t;
|
|
|
|
|
|
|
|
static ssize_t writev_buffer_flush (int fd, writev_buffer_t *buf)
|
|
|
|
{
|
|
|
|
size_t count = 0;
|
|
|
|
ssize_t res;
|
|
|
|
struct iovec *iov;
|
|
|
|
int iovcnt;
|
|
|
|
|
|
|
|
iovcnt = buf->iovcnt;
|
|
|
|
iov = buf->iov;
|
|
|
|
|
|
|
|
while (iovcnt > 0) {
|
|
|
|
do {
|
|
|
|
res = writev(fd, iov, iovcnt);
|
|
|
|
} while (res == -1 && errno == EINTR);
|
|
|
|
|
|
|
|
if (res <= 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
count += res;
|
|
|
|
|
|
|
|
while (res > 0) {
|
|
|
|
size_t from_this = MIN((size_t)res, iov->iov_len);
|
|
|
|
iov->iov_len -= from_this;
|
|
|
|
res -= from_this;
|
|
|
|
|
|
|
|
if (iov->iov_len == 0) {
|
|
|
|
iov++;
|
|
|
|
iovcnt--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
buf->iovcnt = 0;
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t writev_buffer_append_segment(int fd, writev_buffer_t *buf, const void *data, ssize_t len)
|
|
|
|
{
|
|
|
|
if (data == NULL)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
if (len < 0)
|
|
|
|
len = strlen ((char *)data);
|
|
|
|
|
|
|
|
if (buf->iovcnt == WRITEV_BUFFER_N_IOV &&
|
|
|
|
writev_buffer_flush (fd, buf) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (len > 0) {
|
|
|
|
buf->iov[buf->iovcnt].iov_base = (void *)data;
|
|
|
|
buf->iov[buf->iovcnt].iov_len = (size_t)len;
|
|
|
|
buf->iovcnt++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int set_k8s_timestamp(char *buf, ssize_t buflen, const char *pipename)
|
2017-03-31 23:35:31 +00:00
|
|
|
{
|
|
|
|
struct tm *tm;
|
2017-04-25 21:10:36 +00:00
|
|
|
struct timespec ts;
|
2017-03-31 23:35:31 +00:00
|
|
|
char off_sign = '+';
|
2017-04-06 18:40:12 +00:00
|
|
|
int off, len, err = -1;
|
|
|
|
|
2017-04-25 21:10:36 +00:00
|
|
|
if (clock_gettime(CLOCK_REALTIME, &ts) < 0) {
|
|
|
|
/* If CLOCK_REALTIME is not supported, we set nano seconds to 0 */
|
|
|
|
if (errno == EINVAL) {
|
|
|
|
ts.tv_nsec = 0;
|
|
|
|
} else {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((tm = localtime(&ts.tv_sec)) == NULL)
|
2017-04-06 18:40:12 +00:00
|
|
|
return err;
|
2017-03-31 23:35:31 +00:00
|
|
|
|
2017-04-25 21:10:36 +00:00
|
|
|
|
2017-03-31 23:35:31 +00:00
|
|
|
off = (int) tm->tm_gmtoff;
|
|
|
|
if (tm->tm_gmtoff < 0) {
|
|
|
|
off_sign = '-';
|
|
|
|
off = -off;
|
|
|
|
}
|
2017-04-06 18:40:12 +00:00
|
|
|
|
2017-06-02 11:37:53 +00:00
|
|
|
len = snprintf(buf, buflen, "%d-%02d-%02dT%02d:%02d:%02d.%09ld%c%02d:%02d %s ",
|
2017-04-06 18:40:12 +00:00
|
|
|
tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
|
2017-04-25 21:10:36 +00:00
|
|
|
tm->tm_hour, tm->tm_min, tm->tm_sec, ts.tv_nsec,
|
2017-06-02 11:37:53 +00:00
|
|
|
off_sign, off / 3600, off % 3600, pipename);
|
2017-04-06 18:40:12 +00:00
|
|
|
|
|
|
|
if (len < buflen)
|
|
|
|
err = 0;
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2017-04-11 18:20:57 +00:00
|
|
|
/* stdpipe_t represents one of the std pipes (or NONE). */
|
|
|
|
typedef enum {
|
|
|
|
NO_PIPE,
|
|
|
|
STDIN_PIPE, /* unused */
|
|
|
|
STDOUT_PIPE,
|
|
|
|
STDERR_PIPE,
|
|
|
|
} stdpipe_t;
|
|
|
|
|
|
|
|
const char *stdpipe_name(stdpipe_t pipe)
|
|
|
|
{
|
|
|
|
switch (pipe) {
|
|
|
|
case STDIN_PIPE:
|
|
|
|
return "stdin";
|
|
|
|
case STDOUT_PIPE:
|
|
|
|
return "stdout";
|
|
|
|
case STDERR_PIPE:
|
|
|
|
return "stderr";
|
|
|
|
default:
|
|
|
|
return "NONE";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-06 18:40:12 +00:00
|
|
|
/*
|
|
|
|
* The CRI requires us to write logs with a (timestamp, stream, line) format
|
|
|
|
* for every newline-separated line. write_k8s_log writes said format for every
|
|
|
|
* line in buf, and will partially write the final line of the log if buf is
|
|
|
|
* not terminated by a newline.
|
|
|
|
*/
|
2017-04-11 18:20:57 +00:00
|
|
|
int write_k8s_log(int fd, stdpipe_t pipe, const char *buf, ssize_t buflen)
|
2017-04-06 18:40:12 +00:00
|
|
|
{
|
|
|
|
char tsbuf[TSBUFLEN];
|
2017-04-11 18:20:57 +00:00
|
|
|
static stdpipe_t trailing_line = NO_PIPE;
|
2017-06-02 11:37:53 +00:00
|
|
|
writev_buffer_t bufv = {0};
|
2017-04-06 18:40:12 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Use the same timestamp for every line of the log in this buffer.
|
|
|
|
* There is no practical difference in the output since write(2) is
|
|
|
|
* fast.
|
|
|
|
*/
|
2017-06-02 11:37:53 +00:00
|
|
|
if (set_k8s_timestamp(tsbuf, sizeof tsbuf, stdpipe_name(pipe)))
|
2017-04-06 18:40:12 +00:00
|
|
|
/* TODO: We should handle failures much more cleanly than this. */
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
while (buflen > 0) {
|
|
|
|
const char *line_end = NULL;
|
|
|
|
ptrdiff_t line_len = 0;
|
|
|
|
|
|
|
|
/* Find the end of the line, or alternatively the end of the buffer. */
|
|
|
|
line_end = memchr(buf, '\n', buflen);
|
|
|
|
if (line_end == NULL)
|
|
|
|
line_end = &buf[buflen-1];
|
|
|
|
line_len = line_end - buf + 1;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Write the (timestamp, stream) tuple if there isn't any trailing
|
2017-04-11 18:20:57 +00:00
|
|
|
* output from the previous line (or if there is trailing output but
|
|
|
|
* the current buffer being printed is from a different pipe).
|
2017-04-06 18:40:12 +00:00
|
|
|
*/
|
2017-04-11 18:20:57 +00:00
|
|
|
if (trailing_line != pipe) {
|
|
|
|
/*
|
|
|
|
* If there was a trailing line from a different pipe, prepend a
|
|
|
|
* newline to split it properly. This technically breaks the flow
|
|
|
|
* of the previous line (adding a newline in the log where there
|
|
|
|
* wasn't one output) but without modifying the file in a
|
|
|
|
* non-append-only way there's not much we can do.
|
|
|
|
*/
|
2017-06-02 11:37:53 +00:00
|
|
|
if ((trailing_line != NO_PIPE &&
|
|
|
|
writev_buffer_append_segment(fd, &bufv, "\n", -1) < 0) ||
|
|
|
|
writev_buffer_append_segment(fd, &bufv, tsbuf, -1) < 0) {
|
2017-04-06 18:40:12 +00:00
|
|
|
nwarn("failed to write (timestamp, stream) to log");
|
|
|
|
goto next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Output the actual contents. */
|
2017-06-02 11:37:53 +00:00
|
|
|
if (writev_buffer_append_segment(fd, &bufv, buf, line_len) < 0) {
|
2017-04-06 18:40:12 +00:00
|
|
|
nwarn("failed to write buffer to log");
|
|
|
|
goto next;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If we did not output a full line, then we are a trailing_line. */
|
2017-04-11 18:20:57 +00:00
|
|
|
trailing_line = (*line_end == '\n') ? NO_PIPE : pipe;
|
2017-04-06 18:40:12 +00:00
|
|
|
|
|
|
|
next:
|
|
|
|
/* Update the head of the buffer remaining to output. */
|
|
|
|
buf += line_len;
|
|
|
|
buflen -= line_len;
|
|
|
|
}
|
2017-03-31 23:35:31 +00:00
|
|
|
|
2017-06-02 11:37:53 +00:00
|
|
|
if (writev_buffer_flush (fd, &bufv) < 0) {
|
|
|
|
nwarn("failed to flush buffer to log");
|
|
|
|
}
|
|
|
|
|
2017-03-31 23:35:31 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-05-23 02:01:09 +00:00
|
|
|
/*
|
|
|
|
* Returns the path for specified controller name for a pid.
|
|
|
|
* Returns NULL on error.
|
|
|
|
*/
|
|
|
|
static char *process_cgroup_subsystem_path(int pid, const char *subsystem) {
|
|
|
|
_cleanup_free_ char *cgroups_file_path = NULL;
|
|
|
|
int rc;
|
|
|
|
rc = asprintf(&cgroups_file_path, "/proc/%d/cgroup", pid);
|
|
|
|
if (rc < 0) {
|
|
|
|
nwarn("Failed to allocate memory for cgroups file path");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
_cleanup_fclose_ FILE *fp = NULL;
|
2017-06-02 12:48:00 +00:00
|
|
|
fp = fopen(cgroups_file_path, "re");
|
2017-05-23 02:01:09 +00:00
|
|
|
if (fp == NULL) {
|
|
|
|
nwarn("Failed to open cgroups file: %s", cgroups_file_path);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
_cleanup_free_ char *line = NULL;
|
|
|
|
ssize_t read;
|
|
|
|
size_t len = 0;
|
2017-06-02 12:02:38 +00:00
|
|
|
char *ptr, *path;
|
2017-05-23 02:01:09 +00:00
|
|
|
char *subsystem_path = NULL;
|
2017-06-02 12:02:38 +00:00
|
|
|
int i;
|
2017-05-23 02:01:09 +00:00
|
|
|
while ((read = getline(&line, &len, fp)) != -1) {
|
2017-06-02 12:02:38 +00:00
|
|
|
_cleanup_strv_ char **subsystems = NULL;
|
2017-05-23 02:01:09 +00:00
|
|
|
ptr = strchr(line, ':');
|
|
|
|
if (ptr == NULL) {
|
|
|
|
nwarn("Error parsing cgroup, ':' not found: %s", line);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
ptr++;
|
2017-06-02 12:02:38 +00:00
|
|
|
path = strchr(ptr, ':');
|
|
|
|
if (path == NULL) {
|
|
|
|
nwarn("Error parsing cgroup, second ':' not found: %s", line);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
*path = 0;
|
|
|
|
path++;
|
|
|
|
subsystems = g_strsplit (ptr, ",", -1);
|
|
|
|
for (i = 0; subsystems[i] != NULL; i++) {
|
|
|
|
if (strcmp (subsystems[i], subsystem) == 0) {
|
|
|
|
char *subpath = strchr(subsystems[i], '=');
|
|
|
|
if (subpath == NULL) {
|
|
|
|
subpath = ptr;
|
|
|
|
} else {
|
|
|
|
*subpath = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = asprintf(&subsystem_path, "%s/%s%s", CGROUP_ROOT, subpath, path);
|
|
|
|
if (rc < 0) {
|
|
|
|
nwarn("Failed to allocate memory for subsystemd path");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
subsystem_path[strlen(subsystem_path) - 1] = '\0';
|
|
|
|
return subsystem_path;
|
2017-05-23 02:01:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-06-02 13:19:15 +00:00
|
|
|
static char *escape_json_string(const char *str)
|
|
|
|
{
|
|
|
|
GString *escaped;
|
|
|
|
const char *p;
|
|
|
|
|
|
|
|
p = str;
|
|
|
|
escaped = g_string_sized_new(strlen(str));
|
|
|
|
|
|
|
|
while (*p != 0) {
|
|
|
|
char c = *p++;
|
|
|
|
if (c == '\\' || c == '"') {
|
|
|
|
g_string_append_c(escaped, '\\');
|
|
|
|
g_string_append_c(escaped, c);
|
|
|
|
} else if (c == '\n') {
|
|
|
|
g_string_append_printf (escaped, "\\n");
|
|
|
|
} else if (c == '\t') {
|
|
|
|
g_string_append_printf (escaped, "\\t");
|
|
|
|
} else if ((c > 0 && c < 0x1f) || c == 0x7f) {
|
|
|
|
g_string_append_printf (escaped, "\\u00%02x", (guint)c);
|
|
|
|
} else {
|
|
|
|
g_string_append_c (escaped, c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return g_string_free (escaped, FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-26 21:00:30 +00:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2017-01-10 22:08:54 +00:00
|
|
|
int ret, runtime_status;
|
2016-11-14 16:46:58 +00:00
|
|
|
char cwd[PATH_MAX];
|
|
|
|
char default_pid_file[PATH_MAX];
|
2017-05-30 21:35:12 +00:00
|
|
|
char attach_sock_path[PATH_MAX];
|
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;
|
2016-10-07 15:59:39 +00:00
|
|
|
pid_t pid, create_pid;
|
|
|
|
_cleanup_close_ int logfd = -1;
|
2017-04-11 18:20:57 +00:00
|
|
|
_cleanup_close_ int masterfd_stdout = -1;
|
|
|
|
_cleanup_close_ int masterfd_stderr = -1;
|
2016-09-02 21:33:54 +00:00
|
|
|
_cleanup_close_ int epfd = -1;
|
2016-10-07 15:59:39 +00:00
|
|
|
_cleanup_close_ int csfd = -1;
|
2017-04-11 18:20:57 +00:00
|
|
|
/* Used for !terminal cases. */
|
|
|
|
int slavefd_stdout = -1;
|
|
|
|
int slavefd_stderr = -1;
|
2016-10-07 15:59:39 +00:00
|
|
|
char csname[PATH_MAX] = "/tmp/conmon-term.XXXXXXXX";
|
2016-09-02 21:33:54 +00:00
|
|
|
char buf[BUF_SIZE];
|
|
|
|
int num_read;
|
|
|
|
struct epoll_event ev;
|
|
|
|
struct epoll_event evlist[MAX_EVENTS];
|
2017-01-10 22:08:54 +00:00
|
|
|
int sync_pipe_fd = -1;
|
2016-09-15 23:40:44 +00:00
|
|
|
char *sync_pipe, *endptr;
|
|
|
|
int len;
|
2017-04-11 18:20:57 +00:00
|
|
|
int num_stdio_fds = 0;
|
2016-10-01 03:26:29 +00:00
|
|
|
GError *error = NULL;
|
|
|
|
GOptionContext *context;
|
2017-06-02 12:27:00 +00:00
|
|
|
GPtrArray *runtime_argv = NULL;
|
2016-10-01 03:26:29 +00:00
|
|
|
|
2017-05-23 02:02:21 +00:00
|
|
|
/* Used for OOM notification API */
|
|
|
|
_cleanup_close_ int efd = -1;
|
|
|
|
_cleanup_close_ int cfd = -1;
|
|
|
|
_cleanup_close_ int ofd = -1;
|
|
|
|
_cleanup_free_ char *memory_cgroup_path = NULL;
|
|
|
|
int wb;
|
|
|
|
uint64_t oom_event;
|
|
|
|
|
2017-05-30 21:35:12 +00:00
|
|
|
/* Used for attach */
|
|
|
|
_cleanup_close_ int conn_sock = -1;
|
|
|
|
|
2016-10-01 03:26:29 +00:00
|
|
|
/* Command line parameters */
|
2016-10-07 15:59:39 +00:00
|
|
|
context = g_option_context_new("- conmon utility");
|
|
|
|
g_option_context_add_main_entries(context, entries, "conmon");
|
|
|
|
if (!g_option_context_parse(context, &argc, &argv, &error)) {
|
|
|
|
g_print("option parsing failed: %s\n", error->message);
|
|
|
|
exit(1);
|
2016-08-26 21:00:30 +00:00
|
|
|
}
|
|
|
|
|
2016-09-17 09:37:45 +00:00
|
|
|
if (cid == NULL)
|
2016-10-01 03:26:29 +00:00
|
|
|
nexit("Container ID not provided. Use --cid");
|
2016-09-17 09:37:45 +00:00
|
|
|
|
2017-05-30 21:35:12 +00:00
|
|
|
if (!exec && cuuid == NULL)
|
|
|
|
nexit("Container UUID not provided. Use --cuuid");
|
|
|
|
|
2016-09-17 09:37:45 +00:00
|
|
|
if (runtime_path == NULL)
|
2016-10-01 03:26:29 +00:00
|
|
|
nexit("Runtime path not provided. Use --runtime");
|
2016-08-26 21:00:30 +00:00
|
|
|
|
2017-01-10 22:08:54 +00:00
|
|
|
if (bundle_path == NULL && !exec) {
|
2016-11-14 16:46:58 +00:00
|
|
|
if (getcwd(cwd, sizeof(cwd)) == NULL) {
|
|
|
|
nexit("Failed to get working directory");
|
|
|
|
}
|
|
|
|
bundle_path = cwd;
|
|
|
|
}
|
|
|
|
|
2017-05-25 09:11:14 +00:00
|
|
|
if (exec && exec_process_spec == NULL) {
|
|
|
|
nexit("Exec process spec path not provided. Use --exec-process-spec");
|
|
|
|
}
|
|
|
|
|
2016-11-14 16:46:58 +00:00
|
|
|
if (pid_file == NULL) {
|
|
|
|
if (snprintf(default_pid_file, sizeof(default_pid_file),
|
|
|
|
"%s/pidfile-%s", cwd, cid) < 0) {
|
|
|
|
nexit("Failed to generate the pidfile path");
|
|
|
|
}
|
|
|
|
pid_file = default_pid_file;
|
|
|
|
}
|
|
|
|
|
2016-10-07 15:59:39 +00:00
|
|
|
if (log_path == NULL)
|
|
|
|
nexit("Log file path not provided. Use --log-path");
|
|
|
|
|
2016-10-01 03:26:29 +00:00
|
|
|
/* Environment variables */
|
2016-09-15 23:40:44 +00:00
|
|
|
sync_pipe = getenv("_OCI_SYNCPIPE");
|
|
|
|
if (sync_pipe) {
|
|
|
|
errno = 0;
|
2017-01-10 22:08:54 +00:00
|
|
|
sync_pipe_fd = strtol(sync_pipe, &endptr, 10);
|
2016-09-15 23:40:44 +00:00
|
|
|
if (errno != 0 || *endptr != '\0')
|
|
|
|
pexit("unable to parse _OCI_SYNCPIPE");
|
2017-06-02 12:48:00 +00:00
|
|
|
if (fcntl(sync_pipe_fd, F_SETFD, FD_CLOEXEC) == -1)
|
|
|
|
pexit("unable to make _OCI_SYNCPIPE CLOEXEC");
|
2016-09-15 23:40:44 +00:00
|
|
|
}
|
|
|
|
|
2016-10-07 15:59:39 +00:00
|
|
|
/* Open the log path file. */
|
2017-06-02 23:29:19 +00:00
|
|
|
logfd = open(log_path, O_WRONLY | O_APPEND | O_CREAT | O_CLOEXEC, 0600);
|
2016-10-07 15:59:39 +00:00
|
|
|
if (logfd < 0)
|
|
|
|
pexit("Failed to open log file");
|
|
|
|
|
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) {
|
2016-10-07 15:59:39 +00:00
|
|
|
struct sockaddr_un addr = {0};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Generate a temporary name. Is this unsafe? Probably, but we can
|
|
|
|
* replace it with a rename(2) setup if necessary.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int unusedfd = g_mkstemp(csname);
|
|
|
|
if (unusedfd < 0)
|
|
|
|
pexit("Failed to generate random path for console-socket");
|
|
|
|
close(unusedfd);
|
|
|
|
|
|
|
|
addr.sun_family = AF_UNIX;
|
|
|
|
strncpy(addr.sun_path, csname, sizeof(addr.sun_path)-1);
|
|
|
|
|
|
|
|
ninfo("addr{sun_family=AF_UNIX, sun_path=%s}", addr.sun_path);
|
|
|
|
|
|
|
|
/* Bind to the console socket path. */
|
|
|
|
csfd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0);
|
|
|
|
if (csfd < 0)
|
|
|
|
pexit("Failed to create console-socket");
|
2017-05-30 21:35:12 +00:00
|
|
|
if (fchmod(csfd, 0700))
|
2017-06-02 13:31:43 +00:00
|
|
|
pexit("Failed to change console-socket permissions");
|
2016-10-07 15:59:39 +00:00
|
|
|
/* XXX: This should be handled with a rename(2). */
|
|
|
|
if (unlink(csname) < 0)
|
|
|
|
pexit("Failed to unlink temporary ranom path");
|
|
|
|
if (bind(csfd, (struct sockaddr *) &addr, sizeof(addr)) < 0)
|
|
|
|
pexit("Failed to bind to console-socket");
|
|
|
|
if (listen(csfd, 128) < 0)
|
|
|
|
pexit("Failed to listen on console-socket");
|
|
|
|
} else {
|
|
|
|
int fds[2];
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create a "fake" master fd so that we can use the same epoll code in
|
2017-04-11 18:20:57 +00:00
|
|
|
* both cases. The slavefd_*s will be closed after we dup over
|
2016-10-07 15:59:39 +00:00
|
|
|
* everything.
|
|
|
|
*
|
2017-04-04 10:04:03 +00:00
|
|
|
* We use pipes here because open(/dev/std{out,err}) will fail if we
|
|
|
|
* used anything else (and it wouldn't be a good idea to create a new
|
|
|
|
* pty pair in the host).
|
2016-10-07 15:59:39 +00:00
|
|
|
*/
|
2017-06-02 12:48:00 +00:00
|
|
|
if (pipe2(fds, O_CLOEXEC) < 0)
|
2017-04-11 18:20:57 +00:00
|
|
|
pexit("Failed to create !terminal stdout pipe");
|
|
|
|
|
|
|
|
masterfd_stdout = fds[0];
|
|
|
|
slavefd_stdout = fds[1];
|
2016-10-07 15:59:39 +00:00
|
|
|
|
2017-06-02 12:48:00 +00:00
|
|
|
if (pipe2(fds, O_CLOEXEC) < 0)
|
2017-04-11 18:20:57 +00:00
|
|
|
pexit("Failed to create !terminal stderr pipe");
|
|
|
|
|
|
|
|
masterfd_stderr = fds[0];
|
|
|
|
slavefd_stderr = fds[1];
|
2016-09-02 21:33:54 +00:00
|
|
|
}
|
|
|
|
|
2017-06-02 12:27:00 +00:00
|
|
|
runtime_argv = g_ptr_array_new();
|
|
|
|
g_ptr_array_add(runtime_argv, runtime_path);
|
2017-01-10 22:08:54 +00:00
|
|
|
|
2016-10-07 15:59:39 +00:00
|
|
|
/* Generate the cmdline. */
|
2017-04-21 21:20:17 +00:00
|
|
|
if (!exec && systemd_cgroup)
|
2017-06-02 12:27:00 +00:00
|
|
|
g_ptr_array_add(runtime_argv, "--systemd-cgroup");
|
2016-10-07 15:59:39 +00:00
|
|
|
|
2017-06-02 12:27:00 +00:00
|
|
|
if (exec) {
|
|
|
|
g_ptr_array_add (runtime_argv, "exec");
|
|
|
|
g_ptr_array_add (runtime_argv, "-d");
|
|
|
|
g_ptr_array_add (runtime_argv, "--pid-file");
|
|
|
|
g_ptr_array_add (runtime_argv, pid_file);
|
|
|
|
} else {
|
|
|
|
g_ptr_array_add (runtime_argv, "create");
|
|
|
|
g_ptr_array_add (runtime_argv, "--bundle");
|
|
|
|
g_ptr_array_add (runtime_argv, bundle_path);
|
|
|
|
g_ptr_array_add (runtime_argv, "--pid-file");
|
|
|
|
g_ptr_array_add (runtime_argv, pid_file);
|
|
|
|
}
|
2017-01-10 22:08:54 +00:00
|
|
|
|
2017-06-02 12:27:00 +00:00
|
|
|
if (terminal) {
|
|
|
|
g_ptr_array_add(runtime_argv, "--console-socket");
|
|
|
|
g_ptr_array_add(runtime_argv, csname);
|
|
|
|
}
|
2016-10-07 15:59:39 +00:00
|
|
|
|
|
|
|
/* Set the exec arguments. */
|
|
|
|
if (exec) {
|
2017-06-02 12:27:00 +00:00
|
|
|
g_ptr_array_add(runtime_argv, "--process");
|
|
|
|
g_ptr_array_add(runtime_argv, exec_process_spec);
|
2016-10-07 15:59:39 +00:00
|
|
|
}
|
|
|
|
|
2017-05-25 09:11:14 +00:00
|
|
|
/* Container name comes last. */
|
2017-06-02 12:27:00 +00:00
|
|
|
g_ptr_array_add(runtime_argv, cid);
|
|
|
|
g_ptr_array_add(runtime_argv, NULL);
|
2017-05-25 09:11:14 +00:00
|
|
|
|
2016-10-07 15:59:39 +00:00
|
|
|
/*
|
|
|
|
* We have to fork here because the current runC API dups the stdio of the
|
|
|
|
* calling process over the container's fds. This is actually *very bad*
|
|
|
|
* but is currently being discussed for change in
|
|
|
|
* https://github.com/opencontainers/runtime-spec/pull/513. Hopefully this
|
|
|
|
* won't be the case for very long.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Create our container. */
|
|
|
|
create_pid = fork();
|
|
|
|
if (create_pid < 0) {
|
|
|
|
pexit("Failed to fork the create command");
|
|
|
|
} else if (!create_pid) {
|
|
|
|
/* We only need to touch the stdio if we have terminal=false. */
|
2017-05-12 13:36:15 +00:00
|
|
|
/* FIXME: This results in us not outputting runc error messages to crio's log. */
|
2017-04-11 18:20:57 +00:00
|
|
|
if (slavefd_stdout >= 0) {
|
|
|
|
if (dup2(slavefd_stdout, STDOUT_FILENO) < 0)
|
2016-10-07 15:59:39 +00:00
|
|
|
pexit("Failed to dup over stdout");
|
2017-04-11 18:20:57 +00:00
|
|
|
}
|
|
|
|
if (slavefd_stderr >= 0) {
|
|
|
|
if (dup2(slavefd_stderr, STDERR_FILENO) < 0)
|
2016-10-07 15:59:39 +00:00
|
|
|
pexit("Failed to dup over stderr");
|
2017-01-10 22:08:54 +00:00
|
|
|
}
|
2016-10-07 15:59:39 +00:00
|
|
|
|
2017-06-02 12:27:00 +00:00
|
|
|
execv(g_ptr_array_index(runtime_argv,0), (char **)runtime_argv->pdata);
|
2016-10-07 15:59:39 +00:00
|
|
|
exit(127);
|
2016-09-12 23:38:30 +00:00
|
|
|
}
|
2017-01-10 22:08:54 +00:00
|
|
|
|
2017-06-02 12:27:00 +00:00
|
|
|
g_ptr_array_free (runtime_argv, TRUE);
|
|
|
|
|
2016-10-07 15:59:39 +00:00
|
|
|
/* The runtime has that fd now. We don't need to touch it anymore. */
|
2017-04-11 18:20:57 +00:00
|
|
|
close(slavefd_stdout);
|
|
|
|
close(slavefd_stderr);
|
2016-10-07 15:59:39 +00:00
|
|
|
|
|
|
|
/* Get the console fd. */
|
|
|
|
/*
|
|
|
|
* FIXME: If runc fails to start a container, we won't bail because we're
|
|
|
|
* busy waiting for requests. The solution probably involves
|
|
|
|
* epoll(2) and a signalfd(2). This causes a lot of issues.
|
|
|
|
*/
|
|
|
|
if (terminal) {
|
|
|
|
struct file_t console;
|
|
|
|
int connfd = -1;
|
|
|
|
|
|
|
|
ninfo("about to accept from csfd: %d", csfd);
|
|
|
|
connfd = accept4(csfd, NULL, NULL, SOCK_CLOEXEC);
|
|
|
|
if (connfd < 0)
|
|
|
|
pexit("Failed to accept console-socket connection");
|
|
|
|
|
|
|
|
/* Not accepting anything else. */
|
|
|
|
close(csfd);
|
|
|
|
unlink(csname);
|
|
|
|
|
|
|
|
/* We exit if this fails. */
|
|
|
|
ninfo("about to recvfd from connfd: %d", connfd);
|
|
|
|
console = recvfd(connfd);
|
|
|
|
|
|
|
|
ninfo("console = {.name = '%s'; .fd = %d}", console.name, console.fd);
|
|
|
|
free(console.name);
|
|
|
|
|
2017-04-11 18:20:57 +00:00
|
|
|
/* We only have a single fd for both pipes, so we just treat it as
|
|
|
|
* stdout. stderr is ignored. */
|
|
|
|
masterfd_stdout = console.fd;
|
|
|
|
masterfd_stderr = -1;
|
2016-10-07 15:59:39 +00:00
|
|
|
|
|
|
|
/* Clean up everything */
|
|
|
|
close(connfd);
|
2016-08-26 21:00:30 +00:00
|
|
|
}
|
|
|
|
|
2016-10-07 15:59:39 +00:00
|
|
|
ninfo("about to waitpid: %d", create_pid);
|
|
|
|
|
|
|
|
/* Wait for our create child to exit with the return code. */
|
|
|
|
if (waitpid(create_pid, &runtime_status, 0) < 0) {
|
|
|
|
int old_errno = errno;
|
|
|
|
kill(create_pid, SIGKILL);
|
|
|
|
errno = old_errno;
|
|
|
|
pexit("Failed to wait for `runtime %s`", exec ? "exec" : "create");
|
|
|
|
}
|
2017-05-12 20:30:30 +00:00
|
|
|
if (!WIFEXITED(runtime_status) || WEXITSTATUS(runtime_status) != 0) {
|
|
|
|
if (sync_pipe_fd > 0 && !exec) {
|
|
|
|
if (terminal) {
|
|
|
|
/*
|
|
|
|
* For this case, the stderr is captured in the parent when terminal is passed down.
|
|
|
|
* We send -1 as pid to signal to parent that create container has failed.
|
|
|
|
*/
|
|
|
|
len = snprintf(buf, BUF_SIZE, "{\"pid\": %d}\n", -1);
|
2017-06-02 09:52:33 +00:00
|
|
|
if (len < 0 || write_all(sync_pipe_fd, buf, len) != len) {
|
2017-05-12 20:30:30 +00:00
|
|
|
pexit("unable to send container pid to parent");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Read from container stderr for any error and send it to parent
|
|
|
|
* We send -1 as pid to signal to parent that create container has failed.
|
|
|
|
*/
|
|
|
|
num_read = read(masterfd_stderr, buf, BUF_SIZE);
|
|
|
|
if (num_read > 0) {
|
2017-06-02 13:19:15 +00:00
|
|
|
_cleanup_free_ char *escaped_message = NULL;
|
|
|
|
ssize_t len;
|
2017-05-12 20:30:30 +00:00
|
|
|
|
2017-06-02 13:19:15 +00:00
|
|
|
buf[num_read] = '\0';
|
|
|
|
escaped_message = escape_json_string(buf);
|
2017-05-12 20:30:30 +00:00
|
|
|
|
2017-06-02 13:19:15 +00:00
|
|
|
len = snprintf(buf, BUF_SIZE, "{\"pid\": %d, \"message\": \"%s\"}\n", -1, escaped_message);
|
|
|
|
if (len < 0 || write_all(sync_pipe_fd, buf, len) != len) {
|
2017-05-12 20:30:30 +00:00
|
|
|
ninfo("Unable to send container stderr message to parent");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-10-07 15:59:39 +00:00
|
|
|
nexit("Failed to create container: exit status %d", WEXITSTATUS(runtime_status));
|
2017-05-12 20:30:30 +00:00
|
|
|
}
|
2016-10-07 15:59:39 +00:00
|
|
|
|
2016-09-02 21:33:54 +00:00
|
|
|
/* Read the pid so we can wait for the process to exit */
|
2017-01-10 21:48:47 +00:00
|
|
|
g_file_get_contents(pid_file, &contents, NULL, &err);
|
2016-08-26 21:00:30 +00:00
|
|
|
if (err) {
|
2016-10-07 15:59:39 +00:00
|
|
|
nwarn("Failed to read pidfile: %s", err->message);
|
2016-08-26 21:00:30 +00:00
|
|
|
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);
|
2016-10-07 15:59:39 +00:00
|
|
|
ninfo("container PID: %d", cpid);
|
2016-08-26 21:00:30 +00:00
|
|
|
|
2017-05-30 21:35:12 +00:00
|
|
|
/* Setup endpoint for attach */
|
|
|
|
char attach_symlink_dir_path[PATH_MAX];
|
|
|
|
struct sockaddr_un attach_addr = {0};
|
|
|
|
_cleanup_close_ int afd = -1;
|
|
|
|
|
|
|
|
if (!exec) {
|
|
|
|
attach_addr.sun_family = AF_UNIX;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create a symlink so we don't exceed unix domain socket
|
|
|
|
* path length limit.
|
|
|
|
*/
|
|
|
|
snprintf(attach_symlink_dir_path, PATH_MAX, "/var/run/crio/%s", cuuid);
|
|
|
|
if (unlink(attach_symlink_dir_path) == -1 && errno != ENOENT) {
|
|
|
|
pexit("Failed to remove existing symlink for attach socket directory");
|
|
|
|
}
|
|
|
|
if (symlink(bundle_path, attach_symlink_dir_path) == -1)
|
|
|
|
pexit("Failed to create symlink for attach socket");
|
|
|
|
|
|
|
|
snprintf(attach_sock_path, PATH_MAX, "/var/run/crio/%s/attach", cuuid);
|
|
|
|
ninfo("attach sock path: %s", attach_sock_path);
|
|
|
|
|
|
|
|
strncpy(attach_addr.sun_path, attach_sock_path, sizeof(attach_addr.sun_path) - 1);
|
|
|
|
ninfo("addr{sun_family=AF_UNIX, sun_path=%s}", attach_addr.sun_path);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We make the socket non-blocking to avoid a race where client aborts connection
|
|
|
|
* before the server gets a chance to call accept. In that scenario, the server
|
|
|
|
* accept blocks till a new client connection comes in.
|
|
|
|
*/
|
|
|
|
afd = socket(AF_UNIX, SOCK_STREAM|SOCK_NONBLOCK|SOCK_CLOEXEC, 0);
|
|
|
|
if (afd == -1)
|
|
|
|
pexit("Failed to create attach socket");
|
|
|
|
|
|
|
|
if (fchmod(afd, 0700))
|
|
|
|
pexit("Failed to change attach socket permissions");
|
|
|
|
|
|
|
|
if (bind(afd, (struct sockaddr *)&attach_addr, sizeof(struct sockaddr_un)) == -1)
|
|
|
|
pexit("Failed to bind attach socket: %s", attach_sock_path);
|
|
|
|
|
|
|
|
if (listen(afd, 10) == -1)
|
|
|
|
pexit("Failed to listen on attach socket: %s", attach_sock_path);
|
|
|
|
}
|
|
|
|
|
2016-09-15 23:40:44 +00:00
|
|
|
/* Send the container pid back to parent */
|
2016-10-07 15:59:39 +00:00
|
|
|
if (sync_pipe_fd > 0 && !exec) {
|
2016-09-15 23:40:44 +00:00
|
|
|
len = snprintf(buf, BUF_SIZE, "{\"pid\": %d}\n", cpid);
|
2017-06-02 09:52:33 +00:00
|
|
|
if (len < 0 || write_all(sync_pipe_fd, buf, len) != len) {
|
2016-09-15 23:40:44 +00:00
|
|
|
pexit("unable to send container pid to parent");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-23 02:02:21 +00:00
|
|
|
/* Setup OOM notification for container process */
|
|
|
|
memory_cgroup_path = process_cgroup_subsystem_path(cpid, "memory");
|
|
|
|
if (!memory_cgroup_path) {
|
|
|
|
nexit("Failed to get memory cgroup path");
|
|
|
|
}
|
|
|
|
|
2017-05-23 20:24:48 +00:00
|
|
|
bool oom_handling_enabled = true;
|
2017-05-23 02:02:21 +00:00
|
|
|
char memory_cgroup_file_path[PATH_MAX];
|
|
|
|
snprintf(memory_cgroup_file_path, PATH_MAX, "%s/cgroup.event_control", memory_cgroup_path);
|
2017-06-02 12:48:00 +00:00
|
|
|
if ((cfd = open(memory_cgroup_file_path, O_WRONLY | O_CLOEXEC)) == -1) {
|
2017-05-23 20:24:48 +00:00
|
|
|
nwarn("Failed to open %s", memory_cgroup_file_path);
|
|
|
|
oom_handling_enabled = false;
|
|
|
|
}
|
2017-05-23 02:02:21 +00:00
|
|
|
|
2017-05-23 20:24:48 +00:00
|
|
|
if (oom_handling_enabled) {
|
|
|
|
snprintf(memory_cgroup_file_path, PATH_MAX, "%s/memory.oom_control", memory_cgroup_path);
|
2017-06-02 12:48:00 +00:00
|
|
|
if ((ofd = open(memory_cgroup_file_path, O_RDONLY | O_CLOEXEC)) == -1)
|
2017-05-23 20:24:48 +00:00
|
|
|
pexit("Failed to open %s", memory_cgroup_file_path);
|
2017-05-23 02:02:21 +00:00
|
|
|
|
2017-06-02 12:48:00 +00:00
|
|
|
if ((efd = eventfd(0, EFD_CLOEXEC)) == -1)
|
2017-05-23 20:24:48 +00:00
|
|
|
pexit("Failed to create eventfd");
|
2017-05-23 02:02:21 +00:00
|
|
|
|
2017-05-23 20:24:48 +00:00
|
|
|
wb = snprintf(buf, BUF_SIZE, "%d %d", efd, ofd);
|
2017-06-02 09:52:33 +00:00
|
|
|
if (write_all(cfd, buf, wb) < 0)
|
2017-05-23 20:24:48 +00:00
|
|
|
pexit("Failed to write to cgroup.event_control");
|
|
|
|
}
|
2017-05-23 02:02:21 +00:00
|
|
|
|
2016-10-07 15:59:39 +00:00
|
|
|
/* Create epoll_ctl so that we can handle read/write events. */
|
|
|
|
/*
|
|
|
|
* TODO: Switch to libuv so that we can also implement exec as well as
|
|
|
|
* attach and other important things. Using epoll directly is just
|
|
|
|
* really nasty.
|
|
|
|
*/
|
2017-06-02 12:48:00 +00:00
|
|
|
epfd = epoll_create1(EPOLL_CLOEXEC);
|
2016-10-07 15:59:39 +00:00
|
|
|
if (epfd < 0)
|
|
|
|
pexit("epoll_create");
|
|
|
|
ev.events = EPOLLIN;
|
2017-04-11 18:20:57 +00:00
|
|
|
if (masterfd_stdout >= 0) {
|
|
|
|
ev.data.fd = masterfd_stdout;
|
|
|
|
if (epoll_ctl(epfd, EPOLL_CTL_ADD, ev.data.fd, &ev) < 0)
|
|
|
|
pexit("Failed to add console masterfd_stdout to epoll");
|
|
|
|
num_stdio_fds++;
|
2016-10-07 15:59:39 +00:00
|
|
|
}
|
2017-04-11 18:20:57 +00:00
|
|
|
if (masterfd_stderr >= 0) {
|
|
|
|
ev.data.fd = masterfd_stderr;
|
|
|
|
if (epoll_ctl(epfd, EPOLL_CTL_ADD, ev.data.fd, &ev) < 0)
|
|
|
|
pexit("Failed to add console masterfd_stderr to epoll");
|
|
|
|
num_stdio_fds++;
|
2016-10-07 15:59:39 +00:00
|
|
|
}
|
|
|
|
|
2017-05-23 02:03:13 +00:00
|
|
|
/* Add the OOM event fd to epoll */
|
2017-05-23 20:24:48 +00:00
|
|
|
if (oom_handling_enabled) {
|
|
|
|
ev.data.fd = efd;
|
|
|
|
if (epoll_ctl(epfd, EPOLL_CTL_ADD, ev.data.fd, &ev) < 0)
|
|
|
|
pexit("Failed to add OOM eventfd to epoll");
|
|
|
|
}
|
2017-05-23 02:03:13 +00:00
|
|
|
|
2017-05-30 21:35:12 +00:00
|
|
|
/* Add the attach socket to epoll */
|
|
|
|
if (afd > 0) {
|
|
|
|
ev.data.fd = afd;
|
|
|
|
if (epoll_ctl(epfd, EPOLL_CTL_ADD, ev.data.fd, &ev) < 0)
|
|
|
|
pexit("Failed to add attach socket fd to epoll");
|
|
|
|
}
|
|
|
|
|
2017-04-11 18:20:57 +00:00
|
|
|
/* Log all of the container's output. */
|
|
|
|
while (num_stdio_fds > 0) {
|
2016-10-07 15:59:39 +00:00
|
|
|
int ready = epoll_wait(epfd, evlist, MAX_EVENTS, -1);
|
2017-04-11 18:20:57 +00:00
|
|
|
if (ready < 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for (int i = 0; i < ready; i++) {
|
2016-10-07 15:59:39 +00:00
|
|
|
if (evlist[i].events & EPOLLIN) {
|
2017-04-11 18:20:57 +00:00
|
|
|
int masterfd = evlist[i].data.fd;
|
2017-06-02 13:23:11 +00:00
|
|
|
stdpipe_t pipe = NO_PIPE;
|
2017-04-11 18:20:57 +00:00
|
|
|
if (masterfd == masterfd_stdout)
|
|
|
|
pipe = STDOUT_PIPE;
|
|
|
|
else if (masterfd == masterfd_stderr)
|
|
|
|
pipe = STDERR_PIPE;
|
2017-05-23 20:24:48 +00:00
|
|
|
else if (oom_handling_enabled && masterfd == efd) {
|
2017-05-23 02:04:03 +00:00
|
|
|
if (read(efd, &oom_event, sizeof(uint64_t)) != sizeof(uint64_t))
|
|
|
|
nwarn("Failed to read event from eventfd");
|
|
|
|
ninfo("OOM received");
|
|
|
|
if (open("oom", O_CREAT, 0666) < 0) {
|
|
|
|
nwarn("Failed to write oom file");
|
|
|
|
}
|
2017-05-30 21:35:12 +00:00
|
|
|
} else if (evlist[i].data.fd == afd) {
|
|
|
|
conn_sock = accept(afd, NULL, NULL);
|
|
|
|
if (conn_sock == -1) {
|
|
|
|
nwarn("Failed to accept client connection on attach socket");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
ev.events = EPOLLIN;
|
|
|
|
ev.data.fd = conn_sock;
|
|
|
|
if (epoll_ctl(epfd, EPOLL_CTL_ADD, ev.data.fd, &ev) == -1) {
|
|
|
|
pexit("Failed to add client socket fd to epoll");
|
|
|
|
}
|
|
|
|
ninfo("Accepted connection");
|
|
|
|
} else {
|
|
|
|
num_read = read(masterfd, buf, BUF_SIZE);
|
|
|
|
if (num_read <= 0)
|
|
|
|
goto out;
|
|
|
|
ninfo("got data on connection: %d", num_read);
|
|
|
|
if (terminal) {
|
|
|
|
if (write_all(masterfd_stdout, buf, num_read) < 0) {
|
|
|
|
nwarn("Failed to write to master pty");
|
|
|
|
}
|
|
|
|
}
|
2017-04-11 18:20:57 +00:00
|
|
|
}
|
|
|
|
|
2017-05-23 02:04:03 +00:00
|
|
|
if (masterfd == masterfd_stdout || masterfd == masterfd_stderr) {
|
|
|
|
num_read = read(masterfd, buf, BUF_SIZE);
|
|
|
|
if (num_read <= 0)
|
|
|
|
goto out;
|
2017-04-11 18:20:57 +00:00
|
|
|
|
2017-05-23 02:04:03 +00:00
|
|
|
if (write_k8s_log(logfd, pipe, buf, num_read) < 0) {
|
|
|
|
nwarn("write_k8s_log failed");
|
|
|
|
goto out;
|
|
|
|
}
|
2017-05-30 21:35:12 +00:00
|
|
|
|
|
|
|
if (conn_sock > 0) {
|
|
|
|
if (write_all(conn_sock, buf, num_read) < 0) {
|
|
|
|
nwarn("Failed to write to socket");
|
|
|
|
}
|
|
|
|
}
|
2016-09-02 21:33:54 +00:00
|
|
|
}
|
2016-10-07 15:59:39 +00:00
|
|
|
} else if (evlist[i].events & (EPOLLHUP | EPOLLERR)) {
|
|
|
|
printf("closing fd %d\n", evlist[i].data.fd);
|
|
|
|
if (close(evlist[i].data.fd) < 0)
|
|
|
|
pexit("close");
|
2017-05-30 21:35:12 +00:00
|
|
|
if (!exec && evlist[i].data.fd == conn_sock) {
|
|
|
|
conn_sock = -1;
|
|
|
|
continue;
|
|
|
|
}
|
2017-04-11 18:20:57 +00:00
|
|
|
num_stdio_fds--;
|
2016-09-02 21:33:54 +00:00
|
|
|
}
|
|
|
|
}
|
2016-09-12 23:38:30 +00:00
|
|
|
}
|
2016-09-02 21:33:54 +00:00
|
|
|
|
2016-10-07 15:59:39 +00:00
|
|
|
out:
|
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) {
|
2017-01-10 19:19:01 +00:00
|
|
|
int exit_status = WEXITSTATUS(status);
|
|
|
|
|
|
|
|
printf("PID %d exited with status %d\n", pid, exit_status);
|
2016-08-26 21:00:30 +00:00
|
|
|
if (pid == cpid) {
|
2017-01-10 22:08:54 +00:00
|
|
|
if (!exec) {
|
|
|
|
_cleanup_free_ char *status_str = NULL;
|
|
|
|
ret = asprintf(&status_str, "%d", exit_status);
|
|
|
|
if (ret < 0) {
|
|
|
|
pexit("Failed to allocate memory for status");
|
|
|
|
}
|
|
|
|
g_file_set_contents("exit", status_str,
|
|
|
|
strlen(status_str), &err);
|
|
|
|
if (err) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"Failed to write %s to exit file: %s\n",
|
|
|
|
status_str, err->message);
|
|
|
|
g_error_free(err);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* Send the command exec exit code back to the parent */
|
|
|
|
if (sync_pipe_fd > 0) {
|
|
|
|
len = snprintf(buf, BUF_SIZE, "{\"exit_code\": %d}\n", exit_status);
|
2017-06-02 09:52:33 +00:00
|
|
|
if (len < 0 || write_all(sync_pipe_fd, buf, len) != len) {
|
2017-01-10 22:08:54 +00:00
|
|
|
pexit("unable to send exit status");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
2016-08-26 21:00:30 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-10 22:08:54 +00:00
|
|
|
if (exec && pid < 0 && errno == ECHILD && sync_pipe_fd > 0) {
|
|
|
|
/*
|
|
|
|
* waitpid failed and set errno to ECHILD:
|
|
|
|
* The runtime exec call did not create any child
|
|
|
|
* process and we can send the system() exit code
|
|
|
|
* to the parent.
|
|
|
|
*/
|
|
|
|
len = snprintf(buf, BUF_SIZE, "{\"exit_code\": %d}\n", WEXITSTATUS(runtime_status));
|
2017-06-02 09:52:33 +00:00
|
|
|
if (len < 0 || write_all(sync_pipe_fd, buf, len) != len) {
|
2017-01-10 22:08:54 +00:00
|
|
|
pexit("unable to send exit status");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-30 21:35:12 +00:00
|
|
|
if (!exec) {
|
|
|
|
if (unlink(attach_symlink_dir_path) == -1 && errno != ENOENT) {
|
|
|
|
pexit("Failed to remove symlink for attach socket directory");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-26 21:00:30 +00:00
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|