diff --git a/cmd/crio/main.go b/cmd/crio/main.go index a058f296..025e500d 100644 --- a/cmd/crio/main.go +++ b/cmd/crio/main.go @@ -1,5 +1,8 @@ package main +// #include "../../conmon/config.h" +import "C" + import ( "context" "fmt" @@ -40,9 +43,9 @@ func validateConfig(config *server.Config) error { } - // This needs to match the read buffer size in conmon - if config.LogSizeMax >= 0 && config.LogSizeMax < 8192 { - return fmt.Errorf("log size max should be negative or >= 8192") + bufSize := int64(C.BUF_SIZE) + if config.LogSizeMax >= 0 && config.LogSizeMax < bufSize { + return fmt.Errorf("log size max should be negative or >= %d", bufSize) } return nil } diff --git a/conmon/config.h b/conmon/config.h new file mode 100644 index 00000000..5d7d2f03 --- /dev/null +++ b/conmon/config.h @@ -0,0 +1,18 @@ +#pragma once + +#if !defined(CONFIG_H) +#define CONFIG_H + +#define BUF_SIZE 8192 +#define STDIO_BUF_SIZE 8192 + +/* stdpipe_t represents one of the std pipes (or NONE). */ +typedef enum { + NO_PIPE, + STDIN_PIPE, /* unused */ + STDOUT_PIPE, + STDERR_PIPE, +} stdpipe_t; + + +#endif // CONFIG_H diff --git a/conmon/conmon.c b/conmon/conmon.c index 477b98bf..755a68e9 100644 --- a/conmon/conmon.c +++ b/conmon/conmon.c @@ -26,6 +26,7 @@ #include #include "cmsg.h" +#include "config.h" #define pexit(fmt, ...) \ do { \ @@ -91,7 +92,6 @@ static inline void strv_cleanup(char ***strv) #define _cleanup_gstring_ _cleanup_(gstring_free_cleanup) #define _cleanup_strv_ _cleanup_(strv_cleanup) -#define BUF_SIZE 8192 #define CMD_SIZE 1024 #define MAX_EVENTS 10 @@ -263,15 +263,6 @@ int set_k8s_timestamp(char *buf, ssize_t buflen, const char *pipename) return err; } -/* stdpipe_t represents one of the std pipes (or NONE). - * Sync with const in container_attach.go */ -typedef enum { - NO_PIPE, - STDIN_PIPE, /* unused */ - STDOUT_PIPE, - STDERR_PIPE, -} stdpipe_t; - const char *stdpipe_name(stdpipe_t pipe) { switch (pipe) { @@ -571,11 +562,10 @@ static gboolean tty_hup_timeout_cb (G_GNUC_UNUSED gpointer user_data) static bool read_stdio(int fd, stdpipe_t pipe, bool *eof) { - #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; + char *buf = real_buf + 1; ssize_t num_read = 0; if (eof) diff --git a/server/container_attach.go b/server/container_attach.go index ec9bedab..81dbd291 100644 --- a/server/container_attach.go +++ b/server/container_attach.go @@ -1,5 +1,8 @@ package server +// #include "../conmon/config.h" +import "C" + import ( "fmt" "io" @@ -18,13 +21,6 @@ import ( kubecontainer "k8s.io/kubernetes/pkg/kubelet/container" ) -/* Sync with stdpipe_t in conmon.c */ -const ( - AttachPipeStdin = 1 - AttachPipeStdout = 2 - AttachPipeStderr = 3 -) - // Attach prepares a streaming endpoint to attach to a running container. func (s *Server) Attach(ctx context.Context, req *pb.AttachRequest) (resp *pb.AttachResponse, err error) { const operation = "attach" @@ -113,16 +109,17 @@ func (ss streamService) Attach(containerID string, inputStream io.Reader, output } func redirectResponseToOutputStreams(outputStream, errorStream io.Writer, conn io.Reader) error { + stdioBufSize := int(C.STDIO_BUF_SIZE) var err error - buf := make([]byte, 8192+1) /* Sync with conmon STDIO_BUF_SIZE */ + buf := make([]byte, stdioBufSize+1) for { nr, er := conn.Read(buf) if nr > 0 { var dst io.Writer - if buf[0] == AttachPipeStdout { + if buf[0] == byte(C.STDOUT_PIPE) { dst = outputStream - } else if buf[0] == AttachPipeStderr { + } else if buf[0] == byte(C.STDERR_PIPE) { dst = errorStream } else { logrus.Infof("Got unexpected attach type %+d", buf[0])