Skip to content

Commit 57da497

Browse files
committed
Remove need for extra bytes for remote consoles
Remote consoles are not always attached to a `conmon` process. Don't bother adding the "pre-byte" for the pipe, just add a `write()` system call for that byte. We also remove the null termination since all the operations must be done by count (null bytes can be read from the pipe). Signed-off-by: Peter Portante <peter.portante@redhat.com>
1 parent 8debcb5 commit 57da497

3 files changed

Lines changed: 14 additions & 16 deletions

File tree

src/conn_sock.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -355,17 +355,23 @@ void schedule_main_stdin_write()
355355
schedule_local_sock_write(&local_mainfd_stdin);
356356
}
357357

358-
void write_back_to_remote_consoles(char *buf, int len)
358+
void write_back_to_remote_consoles(stdpipe_t pipe, char *buf, int len)
359359
{
360360
if (local_mainfd_stdin.readers == NULL)
361361
return;
362362

363363
for (int i = local_mainfd_stdin.readers->len; i > 0; i--) {
364364
struct remote_sock_s *remote_sock = g_ptr_array_index(local_mainfd_stdin.readers, i - 1);
365365

366-
if (remote_sock->writable && write_all(remote_sock->fd, buf, len) < 0) {
367-
nwarn("Failed to write to remote console socket");
368-
remote_sock_shutdown(remote_sock, SHUT_WR);
366+
if (remote_sock->writable) {
367+
if (write_all(remote_sock->fd, &pipe, 1) < 0) {
368+
nwarn("Failed to write to remote console socket");
369+
remote_sock_shutdown(remote_sock, SHUT_WR);
370+
}
371+
if (write_all(remote_sock->fd, buf, len) < 0) {
372+
nwarn("Failed to write to remote console socket");
373+
remote_sock_shutdown(remote_sock, SHUT_WR);
374+
}
369375
}
370376
}
371377
}

src/conn_sock.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include <glib.h> /* gboolean */
55
#include "config.h" /* CONN_SOCK_BUF_SIZE */
6+
#include "utils.h" /* stdpipe_t */
67

78
#define SOCK_TYPE_CONSOLE 1
89
#define SOCK_TYPE_NOTIFY 2
@@ -52,7 +53,7 @@ char *setup_seccomp_socket(const char *socket);
5253
char *setup_attach_socket(void);
5354
void setup_notify_socket(char *);
5455
void schedule_main_stdin_write();
55-
void write_back_to_remote_consoles(char *buf, int len);
56+
void write_back_to_remote_consoles(stdpipe_t pipe, char *buf, int len);
5657
void close_all_readers();
5758

5859
#endif // CONN_SOCK_H

src/ctr_stdio.c

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,7 @@ static void drain_log_buffers(stdpipe_t pipe)
112112

113113
static bool read_stdio(int fd, stdpipe_t pipe, gboolean *eof)
114114
{
115-
/* We use two extra bytes. One at the start, which we don't read into, instead
116-
we use that for marking the pipe when we write to the attached socket.
117-
One at the end to guarantee a null-terminated buffer for journald logging*/
118-
119-
char real_buf[STDIO_BUF_SIZE + 2];
120-
char *buf = real_buf + 1;
115+
char buf[STDIO_BUF_SIZE];
121116
ssize_t num_read = 0;
122117

123118
if (eof)
@@ -143,15 +138,11 @@ static bool read_stdio(int fd, stdpipe_t pipe, gboolean *eof)
143138
nwarnf("stdio_input read failed: %m");
144139
return false;
145140
} else {
146-
// Always null terminate the buffer, just in case.
147-
buf[num_read] = '\0';
148-
149141
bool written = write_to_logs(pipe, buf, num_read);
150142
if (!written)
151143
return false;
152144

153-
real_buf[0] = pipe;
154-
write_back_to_remote_consoles(real_buf, num_read + 1);
145+
write_back_to_remote_consoles(pipe, buf, num_read);
155146
return true;
156147
}
157148
}

0 commit comments

Comments
 (0)