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>
|
2017-09-25 22:31:00 +00:00
|
|
|
#include <stdint.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>
|
2017-05-30 22:14:44 +00:00
|
|
|
#include <sys/ioctl.h>
|
2017-06-15 14:01:53 +00:00
|
|
|
#include <termios.h>
|
2016-11-15 00:02:03 +00:00
|
|
|
#include <syslog.h>
|
2016-09-02 21:33:54 +00:00
|
|
|
#include <unistd.h>
|
2017-09-25 22:31:00 +00:00
|
|
|
#include <inttypes.h>
|
2016-08-26 21:00:30 +00:00
|
|
|
|
|
|
|
#include <glib.h>
|
2017-06-08 08:42:33 +00:00
|
|
|
#include <glib-unix.h>
|
2016-08-26 21:00:30 +00:00
|
|
|
|
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
|
|
|
|
2017-10-24 14:28:25 +00:00
|
|
|
#define DEFAULT_SOCKET_PATH "/var/lib/crio"
|
|
|
|
|
2018-02-22 19:04:12 +00:00
|
|
|
static bool opt_version = false;
|
2017-06-22 08:45:14 +00:00
|
|
|
static bool opt_terminal = false;
|
2017-06-08 20:08:29 +00:00
|
|
|
static bool opt_stdin = false;
|
2018-01-06 03:36:10 +00:00
|
|
|
static bool opt_leave_stdin_open = false;
|
2017-06-22 08:45:14 +00:00
|
|
|
static char *opt_cid = NULL;
|
|
|
|
static char *opt_cuuid = NULL;
|
|
|
|
static char *opt_runtime_path = NULL;
|
|
|
|
static char *opt_bundle_path = NULL;
|
|
|
|
static char *opt_pid_file = NULL;
|
|
|
|
static bool opt_systemd_cgroup = false;
|
2017-09-22 14:50:48 +00:00
|
|
|
static bool opt_no_pivot = false;
|
2017-06-22 08:45:14 +00:00
|
|
|
static char *opt_exec_process_spec = NULL;
|
|
|
|
static bool opt_exec = false;
|
|
|
|
static char *opt_log_path = NULL;
|
2017-08-13 01:35:55 +00:00
|
|
|
static char *opt_exit_dir = NULL;
|
2017-06-22 08:45:14 +00:00
|
|
|
static int opt_timeout = 0;
|
2017-09-25 22:31:00 +00:00
|
|
|
static int64_t opt_log_size_max = -1;
|
2017-10-24 14:28:25 +00:00
|
|
|
static char *opt_socket_path = DEFAULT_SOCKET_PATH;
|
2018-02-05 19:57:48 +00:00
|
|
|
static bool opt_no_new_keyring = false;
|
2017-06-22 08:45:14 +00:00
|
|
|
static GOptionEntry opt_entries[] =
|
2016-10-01 03:26:29 +00:00
|
|
|
{
|
2017-06-22 08:45:14 +00:00
|
|
|
{ "terminal", 't', 0, G_OPTION_ARG_NONE, &opt_terminal, "Terminal", NULL },
|
2017-06-08 20:08:29 +00:00
|
|
|
{ "stdin", 'i', 0, G_OPTION_ARG_NONE, &opt_stdin, "Stdin", NULL },
|
2018-01-06 03:36:10 +00:00
|
|
|
{ "leave-stdin-open", 0, 0, G_OPTION_ARG_NONE, &opt_leave_stdin_open, "Leave stdin open when attached client disconnects", NULL },
|
2017-06-22 08:45:14 +00:00
|
|
|
{ "cid", 'c', 0, G_OPTION_ARG_STRING, &opt_cid, "Container ID", NULL },
|
|
|
|
{ "cuuid", 'u', 0, G_OPTION_ARG_STRING, &opt_cuuid, "Container UUID", NULL },
|
|
|
|
{ "runtime", 'r', 0, G_OPTION_ARG_STRING, &opt_runtime_path, "Runtime path", NULL },
|
2018-02-06 13:14:36 +00:00
|
|
|
{ "no-new-keyring", 0, 0, G_OPTION_ARG_NONE, &opt_no_new_keyring, "Do not create a new session keyring for the container", NULL },
|
2018-02-05 19:57:48 +00:00
|
|
|
{ "no-pivot", 0, 0, G_OPTION_ARG_NONE, &opt_no_pivot, "Do not use pivot_root", NULL },
|
2017-06-22 08:45:14 +00:00
|
|
|
{ "bundle", 'b', 0, G_OPTION_ARG_STRING, &opt_bundle_path, "Bundle path", NULL },
|
|
|
|
{ "pidfile", 'p', 0, G_OPTION_ARG_STRING, &opt_pid_file, "PID file", NULL },
|
|
|
|
{ "systemd-cgroup", 's', 0, G_OPTION_ARG_NONE, &opt_systemd_cgroup, "Enable systemd cgroup manager", NULL },
|
|
|
|
{ "exec", 'e', 0, G_OPTION_ARG_NONE, &opt_exec, "Exec a command in a running container", NULL },
|
|
|
|
{ "exec-process-spec", 0, 0, G_OPTION_ARG_STRING, &opt_exec_process_spec, "Path to the process spec for exec", NULL },
|
2017-08-13 01:35:55 +00:00
|
|
|
{ "exit-dir", 0, 0, G_OPTION_ARG_STRING, &opt_exit_dir, "Path to the directory where exit files are written", NULL },
|
2017-06-22 08:45:14 +00:00
|
|
|
{ "log-path", 'l', 0, G_OPTION_ARG_STRING, &opt_log_path, "Log file path", NULL },
|
|
|
|
{ "timeout", 'T', 0, G_OPTION_ARG_INT, &opt_timeout, "Timeout in seconds", NULL },
|
2017-09-25 22:31:00 +00:00
|
|
|
{ "log-size-max", 0, 0, G_OPTION_ARG_INT64, &opt_log_size_max, "Maximum size of log file", NULL },
|
2017-10-24 22:28:53 +00:00
|
|
|
{ "socket-dir-path", 0, 0, G_OPTION_ARG_STRING, &opt_socket_path, "Location of container attach sockets", NULL },
|
2018-02-22 19:04:12 +00:00
|
|
|
{ "version", 0, 0, G_OPTION_ARG_NONE, &opt_version, "Print the version and exit", 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-09-25 22:36:14 +00:00
|
|
|
static int log_fd = -1;
|
|
|
|
|
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-06-08 20:08:29 +00:00
|
|
|
/* stdpipe_t represents one of the std pipes (or NONE).
|
|
|
|
* Sync with const in container_attach.go */
|
2017-04-11 18:20:57 +00:00
|
|
|
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-09-25 22:35:25 +00:00
|
|
|
static 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-06-02 11:37:53 +00:00
|
|
|
writev_buffer_t bufv = {0};
|
2017-09-25 22:35:25 +00:00
|
|
|
static int64_t bytes_written = 0;
|
|
|
|
int64_t bytes_to_be_written = 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;
|
2017-11-17 18:58:12 +00:00
|
|
|
bool partial = FALSE;
|
2017-04-06 18:40:12 +00:00
|
|
|
|
|
|
|
/* Find the end of the line, or alternatively the end of the buffer. */
|
|
|
|
line_end = memchr(buf, '\n', buflen);
|
2017-11-17 18:58:12 +00:00
|
|
|
if (line_end == NULL) {
|
2017-04-06 18:40:12 +00:00
|
|
|
line_end = &buf[buflen-1];
|
2017-11-17 18:58:12 +00:00
|
|
|
partial = TRUE;
|
|
|
|
}
|
2017-04-06 18:40:12 +00:00
|
|
|
line_len = line_end - buf + 1;
|
|
|
|
|
2017-11-17 18:58:12 +00:00
|
|
|
/* This is line_len bytes + TSBUFLEN - 1 + 2 (- 1 is for ignoring \0). */
|
|
|
|
bytes_to_be_written = line_len + TSBUFLEN + 1;
|
|
|
|
|
|
|
|
/* If partial, then we add a \n */
|
|
|
|
if (partial) {
|
|
|
|
bytes_to_be_written += 1;
|
2017-09-25 22:35:25 +00:00
|
|
|
}
|
|
|
|
|
2017-09-25 22:36:14 +00:00
|
|
|
/*
|
|
|
|
* We re-open the log file if writing out the bytes will exceed the max
|
|
|
|
* log size. We also reset the state so that the new file is started with
|
|
|
|
* a timestamp.
|
|
|
|
*/
|
|
|
|
if ((opt_log_size_max > 0) && (bytes_written + bytes_to_be_written) > opt_log_size_max) {
|
2018-02-23 10:09:41 +00:00
|
|
|
_cleanup_free_ char *opt_log_path_tmp = g_strdup_printf("%s.tmp", opt_log_path);
|
2017-09-25 22:36:14 +00:00
|
|
|
ninfo("Creating new log file");
|
|
|
|
bytes_written = 0;
|
|
|
|
|
|
|
|
/* Close the existing fd */
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
/* Open the log path file again */
|
2018-02-23 10:09:41 +00:00
|
|
|
log_fd = open(opt_log_path_tmp, O_WRONLY | O_TRUNC | O_CREAT | O_CLOEXEC, 0600);
|
2017-09-25 22:36:14 +00:00
|
|
|
if (log_fd < 0)
|
2017-12-11 20:38:31 +00:00
|
|
|
pexit("Failed to open log file %s: %s", opt_log_path, strerror(errno));
|
2018-02-23 10:09:41 +00:00
|
|
|
|
|
|
|
/* Replace the previous file */
|
|
|
|
if (rename(opt_log_path_tmp, opt_log_path) < 0) {
|
|
|
|
pexit("Failed to rename log file");
|
|
|
|
}
|
|
|
|
|
2017-09-25 22:36:14 +00:00
|
|
|
fd = log_fd;
|
|
|
|
}
|
2017-09-25 22:35:25 +00:00
|
|
|
|
2017-11-17 18:58:12 +00:00
|
|
|
/* Output the timestamp */
|
|
|
|
if (writev_buffer_append_segment(fd, &bufv, tsbuf, -1) < 0) {
|
|
|
|
nwarn("failed to write (timestamp, stream) to log");
|
|
|
|
goto next;
|
2017-09-25 22:35:25 +00:00
|
|
|
}
|
|
|
|
|
2017-11-17 18:58:12 +00:00
|
|
|
/* Output log tag for partial or newline */
|
|
|
|
if (partial) {
|
|
|
|
if (writev_buffer_append_segment(fd, &bufv, "P ", -1) < 0) {
|
|
|
|
nwarn("failed to write partial log tag");
|
|
|
|
goto next;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (writev_buffer_append_segment(fd, &bufv, "F ", -1) < 0) {
|
|
|
|
nwarn("failed to write end log tag");
|
2017-04-06 18:40:12 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-11-17 18:58:12 +00:00
|
|
|
/* Output a newline for partial */
|
|
|
|
if (partial) {
|
|
|
|
if (writev_buffer_append_segment(fd, &bufv, "\n", -1) < 0) {
|
|
|
|
nwarn("failed to write newline to log");
|
|
|
|
goto next;
|
|
|
|
}
|
|
|
|
}
|
2017-04-06 18:40:12 +00:00
|
|
|
|
2017-11-17 18:58:12 +00:00
|
|
|
bytes_written += bytes_to_be_written;
|
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-09-25 22:35:25 +00:00
|
|
|
ninfo("Total bytes written: %"PRId64"", bytes_written);
|
|
|
|
|
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) {
|
2017-06-22 09:57:47 +00:00
|
|
|
_cleanup_free_ char *cgroups_file_path = g_strdup_printf("/proc/%d/cgroup", pid);
|
2017-05-23 02:01:09 +00:00
|
|
|
_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;
|
|
|
|
}
|
|
|
|
|
2017-06-22 09:57:47 +00:00
|
|
|
subsystem_path = g_strdup_printf("%s/%s%s", CGROUP_ROOT, subpath, path);
|
2017-06-02 12:02:38 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2017-06-22 08:57:27 +00:00
|
|
|
static int get_pipe_fd_from_env(const char *envname)
|
|
|
|
{
|
|
|
|
char *pipe_str, *endptr;
|
|
|
|
int pipe_fd;
|
|
|
|
|
|
|
|
pipe_str = getenv(envname);
|
|
|
|
if (pipe_str == NULL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
pipe_fd = strtol(pipe_str, &endptr, 10);
|
|
|
|
if (errno != 0 || *endptr != '\0')
|
|
|
|
pexit("unable to parse %s", envname);
|
|
|
|
if (fcntl(pipe_fd, F_SETFD, FD_CLOEXEC) == -1)
|
|
|
|
pexit("unable to make %s CLOEXEC", envname);
|
|
|
|
|
|
|
|
return pipe_fd;
|
|
|
|
}
|
2017-06-02 13:19:15 +00:00
|
|
|
|
2017-06-22 09:30:19 +00:00
|
|
|
static void add_argv(GPtrArray *argv_array, ...) G_GNUC_NULL_TERMINATED;
|
|
|
|
|
|
|
|
static void add_argv(GPtrArray *argv_array, ...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
char *arg;
|
|
|
|
|
|
|
|
va_start (args, argv_array);
|
|
|
|
while ((arg = va_arg (args, char *)))
|
|
|
|
g_ptr_array_add (argv_array, arg);
|
|
|
|
va_end (args);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void end_argv(GPtrArray *argv_array)
|
|
|
|
{
|
|
|
|
g_ptr_array_add(argv_array, NULL);
|
|
|
|
}
|
|
|
|
|
2017-06-08 08:42:33 +00:00
|
|
|
/* Global state */
|
|
|
|
|
2017-06-08 15:57:15 +00:00
|
|
|
static int runtime_status = -1;
|
2017-06-28 15:44:49 +00:00
|
|
|
static int container_status = -1;
|
2017-06-08 15:57:15 +00:00
|
|
|
|
2017-06-08 20:08:29 +00:00
|
|
|
static int masterfd_stdin = -1;
|
2017-06-08 08:42:33 +00:00
|
|
|
static int masterfd_stdout = -1;
|
|
|
|
static int masterfd_stderr = -1;
|
|
|
|
|
|
|
|
/* Used for attach */
|
|
|
|
static int conn_sock = -1;
|
2017-06-08 20:08:29 +00:00
|
|
|
static int conn_sock_readable;
|
|
|
|
static int conn_sock_writable;
|
2017-06-08 08:42:33 +00:00
|
|
|
|
2017-06-22 11:03:51 +00:00
|
|
|
static int oom_event_fd = -1;
|
|
|
|
static int attach_socket_fd = -1;
|
|
|
|
static int console_socket_fd = -1;
|
|
|
|
static int terminal_ctrl_fd = -1;
|
2017-06-08 08:42:33 +00:00
|
|
|
|
2017-06-21 17:39:02 +00:00
|
|
|
static bool timed_out = FALSE;
|
|
|
|
|
2017-06-08 08:42:33 +00:00
|
|
|
static GMainLoop *main_loop = NULL;
|
|
|
|
|
2017-06-08 20:08:29 +00:00
|
|
|
static void conn_sock_shutdown(int how)
|
|
|
|
{
|
|
|
|
if (conn_sock == -1)
|
|
|
|
return;
|
|
|
|
shutdown(conn_sock, how);
|
|
|
|
if (how & SHUT_RD)
|
|
|
|
conn_sock_readable = false;
|
|
|
|
if (how & SHUT_WR)
|
|
|
|
conn_sock_writable = false;
|
|
|
|
if (!conn_sock_writable && !conn_sock_readable) {
|
|
|
|
close(conn_sock);
|
|
|
|
conn_sock = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-28 15:44:49 +00:00
|
|
|
static gboolean stdio_cb(int fd, GIOCondition condition, gpointer user_data);
|
|
|
|
|
|
|
|
static gboolean tty_hup_timeout_scheduled = false;
|
|
|
|
|
|
|
|
static gboolean tty_hup_timeout_cb (G_GNUC_UNUSED gpointer user_data)
|
|
|
|
{
|
|
|
|
tty_hup_timeout_scheduled = false;
|
|
|
|
g_unix_fd_add (masterfd_stdout, G_IO_IN, stdio_cb, GINT_TO_POINTER(STDOUT_PIPE));
|
|
|
|
return G_SOURCE_REMOVE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool read_stdio(int fd, stdpipe_t pipe, bool *eof)
|
2017-06-08 08:42:33 +00:00
|
|
|
{
|
2017-06-08 20:08:29 +00:00
|
|
|
#define STDIO_BUF_SIZE 8192 /* Sync with redirectResponseToOutputStreams() */
|
|
|
|
/* We use one extra byte at the start, which we don't read into, instead
|
|
|
|
we use that for marking the pipe when we write to the attached socket */
|
|
|
|
char real_buf[STDIO_BUF_SIZE + 1];
|
|
|
|
char *buf = real_buf + 1;
|
2017-06-08 08:42:33 +00:00
|
|
|
ssize_t num_read = 0;
|
|
|
|
|
2017-06-28 15:44:49 +00:00
|
|
|
if (eof)
|
|
|
|
*eof = false;
|
|
|
|
|
|
|
|
num_read = read(fd, buf, STDIO_BUF_SIZE);
|
|
|
|
if (num_read == 0) {
|
|
|
|
if (eof)
|
|
|
|
*eof = true;
|
|
|
|
return false;
|
|
|
|
} else if (num_read < 0) {
|
|
|
|
nwarn("stdio_input read failed %s", strerror(errno));
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
if (write_k8s_log(log_fd, pipe, buf, num_read) < 0) {
|
|
|
|
nwarn("write_k8s_log failed");
|
2017-06-08 08:42:33 +00:00
|
|
|
return G_SOURCE_CONTINUE;
|
|
|
|
}
|
|
|
|
|
2017-06-28 15:44:49 +00:00
|
|
|
real_buf[0] = pipe;
|
|
|
|
if (conn_sock_writable && write_all(conn_sock, real_buf, num_read+1) < 0) {
|
|
|
|
nwarn("Failed to write to socket");
|
|
|
|
conn_sock_shutdown(SHUT_WR);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2017-06-08 08:42:33 +00:00
|
|
|
|
2017-07-22 15:17:14 +00:00
|
|
|
static void on_sigchld(G_GNUC_UNUSED int signal)
|
|
|
|
{
|
|
|
|
raise (SIGUSR1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void check_child_processes(GHashTable *pid_to_handler)
|
|
|
|
{
|
|
|
|
void (*cb) (GPid, int, gpointer);
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
int status;
|
|
|
|
pid_t pid = waitpid(-1, &status, WNOHANG);
|
|
|
|
|
|
|
|
if (pid < 0 && errno == EINTR)
|
|
|
|
continue;
|
|
|
|
if (pid < 0 && errno == ECHILD) {
|
|
|
|
g_main_loop_quit (main_loop);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (pid < 0)
|
|
|
|
pexit("Failed to read child process status");
|
|
|
|
|
|
|
|
if (pid == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* If we got here, pid > 0, so we have a valid pid to check. */
|
|
|
|
cb = g_hash_table_lookup(pid_to_handler, &pid);
|
|
|
|
if (cb)
|
|
|
|
cb(pid, status, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean on_sigusr1_cb(gpointer user_data)
|
|
|
|
{
|
|
|
|
GHashTable *pid_to_handler = (GHashTable *) user_data;
|
|
|
|
check_child_processes (pid_to_handler);
|
|
|
|
return G_SOURCE_CONTINUE;
|
|
|
|
}
|
|
|
|
|
2017-06-28 15:44:49 +00:00
|
|
|
static gboolean stdio_cb(int fd, GIOCondition condition, gpointer user_data)
|
|
|
|
{
|
|
|
|
stdpipe_t pipe = GPOINTER_TO_INT(user_data);
|
|
|
|
bool read_eof = false;
|
2017-06-29 21:20:12 +00:00
|
|
|
bool has_input = (condition & G_IO_IN) != 0;
|
|
|
|
bool has_hup = (condition & G_IO_HUP) != 0;
|
2017-06-28 15:44:49 +00:00
|
|
|
|
|
|
|
/* When we get here, condition can be G_IO_IN and/or G_IO_HUP.
|
|
|
|
IN means there is some data to read.
|
|
|
|
HUP means the other side closed the fd. In the case of a pine
|
|
|
|
this in final, and we will never get more data. However, in the
|
|
|
|
terminal case this just means that nobody has the terminal
|
|
|
|
open at this point, and this can be change whenever someone
|
|
|
|
opens the tty */
|
|
|
|
|
|
|
|
/* Read any data before handling hup */
|
2017-06-29 21:20:12 +00:00
|
|
|
if (has_input) {
|
2017-06-28 15:44:49 +00:00
|
|
|
read_stdio(fd, pipe, &read_eof);
|
|
|
|
}
|
2017-06-08 08:42:33 +00:00
|
|
|
|
2017-06-29 21:20:12 +00:00
|
|
|
if (has_hup && opt_terminal && pipe == STDOUT_PIPE) {
|
|
|
|
/* We got a HUP from the terminal master this means there
|
2017-06-28 15:44:49 +00:00
|
|
|
are no open slaves ptys atm, and we will get a lot
|
|
|
|
of wakeups until we have one, switch to polling
|
|
|
|
mode. */
|
2017-06-29 21:20:12 +00:00
|
|
|
|
|
|
|
/* If we read some data this cycle, wait one more, maybe there
|
|
|
|
is more in the buffer before we handle the hup */
|
|
|
|
if (has_input && !read_eof) {
|
|
|
|
return G_SOURCE_CONTINUE;
|
|
|
|
}
|
|
|
|
|
2017-06-28 15:44:49 +00:00
|
|
|
if (!tty_hup_timeout_scheduled) {
|
|
|
|
g_timeout_add (100, tty_hup_timeout_cb, NULL);
|
2017-06-08 08:42:33 +00:00
|
|
|
}
|
2017-06-28 15:44:49 +00:00
|
|
|
tty_hup_timeout_scheduled = true;
|
|
|
|
return G_SOURCE_REMOVE;
|
2017-06-08 08:42:33 +00:00
|
|
|
}
|
|
|
|
|
2017-06-29 21:20:12 +00:00
|
|
|
if (read_eof || (has_hup && !has_input)) {
|
2017-06-28 15:44:49 +00:00
|
|
|
/* End of input */
|
|
|
|
if (pipe == STDOUT_PIPE)
|
|
|
|
masterfd_stdout = -1;
|
|
|
|
if (pipe == STDERR_PIPE)
|
|
|
|
masterfd_stderr = -1;
|
2017-06-08 08:42:33 +00:00
|
|
|
|
2017-06-28 15:44:49 +00:00
|
|
|
close (fd);
|
|
|
|
return G_SOURCE_REMOVE;
|
|
|
|
}
|
2017-06-29 21:20:12 +00:00
|
|
|
|
|
|
|
return G_SOURCE_CONTINUE;
|
2017-06-08 08:42:33 +00:00
|
|
|
}
|
|
|
|
|
2017-06-21 17:39:02 +00:00
|
|
|
static gboolean timeout_cb (G_GNUC_UNUSED gpointer user_data)
|
|
|
|
{
|
|
|
|
timed_out = TRUE;
|
|
|
|
ninfo ("Timed out, killing main loop");
|
|
|
|
g_main_loop_quit (main_loop);
|
|
|
|
return G_SOURCE_REMOVE;
|
|
|
|
}
|
|
|
|
|
2017-06-08 08:42:33 +00:00
|
|
|
static gboolean oom_cb(int fd, GIOCondition condition, G_GNUC_UNUSED gpointer user_data)
|
|
|
|
{
|
|
|
|
uint64_t oom_event;
|
|
|
|
ssize_t num_read = 0;
|
|
|
|
|
|
|
|
if ((condition & G_IO_IN) != 0) {
|
|
|
|
num_read = read(fd, &oom_event, sizeof(uint64_t));
|
|
|
|
if (num_read < 0) {
|
|
|
|
nwarn("Failed to read oom event from eventfd");
|
|
|
|
return G_SOURCE_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (num_read > 0) {
|
|
|
|
if (num_read != sizeof(uint64_t))
|
|
|
|
nwarn("Failed to read full oom event from eventfd");
|
|
|
|
ninfo("OOM received");
|
|
|
|
if (open("oom", O_CREAT, 0666) < 0) {
|
|
|
|
nwarn("Failed to write oom file");
|
|
|
|
}
|
|
|
|
return G_SOURCE_CONTINUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* End of input */
|
|
|
|
close (fd);
|
2017-06-22 11:03:51 +00:00
|
|
|
oom_event_fd = -1;
|
2017-06-08 08:42:33 +00:00
|
|
|
return G_SOURCE_REMOVE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean conn_sock_cb(int fd, GIOCondition condition, G_GNUC_UNUSED gpointer user_data)
|
|
|
|
{
|
2017-06-08 20:08:29 +00:00
|
|
|
#define CONN_SOCK_BUF_SIZE 32*1024 /* Match the write size in CopyDetachable */
|
|
|
|
char buf[CONN_SOCK_BUF_SIZE];
|
2017-06-08 08:42:33 +00:00
|
|
|
ssize_t num_read = 0;
|
|
|
|
|
|
|
|
if ((condition & G_IO_IN) != 0) {
|
2017-06-08 20:08:29 +00:00
|
|
|
num_read = read(fd, buf, CONN_SOCK_BUF_SIZE);
|
2017-06-08 08:42:33 +00:00
|
|
|
if (num_read < 0)
|
|
|
|
return G_SOURCE_CONTINUE;
|
|
|
|
|
2017-06-08 20:08:29 +00:00
|
|
|
if (num_read > 0 && masterfd_stdin >= 0) {
|
|
|
|
if (write_all(masterfd_stdin, buf, num_read) < 0) {
|
|
|
|
nwarn("Failed to write to container stdin");
|
2017-06-08 08:42:33 +00:00
|
|
|
}
|
|
|
|
return G_SOURCE_CONTINUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* End of input */
|
2017-06-08 20:08:29 +00:00
|
|
|
conn_sock_shutdown(SHUT_RD);
|
|
|
|
if (masterfd_stdin >= 0 && opt_stdin) {
|
2018-01-06 03:36:10 +00:00
|
|
|
if (!opt_leave_stdin_open) {
|
|
|
|
close(masterfd_stdin);
|
|
|
|
masterfd_stdin = -1;
|
|
|
|
} else {
|
|
|
|
ninfo("Not closing input");
|
|
|
|
}
|
2017-06-08 20:08:29 +00:00
|
|
|
}
|
2017-06-08 08:42:33 +00:00
|
|
|
return G_SOURCE_REMOVE;
|
|
|
|
}
|
|
|
|
|
2017-06-08 20:08:29 +00:00
|
|
|
static gboolean attach_cb(int fd, G_GNUC_UNUSED GIOCondition condition, G_GNUC_UNUSED gpointer user_data)
|
2017-06-08 08:42:33 +00:00
|
|
|
{
|
|
|
|
conn_sock = accept(fd, NULL, NULL);
|
|
|
|
if (conn_sock == -1) {
|
|
|
|
if (errno != EWOULDBLOCK)
|
|
|
|
nwarn("Failed to accept client connection on attach socket");
|
|
|
|
} else {
|
2017-06-08 20:08:29 +00:00
|
|
|
conn_sock_readable = true;
|
|
|
|
conn_sock_writable = true;
|
|
|
|
g_unix_fd_add (conn_sock, G_IO_IN|G_IO_HUP|G_IO_ERR, conn_sock_cb, GINT_TO_POINTER(STDOUT_PIPE));
|
2017-06-08 08:42:33 +00:00
|
|
|
ninfo("Accepted connection %d", conn_sock);
|
|
|
|
}
|
|
|
|
|
|
|
|
return G_SOURCE_CONTINUE;
|
|
|
|
}
|
|
|
|
|
2017-06-08 20:08:29 +00:00
|
|
|
static gboolean ctrl_cb(int fd, G_GNUC_UNUSED GIOCondition condition, G_GNUC_UNUSED gpointer user_data)
|
2017-06-08 08:42:33 +00:00
|
|
|
{
|
|
|
|
#define CTLBUFSZ 200
|
|
|
|
static char ctlbuf[CTLBUFSZ];
|
|
|
|
static int readsz = CTLBUFSZ - 1;
|
|
|
|
static char *readptr = ctlbuf;
|
|
|
|
ssize_t num_read = 0;
|
|
|
|
int ctl_msg_type = -1;
|
|
|
|
int height = -1;
|
|
|
|
int width = -1;
|
|
|
|
struct winsize ws;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
num_read = read(fd, readptr, readsz);
|
|
|
|
if (num_read <= 0) {
|
|
|
|
nwarn("Failed to read from control fd");
|
|
|
|
return G_SOURCE_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
readptr[num_read] = '\0';
|
|
|
|
ninfo("Got ctl message: %s\n", ctlbuf);
|
|
|
|
|
|
|
|
char *beg = ctlbuf;
|
|
|
|
char *newline = strchrnul(beg, '\n');
|
|
|
|
/* Process each message which ends with a line */
|
|
|
|
while (*newline != '\0') {
|
|
|
|
ret = sscanf(ctlbuf, "%d %d %d\n", &ctl_msg_type, &height, &width);
|
|
|
|
if (ret != 3) {
|
|
|
|
nwarn("Failed to sscanf message");
|
|
|
|
return G_SOURCE_CONTINUE;
|
|
|
|
}
|
|
|
|
ninfo("Message type: %d, Height: %d, Width: %d", ctl_msg_type, height, width);
|
|
|
|
ret = ioctl(masterfd_stdout, TIOCGWINSZ, &ws);
|
|
|
|
ninfo("Existing size: %d %d", ws.ws_row, ws.ws_col);
|
|
|
|
ws.ws_row = height;
|
|
|
|
ws.ws_col = width;
|
|
|
|
ret = ioctl(masterfd_stdout, TIOCSWINSZ, &ws);
|
|
|
|
if (ret == -1) {
|
|
|
|
nwarn("Failed to set process pty terminal size");
|
|
|
|
}
|
|
|
|
beg = newline + 1;
|
|
|
|
newline = strchrnul(beg, '\n');
|
|
|
|
}
|
|
|
|
if (num_read == (CTLBUFSZ - 1) && beg == ctlbuf) {
|
|
|
|
/*
|
|
|
|
* We did not find a newline in the entire buffer.
|
|
|
|
* This shouldn't happen as our buffer is larger than
|
|
|
|
* the message that we expect to receive.
|
|
|
|
*/
|
|
|
|
nwarn("Could not find newline in entire buffer\n");
|
|
|
|
} else if (*beg == '\0') {
|
|
|
|
/* We exhausted all messages that were complete */
|
|
|
|
readptr = ctlbuf;
|
|
|
|
readsz = CTLBUFSZ - 1;
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* We copy remaining data to beginning of buffer
|
|
|
|
* and advance readptr after that.
|
|
|
|
*/
|
|
|
|
int cp_rem = 0;
|
|
|
|
do {
|
|
|
|
ctlbuf[cp_rem++] = *beg++;
|
|
|
|
} while (*beg != '\0');
|
|
|
|
readptr = ctlbuf + cp_rem;
|
|
|
|
readsz = CTLBUFSZ - 1 - cp_rem;
|
|
|
|
}
|
|
|
|
|
|
|
|
return G_SOURCE_CONTINUE;
|
|
|
|
}
|
|
|
|
|
2017-06-08 15:57:15 +00:00
|
|
|
static gboolean terminal_accept_cb(int fd, G_GNUC_UNUSED GIOCondition condition, G_GNUC_UNUSED gpointer user_data)
|
|
|
|
{
|
|
|
|
const char *csname = user_data;
|
|
|
|
struct file_t console;
|
|
|
|
int connfd = -1;
|
2017-06-15 14:01:53 +00:00
|
|
|
struct termios tset;
|
2017-06-08 15:57:15 +00:00
|
|
|
|
2017-06-22 11:03:51 +00:00
|
|
|
ninfo("about to accept from console_socket_fd: %d", fd);
|
2017-06-08 15:57:15 +00:00
|
|
|
connfd = accept4(fd, NULL, NULL, SOCK_CLOEXEC);
|
2017-06-20 10:18:07 +00:00
|
|
|
if (connfd < 0) {
|
|
|
|
nwarn("Failed to accept console-socket connection");
|
|
|
|
return G_SOURCE_CONTINUE;
|
|
|
|
}
|
2017-06-08 15:57:15 +00:00
|
|
|
|
|
|
|
/* Not accepting anything else. */
|
|
|
|
close(fd);
|
|
|
|
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-06-15 14:01:53 +00:00
|
|
|
/* We change the terminal settings to match kube settings */
|
|
|
|
if (tcgetattr(console.fd, &tset) == -1)
|
|
|
|
pexit("Failed to get console terminal settings");
|
|
|
|
|
|
|
|
tset.c_oflag |= ONLCR;
|
|
|
|
|
|
|
|
if (tcsetattr(console.fd, TCSANOW, &tset) == -1)
|
|
|
|
pexit("Failed to set console terminal settings");
|
|
|
|
|
2017-06-08 15:57:15 +00:00
|
|
|
/* We only have a single fd for both pipes, so we just treat it as
|
|
|
|
* stdout. stderr is ignored. */
|
2017-06-08 20:08:29 +00:00
|
|
|
masterfd_stdin = console.fd;
|
2017-06-08 15:57:15 +00:00
|
|
|
masterfd_stdout = console.fd;
|
|
|
|
|
|
|
|
/* Clean up everything */
|
|
|
|
close(connfd);
|
|
|
|
|
|
|
|
return G_SOURCE_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2017-06-08 20:08:29 +00:00
|
|
|
runtime_exit_cb (G_GNUC_UNUSED GPid pid, int status, G_GNUC_UNUSED gpointer user_data)
|
2017-06-08 15:57:15 +00:00
|
|
|
{
|
|
|
|
runtime_status = status;
|
|
|
|
g_main_loop_quit (main_loop);
|
|
|
|
}
|
|
|
|
|
2017-06-28 15:44:49 +00:00
|
|
|
static void
|
|
|
|
container_exit_cb (G_GNUC_UNUSED GPid pid, int status, G_GNUC_UNUSED gpointer user_data)
|
|
|
|
{
|
|
|
|
ninfo("container %d exited with status %d\n", pid, status);
|
|
|
|
container_status = status;
|
|
|
|
g_main_loop_quit (main_loop);
|
|
|
|
}
|
|
|
|
|
2017-06-21 17:39:02 +00:00
|
|
|
static void write_sync_fd(int sync_pipe_fd, int res, const char *message)
|
|
|
|
{
|
|
|
|
_cleanup_free_ char *escaped_message = NULL;
|
|
|
|
_cleanup_free_ char *json = NULL;
|
|
|
|
const char *res_key;
|
|
|
|
ssize_t len;
|
|
|
|
|
|
|
|
if (sync_pipe_fd == -1)
|
|
|
|
return;
|
|
|
|
|
2017-06-22 08:45:14 +00:00
|
|
|
if (opt_exec)
|
2017-06-21 17:39:02 +00:00
|
|
|
res_key = "exit_code";
|
|
|
|
else
|
|
|
|
res_key = "pid";
|
|
|
|
|
|
|
|
if (message) {
|
|
|
|
escaped_message = escape_json_string(message);
|
|
|
|
json = g_strdup_printf ("{\"%s\": %d, \"message\": \"%s\"}\n", res_key, res, escaped_message);
|
|
|
|
} else {
|
|
|
|
json = g_strdup_printf ("{\"%s\": %d}\n", res_key, res);
|
|
|
|
}
|
|
|
|
|
|
|
|
len = strlen(json);
|
|
|
|
if (write_all(sync_pipe_fd, json, len) != len) {
|
|
|
|
pexit("Unable to send container stderr message to parent");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-22 10:13:16 +00:00
|
|
|
static char *setup_console_socket(void)
|
|
|
|
{
|
|
|
|
struct sockaddr_un addr = {0};
|
2017-08-06 15:26:14 +00:00
|
|
|
_cleanup_free_ const char *tmpdir = g_get_tmp_dir();
|
|
|
|
_cleanup_free_ char *csname = g_build_filename(tmpdir, "conmon-term.XXXXXX", NULL);
|
2017-06-22 10:13:16 +00:00
|
|
|
/*
|
|
|
|
* 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. */
|
2017-06-22 11:03:51 +00:00
|
|
|
console_socket_fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0);
|
|
|
|
if (console_socket_fd < 0)
|
2017-06-22 10:13:16 +00:00
|
|
|
pexit("Failed to create console-socket");
|
2017-06-22 11:03:51 +00:00
|
|
|
if (fchmod(console_socket_fd, 0700))
|
2017-06-22 10:13:16 +00:00
|
|
|
pexit("Failed to change console-socket permissions");
|
|
|
|
/* XXX: This should be handled with a rename(2). */
|
|
|
|
if (unlink(csname) < 0)
|
2017-08-06 15:26:14 +00:00
|
|
|
pexit("Failed to unlink temporary random path");
|
2017-06-22 11:03:51 +00:00
|
|
|
if (bind(console_socket_fd, (struct sockaddr *) &addr, sizeof(addr)) < 0)
|
2017-06-22 10:13:16 +00:00
|
|
|
pexit("Failed to bind to console-socket");
|
2017-06-22 11:03:51 +00:00
|
|
|
if (listen(console_socket_fd, 128) < 0)
|
2017-06-22 10:13:16 +00:00
|
|
|
pexit("Failed to listen on console-socket");
|
|
|
|
|
|
|
|
return g_strdup(csname);
|
|
|
|
}
|
|
|
|
|
2017-06-22 10:18:09 +00:00
|
|
|
static char *setup_attach_socket(void)
|
|
|
|
{
|
|
|
|
_cleanup_free_ char *attach_sock_path = NULL;
|
|
|
|
char *attach_symlink_dir_path;
|
|
|
|
struct sockaddr_un attach_addr = {0};
|
|
|
|
attach_addr.sun_family = AF_UNIX;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create a symlink so we don't exceed unix domain socket
|
|
|
|
* path length limit.
|
|
|
|
*/
|
2017-10-23 15:35:38 +00:00
|
|
|
attach_symlink_dir_path = g_build_filename(opt_socket_path, opt_cuuid, NULL);
|
2017-06-22 10:18:09 +00:00
|
|
|
if (unlink(attach_symlink_dir_path) == -1 && errno != ENOENT)
|
|
|
|
pexit("Failed to remove existing symlink for attach socket directory");
|
|
|
|
|
|
|
|
if (symlink(opt_bundle_path, attach_symlink_dir_path) == -1)
|
|
|
|
pexit("Failed to create symlink for attach socket");
|
|
|
|
|
2017-10-23 15:35:38 +00:00
|
|
|
attach_sock_path = g_build_filename(opt_socket_path, opt_cuuid, "attach", NULL);
|
2017-06-22 10:18:09 +00:00
|
|
|
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.
|
|
|
|
*/
|
2017-06-22 11:03:51 +00:00
|
|
|
attach_socket_fd = socket(AF_UNIX, SOCK_SEQPACKET|SOCK_NONBLOCK|SOCK_CLOEXEC, 0);
|
|
|
|
if (attach_socket_fd == -1)
|
2017-06-22 10:18:09 +00:00
|
|
|
pexit("Failed to create attach socket");
|
|
|
|
|
2017-06-22 11:03:51 +00:00
|
|
|
if (fchmod(attach_socket_fd, 0700))
|
2017-06-22 10:18:09 +00:00
|
|
|
pexit("Failed to change attach socket permissions");
|
|
|
|
|
2017-06-22 11:03:51 +00:00
|
|
|
if (bind(attach_socket_fd, (struct sockaddr *)&attach_addr, sizeof(struct sockaddr_un)) == -1)
|
2017-06-22 10:18:09 +00:00
|
|
|
pexit("Failed to bind attach socket: %s", attach_sock_path);
|
|
|
|
|
2017-06-22 11:03:51 +00:00
|
|
|
if (listen(attach_socket_fd, 10) == -1)
|
2017-06-22 10:18:09 +00:00
|
|
|
pexit("Failed to listen on attach socket: %s", attach_sock_path);
|
|
|
|
|
2017-06-22 11:30:02 +00:00
|
|
|
g_unix_fd_add (attach_socket_fd, G_IO_IN, attach_cb, NULL);
|
|
|
|
|
2017-06-22 10:18:09 +00:00
|
|
|
return attach_symlink_dir_path;
|
|
|
|
}
|
|
|
|
|
2017-06-22 10:39:29 +00:00
|
|
|
static void setup_terminal_control_fifo()
|
|
|
|
{
|
|
|
|
_cleanup_free_ char *ctl_fifo_path = g_build_filename(opt_bundle_path, "ctl", NULL);
|
|
|
|
ninfo("ctl fifo path: %s", ctl_fifo_path);
|
|
|
|
|
|
|
|
/* Setup fifo for reading in terminal resize and other stdio control messages */
|
|
|
|
|
|
|
|
if (mkfifo(ctl_fifo_path, 0666) == -1)
|
|
|
|
pexit("Failed to mkfifo at %s", ctl_fifo_path);
|
|
|
|
|
2017-06-22 11:03:51 +00:00
|
|
|
terminal_ctrl_fd = open(ctl_fifo_path, O_RDONLY|O_NONBLOCK|O_CLOEXEC);
|
|
|
|
if (terminal_ctrl_fd == -1)
|
2017-06-22 10:39:29 +00:00
|
|
|
pexit("Failed to open control fifo");
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Open a dummy writer to prevent getting flood of POLLHUPs when
|
|
|
|
* last writer closes.
|
|
|
|
*/
|
|
|
|
int dummyfd = open(ctl_fifo_path, O_WRONLY|O_CLOEXEC);
|
|
|
|
if (dummyfd == -1)
|
|
|
|
pexit("Failed to open dummy writer for fifo");
|
|
|
|
|
2017-06-22 11:30:02 +00:00
|
|
|
g_unix_fd_add (terminal_ctrl_fd, G_IO_IN, ctrl_cb, NULL);
|
|
|
|
|
2017-06-22 11:03:51 +00:00
|
|
|
ninfo("terminal_ctrl_fd: %d", terminal_ctrl_fd);
|
2017-06-22 10:39:29 +00:00
|
|
|
}
|
|
|
|
|
2017-06-28 15:44:49 +00:00
|
|
|
static void setup_oom_handling(int container_pid)
|
2017-06-22 10:40:35 +00:00
|
|
|
{
|
|
|
|
/* Setup OOM notification for container process */
|
2017-06-28 15:44:49 +00:00
|
|
|
_cleanup_free_ char *memory_cgroup_path = process_cgroup_subsystem_path(container_pid, "memory");
|
2017-06-22 11:03:51 +00:00
|
|
|
_cleanup_close_ int cfd = -1;
|
|
|
|
int ofd = -1; /* Not closed */
|
2017-06-22 10:40:35 +00:00
|
|
|
if (!memory_cgroup_path) {
|
|
|
|
nexit("Failed to get memory cgroup path");
|
|
|
|
}
|
|
|
|
|
|
|
|
_cleanup_free_ char *memory_cgroup_file_path = g_build_filename(memory_cgroup_path, "cgroup.event_control", NULL);
|
|
|
|
|
|
|
|
if ((cfd = open(memory_cgroup_file_path, O_WRONLY | O_CLOEXEC)) == -1) {
|
|
|
|
nwarn("Failed to open %s", memory_cgroup_file_path);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_cleanup_free_ char *memory_cgroup_file_oom_path = g_build_filename(memory_cgroup_path, "memory.oom_control", NULL);
|
|
|
|
if ((ofd = open(memory_cgroup_file_oom_path, O_RDONLY | O_CLOEXEC)) == -1)
|
|
|
|
pexit("Failed to open %s", memory_cgroup_file_oom_path);
|
|
|
|
|
2017-06-22 11:03:51 +00:00
|
|
|
if ((oom_event_fd = eventfd(0, EFD_CLOEXEC)) == -1)
|
2017-06-22 10:40:35 +00:00
|
|
|
pexit("Failed to create eventfd");
|
|
|
|
|
2017-06-22 11:03:51 +00:00
|
|
|
_cleanup_free_ char *data = g_strdup_printf("%d %d", oom_event_fd, ofd);
|
2017-06-22 10:40:35 +00:00
|
|
|
if (write_all(cfd, data, strlen(data)) < 0)
|
|
|
|
pexit("Failed to write to cgroup.event_control");
|
2017-06-22 11:30:02 +00:00
|
|
|
|
|
|
|
g_unix_fd_add (oom_event_fd, G_IO_IN, oom_cb, NULL);
|
2017-06-22 10:40:35 +00:00
|
|
|
}
|
|
|
|
|
2016-08-26 21:00:30 +00:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2017-06-08 15:57:15 +00:00
|
|
|
int ret;
|
2016-11-14 16:46:58 +00:00
|
|
|
char cwd[PATH_MAX];
|
2017-06-22 09:57:47 +00:00
|
|
|
_cleanup_free_ char *default_pid_file = NULL;
|
2017-06-22 10:13:16 +00:00
|
|
|
_cleanup_free_ char *csname = NULL;
|
2016-08-26 21:00:30 +00:00
|
|
|
GError *err = NULL;
|
2017-06-22 09:57:47 +00:00
|
|
|
_cleanup_free_ char *contents = NULL;
|
2017-06-28 15:44:49 +00:00
|
|
|
int container_pid = -1;
|
|
|
|
pid_t main_pid, create_pid;
|
2017-04-11 18:20:57 +00:00
|
|
|
/* Used for !terminal cases. */
|
2017-06-08 20:08:29 +00:00
|
|
|
int slavefd_stdin = -1;
|
2017-04-11 18:20:57 +00:00
|
|
|
int slavefd_stdout = -1;
|
|
|
|
int slavefd_stderr = -1;
|
2016-09-02 21:33:54 +00:00
|
|
|
char buf[BUF_SIZE];
|
|
|
|
int num_read;
|
2017-01-10 22:08:54 +00:00
|
|
|
int sync_pipe_fd = -1;
|
2017-06-09 14:18:27 +00:00
|
|
|
int start_pipe_fd = -1;
|
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;
|
2017-06-09 14:18:27 +00:00
|
|
|
_cleanup_close_ int dev_null_r = -1;
|
|
|
|
_cleanup_close_ int dev_null_w = -1;
|
2017-06-20 10:18:07 +00:00
|
|
|
int fds[2];
|
2016-10-01 03:26:29 +00:00
|
|
|
|
2017-06-08 08:42:33 +00:00
|
|
|
main_loop = g_main_loop_new (NULL, FALSE);
|
2017-05-30 21:35:12 +00:00
|
|
|
|
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");
|
2017-06-22 08:45:14 +00:00
|
|
|
g_option_context_add_main_entries(context, opt_entries, "conmon");
|
2016-10-07 15:59:39 +00:00
|
|
|
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
|
|
|
}
|
2018-02-22 19:04:12 +00:00
|
|
|
if (opt_version) {
|
|
|
|
g_print("conmon version " VERSION "\ncommit: " GIT_COMMIT "\n");
|
|
|
|
exit(0);
|
|
|
|
}
|
2016-08-26 21:00:30 +00:00
|
|
|
|
2017-06-22 08:45:14 +00:00
|
|
|
if (opt_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-06-22 08:45:14 +00:00
|
|
|
if (!opt_exec && opt_cuuid == NULL)
|
2017-05-30 21:35:12 +00:00
|
|
|
nexit("Container UUID not provided. Use --cuuid");
|
|
|
|
|
2017-06-22 08:45:14 +00:00
|
|
|
if (opt_runtime_path == NULL)
|
2016-10-01 03:26:29 +00:00
|
|
|
nexit("Runtime path not provided. Use --runtime");
|
2017-12-11 20:38:31 +00:00
|
|
|
if (access(opt_runtime_path, X_OK) < 0)
|
|
|
|
pexit("Runtime path %s is not valid: %s", opt_runtime_path, strerror(errno));
|
2016-08-26 21:00:30 +00:00
|
|
|
|
2017-06-22 08:45:14 +00:00
|
|
|
if (opt_bundle_path == NULL && !opt_exec) {
|
2016-11-14 16:46:58 +00:00
|
|
|
if (getcwd(cwd, sizeof(cwd)) == NULL) {
|
|
|
|
nexit("Failed to get working directory");
|
|
|
|
}
|
2017-06-22 08:45:14 +00:00
|
|
|
opt_bundle_path = cwd;
|
2016-11-14 16:46:58 +00:00
|
|
|
}
|
|
|
|
|
2017-06-09 14:18:27 +00:00
|
|
|
dev_null_r = open("/dev/null", O_RDONLY | O_CLOEXEC);
|
|
|
|
if (dev_null_r < 0)
|
|
|
|
pexit("Failed to open /dev/null");
|
|
|
|
|
|
|
|
dev_null_w = open("/dev/null", O_WRONLY | O_CLOEXEC);
|
|
|
|
if (dev_null_w < 0)
|
|
|
|
pexit("Failed to open /dev/null");
|
|
|
|
|
2017-06-22 08:45:14 +00:00
|
|
|
if (opt_exec && opt_exec_process_spec == NULL) {
|
2017-05-25 09:11:14 +00:00
|
|
|
nexit("Exec process spec path not provided. Use --exec-process-spec");
|
|
|
|
}
|
|
|
|
|
2017-06-22 08:45:14 +00:00
|
|
|
if (opt_pid_file == NULL) {
|
2017-06-22 09:57:47 +00:00
|
|
|
default_pid_file = g_strdup_printf ("%s/pidfile-%s", cwd, opt_cid);
|
2017-06-22 08:45:14 +00:00
|
|
|
opt_pid_file = default_pid_file;
|
2016-11-14 16:46:58 +00:00
|
|
|
}
|
|
|
|
|
2017-06-22 08:45:14 +00:00
|
|
|
if (opt_log_path == NULL)
|
2016-10-07 15:59:39 +00:00
|
|
|
nexit("Log file path not provided. Use --log-path");
|
|
|
|
|
2017-06-22 08:57:27 +00:00
|
|
|
start_pipe_fd = get_pipe_fd_from_env("_OCI_STARTPIPE");
|
|
|
|
if (start_pipe_fd >= 0) {
|
2017-06-09 14:18:27 +00:00
|
|
|
/* Block for an initial write to the start pipe before
|
|
|
|
spawning any childred or exiting, to ensure the
|
|
|
|
parent can put us in the right cgroup. */
|
2018-02-23 19:25:29 +00:00
|
|
|
num_read = read(start_pipe_fd, buf, BUF_SIZE);
|
|
|
|
if (num_read < 0) {
|
|
|
|
pexit("start-pipe read failed");
|
|
|
|
}
|
2017-06-09 14:18:27 +00:00
|
|
|
close(start_pipe_fd);
|
|
|
|
}
|
|
|
|
|
2017-06-21 17:39:02 +00:00
|
|
|
/* In the create-container case we double-fork in
|
|
|
|
order to disconnect from the parent, as we want to
|
|
|
|
continue in a daemon-like way */
|
|
|
|
main_pid = fork();
|
|
|
|
if (main_pid < 0) {
|
|
|
|
pexit("Failed to fork the create command");
|
|
|
|
} else if (main_pid != 0) {
|
|
|
|
exit(0);
|
|
|
|
}
|
2017-06-09 14:18:27 +00:00
|
|
|
|
2017-06-21 17:39:02 +00:00
|
|
|
/* Disconnect stdio from parent. We need to do this, because
|
|
|
|
the parent is waiting for the stdout to end when the intermediate
|
|
|
|
child dies */
|
|
|
|
if (dup2(dev_null_r, STDIN_FILENO) < 0)
|
2017-08-06 15:26:14 +00:00
|
|
|
pexit("Failed to dup over stdin");
|
2017-06-21 17:39:02 +00:00
|
|
|
if (dup2(dev_null_w, STDOUT_FILENO) < 0)
|
|
|
|
pexit("Failed to dup over stdout");
|
|
|
|
if (dup2(dev_null_w, STDERR_FILENO) < 0)
|
|
|
|
pexit("Failed to dup over stderr");
|
2017-06-09 14:18:27 +00:00
|
|
|
|
2017-06-21 17:39:02 +00:00
|
|
|
/* Create a new session group */
|
|
|
|
setsid();
|
2017-06-09 14:18:27 +00:00
|
|
|
|
2016-10-01 03:26:29 +00:00
|
|
|
/* Environment variables */
|
2017-06-22 08:57:27 +00:00
|
|
|
sync_pipe_fd = get_pipe_fd_from_env("_OCI_SYNCPIPE");
|
2016-09-15 23:40:44 +00:00
|
|
|
|
2016-10-07 15:59:39 +00:00
|
|
|
/* Open the log path file. */
|
2017-06-22 11:03:51 +00:00
|
|
|
log_fd = open(opt_log_path, O_WRONLY | O_APPEND | O_CREAT | O_CLOEXEC, 0600);
|
|
|
|
if (log_fd < 0)
|
2016-10-07 15:59:39 +00:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
2017-06-22 08:45:14 +00:00
|
|
|
if (opt_terminal) {
|
2017-06-22 10:13:16 +00:00
|
|
|
csname = setup_console_socket();
|
2016-10-07 15:59:39 +00:00
|
|
|
} else {
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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-08 20:08:29 +00:00
|
|
|
|
|
|
|
if (opt_stdin) {
|
|
|
|
if (pipe2(fds, O_CLOEXEC) < 0)
|
|
|
|
pexit("Failed to create !terminal stdin pipe");
|
|
|
|
|
|
|
|
masterfd_stdin = fds[1];
|
|
|
|
slavefd_stdin = fds[0];
|
|
|
|
}
|
|
|
|
|
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];
|
2017-06-20 10:18:07 +00:00
|
|
|
}
|
2016-10-07 15:59:39 +00:00
|
|
|
|
2017-06-20 10:18:07 +00:00
|
|
|
/* We always create a stderr pipe, because that way we can capture
|
|
|
|
runc stderr messages before the tty is created */
|
|
|
|
if (pipe2(fds, O_CLOEXEC) < 0)
|
|
|
|
pexit("Failed to create stderr pipe");
|
2017-04-11 18:20:57 +00:00
|
|
|
|
2017-06-20 10:18:07 +00:00
|
|
|
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();
|
2017-06-22 09:30:19 +00:00
|
|
|
add_argv(runtime_argv,
|
|
|
|
opt_runtime_path,
|
|
|
|
NULL);
|
2017-01-10 22:08:54 +00:00
|
|
|
|
2016-10-07 15:59:39 +00:00
|
|
|
/* Generate the cmdline. */
|
2017-06-22 08:45:14 +00:00
|
|
|
if (!opt_exec && opt_systemd_cgroup)
|
2017-06-22 09:30:19 +00:00
|
|
|
add_argv(runtime_argv,
|
|
|
|
"--systemd-cgroup",
|
|
|
|
NULL);
|
2016-10-07 15:59:39 +00:00
|
|
|
|
2017-06-22 08:45:14 +00:00
|
|
|
if (opt_exec) {
|
2017-06-22 09:30:19 +00:00
|
|
|
add_argv(runtime_argv,
|
|
|
|
"exec", "-d",
|
|
|
|
"--pid-file", opt_pid_file,
|
|
|
|
NULL);
|
2017-06-02 12:27:00 +00:00
|
|
|
} else {
|
2017-06-22 09:30:19 +00:00
|
|
|
add_argv(runtime_argv,
|
|
|
|
"create",
|
|
|
|
"--bundle", opt_bundle_path,
|
|
|
|
"--pid-file", opt_pid_file,
|
|
|
|
NULL);
|
2017-06-02 12:27:00 +00:00
|
|
|
}
|
2017-01-10 22:08:54 +00:00
|
|
|
|
2017-09-22 14:50:48 +00:00
|
|
|
if (!opt_exec && opt_no_pivot) {
|
|
|
|
add_argv(runtime_argv,
|
|
|
|
"--no-pivot",
|
|
|
|
NULL);
|
|
|
|
}
|
|
|
|
|
2018-02-05 19:57:48 +00:00
|
|
|
if (!opt_exec && opt_no_new_keyring) {
|
|
|
|
add_argv(runtime_argv,
|
|
|
|
"--no-new-keyring",
|
|
|
|
NULL);
|
|
|
|
}
|
|
|
|
|
2017-06-22 10:13:16 +00:00
|
|
|
if (csname != NULL) {
|
2017-06-22 09:30:19 +00:00
|
|
|
add_argv(runtime_argv,
|
|
|
|
"--console-socket", csname,
|
|
|
|
NULL);
|
2017-06-02 12:27:00 +00:00
|
|
|
}
|
2016-10-07 15:59:39 +00:00
|
|
|
|
|
|
|
/* Set the exec arguments. */
|
2017-06-22 08:45:14 +00:00
|
|
|
if (opt_exec) {
|
2017-06-22 09:30:19 +00:00
|
|
|
add_argv(runtime_argv,
|
|
|
|
"--process", opt_exec_process_spec,
|
|
|
|
NULL);
|
2016-10-07 15:59:39 +00:00
|
|
|
}
|
|
|
|
|
2017-05-25 09:11:14 +00:00
|
|
|
/* Container name comes last. */
|
2017-06-22 09:30:19 +00:00
|
|
|
add_argv(runtime_argv, opt_cid, NULL);
|
|
|
|
end_argv(runtime_argv);
|
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) {
|
2017-05-12 13:36:15 +00:00
|
|
|
/* FIXME: This results in us not outputting runc error messages to crio's log. */
|
2017-06-09 14:18:27 +00:00
|
|
|
if (slavefd_stdin < 0)
|
|
|
|
slavefd_stdin = dev_null_r;
|
2017-06-08 20:08:29 +00:00
|
|
|
if (dup2(slavefd_stdin, STDIN_FILENO) < 0)
|
|
|
|
pexit("Failed to dup over stdout");
|
|
|
|
|
2017-06-09 14:18:27 +00:00
|
|
|
if (slavefd_stdout < 0)
|
|
|
|
slavefd_stdout = dev_null_w;
|
|
|
|
if (dup2(slavefd_stdout, STDOUT_FILENO) < 0)
|
|
|
|
pexit("Failed to dup over stdout");
|
|
|
|
|
|
|
|
if (slavefd_stderr < 0)
|
|
|
|
slavefd_stderr = slavefd_stdout;
|
|
|
|
if (dup2(slavefd_stderr, STDERR_FILENO) < 0)
|
|
|
|
pexit("Failed to dup over stderr");
|
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-06-08 20:08:29 +00:00
|
|
|
close(slavefd_stdin);
|
2017-04-11 18:20:57 +00:00
|
|
|
close(slavefd_stdout);
|
|
|
|
close(slavefd_stderr);
|
2016-10-07 15:59:39 +00:00
|
|
|
|
2017-07-22 15:17:14 +00:00
|
|
|
/* Map pid to its handler. */
|
|
|
|
GHashTable *pid_to_handler = g_hash_table_new (g_int_hash, g_int_equal);
|
|
|
|
g_hash_table_insert (pid_to_handler, &create_pid, runtime_exit_cb);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Glib does not support SIGCHLD so use SIGUSR1 with the same semantic. We will
|
|
|
|
* catch SIGCHLD and raise(SIGUSR1) in the signal handler.
|
|
|
|
*/
|
|
|
|
g_unix_signal_add (SIGUSR1, on_sigusr1_cb, pid_to_handler);
|
|
|
|
|
|
|
|
if (signal(SIGCHLD, on_sigchld) == SIG_ERR)
|
|
|
|
pexit("Failed to set handler for SIGCHLD");
|
|
|
|
|
2017-06-08 15:57:15 +00:00
|
|
|
ninfo("about to waitpid: %d", create_pid);
|
2017-06-22 10:13:16 +00:00
|
|
|
if (csname != NULL) {
|
2017-06-22 11:03:51 +00:00
|
|
|
guint terminal_watch = g_unix_fd_add (console_socket_fd, G_IO_IN, terminal_accept_cb, csname);
|
2017-07-22 15:17:14 +00:00
|
|
|
/* Process any SIGCHLD we may have missed before the signal handler was in place. */
|
|
|
|
check_child_processes (pid_to_handler);
|
2017-06-08 15:57:15 +00:00
|
|
|
g_main_loop_run (main_loop);
|
|
|
|
g_source_remove (terminal_watch);
|
|
|
|
} else {
|
2017-07-23 16:54:03 +00:00
|
|
|
int ret;
|
2017-06-08 15:57:15 +00:00
|
|
|
/* Wait for our create child to exit with the return code. */
|
2017-07-23 16:54:03 +00:00
|
|
|
do
|
|
|
|
ret = waitpid(create_pid, &runtime_status, 0);
|
|
|
|
while (ret < 0 && errno == EINTR);
|
|
|
|
if (ret < 0) {
|
2017-06-08 15:57:15 +00:00
|
|
|
int old_errno = errno;
|
|
|
|
kill(create_pid, SIGKILL);
|
|
|
|
errno = old_errno;
|
2017-06-22 08:45:14 +00:00
|
|
|
pexit("Failed to wait for `runtime %s`", opt_exec ? "exec" : "create");
|
2017-06-08 15:57:15 +00:00
|
|
|
}
|
2017-07-22 15:17:14 +00:00
|
|
|
|
2016-08-26 21:00:30 +00:00
|
|
|
}
|
|
|
|
|
2017-05-12 20:30:30 +00:00
|
|
|
if (!WIFEXITED(runtime_status) || WEXITSTATUS(runtime_status) != 0) {
|
2017-06-21 17:39:02 +00:00
|
|
|
if (sync_pipe_fd > 0) {
|
2017-06-20 10:18:07 +00:00
|
|
|
/*
|
|
|
|
* 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) {
|
|
|
|
buf[num_read] = '\0';
|
2017-06-21 17:39:02 +00:00
|
|
|
write_sync_fd(sync_pipe_fd, -1, buf);
|
2017-05-12 20:30:30 +00:00
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
2017-06-22 08:45:14 +00:00
|
|
|
if (opt_terminal && masterfd_stdout == -1)
|
2017-06-20 10:18:07 +00:00
|
|
|
nexit("Runtime did not set up terminal");
|
2017-06-08 15:57:15 +00:00
|
|
|
|
2016-09-02 21:33:54 +00:00
|
|
|
/* Read the pid so we can wait for the process to exit */
|
2017-06-22 08:45:14 +00:00
|
|
|
g_file_get_contents(opt_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
|
|
|
|
2017-06-28 15:44:49 +00:00
|
|
|
container_pid = atoi(contents);
|
|
|
|
ninfo("container PID: %d", container_pid);
|
2016-08-26 21:00:30 +00:00
|
|
|
|
2017-07-22 15:17:14 +00:00
|
|
|
g_hash_table_insert (pid_to_handler, &container_pid, container_exit_cb);
|
|
|
|
|
2017-05-30 21:35:12 +00:00
|
|
|
/* Setup endpoint for attach */
|
2017-06-22 09:57:47 +00:00
|
|
|
_cleanup_free_ char *attach_symlink_dir_path = NULL;
|
2017-06-22 08:45:14 +00:00
|
|
|
if (!opt_exec) {
|
2017-06-22 10:18:09 +00:00
|
|
|
attach_symlink_dir_path = setup_attach_socket();
|
2017-05-30 21:35:12 +00:00
|
|
|
}
|
|
|
|
|
2017-06-22 08:45:14 +00:00
|
|
|
if (!opt_exec) {
|
2017-06-22 10:39:29 +00:00
|
|
|
setup_terminal_control_fifo();
|
2017-05-30 22:14:44 +00:00
|
|
|
}
|
|
|
|
|
2016-09-15 23:40:44 +00:00
|
|
|
/* Send the container pid back to parent */
|
2017-06-22 08:45:14 +00:00
|
|
|
if (!opt_exec) {
|
2017-06-28 15:44:49 +00:00
|
|
|
write_sync_fd(sync_pipe_fd, container_pid, NULL);
|
2016-09-15 23:40:44 +00:00
|
|
|
}
|
|
|
|
|
2017-06-28 15:44:49 +00:00
|
|
|
setup_oom_handling(container_pid);
|
2017-05-23 02:02:21 +00:00
|
|
|
|
2017-04-11 18:20:57 +00:00
|
|
|
if (masterfd_stdout >= 0) {
|
2017-06-08 08:42:33 +00:00
|
|
|
g_unix_fd_add (masterfd_stdout, G_IO_IN, stdio_cb, GINT_TO_POINTER(STDOUT_PIPE));
|
2016-10-07 15:59:39 +00:00
|
|
|
}
|
2017-04-11 18:20:57 +00:00
|
|
|
if (masterfd_stderr >= 0) {
|
2017-06-08 08:42:33 +00:00
|
|
|
g_unix_fd_add (masterfd_stderr, G_IO_IN, stdio_cb, GINT_TO_POINTER(STDERR_PIPE));
|
2016-10-07 15:59:39 +00:00
|
|
|
}
|
|
|
|
|
2017-06-22 08:45:14 +00:00
|
|
|
if (opt_timeout > 0) {
|
|
|
|
g_timeout_add_seconds (opt_timeout, timeout_cb, NULL);
|
2017-06-21 17:39:02 +00:00
|
|
|
}
|
2017-06-28 15:44:49 +00:00
|
|
|
|
2017-07-22 15:17:14 +00:00
|
|
|
check_child_processes(pid_to_handler);
|
2017-06-28 15:44:49 +00:00
|
|
|
|
2017-06-08 08:42:33 +00:00
|
|
|
g_main_loop_run (main_loop);
|
2016-09-02 21:33:54 +00:00
|
|
|
|
2017-06-28 15:44:49 +00:00
|
|
|
/* Drain stdout and stderr */
|
|
|
|
if (masterfd_stdout != -1) {
|
|
|
|
g_unix_set_fd_nonblocking(masterfd_stdout, TRUE, NULL);
|
|
|
|
while (read_stdio(masterfd_stdout, STDOUT_PIPE, NULL))
|
|
|
|
;
|
|
|
|
}
|
|
|
|
if (masterfd_stderr != -1) {
|
|
|
|
g_unix_set_fd_nonblocking(masterfd_stderr, TRUE, NULL);
|
|
|
|
while (read_stdio(masterfd_stderr, STDERR_PIPE, NULL))
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
2017-06-21 17:39:02 +00:00
|
|
|
int exit_status = -1;
|
|
|
|
const char *exit_message = NULL;
|
|
|
|
|
|
|
|
if (timed_out) {
|
2017-06-28 15:44:49 +00:00
|
|
|
kill(container_pid, SIGKILL);
|
2017-06-21 17:39:02 +00:00
|
|
|
exit_message = "command timed out";
|
|
|
|
} else {
|
2017-06-28 15:44:49 +00:00
|
|
|
exit_status = WEXITSTATUS(container_status);
|
2016-08-26 21:00:30 +00:00
|
|
|
}
|
|
|
|
|
2018-02-23 21:11:12 +00:00
|
|
|
if (opt_exit_dir) {
|
2017-06-22 09:57:47 +00:00
|
|
|
_cleanup_free_ char *status_str = g_strdup_printf("%d", exit_status);
|
2017-08-13 01:35:55 +00:00
|
|
|
_cleanup_free_ char *exit_file_path = g_build_filename(opt_exit_dir, opt_cid, NULL);
|
|
|
|
if (!g_file_set_contents(exit_file_path, status_str, -1, &err))
|
2017-06-22 09:57:47 +00:00
|
|
|
nexit("Failed to write %s to exit file: %s\n",
|
|
|
|
status_str, err->message);
|
2018-02-23 21:11:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (opt_exec) {
|
2017-06-21 17:39:02 +00:00
|
|
|
/* Send the command exec exit code back to the parent */
|
|
|
|
write_sync_fd(sync_pipe_fd, exit_status, exit_message);
|
2017-01-10 22:08:54 +00:00
|
|
|
}
|
|
|
|
|
2017-06-22 09:57:47 +00:00
|
|
|
if (attach_symlink_dir_path != NULL &&
|
2017-06-21 17:39:02 +00:00
|
|
|
unlink(attach_symlink_dir_path) == -1 && errno != ENOENT) {
|
|
|
|
pexit("Failed to remove symlink for attach socket directory");
|
2017-05-30 21:35:12 +00:00
|
|
|
}
|
|
|
|
|
2016-08-26 21:00:30 +00:00
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|