From 1e95c542346a88f21d09e0ea7490f074f6f9610a Mon Sep 17 00:00:00 2001 From: Marcel Holtmann Date: Sun, 8 Nov 2015 02:18:41 +0100 Subject: [PATCH] monitor: Use cleaner decoding for system note and user logging packets --- monitor/packet.c | 146 +++++++++++++++++++++---------------------- monitor/packet.h | 5 ++ src/shared/btsnoop.h | 5 ++ 3 files changed, 82 insertions(+), 74 deletions(-) diff --git a/monitor/packet.c b/monitor/packet.c index 2bb6ba8f7..86745420d 100644 --- a/monitor/packet.c +++ b/monitor/packet.c @@ -3685,86 +3685,16 @@ struct index_data { static struct index_data index_list[MAX_INDEX]; -static void packet_system_note(struct timeval *tv, struct ucred *cred, - uint16_t index, const void *data, uint16_t size) -{ - print_packet(tv, cred, index, '=', COLOR_SYSTEM_NOTE, - "Note", data, NULL); -} - -static void packet_user_logging(struct timeval *tv, struct ucred *cred, - uint16_t index, const void *data, uint16_t size) -{ - char pid_str[128]; - const char *label; - uint8_t priority; - uint8_t ident_len; - const char *color; - - if (size < 2) - return; - - priority = *((uint8_t *) data); - ident_len = *((uint8_t *) (data + 1)); - - switch (priority) { - case 0x03: - color = COLOR_ERROR; - break; - case 0x04: - color = COLOR_WARN; - break; - case 0x06: - color = COLOR_INFO; - break; - case 0x07: - color = COLOR_DEBUG; - break; - default: - color = COLOR_WHITE_BG; - break; - } - - if (cred) { - char *path = alloca(24); - char line[128]; - FILE *fp; - - snprintf(path, 23, "/proc/%u/comm", cred->pid); - - fp = fopen(path, "re"); - if (fp) { - if (fgets(line, sizeof(line), fp)) { - line[strcspn(line, "\r\n")] = '\0'; - snprintf(pid_str, sizeof(pid_str), "%s[%u]", - line, cred->pid); - } else - snprintf(pid_str, sizeof(pid_str), "%u", - cred->pid); - fclose(fp); - } else - snprintf(pid_str, sizeof(pid_str), "%u", cred->pid); - - label = pid_str; - } else { - if (ident_len) - label = data + 2; - else - label = "Message"; - } - - print_packet(tv, cred, index, '=', color, label, - data + 2 + ident_len, NULL); -} - void packet_monitor(struct timeval *tv, struct ucred *cred, uint16_t index, uint16_t opcode, const void *data, uint16_t size) { const struct btsnoop_opcode_new_index *ni; const struct btsnoop_opcode_index_info *ii; + const struct btsnoop_opcode_user_logging *ul; char str[18], extra_str[24]; uint16_t manufacturer; + const char *ident; if (index_filter && index_number != index) return; @@ -3850,10 +3780,14 @@ void packet_monitor(struct timeval *tv, struct ucred *cred, packet_vendor_diag(tv, index, manufacturer, data, size); break; case BTSNOOP_OPCODE_SYSTEM_NOTE: - packet_system_note(tv, cred, index, data, size); + packet_system_note(tv, cred, index, data); break; case BTSNOOP_OPCODE_USER_LOGGING: - packet_user_logging(tv, cred, index, data, size); + ul = data; + ident = ul->ident_len ? data + sizeof(*ul) : NULL; + + packet_user_logging(tv, cred, index, ul->priority, ident, + data + sizeof(*ul) + ul->ident_len); break; default: sprintf(extra_str, "(code %d len %d)", opcode, size); @@ -8768,6 +8702,70 @@ void packet_vendor_diag(struct timeval *tv, uint16_t index, } } +void packet_system_note(struct timeval *tv, struct ucred *cred, + uint16_t index, const void *message) +{ + print_packet(tv, cred, index, '=', COLOR_SYSTEM_NOTE, + "Note", message, NULL); +} + +void packet_user_logging(struct timeval *tv, struct ucred *cred, + uint16_t index, uint8_t priority, + const char *ident, const char *message) +{ + char pid_str[128]; + const char *label; + const char *color; + + switch (priority) { + case 0x03: + color = COLOR_ERROR; + break; + case 0x04: + color = COLOR_WARN; + break; + case 0x06: + color = COLOR_INFO; + break; + case 0x07: + color = COLOR_DEBUG; + break; + default: + color = COLOR_WHITE_BG; + break; + } + + if (cred) { + char *path = alloca(24); + char line[128]; + FILE *fp; + + snprintf(path, 23, "/proc/%u/comm", cred->pid); + + fp = fopen(path, "re"); + if (fp) { + if (fgets(line, sizeof(line), fp)) { + line[strcspn(line, "\r\n")] = '\0'; + snprintf(pid_str, sizeof(pid_str), "%s[%u]", + line, cred->pid); + } else + snprintf(pid_str, sizeof(pid_str), "%u", + cred->pid); + fclose(fp); + } else + snprintf(pid_str, sizeof(pid_str), "%u", cred->pid); + + label = pid_str; + } else { + if (ident) + label = ident; + else + label = "Message"; + } + + print_packet(tv, cred, index, '=', color, label, message, NULL); +} + void packet_hci_command(struct timeval *tv, struct ucred *cred, uint16_t index, const void *data, uint16_t size) { diff --git a/monitor/packet.h b/monitor/packet.h index 59da5af89..218a63b9d 100644 --- a/monitor/packet.h +++ b/monitor/packet.h @@ -73,6 +73,11 @@ void packet_index_info(struct timeval *tv, uint16_t index, const char *label, void packet_vendor_diag(struct timeval *tv, uint16_t index, uint16_t manufacturer, const void *data, uint16_t size); +void packet_system_note(struct timeval *tv, struct ucred *cred, + uint16_t index, const void *message); +void packet_user_logging(struct timeval *tv, struct ucred *cred, + uint16_t index, uint8_t priority, + const char *ident, const char *message); void packet_hci_command(struct timeval *tv, struct ucred *cred, uint16_t index, const void *data, uint16_t size); diff --git a/src/shared/btsnoop.h b/src/shared/btsnoop.h index 950de113c..37788d681 100644 --- a/src/shared/btsnoop.h +++ b/src/shared/btsnoop.h @@ -64,6 +64,11 @@ struct btsnoop_opcode_index_info { uint16_t manufacturer; } __attribute__((packed)); +struct btsnoop_opcode_user_logging { + uint8_t priority; + uint8_t ident_len; +} __attribute__((packed)); + struct btsnoop; struct btsnoop *btsnoop_open(const char *path, unsigned long flags); -- 2.47.3