Skip to content

Commit

Permalink
config: include the two ctrl events
Browse files Browse the repository at this point in the history
Rather than having a comment saying "this should stay in sync", let's define a variable and import it there, so we're guarenteed to stay in sync

Signed-off-by: Peter Hunt <[email protected]>
  • Loading branch information
haircommander committed Nov 8, 2019
1 parent 1ed4f02 commit 496e8e4
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 19 deletions.
14 changes: 12 additions & 2 deletions cmd/conmon-config/conmon-config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,20 @@ func main() {
#define STDIO_BUF_SIZE %d
#define CONN_SOCK_BUF_SIZE %d
#define DEFAULT_SOCKET_PATH "%s"
#define WIN_RESIZE_EVENT %d
#define REOPEN_LOGS_EVENT %d
#endif // CONFIG_H
`
if err := ioutil.WriteFile("config.h", []byte(fmt.Sprintf(output, config.BufSize, config.BufSize, config.ConnSockBufSize, config.ContainerAttachSocketDir)), 0644); err != nil {
fmt.Errorf(err.Error())
if err := ioutil.WriteFile("config.h", []byte(fmt.Sprintf(
output,
config.BufSize,
config.BufSize,
config.ConnSockBufSize,
config.ContainerAttachSocketDir,
config.WinResizeEvent,
config.ReopenLogsEvent)),
0644); err != nil {
fmt.Errorf(err.Error())
}
}
7 changes: 7 additions & 0 deletions runner/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,11 @@ const (
// ConnSockBufSize is the size of the socket used for
// to attach to the container
ConnSockBufSize = 32768
// WinResizeEvent is the event code the caller program will
// send along the ctrl fd to signal conmon to resize
// the pty window
WinResizeEvent = 1
// ReopenLogsEvent is the event code the caller program will
// send along the ctrl fd to signal conmon to reopen the log files
ReopenLogsEvent = 2
)
2 changes: 2 additions & 0 deletions src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@
#define STDIO_BUF_SIZE 8192
#define CONN_SOCK_BUF_SIZE 32768
#define DEFAULT_SOCKET_PATH "/var/run/crio"
#define WIN_RESIZE_EVENT 1
#define REOPEN_LOGS_EVENT 2

#endif // CONFIG_H
33 changes: 16 additions & 17 deletions src/conmon.c
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,7 @@ static void resize_winsz(int height, int width)
* line_process_func should return TRUE if it succeeds, and FALSE if it fails
* to process the line.
*/
static gboolean read_from_ctrl_buffer(int fd, gboolean(*line_process_func)(char*,int))
static gboolean read_from_ctrl_buffer(int fd, gboolean(*line_process_func)(char*))
{
static char ctlbuf[CTLBUFSZ];
static int readsz = CTLBUFSZ - 1;
Expand All @@ -710,7 +710,7 @@ static gboolean read_from_ctrl_buffer(int fd, gboolean(*line_process_func)(char*
char *newline = strchrnul(beg, '\n');
/* Process each message which ends with a line */
while (*newline != '\0') {
if (!line_process_func(ctlbuf, num_read)) {
if (!line_process_func(ctlbuf)) {
return G_SOURCE_CONTINUE;
}
beg = newline + 1;
Expand Down Expand Up @@ -749,26 +749,29 @@ static gboolean read_from_ctrl_buffer(int fd, gboolean(*line_process_func)(char*
* and either writes to the winsz fd (to handle terminal resize events)
* or reopens log files.
*/
static gboolean process_terminal_ctrl_line(char* line, int len)
static gboolean process_terminal_ctrl_line(char* line)
{
int ctl_msg_type, height, width, ret = -1;
_cleanup_free_ char *hw_str = NULL;

// while the height and width won't be used in this function,
// we want to remove them from the buffer anyway
ret = sscanf(line, "%d %d %d\n", &ctl_msg_type, &height, &width);
if (ret != 3) {
nwarn("Failed to sscanf message");
return FALSE;
}

ninfof("Message type: %d, Height: %d, Width: %d", ctl_msg_type, height, width);
ninfof("Message type: %d", ctl_msg_type);
switch (ctl_msg_type) {
// This matches what we write from container_attach.go
case 1:
if (write(winsz_fd_w, line, len) < 0) {
case WIN_RESIZE_EVENT:
hw_str = g_strdup_printf("%d %d\n", height, width);
if (write(winsz_fd_w, hw_str, strlen(hw_str)) < 0) {
nwarn("Failed to write to window resizing fd. A resize event may have been dropped");
return FALSE;
}
break;
case 2:
case REOPEN_LOGS_EVENT:
reopen_log_files();
break;
default:
Expand All @@ -791,19 +794,15 @@ static gboolean ctrl_cb(int fd, G_GNUC_UNUSED GIOCondition condition, G_GNUC_UNU
* after the terminal_ctrl fd receives a winsz event.
* It reads a height and length, and resizes the pty with it.
*/
static gboolean process_winsz_ctrl_line(char * line, G_GNUC_UNUSED int len)
static gboolean process_winsz_ctrl_line(char * line)
{
int ctl_msg_type, height, width, ret = -1;
ret = sscanf(line, "%d %d %d\n", &ctl_msg_type, &height, &width);
if (ret != 3) {
int height, width, ret = -1;
ret = sscanf(line, "%d %d\n", &height, &width);
ninfof("Height: %d, Width: %d", height, width);
if (ret != 2) {
nwarn("Failed to sscanf message");
return FALSE;
}
if (ctl_msg_type != 1) {
ninfof("Unknown message type: %d", ctl_msg_type);
return FALSE;
}
ninfof("Message type: %d, Height: %d, Width: %d", ctl_msg_type, height, width);
resize_winsz(height, width);
return TRUE;
}
Expand Down

0 comments on commit 496e8e4

Please sign in to comment.