Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/cmsg.c
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,16 @@ struct file_t recvfd(int sockfd)
msg.msg_controllen = sizeof(u.buf);

ssize_t ret = recvmsg(sockfd, &msg, 0);
if (ret < 0)
if (ret < 0) {
/* Add specific error information for debugging console fd issues */
fprintf(stderr, "recvfd: recvmsg failed: %m (sockfd=%d)\n", sockfd);
goto err;
}
if (ret >= TAG_BUFFER) {
fprintf(stderr, "recvfd: received data too large: %zd >= %d\n", ret, TAG_BUFFER);
errno = EMSGSIZE;
goto err;
}
file.name[ret] = '\0';

/* Shrink the buffer to what is effectively used. */
Expand Down
51 changes: 45 additions & 6 deletions src/parent_pipe_fd.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,24 @@ void write_or_close_sync_fd(int *fd, int res, const char *message)
return;

_cleanup_free_ char *json = NULL;
if (message) {
if (message && strlen(message) > 0) {
_cleanup_free_ char *escaped_message = escape_json_string(message);
json = g_strdup_printf("{\"%s\": %d, \"message\": \"%s\"}\n", res_key, res, escaped_message);
if (escaped_message == NULL) {
/* Fallback to JSON without message if escaping fails */
json = g_strdup_printf("{\"%s\": %d}\n", res_key, res);
} else {
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);
}

/* Ensure we have valid JSON before attempting to write */
if (json == NULL) {
/* Fallback to minimal valid JSON */
json = g_strdup_printf("{\"%s\": %d}\n", res_key, res);
}

len = strlen(json);
if (write_all(*fd, json, len) != len) {
if (errno == EPIPE) {
Expand All @@ -63,21 +74,49 @@ void write_or_close_sync_fd(int *fd, int res, const char *message)

static char *escape_json_string(const char *str)
{
if (str == NULL) {
return NULL;
}

size_t str_len = strlen(str);
if (str_len == 0) {
return g_strdup("");
}

const char *p = str;
GString *escaped = g_string_sized_new(strlen(str));
GString *escaped = g_string_sized_new(str_len * 2); /* Pre-allocate extra space for escaping */

if (escaped == NULL) {
return NULL;
}

while (*p != 0) {
char c = *p++;
unsigned char c = (unsigned char)*p++;

/* Handle standard JSON escape sequences */
if (c == '\\' || c == '"') {
g_string_append_c(escaped, '\\');
g_string_append_c(escaped, c);
} else if (c == '/') {
g_string_append_printf(escaped, "\\/");
} else if (c == '\n') {
g_string_append_printf(escaped, "\\n");
} else if (c == '\r') {
g_string_append_printf(escaped, "\\r");
} 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 if (c == '\b') {
g_string_append_printf(escaped, "\\b");
} else if (c == '\f') {
g_string_append_printf(escaped, "\\f");
} else if (c < 0x20 || c == 0x7f) {
/* Escape control characters */
g_string_append_printf(escaped, "\\u00%02x", c);
} else if (c >= 0x80) {
/* For non-ASCII characters, pass through as-is for UTF-8 compatibility */
g_string_append_c(escaped, c);
} else {
/* Regular ASCII characters */
g_string_append_c(escaped, c);
}
}
Expand Down